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

269 lines
6.4 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 = ""
this.disabled = 0
2018-11-01 23:05:18 +01:00
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(){
Bug fixes - Change song select mouse wheel song scrolling to be instant - Clicking on don chan in account settings toggles the animation - If the music is too long for the chart, the results screen is shown earlier - Fix weird BPM values freezing the browser (zero, negative, and very large) - Add a warning to the page when JavaScript is disabled in the browser - Fix Chrome auto dark mode by forcing light mode on the page - Add a meta keywords tag to the page - Fix plugin names getting cut off in the menu - Delay the function editing of the EditFunction class in plugins to the start() function instead of load() - When stopping one of the plugins, all the plugins have to be stopped in reverse order and started again so that patched code of a stopped plugin does not linger around - Fix importing plugins that have a SyntaxError - Fix plugins getting the same internal name when added without one, causing them to not appear in the plugin settings - Support editing args in EditFunction for plugins - Prevent multiple websockets from being opened - Fix page freezing after selecting Random song with no songs - Fix the back button being repeated twice when there are no songs - Fix /admin/users not accepting case insensitive usernames - Pressing enter on the Delete Account field does the expected action instead of refreshing the page - Better error message when custom folder access is denied - Fix being able to start netplay in custom songs after refreshing the page (#383) - Fix an error when importing songs from previous session and clicking on the white spot where you normally start multiplayer session - Fix canvas elements becoming smaller than 1x1 resolution and crashing the game (#390) - Fix song frame shadow cache on song select not being cleared when resizing the browser window, causing it to become blurry - Fix a pause-restart error when you hit both confirm keys on the restart button
2022-02-17 21:50:07 +01:00
if(this.closed && !this.disabled){
this.closed = false
var wsProtocol = location.protocol == "https:" ? "wss:" : "ws:"
this.socket = new WebSocket(gameConfig.multiplayer_url ? gameConfig.multiplayer_url : wsProtocol + "//" + location.host + location.pathname + "p2")
Bug fixes - Change song select mouse wheel song scrolling to be instant - Clicking on don chan in account settings toggles the animation - If the music is too long for the chart, the results screen is shown earlier - Fix weird BPM values freezing the browser (zero, negative, and very large) - Add a warning to the page when JavaScript is disabled in the browser - Fix Chrome auto dark mode by forcing light mode on the page - Add a meta keywords tag to the page - Fix plugin names getting cut off in the menu - Delay the function editing of the EditFunction class in plugins to the start() function instead of load() - When stopping one of the plugins, all the plugins have to be stopped in reverse order and started again so that patched code of a stopped plugin does not linger around - Fix importing plugins that have a SyntaxError - Fix plugins getting the same internal name when added without one, causing them to not appear in the plugin settings - Support editing args in EditFunction for plugins - Prevent multiple websockets from being opened - Fix page freezing after selecting Random song with no songs - Fix the back button being repeated twice when there are no songs - Fix /admin/users not accepting case insensitive usernames - Pressing enter on the Delete Account field does the expected action instead of refreshing the page - Better error message when custom folder access is denied - Fix being able to start netplay in custom songs after refreshing the page (#383) - Fix an error when importing songs from previous session and clicking on the white spot where you normally start multiplayer session - Fix canvas elements becoming smaller than 1x1 resolution and crashing the game (#390) - Fix song frame shadow cache on song select not being cleared when resizing the browser window, causing it to become blurry - Fix a pause-restart error when you hit both confirm keys on the restart button
2022-02-17 21:50:07 +01:00
pageEvents.race(this.socket, "open", "close").then(response => {
if(response.type === "open"){
return this.openEvent()
}
return this.closeEvent()
})
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(){
Bug fixes - Change song select mouse wheel song scrolling to be instant - Clicking on don chan in account settings toggles the animation - If the music is too long for the chart, the results screen is shown earlier - Fix weird BPM values freezing the browser (zero, negative, and very large) - Add a warning to the page when JavaScript is disabled in the browser - Fix Chrome auto dark mode by forcing light mode on the page - Add a meta keywords tag to the page - Fix plugin names getting cut off in the menu - Delay the function editing of the EditFunction class in plugins to the start() function instead of load() - When stopping one of the plugins, all the plugins have to be stopped in reverse order and started again so that patched code of a stopped plugin does not linger around - Fix importing plugins that have a SyntaxError - Fix plugins getting the same internal name when added without one, causing them to not appear in the plugin settings - Support editing args in EditFunction for plugins - Prevent multiple websockets from being opened - Fix page freezing after selecting Random song with no songs - Fix the back button being repeated twice when there are no songs - Fix /admin/users not accepting case insensitive usernames - Pressing enter on the Delete Account field does the expected action instead of refreshing the page - Better error message when custom folder access is denied - Fix being able to start netplay in custom songs after refreshing the page (#383) - Fix an error when importing songs from previous session and clicking on the white spot where you normally start multiplayer session - Fix canvas elements becoming smaller than 1x1 resolution and crashing the game (#390) - Fix song frame shadow cache on song select not being cleared when resizing the browser window, causing it to become blurry - Fix a pause-restart error when you hit both confirm keys on the restart button
2022-02-17 21:50:07 +01:00
if(!this.closed){
this.closed = true
if(this.socket){
this.socket.close()
}
}
2018-09-12 19:10:00 +02:00
}
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
}
}
Bug fixes - Change song select mouse wheel song scrolling to be instant - Clicking on don chan in account settings toggles the animation - If the music is too long for the chart, the results screen is shown earlier - Fix weird BPM values freezing the browser (zero, negative, and very large) - Add a warning to the page when JavaScript is disabled in the browser - Fix Chrome auto dark mode by forcing light mode on the page - Add a meta keywords tag to the page - Fix plugin names getting cut off in the menu - Delay the function editing of the EditFunction class in plugins to the start() function instead of load() - When stopping one of the plugins, all the plugins have to be stopped in reverse order and started again so that patched code of a stopped plugin does not linger around - Fix importing plugins that have a SyntaxError - Fix plugins getting the same internal name when added without one, causing them to not appear in the plugin settings - Support editing args in EditFunction for plugins - Prevent multiple websockets from being opened - Fix page freezing after selecting Random song with no songs - Fix the back button being repeated twice when there are no songs - Fix /admin/users not accepting case insensitive usernames - Pressing enter on the Delete Account field does the expected action instead of refreshing the page - Better error message when custom folder access is denied - Fix being able to start netplay in custom songs after refreshing the page (#383) - Fix an error when importing songs from previous session and clicking on the white spot where you normally start multiplayer session - Fix canvas elements becoming smaller than 1x1 resolution and crashing the game (#390) - Fix song frame shadow cache on song select not being cleared when resizing the browser window, causing it to become blurry - Fix a pause-restart error when you hit both confirm keys on the restart button
2022-02-17 21:50:07 +01:00
enable(){
this.disabled = Math.max(0, this.disabled - 1)
setTimeout(this.open.bind(this), 100)
Bug fixes - Change song select mouse wheel song scrolling to be instant - Clicking on don chan in account settings toggles the animation - If the music is too long for the chart, the results screen is shown earlier - Fix weird BPM values freezing the browser (zero, negative, and very large) - Add a warning to the page when JavaScript is disabled in the browser - Fix Chrome auto dark mode by forcing light mode on the page - Add a meta keywords tag to the page - Fix plugin names getting cut off in the menu - Delay the function editing of the EditFunction class in plugins to the start() function instead of load() - When stopping one of the plugins, all the plugins have to be stopped in reverse order and started again so that patched code of a stopped plugin does not linger around - Fix importing plugins that have a SyntaxError - Fix plugins getting the same internal name when added without one, causing them to not appear in the plugin settings - Support editing args in EditFunction for plugins - Prevent multiple websockets from being opened - Fix page freezing after selecting Random song with no songs - Fix the back button being repeated twice when there are no songs - Fix /admin/users not accepting case insensitive usernames - Pressing enter on the Delete Account field does the expected action instead of refreshing the page - Better error message when custom folder access is denied - Fix being able to start netplay in custom songs after refreshing the page (#383) - Fix an error when importing songs from previous session and clicking on the white spot where you normally start multiplayer session - Fix canvas elements becoming smaller than 1x1 resolution and crashing the game (#390) - Fix song frame shadow cache on song select not being cleared when resizing the browser window, causing it to become blurry - Fix a pause-restart error when you hit both confirm keys on the restart button
2022-02-17 21:50:07 +01:00
}
disable(){
this.disabled++
Bug fixes - Change song select mouse wheel song scrolling to be instant - Clicking on don chan in account settings toggles the animation - If the music is too long for the chart, the results screen is shown earlier - Fix weird BPM values freezing the browser (zero, negative, and very large) - Add a warning to the page when JavaScript is disabled in the browser - Fix Chrome auto dark mode by forcing light mode on the page - Add a meta keywords tag to the page - Fix plugin names getting cut off in the menu - Delay the function editing of the EditFunction class in plugins to the start() function instead of load() - When stopping one of the plugins, all the plugins have to be stopped in reverse order and started again so that patched code of a stopped plugin does not linger around - Fix importing plugins that have a SyntaxError - Fix plugins getting the same internal name when added without one, causing them to not appear in the plugin settings - Support editing args in EditFunction for plugins - Prevent multiple websockets from being opened - Fix page freezing after selecting Random song with no songs - Fix the back button being repeated twice when there are no songs - Fix /admin/users not accepting case insensitive usernames - Pressing enter on the Delete Account field does the expected action instead of refreshing the page - Better error message when custom folder access is denied - Fix being able to start netplay in custom songs after refreshing the page (#383) - Fix an error when importing songs from previous session and clicking on the white spot where you normally start multiplayer session - Fix canvas elements becoming smaller than 1x1 resolution and crashing the game (#390) - Fix song frame shadow cache on song select not being cleared when resizing the browser window, causing it to become blurry - Fix a pause-restart error when you hit both confirm keys on the restart button
2022-02-17 21:50:07 +01:00
this.close()
}
2018-09-12 19:10:00 +02:00
}