japanese-drum-game/public/src/js/p2.js

202 lines
4.8 KiB
JavaScript
Raw Normal View History

2018-09-13 01:10:00 +08:00
class P2Connection{
constructor(){
this.closed = true
this.lastMessages = {}
this.otherConnected = false
2018-09-18 06:37:59 +08:00
this.allEvents = new Map()
this.addEventListener("message", this.message.bind(this))
2018-11-02 06:05:18 +08:00
this.currentHash = ""
pageEvents.add(window, "hashchange", this.onhashchange.bind(this))
2018-09-18 06:37:59 +08: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-13 01:10:00 +08:00
}
open(){
this.closed = false
var wsProtocol = location.protocol == "https:" ? "wss:" : "ws:"
2018-09-13 01:39:29 +08:00
this.socket = new WebSocket(wsProtocol + "//" + location.host + "/p2")
2018-11-02 06:05:18 +08:00
pageEvents.race(this.socket, "open", "close").then(response => {
if(response.type === "open"){
2018-09-18 06:37:59 +08:00
return this.openEvent()
2018-09-13 01:10:00 +08:00
}
2018-09-18 06:37:59 +08:00
return this.closeEvent()
2018-09-13 01:10:00 +08:00
})
2018-09-18 06:37:59 +08:00
pageEvents.add(this.socket, "message", this.messageEvent.bind(this))
2018-09-13 01:10:00 +08:00
}
2018-09-18 06:37:59 +08:00
openEvent(){
var addedType = this.allEvents.get("open")
if(addedType){
addedType.forEach(callback => callback())
}
2018-09-13 01:10:00 +08:00
}
close(){
this.closed = true
this.socket.close()
}
2018-09-18 06:37:59 +08:00
closeEvent(){
this.removeEventListener(onmessage)
this.otherConnected = false
this.session = false
if(this.hashLock){
this.hash("")
this.hashLock = false
}
2018-09-13 01:10:00 +08:00
if(!this.closed){
setTimeout(() => {
2018-09-18 06:37:59 +08:00
if(this.socket.readyState !== this.socket.OPEN){
2018-09-13 01:10:00 +08:00
this.open()
}
}, 500)
pageEvents.send("p2-disconnected")
2018-09-13 01:10:00 +08:00
}
2018-09-18 06:37:59 +08:00
var addedType = this.allEvents.get("close")
if(addedType){
addedType.forEach(callback => callback())
}
2018-09-13 01:10:00 +08:00
}
send(type, value){
2018-09-18 06:37:59 +08:00
if(this.socket.readyState === this.socket.OPEN){
if(typeof value === "undefined"){
2018-09-13 01:10:00 +08:00
this.socket.send(JSON.stringify({type: type}))
}else{
this.socket.send(JSON.stringify({type: type, value: value}))
}
}else{
2018-09-18 06:37:59 +08:00
pageEvents.once(this, "open").then(() => {
2018-09-13 01:10:00 +08:00
this.send(type, value)
2018-09-18 06:37:59 +08:00
})
2018-09-13 01:10:00 +08:00
}
}
messageEvent(event){
try{
2018-09-18 06:37:59 +08:00
var response = JSON.parse(event.data)
2018-09-13 01:10:00 +08:00
}catch(e){
2018-09-18 06:37:59 +08:00
var response = {}
2018-09-13 01:10:00 +08:00
}
2018-11-02 06:05:18 +08:00
this.lastMessages[response.type] = response
2018-09-18 06:37:59 +08:00
var addedType = this.allEvents.get("message")
if(addedType){
addedType.forEach(callback => callback(response))
2018-09-13 01:10:00 +08:00
}
}
2018-11-02 06:05:18 +08:00
getMessage(type){
2018-09-13 01:10:00 +08:00
if(type in this.lastMessages){
2018-09-18 06:37:59 +08:00
return this.lastMessages[type]
}
}
2018-11-02 06:05:18 +08:00
clearMessage(type){
if(type in this.lastMessages){
this.lastMessages[type] = null
}
}
2018-09-18 06:37:59 +08:00
message(response){
switch(response.type){
case "gamestart":
this.otherConnected = true
this.notes = []
this.drumrollPace = 45
this.dai = 2
2018-11-13 12:36:15 +08:00
this.kaAmount = 0
2018-09-18 06:37:59 +08:00
this.results = false
this.branch = "normal"
2018-09-18 06:37:59 +08: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-02 06:05:18 +08:00
this.session = false
if(this.hashLock){
this.hash("")
this.hashLock = false
}
2018-09-18 06:37:59 +08:00
break
case "gameresults":
2018-10-03 17:48:18 +08:00
this.results = {}
for(var i in response.value){
this.results[i] = response.value[i].toString()
}
2018-09-18 06:37:59 +08:00
break
case "note":
this.notes.push(response.value)
if(response.value.dai){
this.dai = response.value.dai
}
2018-09-18 06:37:59 +08:00
break
case "drumroll":
this.drumrollPace = response.value.pace
2018-11-13 12:36:15 +08:00
if("kaAmount" in response.value){
this.kaAmount = response.value.kaAmount
}
2018-09-18 06:37:59 +08:00
break
case "branch":
this.branch = response.value
this.branchSet = false
break
2018-11-02 06:05:18 +08:00
case "session":
this.clearMessage("users")
this.otherConnected = true
this.session = true
break
2018-09-13 01:10:00 +08:00
}
}
2018-11-02 06:05:18 +08: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-13 01:10:00 +08:00
play(circle, mekadon){
2018-09-14 06:55:23 +08:00
if(this.otherConnected || this.notes.length > 0){
var type = circle.type
2018-09-22 04:31:35 +08:00
var drumrollNotes = type === "balloon" || type === "drumroll" || type === "daiDrumroll"
if(drumrollNotes && mekadon.getMS() > circle.endTime){
2018-09-22 04:31:35 +08:00
circle.played(-1, false)
mekadon.game.updateCurrentCircle()
}
if(drumrollNotes){
2018-11-13 12:36:15 +08:00
mekadon.playDrumrollAt(circle, 0, this.drumrollPace, type === "drumroll" || type === "daiDrumroll" ? this.kaAmount : 0)
2018-09-18 06:37:59 +08:00
}else if(this.notes.length === 0){
2018-09-13 01:10:00 +08: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 12:36:15 +08:00
if(mekadon.playAt(circle, note.ms, note.score, dai, note.reverse)){
2018-09-13 01:10:00 +08:00
this.notes.shift()
}
}else{
if(mekadon.miss(circle)){
this.notes.shift()
}
}
}
2018-09-14 06:55:23 +08:00
}else if(mekadon.miss(circle)){
this.notes.shift()
2018-09-13 01:10:00 +08:00
}
}
}