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..."]
10 // TODO: my-challenge-list, gérant clicks sur challenges, affichage, réception/émission des infos sur challenges ; de même, my-player-list
11 // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés
13 Players + challenges : == "room" home of variant (surligner si nouveau défi perso et pas affichage courant)
14 joueurs en ligne (dte),
15 Nouvelle partie + défis en temps réel + parties en cours (milieu, tabs),
16 chat général (gauche, activé ou non (bool global storage)).
17 (cadences base + incrément, corr == incr >= 1jour ou base >= 7j)
18 --> correspondance: stocker sur serveur lastMove + peerId + color + movesCount + gameId + variant + timeleft
19 quand je poste un lastMove corr, supprimer mon ancien lastMove le cas échéant (tlm l'a eu)
20 fin de partie corr: garder maxi nbPlayers lastMove sur serveur, pendant 7 jours (arbitraire)
22 Vue
.component('my-room', {
23 props: ["conn","settings"],
30 case "newgame": //challenge accepted
31 // oppid: opponent socket ID (or DB id if registered)
32 this.newGame("human", data
.fen
, data
.color
, data
.oppid
, data
.gameid
);
35 clickGameSeek: function(e
) {
36 if (this.mode
== "human" && this.score
== "*")
37 return; //no newgame while playing
40 this.conn
.send(JSON
.stringify({code:"cancelnewgame"}));
44 this.newGame("human");
46 clickComputerGame: function(e
) {
47 if (this.mode
== "computer" && this.score
== "*"
48 && this.vr
.turn
!= this.mycolor
)
50 // Wait for computer reply first (avoid potential "ghost move" bug)
53 this.newGame("computer");
55 newGame: function(mode
, fenInit
, color
, oppId
, gameId
) {
56 const fen
= fenInit
|| VariantRules
.GenRandInitFen();
57 console
.log(fen
); //DEBUG
58 if (mode
=="human" && !oppId
)
60 const storageVariant
= localStorage
.getItem("variant");
61 if (!!storageVariant
&& storageVariant
!== variant
.name
62 && localStorage
["score"] == "*")
64 return alert(translations
["Finish your "] +
65 storageVariant
+ translations
[" game first!"]);
67 // Send game request and wait..
69 this.conn
.send(JSON
.stringify({code:"newgame", fen:fen
, gameid: getRandString() }));
70 } catch (INVALID_STATE_ERR
) {
71 return; //nothing achieved
74 let modalBox
= document
.getElementById("modal-newgame");
75 modalBox
.checked
= true;
76 setTimeout(() => { modalBox
.checked
= false; }, 2000);
79 const prefix
= this.getStoragePrefix(mode
);
80 if (mode
== "computer")
82 const storageVariant
= localStorage
.getItem(prefix
+"variant");
85 const score
= localStorage
.getItem(prefix
+"score");
86 if (storageVariant
!== variant
.name
&& score
== "*")
88 if (!confirm(storageVariant
+
89 translations
[": unfinished computer game will be erased"]))
96 else if (mode
== "friend")
98 const storageVariant
= localStorage
.getItem(prefix
+"variant");
101 const score
= localStorage
.getItem(prefix
+"score");
102 if (storageVariant
!== variant
.name
&& score
== "*")
104 if (!confirm(storageVariant
+
105 translations
[": current analysis will be erased"]))
112 this.vr
= new VariantRules(fen
, []);
114 this.pgnTxt
= ""; //redundant with this.score = "*", but cleaner
117 this.fenStart
= V
.ParseFen(fen
).position
; //this is enough
121 this.gameId
= gameId
;
123 this.oppConnected
= true;
124 this.mycolor
= color
;
127 new Audio("/sounds/newgame.mp3").play().catch(err
=> {});
128 document
.getElementById("modal-newgame").checked
= false;
130 else if (mode
== "computer")
132 this.compWorker
.postMessage(["init",this.vr
.getFen()]);
133 this.mycolor
= (Math
.random() < 0.5 ? 'w' : 'b');
134 if (this.mycolor
!= this.vr
.turn
)
135 this.playComputerMove();
137 else if (mode
== "friend")
138 this.mycolor
= "w"; //convention...
139 //else: problem solving: nothing more to do
140 if (mode
!= "problem")
141 this.setStorage(); //store game state in case of interruptions
143 continueGame: function(mode
) {
145 this.oppid
= (mode
=="human" ? localStorage
.getItem("oppid") : undefined);
146 const prefix
= this.getStoragePrefix(mode
);
147 this.mycolor
= localStorage
.getItem(prefix
+"mycolor");
148 const moves
= JSON
.parse(localStorage
.getItem(prefix
+"moves"));
149 const fen
= localStorage
.getItem(prefix
+"fen");
150 const score
= localStorage
.getItem(prefix
+"score"); //set in "endGame()"
151 this.fenStart
= localStorage
.getItem(prefix
+"fenStart");
152 this.vr
= new VariantRules(fen
, moves
);
153 this.incheck
= this.vr
.getCheckSquares(this.vr
.turn
);
156 this.gameId
= localStorage
.getItem("gameId");
157 // Send ping to server (answer pong if opponent is connected)
158 this.conn
.send(JSON
.stringify({
159 code:"ping",oppid:this.oppid
,gameId:this.gameId
}));
161 else if (mode
== "computer")
163 this.compWorker
.postMessage(["init",fen
]);
164 if (score
== "*" && this.mycolor
!= this.vr
.turn
)
165 this.playComputerMove();
167 //else: nothing special to do in friend mode
170 // Small delay required when continuation run faster than drawing page
171 setTimeout(() => this.endGame(score
), 100);
176 // TODO: option du bouton "new game" :: seulement pour challenge indiv
177 const modalFenEdit
= [
180 attrs: { "id": "modal-fenedit", type: "checkbox" },
181 "class": { "modal": true },
185 attrs: { "role": "dialog", "aria-labelledby": "titleFenedit" },
190 "class": { "card": true, "smallpad": true },
195 attrs: { "id": "close-fenedit", "for": "modal-fenedit" },
196 "class": { "modal-close": true },
201 attrs: { "id": "titleFenedit" },
202 "class": { "section": true },
203 domProps: { innerHTML: translations
["Game state (FEN):"] },
211 value: VariantRules
.GenRandInitFen(),
219 const fen
= document
.getElementById("input-fen").value
;
220 document
.getElementById("modal-fenedit").checked
= false;
221 this.newGame("friend", fen
);
224 domProps: { innerHTML: translations
["Ok"] },
231 document
.getElementById("input-fen").value
=
232 VariantRules
.GenRandInitFen();
235 domProps: { innerHTML: translations
["Random"] },
243 elementArray
= elementArray
.concat(modalFenEdit
);