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
|
2018-11-02 18:26:46 +08:00
|
|
|
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)
|
Custom scripting, #song=, translations
- A song can be linked directly by adding "#song=<id>" to the url, replace `<id>` with the id in the database, after loading it jumps immediately jumps to the difficulty selection
- Added tutorial translations
- Fixed song preview not playing
- Use text fallback for the logo when there are no vectors
- Increased combo cache by 1 pixel
- A custom javascript file can be loaded from config.json by defining "custom_js" value
- Added lots of events to help writing custom js files: `version-link, title-screen, language-change, song-select, song-select-move, song-select-difficulty, song-select-back, about, about-link, tutorial, import-songs, import-songs-default, session, session-start, session-end, debug, load-song, load-song-player2, load-song-unfocused, load-song-cancel, load-song-error, game-start, key-events, p2-game-end, p2-disconnected, p2-abandoned, pause, unpause, pause-restart, pause-song-select, game-lag, scoresheet, scoresheet-player2`
- Event syntax example:
```js
addEventListener("game-start", event => {
console.log("game-start", event.detail)
})
```
2019-02-14 17:32:45 +08:00
|
|
|
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
|
2018-09-19 01:33:18 +08:00
|
|
|
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
|
2019-02-18 00:26:46 +08:00
|
|
|
this.branch = "normal"
|
2018-09-18 06:37:59 +08:00
|
|
|
break
|
|
|
|
case "gameend":
|
|
|
|
this.otherConnected = false
|
Custom scripting, #song=, translations
- A song can be linked directly by adding "#song=<id>" to the url, replace `<id>` with the id in the database, after loading it jumps immediately jumps to the difficulty selection
- Added tutorial translations
- Fixed song preview not playing
- Use text fallback for the logo when there are no vectors
- Increased combo cache by 1 pixel
- A custom javascript file can be loaded from config.json by defining "custom_js" value
- Added lots of events to help writing custom js files: `version-link, title-screen, language-change, song-select, song-select-move, song-select-difficulty, song-select-back, about, about-link, tutorial, import-songs, import-songs-default, session, session-start, session-end, debug, load-song, load-song-player2, load-song-unfocused, load-song-cancel, load-song-error, game-start, key-events, p2-game-end, p2-disconnected, p2-abandoned, pause, unpause, pause-restart, pause-song-select, game-lag, scoresheet, scoresheet-player2`
- Event syntax example:
```js
addEventListener("game-start", event => {
console.log("game-start", event.detail)
})
```
2019-02-14 17:32:45 +08:00
|
|
|
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)
|
2018-09-19 01:33:18 +08:00
|
|
|
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
|
2019-02-18 00:26:46 +08:00
|
|
|
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){
|
2019-02-18 00:26:46 +08:00
|
|
|
var type = circle.type
|
2018-09-22 04:31:35 +08:00
|
|
|
var drumrollNotes = type === "balloon" || type === "drumroll" || type === "daiDrumroll"
|
|
|
|
|
2019-02-18 00:26:46 +08:00
|
|
|
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){
|
2018-09-19 01:33:18 +08:00
|
|
|
var dai = 1
|
2019-02-18 00:26:46 +08:00
|
|
|
if(circle.type === "daiDon" || circle.type === "daiKa"){
|
2018-09-19 01:33:18 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|