2024-03-01 22:24:30 +01:00
const socket = io ( ) ;
2024-03-02 13:56:01 +01:00
var playerIdx ;
var timerDestination = null ;
2024-03-03 01:29:11 +01:00
var gamePhase = 'pregame' ;
2024-03-03 22:59:52 +01:00
var occupiedFields = [ ] ;
2024-03-03 01:29:11 +01:00
2024-03-08 19:18:53 +01:00
var lastTimeClick = 0 ;
2024-03-07 21:56:44 +01:00
$ ( '#board .field' ) . on ( 'click' , function ( ) {
2024-03-08 19:18:53 +01:00
console . log ( new Date ( ) . getTime ( ) / 1000 - lastTimeClick ) ;
if ( new Date ( ) . getTime ( ) / 1000 - lastTimeClick > 0.3 ) {
socket . emit ( "place ship" , selectedShip , $ ( this ) . data ( 'pos-x' ) , $ ( this ) . data ( 'pos-y' ) , shipRotation ) ;
lastTimeClick = new Date ( ) . getTime ( ) / 1000 ;
}
2024-03-03 01:29:11 +01:00
} ) ;
2024-03-07 21:56:44 +01:00
$ ( '#secondaryBoard .field' ) . on ( 'click' , function ( ) {
2024-03-08 19:18:53 +01:00
if ( new Date ( ) . getTime ( ) / 1000 - lastTimeClick > 0.3 ) {
socket . emit ( "shoot" , $ ( this ) . data ( 'pos-x' ) , $ ( this ) . data ( 'pos-y' ) ) ;
lastTimeClick = new Date ( ) . getTime ( ) / 1000 ;
}
2024-03-07 21:56:44 +01:00
} ) ;
2024-03-03 22:59:52 +01:00
$ ( '.field' ) . on ( 'contextmenu' , function ( ) {
2024-03-08 19:18:53 +01:00
if ( $ ( this ) . hasClass ( 'active' ) && new Date ( ) . getTime ( ) / 1000 - lastTimeClick > 0.3 ) {
2024-03-03 22:59:52 +01:00
let originPos = occupiedFields . find ( ( elem ) => elem . pos [ 0 ] == $ ( this ) . data ( 'pos-x' ) && elem . pos [ 1 ] == $ ( this ) . data ( 'pos-y' ) ) . origin ;
socket . emit ( "remove ship" , originPos [ 0 ] , originPos [ 1 ] ) ;
2024-03-08 19:18:53 +01:00
lastTimeClick = new Date ( ) . getTime ( ) / 1000 ;
2024-03-03 22:59:52 +01:00
}
} ) ;
2024-03-03 01:29:11 +01:00
socket . on ( 'toast' , ( msg ) => {
Toastify ( {
text : msg ,
duration : 5000 ,
newWindow : true ,
gravity : "bottom" ,
position : "right" ,
stopOnFocus : true ,
className : "bshipstoast" ,
} ) . showToast ( ) ;
} ) ;
2024-03-02 13:56:01 +01:00
2024-03-03 16:55:38 +01:00
socket . on ( "placed ship" , ( data ) => {
2024-03-03 22:59:52 +01:00
let shipFields = bsc . placeShip ( data ) ;
2024-03-10 11:59:18 +01:00
lastTimeClick = new Date ( ) . getTime ( ) / 1000 ;
2024-03-03 22:59:52 +01:00
shipFields . forEach ( field => {
occupiedFields . push ( { pos : field , origin : [ data . posX , data . posY ] } ) ;
} ) ;
2024-03-03 16:55:38 +01:00
shipsLeft [ data . type ] -- ;
refreshBoardView ( ) ;
} ) ;
2024-03-03 22:59:52 +01:00
socket . on ( "removed ship" , ( data ) => {
const shipFields = occupiedFields . filter ( elem => {
return elem . origin [ 0 ] == data . posX && elem . origin [ 1 ] == data . posY ;
} ) ;
2024-03-08 19:18:53 +01:00
for ( let i = 0 ; i < shipFields . length ; i ++ ) {
const field = shipFields [ i ] ;
setTimeout ( ( ) => {
bsc . getField ( field . pos [ 0 ] , field . pos [ 1 ] ) . removeClass ( "active" ) ;
} , i * 150 ) ;
}
2024-03-03 22:59:52 +01:00
2024-03-04 12:23:47 +01:00
occupiedFields = occupiedFields . filter ( n => ! shipFields . includes ( n ) ) ;
2024-03-03 22:59:52 +01:00
shipsLeft [ data . type ] ++ ;
refreshBoardView ( ) ;
} ) ;
2024-03-07 21:56:44 +01:00
socket . on ( "shot hit" , ( victimIdx , posX , posY ) => {
if ( victimIdx === playerIdx ) {
bsc . setField ( posX , posY , "hit" ) ;
} else {
bsc . setFieldEnemy ( posX , posY , "hit" ) ;
}
} ) ;
socket . on ( "shot missed" , ( victimIdx , posX , posY ) => {
if ( victimIdx === playerIdx ) {
bsc . setField ( posX , posY , "miss" ) ;
} else {
bsc . setFieldEnemy ( posX , posY , "miss" ) ;
}
} ) ;
2024-03-08 19:18:53 +01:00
socket . on ( "ship sunk" , ( victimIdx , ship ) => {
switch ( ship . rot ) {
case 0 :
multips = [ 1 , 0 ] ;
break ;
case 1 :
multips = [ 0 , 1 ] ;
break ;
case 2 :
multips = [ - 1 , 0 ] ;
break ;
case 3 :
multips = [ 0 , - 1 ] ;
break ;
}
let l = ! ship . type ? ship . type + 1 : ship . type + 2 ;
if ( victimIdx === playerIdx ) {
for ( let i = 0 ; i < l ; i ++ ) {
console . log ( "ourship" ) ;
setTimeout ( ( ) => {
bsc . setField ( ship . posX + multips [ 0 ] * i , ship . posY + multips [ 1 ] * i , "sunken" ) ;
} , i * 150 ) ;
}
} else {
for ( let i = 0 ; i < l ; i ++ ) {
console . log ( "theirship" ) ;
setTimeout ( ( ) => {
bsc . setFieldEnemy ( ship . posX + multips [ 0 ] * i , ship . posY + multips [ 1 ] * i , "sunken" ) ;
} , i * 150 ) ;
}
}
} ) ;
2024-03-08 21:35:01 +01:00
// Update timer
var updateTimer = setInterval ( ( ) => {
if ( timerDestination == null ) {
$ ( "#timer" ) . html ( "" ) ;
} else {
const UTCNow = Math . floor ( ( new Date ( ) ) . getTime ( ) / 1000 ) ;
const time = Math . abs ( UTCNow - timerDestination ) ;
if ( time < 10 ) {
$ ( "#timer" ) . addClass ( "active" ) ;
} else {
$ ( "#timer" ) . removeClass ( "active" ) ;
}
const minutes = Math . floor ( time / 60 ) . toLocaleString ( 'pl-PL' , { minimumIntegerDigits : 2 , useGrouping : false } ) ;
const seconds = ( time - minutes * 60 ) . toLocaleString ( 'pl-PL' , { minimumIntegerDigits : 2 , useGrouping : false } ) ;
$ ( "#timer" ) . html ( ` ${ minutes } : ${ seconds } ` ) ;
}
} , 250 ) ;
socket . on ( "game finished" , ( winnerIdx , oppName ) => {
socket . disconnect ( ) ;
$ ( "#opponent" ) . html ( ` Vs. <span class="important"> ${ oppName } </span> ` ) ;
2024-03-08 19:18:53 +01:00
if ( winnerIdx === playerIdx ) {
2024-03-08 21:35:01 +01:00
$ ( "#state" ) . html ( "Zwycięstwo" ) ;
$ ( "#state" ) . addClass ( "dynamic" ) ;
2024-03-08 19:18:53 +01:00
} else {
2024-03-08 21:35:01 +01:00
$ ( "#state" ) . html ( "Porażka" ) ;
$ ( "#state" ) . addClass ( "danger" ) ;
2024-03-08 19:18:53 +01:00
}
2024-03-08 21:35:01 +01:00
$ ( ".cover" ) . addClass ( "postGame" ) ;
clearInterval ( updateTimer ) ;
$ ( "#timer" ) . html ( ` 00:00 ` ) ;
$ ( ".cover" ) . css ( { opacity : 1 , pointerEvents : "all" } ) ;
2024-03-08 19:18:53 +01:00
} ) ;
2024-03-02 13:56:01 +01:00
socket . on ( 'connect' , ( ) => {
2024-03-08 21:35:01 +01:00
$ ( ".cover .title" ) . html ( "Oczekiwanie na serwer..." ) ;
2024-03-02 13:56:01 +01:00
} ) ;
2024-03-01 22:24:30 +01:00
socket . on ( "players ready" , ( ) => {
2024-03-08 21:35:01 +01:00
$ ( ".cover" ) . css ( { opacity : 0 , pointerEvents : "none" } ) ;
2024-03-02 13:56:01 +01:00
} ) ;
socket . on ( "player idx" , ( idx ) => {
playerIdx = idx ;
} ) ;
socket . on ( 'turn update' , ( turnData ) => {
2024-03-02 18:28:33 +01:00
if ( turnData . phase === "preparation" ) {
$ ( "#whosTurn" ) . html ( "Faza przygotowań" ) ;
2024-03-05 21:34:16 +01:00
$ ( "#boardSwitch" ) . css ( "opacity" , 0.3 ) ;
2024-03-02 18:28:33 +01:00
} else {
2024-03-04 20:34:45 +01:00
postPrep = true ;
myTurn = turnData . turn === playerIdx ;
2024-03-02 18:28:33 +01:00
turnData . turn === playerIdx ? $ ( "#whosTurn" ) . html ( "Twoja tura" ) : $ ( "#whosTurn" ) . html ( "Tura przeciwnika" ) ;
2024-03-05 21:34:16 +01:00
$ ( "#boardSwitch" ) . css ( "opacity" , 1 ) ;
2024-03-02 18:28:33 +01:00
}
2024-03-02 13:56:01 +01:00
timerDestination = turnData . timerToUTC ;
gamePhase = turnData . phase ;
2024-03-04 20:34:45 +01:00
refreshBoardView ( ) ;
2024-03-02 13:56:01 +01:00
} ) ;
socket . on ( 'player left' , ( ) => {
window . location . replace ( "/" ) ;
2024-03-22 11:47:12 +01:00
} ) ;
// SELECT ROUND((1 - AVG(statistics.won)) * 100) AS winrate, COUNT(statistics.match_id) AS alltime_matches, COUNT(CASE WHEN (YEAR(matches.date) = YEAR(NOW()) AND MONTH(matches.date) = MONTH(NOW())) THEN matches.match_id END) AS monthly_matches FROM accounts NATURAL JOIN statistics NATURAL JOIN matches WHERE accounts.nickname = "MaciejkaG";