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

77 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-11-02 06:05:18 +08:00
class Session{
constructor(touchEnabled){
this.touchEnabled = touchEnabled
loader.changePage("session", true)
2018-11-02 06:05:18 +08:00
this.endButton = document.getElementById("tutorial-end-button")
if(touchEnabled){
document.getElementById("tutorial-outer").classList.add("touch-enabled")
}
this.sessionInvite = document.getElementById("session-invite")
2019-01-23 02:47:09 +08:00
var tutorialTitle = document.getElementById("tutorial-title")
tutorialTitle.innerText = strings.session.multiplayerSession
tutorialTitle.setAttribute("alt", strings.session.multiplayerSession)
this.sessionInvite.parentNode.insertBefore(document.createTextNode(strings.session.linkTutorial), this.sessionInvite)
this.endButton.innerText = strings.session.cancel
this.endButton.setAttribute("alt", strings.session.cancel)
2018-11-02 06:05:18 +08:00
pageEvents.add(window, ["mousedown", "touchstart"], this.mouseDown.bind(this))
pageEvents.keyOnce(this, 27, "down").then(this.onEnd.bind(this))
this.gamepad = new Gamepad({
"confirm": ["start", "b", "ls", "rs"]
}, this.onEnd.bind(this))
p2.hashLock = true
pageEvents.add(p2, "message", response => {
if(response.type === "invite"){
this.sessionInvite.innerText = location.origin + location.pathname + "#" + response.value
p2.hash(response.value)
}else if(response.type === "songsel"){
p2.clearMessage("users")
this.onEnd(false, true)
pageEvents.send("session-start")
2018-11-02 06:05:18 +08:00
}
})
p2.send("invite")
pageEvents.send("session")
2018-11-02 06:05:18 +08:00
}
mouseDown(event){
if(event.target === this.sessionInvite){
this.sessionInvite.focus()
}else{
getSelection().removeAllRanges()
this.sessionInvite.blur()
}
if(event.target === this.endButton){
this.onEnd()
}
}
onEnd(event, fromP2){
if(!p2.session){
p2.send("leave")
p2.hash("")
p2.hashLock = false
pageEvents.send("session-cancel")
2018-11-02 06:05:18 +08:00
}else if(!fromP2){
return p2.send("songsel")
}
if(event && event.type === "keydown"){
event.preventDefault()
}
this.clean()
2019-02-04 17:14:42 +08:00
assets.sounds["se_don"].play()
2018-11-02 06:05:18 +08:00
setTimeout(() => {
new SongSelect(false, false, this.touchEnabled)
}, 500)
}
clean(){
this.gamepad.clean()
pageEvents.remove(window, ["mousedown", "touchstart"])
pageEvents.keyRemove(this, 27)
2018-12-16 03:13:16 +08:00
pageEvents.remove(p2, "message")
2018-11-02 06:05:18 +08:00
delete this.endButton
delete this.sessionInvite
}
}