2024-01-06 21:09:41 +01:00
const socket = io ( ) ;
2024-03-08 19:18:53 +01:00
// Handling server-sent events
2024-02-29 21:55:56 +01:00
socket . on ( "joined" , ( nick ) => {
2024-03-25 12:41:02 +01:00
returnLock = false ;
2024-02-29 21:55:56 +01:00
lockUI ( true ) ;
$ ( "#oppNameField" ) . html ( nick ) ;
switchView ( "preparingGame" ) ;
lockUI ( false ) ;
2024-04-04 16:08:27 +02:00
console . log ( "Player joined the game:" , nick ) ;
2024-01-08 16:53:12 +01:00
} ) ;
2024-03-01 22:24:30 +01:00
socket . on ( "player left" , ( ) => {
lockUI ( true ) ;
switchView ( "mainMenuView" ) ;
lockUI ( false ) ;
2024-04-04 16:08:27 +02:00
console . log ( "Player left the game" ) ;
2024-03-01 22:24:30 +01:00
} ) ;
socket . on ( "gameReady" , ( gameId ) => {
2024-04-04 16:08:27 +02:00
console . log ( "Game is ready, redirecting in 2 seconds. Game ID:" , gameId ) ;
2024-03-01 22:24:30 +01:00
setTimeout ( ( ) => {
2024-04-04 16:08:27 +02:00
console . log ( "Redirecting..." ) ;
2024-03-01 22:24:30 +01:00
window . location . replace ( "/game?id=" + gameId ) ;
} , 2000 ) ;
} ) ;
var nickname ;
2024-03-19 22:33:14 +01:00
2024-04-06 01:48:49 +02:00
socket . emit ( 'locale options' , ( langs ) => {
console . log ( "Fetching available locale options" ) ;
let menu = "" ;
langs . forEach ( lang => {
menu += ` <option value=" ${ lang . id } "> ${ lang . name } </option> ` ;
} ) ;
$ ( "#languages" ) . html ( menu ) ;
console . log ( "Locale options fetched" ) ;
} ) ;
$ ( "#languages" ) . on ( "change" , function ( ) {
lockUI ( true ) ;
console . log ( "Switching language to" , $ ( this ) . val ( ) ) ;
socket . emit ( "change locale" , $ ( this ) . val ( ) , ( response ) => {
switch ( response . status ) {
case "ok" :
console . log ( "Switched languages, refreshing" ) ;
window . location . reload ( ) ;
break ;
default :
alert ( ` ${ window . locale [ "Unknown error occured" ] } \n ${ window . locale [ "Status:" ] } ${ response . status } ` ) ;
lockUI ( false ) ;
break ;
}
} ) ;
} ) ;
2024-03-24 19:51:49 +01:00
socket . emit ( "my profile" , ( profile ) => {
2024-04-06 01:48:49 +02:00
console . log ( "Received user data. UID:" , profile . uid ) ;
2024-03-24 19:51:49 +01:00
// General profile data
let options = { weekday : 'long' , year : 'numeric' , month : 'long' , day : 'numeric' } ;
2024-03-27 15:51:33 +01:00
$ ( "#playerSince" ) . html ( new Date ( profile . profile . account _creation ) . toLocaleDateString ( undefined , options ) ) ;
2024-03-24 19:51:49 +01:00
$ ( "#nickname" ) . html ( profile . profile . nickname ) ;
// Profile stats
$ ( "#monthlyPlayed" ) . html ( profile . stats . monthly _matches ) ;
$ ( "#totalPlayed" ) . html ( profile . stats . alltime _matches ) ;
2024-03-25 12:41:02 +01:00
$ ( "#winrate" ) . html ( profile . stats . winrate !== null ? ` ${ profile . stats . winrate } % ` : "-" ) ;
2024-03-24 19:51:49 +01:00
2024-03-24 22:00:05 +01:00
// Match history
2024-03-24 19:51:49 +01:00
var matchHistory = profile . matchHistory ;
var matchHistoryDOM = "" ;
options = { hour : '2-digit' , minute : '2-digit' , time : 'numeric' , weekday : 'long' , year : 'numeric' , month : 'long' , day : 'numeric' } ;
for ( let i = 0 ; i < matchHistory . length ; i ++ ) {
const match = matchHistory [ i ] ;
2024-03-26 12:41:28 +01:00
let date = new Date ( match . date ) . toLocaleDateString ( undefined , options ) ;
2024-03-24 19:51:49 +01:00
2024-03-26 12:41:28 +01:00
const minutes = Math . floor ( match . duration / 60 ) . toLocaleString ( undefined , { minimumIntegerDigits : 2 , useGrouping : false } ) ;
2024-03-27 15:51:33 +01:00
const seconds = ( match . duration - minutes * 60 ) . toLocaleString ( undefined , { minimumIntegerDigits : 2 , useGrouping : false } ) ;
2024-03-24 19:51:49 +01:00
const duration = ` ${ minutes } : ${ seconds } ` ;
2024-03-27 15:51:33 +01:00
matchHistoryDOM += ` <div class="match" data-matchid=" ${ match . match _id } "><div><h1 class="dynamic ${ match . won === 1 ? "" : " danger" } "> ${ match . won === 1 ? window . locale [ "Victory" ] : window . locale [ "Defeat" ] } </h1><span> vs. ${ match . match _type === "pvp" ? match . opponent : "AI" } </span></div><h2 class="statsButton"> ${ window . locale [ "Click to view match statistics" ] } </h2><span> ${ date } </span><br><span> ${ duration } </span></div> ` ;
2024-03-24 19:51:49 +01:00
}
2024-04-06 01:48:49 +02:00
if ( ! matchHistoryDOM ) {
2024-03-27 15:51:33 +01:00
matchHistoryDOM = ` <h2> ${ window . locale [ "No matches played" ] } </h2> ` ;
2024-03-25 12:41:02 +01:00
}
2024-03-24 19:51:49 +01:00
$ ( ".matchList" ) . html ( matchHistoryDOM ) ;
2024-04-04 16:08:27 +02:00
console . log ( "Profile data fetched successfully" ) ;
2024-03-19 22:33:14 +01:00
} ) ;
2024-03-01 22:24:30 +01:00
socket . emit ( "whats my nick" , ( myNickname ) => {
nickname = myNickname ;
2024-03-19 22:33:14 +01:00
$ ( "#profileButton" ) . html ( nickname ) ;
2024-04-04 16:08:27 +02:00
console . log ( "Received player nickname:" , myNickname ) ;
2024-03-01 22:24:30 +01:00
} ) ;
2024-01-08 16:53:12 +01:00
$ ( "#createGameButton" ) . on ( "click" , function ( ) {
lockUI ( true ) ;
2024-04-04 16:08:27 +02:00
console . log ( "Creating a lobby..." ) ;
2024-01-08 16:53:12 +01:00
socket . emit ( "create lobby" , ( response ) => {
switch ( response . status ) {
case "ok" :
2024-04-04 16:08:27 +02:00
console . log ( "Lobby created" ) ;
2024-01-08 16:53:12 +01:00
$ ( "#createGameCode" ) . val ( response . gameCode ) ;
switchView ( "pvpCreateView" ) ;
2024-03-19 22:33:14 +01:00
returnLock = true ;
2024-01-08 16:53:12 +01:00
lockUI ( false ) ;
break ;
case "alreadyInLobby" :
2024-04-04 16:08:27 +02:00
console . log ( "Lobby creation failed (player is already in a lobby)" ) ;
2024-01-08 16:53:12 +01:00
$ ( "#createGameCode" ) . val ( response . gameCode ) ;
switchView ( "pvpCreateView" ) ;
lockUI ( false ) ;
break ;
default :
2024-04-04 16:08:27 +02:00
console . log ( "Lobby creation failed (unknown)" ) ;
2024-03-27 15:51:33 +01:00
alert ( ` ${ window . locale [ "Unknown error occured" ] } \n ${ window . locale [ "Status:" ] } ${ response . status } ` ) ;
2024-01-08 16:53:12 +01:00
lockUI ( false ) ;
break ;
}
} ) ;
} ) ;
$ ( "#leaveGameButton" ) . on ( "click" , function ( ) {
lockUI ( true ) ;
2024-03-09 01:11:03 +01:00
window . location . reload ( ) ;
2024-01-08 16:53:12 +01:00
} ) ;
$ ( "#pvpMenuButton" ) . on ( "click" , function ( ) {
switchView ( 'pvpMenuView' ) ;
} ) ;
const form = document . getElementById ( 'pvpJoinForm' ) ;
const input = document . getElementById ( 'pvpJoinCode' ) ;
form . addEventListener ( 'submit' , ( e ) => {
e . preventDefault ( ) ;
if ( input . value && input . value . length === 6 ) {
lockUI ( true ) ;
2024-04-04 16:08:27 +02:00
console . log ( "Joining a lobby with code:" , input . value ) ;
2024-01-08 16:53:12 +01:00
socket . emit ( "join lobby" , input . value , ( response ) => {
switch ( response . status ) {
case "ok" :
2024-04-04 16:08:27 +02:00
console . log ( "Joined a lobby by:" , response . oppNickname ) ;
2024-02-29 21:55:56 +01:00
$ ( "#oppNameField" ) . html ( response . oppNickname ) ;
switchView ( "preparingGame" ) ;
2024-01-08 16:53:12 +01:00
lockUI ( false ) ;
break ;
//case "alreadyInLobby":
// $("#createGameCode").val(response.gameCode);
// switchView("pvpCreateView");
// lockUI(false);
// break;
default :
2024-03-27 15:51:33 +01:00
alert ( ` ${ window . locale [ "Unknown error occured" ] } \n ${ window . locale [ "Status:" ] } ${ response . status } ` ) ;
2024-01-08 16:53:12 +01:00
lockUI ( false ) ;
switchView ( "mainMenuView" ) ;
break ;
}
} ) ;
input . value = '' ;
}
} ) ;