1 // TODO: main playing hall, chat + online players + current challenges + button "new game"
3 input#modal-newgame.modal(type="checkbox")
4 div(role="dialog" aria-labelledby="newGameTxt")
5 .card.smallpad.small-modal
6 label#close-newgame.modal-close(for="modal-newgame")
7 h3#newGameTxt= translations["New game"]
8 p= translations["Waiting for opponent..."]
14 // TODO: my-challenge-list, gérant clicks sur challenges, affichage, réception/émission des infos sur challenges
15 // de même, my-player-list
20 // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés
23 Players + challenges : == "room" home of variant (surligner si nouveau défi perso et pas affichage courant)
24 joueurs en ligne (dte),
25 Nouvelle partie + défis en temps réel + parties en cours (milieu, tabs),
26 chat général (gauche, activé ou non (bool global storage)).
27 (cadences base + incrément, corr == incr >= 1jour ou base >= 7j)
28 --> correspondance: stocker sur serveur lastMove + peerId + color + movesCount + gameId + variant + timeleft
29 quand je poste un lastMove corr, supprimer mon ancien lastMove le cas échéant (tlm l'a eu)
30 fin de partie corr: garder maxi nbPlayers lastMove sur serveur, pendant 7 jours (arbitraire)
32 case "newgame": //opponent found
33 // oppid: opponent socket ID
34 this.newGame("human", data
.fen
, data
.color
, data
.oppid
, data
.gameid
);
37 // TODO: elsewhere, probably (new game button)
38 clickGameSeek: function(e
) {
39 this.getRidOfTooltip(e
.currentTarget
);
40 if (this.mode
== "human" && this.score
== "*")
41 return; //no newgame while playing
44 this.conn
.send(JSON
.stringify({code:"cancelnewgame"}));
48 this.newGame("human");
50 clickComputerGame: function(e
) {
51 this.getRidOfTooltip(e
.currentTarget
);
52 if (this.mode
== "computer" && this.score
== "*"
53 && this.vr
.turn
!= this.mycolor
)
55 // Wait for computer reply first (avoid potential "ghost move" bug)
58 this.newGame("computer");
60 clickFriendGame: function(e
) {
61 this.getRidOfTooltip(e
.currentTarget
);
62 document
.getElementById("modal-fenedit").checked
= true;
65 newGame: function(mode
, fenInit
, color
, oppId
, gameId
) {
66 const fen
= fenInit
|| VariantRules
.GenRandInitFen();
67 console
.log(fen
); //DEBUG
68 if (mode
=="human" && !oppId
)
70 const storageVariant
= localStorage
.getItem("variant");
71 if (!!storageVariant
&& storageVariant
!== variant
.name
72 && localStorage
["score"] == "*")
74 return alert(translations
["Finish your "] +
75 storageVariant
+ translations
[" game first!"]);
77 // Send game request and wait..
79 this.conn
.send(JSON
.stringify({code:"newgame", fen:fen
, gameid: getRandString() }));
80 } catch (INVALID_STATE_ERR
) {
81 return; //nothing achieved
84 let modalBox
= document
.getElementById("modal-newgame");
85 modalBox
.checked
= true;
86 setTimeout(() => { modalBox
.checked
= false; }, 2000);
89 const prefix
= this.getStoragePrefix(mode
);
90 if (mode
== "computer")
92 const storageVariant
= localStorage
.getItem(prefix
+"variant");
95 const score
= localStorage
.getItem(prefix
+"score");
96 if (storageVariant
!== variant
.name
&& score
== "*")
98 if (!confirm(storageVariant
+
99 translations
[": unfinished computer game will be erased"]))
106 else if (mode
== "friend")
108 const storageVariant
= localStorage
.getItem(prefix
+"variant");
109 if (!!storageVariant
)
111 const score
= localStorage
.getItem(prefix
+"score");
112 if (storageVariant
!== variant
.name
&& score
== "*")
114 if (!confirm(storageVariant
+
115 translations
[": current analysis will be erased"]))
122 this.vr
= new VariantRules(fen
, []);
124 this.pgnTxt
= ""; //redundant with this.score = "*", but cleaner
127 this.fenStart
= V
.ParseFen(fen
).position
; //this is enough
131 this.gameId
= gameId
;
133 this.oppConnected
= true;
134 this.mycolor
= color
;
137 new Audio("/sounds/newgame.mp3").play().catch(err
=> {});
138 document
.getElementById("modal-newgame").checked
= false;
140 else if (mode
== "computer")
142 this.compWorker
.postMessage(["init",this.vr
.getFen()]);
143 this.mycolor
= (Math
.random() < 0.5 ? 'w' : 'b');
144 if (this.mycolor
!= this.vr
.turn
)
145 this.playComputerMove();
147 else if (mode
== "friend")
148 this.mycolor
= "w"; //convention...
149 //else: problem solving: nothing more to do
150 if (mode
!= "problem")
151 this.setStorage(); //store game state in case of interruptions
153 continueGame: function(mode
) {
155 this.oppid
= (mode
=="human" ? localStorage
.getItem("oppid") : undefined);
156 const prefix
= this.getStoragePrefix(mode
);
157 this.mycolor
= localStorage
.getItem(prefix
+"mycolor");
158 const moves
= JSON
.parse(localStorage
.getItem(prefix
+"moves"));
159 const fen
= localStorage
.getItem(prefix
+"fen");
160 const score
= localStorage
.getItem(prefix
+"score"); //set in "endGame()"
161 this.fenStart
= localStorage
.getItem(prefix
+"fenStart");
162 this.vr
= new VariantRules(fen
, moves
);
163 this.incheck
= this.vr
.getCheckSquares(this.vr
.turn
);
166 this.gameId
= localStorage
.getItem("gameId");
167 // Send ping to server (answer pong if opponent is connected)
168 this.conn
.send(JSON
.stringify({
169 code:"ping",oppid:this.oppid
,gameId:this.gameId
}));
171 else if (mode
== "computer")
173 this.compWorker
.postMessage(["init",fen
]);
174 if (score
== "*" && this.mycolor
!= this.vr
.turn
)
175 this.playComputerMove();
177 //else: nothing special to do in friend mode
180 // Small delay required when continuation run faster than drawing page
181 setTimeout(() => this.endGame(score
), 100);
186 // TODO: option du bouton "new game"
187 const modalFenEdit
= [
190 attrs: { "id": "modal-fenedit", type: "checkbox" },
191 "class": { "modal": true },
195 attrs: { "role": "dialog", "aria-labelledby": "titleFenedit" },
200 "class": { "card": true, "smallpad": true },
205 attrs: { "id": "close-fenedit", "for": "modal-fenedit" },
206 "class": { "modal-close": true },
211 attrs: { "id": "titleFenedit" },
212 "class": { "section": true },
213 domProps: { innerHTML: translations
["Game state (FEN):"] },
221 value: VariantRules
.GenRandInitFen(),
229 const fen
= document
.getElementById("input-fen").value
;
230 document
.getElementById("modal-fenedit").checked
= false;
231 this.newGame("friend", fen
);
234 domProps: { innerHTML: translations
["Ok"] },
241 document
.getElementById("input-fen").value
=
242 VariantRules
.GenRandInitFen();
245 domProps: { innerHTML: translations
["Random"] },
253 elementArray
= elementArray
.concat(modalFenEdit
);