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

144 lines
3.4 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))
}
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-09-18 06:37:59 +08:00
pageEvents.race(this.socket, "open", "close", listener =>{
if(listener === "open"){
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
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)
}
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-09-18 06:37:59 +08:00
this.lastMessages[response.type] = response.value
var addedType = this.allEvents.get("message")
if(addedType){
addedType.forEach(callback => callback(response))
2018-09-13 01:10:00 +08:00
}
}
getMessage(type, callback){
if(type in this.lastMessages){
2018-09-18 06:37:59 +08:00
return this.lastMessages[type]
}
}
message(response){
switch(response.type){
case "gamestart":
this.otherConnected = true
this.notes = []
this.drumrollPace = 45
this.dai = 2
2018-09-18 06:37:59 +08:00
this.results = false
break
case "gameend":
this.otherConnected = false
break
case "gameresults":
this.results = response.value
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
break
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){
2018-09-15 22:34:53 +08:00
var type = circle.getType()
2018-09-18 06:37:59 +08:00
if(type === "balloon"|| type === "drumroll" || type === "daiDrumroll"){
2018-09-15 22:34:53 +08:00
mekadon.playDrumrollAt(circle, 0, this.drumrollPace)
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.getType() === "daiDon" || circle.getType() === "daiKa"){
dai = this.dai
}
if(mekadon.playAt(circle, note.ms, note.score, dai)){
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
}
}
}