taiko-web/public/src/js/p2.js

254 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-09-12 19:10:00 +02:00
class P2Connection{
constructor(...args){
this.init(...args)
}
init(){
2018-09-12 19:10:00 +02:00
this.closed = true
this.lastMessages = {}
this.otherConnected = false
2020-03-13 03:34:54 +01:00
this.name = null
this.player = 1
2018-09-18 00:37:59 +02:00
this.allEvents = new Map()
this.addEventListener("message", this.message.bind(this))
2018-11-01 23:05:18 +01:00
this.currentHash = ""
pageEvents.add(window, "hashchange", this.onhashchange.bind(this))
2018-09-18 00:37:59 +02:00
}
addEventListener(type, callback){
var addedType = this.allEvents.get(type)
if(!addedType){
addedType = new Set()
this.allEvents.set(type, addedType)
}
return addedType.add(callback)
}
removeEventListener(type, callback){
var addedType = this.allEvents.get(type)
if(addedType){
return addedType.delete(callback)
}
2018-09-12 19:10:00 +02:00
}
open(){
this.closed = false
var wsProtocol = location.protocol == "https:" ? "wss:" : "ws:"
2018-09-12 19:39:29 +02:00
this.socket = new WebSocket(wsProtocol + "//" + location.host + "/p2")
2018-11-01 23:05:18 +01:00
pageEvents.race(this.socket, "open", "close").then(response => {
if(response.type === "open"){
2018-09-18 00:37:59 +02:00
return this.openEvent()
2018-09-12 19:10:00 +02:00
}
2018-09-18 00:37:59 +02:00
return this.closeEvent()
2018-09-12 19:10:00 +02:00
})
2018-09-18 00:37:59 +02:00
pageEvents.add(this.socket, "message", this.messageEvent.bind(this))
2018-09-12 19:10:00 +02:00
}
2018-09-18 00:37:59 +02:00
openEvent(){
var addedType = this.allEvents.get("open")
if(addedType){
addedType.forEach(callback => callback())
}
2018-09-12 19:10:00 +02:00
}
close(){
this.closed = true
this.socket.close()
}
2018-09-18 00:37:59 +02:00
closeEvent(){
this.removeEventListener(onmessage)
this.otherConnected = false
this.session = false
if(this.hashLock){
this.hash("")
this.hashLock = false
}
2018-09-12 19:10:00 +02:00
if(!this.closed){
setTimeout(() => {
2018-09-18 00:37:59 +02:00
if(this.socket.readyState !== this.socket.OPEN){
2018-09-12 19:10:00 +02:00
this.open()
}
}, 500)
pageEvents.send("p2-disconnected")
2018-09-12 19:10:00 +02:00
}
2018-09-18 00:37:59 +02:00
var addedType = this.allEvents.get("close")
if(addedType){
addedType.forEach(callback => callback())
}
2018-09-12 19:10:00 +02:00
}
send(type, value){
2018-09-18 00:37:59 +02:00
if(this.socket.readyState === this.socket.OPEN){
if(typeof value === "undefined"){
2018-09-12 19:10:00 +02:00
this.socket.send(JSON.stringify({type: type}))
}else{
this.socket.send(JSON.stringify({type: type, value: value}))
}
}else{
2018-09-18 00:37:59 +02:00
pageEvents.once(this, "open").then(() => {
2018-09-12 19:10:00 +02:00
this.send(type, value)
2018-09-18 00:37:59 +02:00
})
2018-09-12 19:10:00 +02:00
}
}
messageEvent(event){
try{
2018-09-18 00:37:59 +02:00
var response = JSON.parse(event.data)
2018-09-12 19:10:00 +02:00
}catch(e){
2018-09-18 00:37:59 +02:00
var response = {}
2018-09-12 19:10:00 +02:00
}
2018-11-01 23:05:18 +01:00
this.lastMessages[response.type] = response
2018-09-18 00:37:59 +02:00
var addedType = this.allEvents.get("message")
if(addedType){
addedType.forEach(callback => callback(response))
2018-09-12 19:10:00 +02:00
}
}
2018-11-01 23:05:18 +01:00
getMessage(type){
2018-09-12 19:10:00 +02:00
if(type in this.lastMessages){
2018-09-18 00:37:59 +02:00
return this.lastMessages[type]
}
}
2018-11-01 23:05:18 +01:00
clearMessage(type){
if(type in this.lastMessages){
this.lastMessages[type] = null
}
}
2018-09-18 00:37:59 +02:00
message(response){
switch(response.type){
case "gameload":
if("player" in response.value){
this.player = response.value.player === 2 ? 2 : 1
}
2018-09-18 00:37:59 +02:00
case "gamestart":
this.otherConnected = true
this.notes = []
this.drumrollPace = 45
this.dai = 2
2018-11-13 05:36:15 +01:00
this.kaAmount = 0
2018-09-18 00:37:59 +02:00
this.results = false
this.branch = "normal"
2020-03-16 20:49:18 +01:00
scoreStorage.clearP2()
2018-09-18 00:37:59 +02:00
break
case "gameend":
this.otherConnected = false
if(this.session){
pageEvents.send("session-end")
}else if(!this.results){
pageEvents.send("p2-game-end")
}
2018-11-01 23:05:18 +01:00
this.session = false
if(this.hashLock){
this.hash("")
this.hashLock = false
}
2020-03-13 03:34:54 +01:00
this.name = null
this.don = null
2020-03-16 20:49:18 +01:00
scoreStorage.clearP2()
2018-09-18 00:37:59 +02:00
break
case "gameresults":
2018-10-03 11:48:18 +02:00
this.results = {}
for(var i in response.value){
this.results[i] = response.value[i] === null ? null : response.value[i].toString()
2018-10-03 11:48:18 +02:00
}
2018-09-18 00:37:59 +02:00
break
case "note":
this.notes.push(response.value)
if(response.value.dai){
this.dai = response.value.dai
}
2018-09-18 00:37:59 +02:00
break
case "drumroll":
this.drumrollPace = response.value.pace
2018-11-13 05:36:15 +01:00
if("kaAmount" in response.value){
this.kaAmount = response.value.kaAmount
}
2018-09-18 00:37:59 +02:00
break
case "branch":
this.branch = response.value
this.branchSet = false
break
2018-11-01 23:05:18 +01:00
case "session":
this.clearMessage("users")
this.otherConnected = true
this.session = true
2020-03-16 20:49:18 +01:00
scoreStorage.clearP2()
if("player" in response.value){
this.player = response.value.player === 2 ? 2 : 1
}
2018-11-01 23:05:18 +01:00
break
2020-03-13 03:34:54 +01:00
case "name":
this.name = response.value ? (response.value.name || "").toString() : ""
this.don = response.value ? (response.value.don) : null
2020-03-13 03:34:54 +01:00
break
2020-03-16 20:49:18 +01:00
case "getcrowns":
if(response.value){
var output = {}
for(var i in response.value){
if(response.value[i]){
var score = scoreStorage.get(response.value[i], false, true)
if(score){
var crowns = {}
for(var diff in score){
if(diff !== "title"){
crowns[diff] = {
crown: score[diff].crown
}
}
}
}else{
var crowns = null
}
output[response.value[i]] = crowns
}
}
p2.send("crowns", output)
}
break
case "crowns":
if(response.value){
for(var i in response.value){
scoreStorage.addP2(i, false, response.value[i], true)
}
}
break
2018-09-12 19:10:00 +02:00
}
}
2018-11-01 23:05:18 +01:00
onhashchange(){
if(this.hashLock){
this.hash(this.currentHash)
}else{
location.reload()
}
}
hash(string){
this.currentHash = string
history.replaceState("", "", location.pathname + (string ? "#" + string : ""))
}
2018-09-12 19:10:00 +02:00
play(circle, mekadon){
2018-09-14 00:55:23 +02:00
if(this.otherConnected || this.notes.length > 0){
var type = circle.type
2018-09-21 22:31:35 +02:00
var drumrollNotes = type === "balloon" || type === "drumroll" || type === "daiDrumroll"
if(drumrollNotes && mekadon.getMS() > circle.endTime + mekadon.delay){
2018-09-21 22:31:35 +02:00
circle.played(-1, false)
mekadon.game.updateCurrentCircle()
}
if(drumrollNotes){
2018-11-13 05:36:15 +01:00
mekadon.playDrumrollAt(circle, 0, this.drumrollPace, type === "drumroll" || type === "daiDrumroll" ? this.kaAmount : 0)
2018-09-18 00:37:59 +02:00
}else if(this.notes.length === 0){
2018-09-12 19:10:00 +02:00
mekadon.play(circle)
}else{
var note = this.notes[0]
if(note.score >= 0){
var dai = 1
if(circle.type === "daiDon" || circle.type === "daiKa"){
dai = this.dai
}
2018-11-13 05:36:15 +01:00
if(mekadon.playAt(circle, note.ms, note.score, dai, note.reverse)){
2018-09-12 19:10:00 +02:00
this.notes.shift()
}
}else{
if(mekadon.miss(circle)){
this.notes.shift()
}
}
}
2018-09-14 00:55:23 +02:00
}else if(mekadon.miss(circle)){
this.notes.shift()
2018-09-12 19:10:00 +02:00
}
}
}