Commit | Line | Data |
---|---|---|
b6487fb9 BA |
1 | // TODO: main playing hall, chat + online players + current challenges + button "new game" |
2 | /* | |
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..."] | |
9 | */ | |
a3ab5fdb | 10 | // TODO: my-challenge-list, gérant clicks sur challenges, affichage, réception/émission des infos sur challenges ; de même, my-player-list |
2305d34a | 11 | // TODO: si on est en train de jouer une partie, le notifier aux nouveaux connectés |
b6487fb9 BA |
12 | /* |
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) | |
21 | */ | |
a6403027 | 22 | // TODO: au moins l'échange des coups en P2P ? |
a3ab5fdb BA |
23 | Vue.component('my-room', { |
24 | props: ["conn","settings"], | |
25 | data: { | |
26 | something: "", //TODO | |
27 | }, | |
a6403027 BA |
28 | // Modal new game, and then sub-components |
29 | template: ` | |
30 | <div> | |
31 | <input id="modalNewgame" type="checkbox" class"="modal"/> | |
32 | <div role="dialog" aria-labelledby="titleFenedit"> | |
33 | <div class="card smallpad"> | |
34 | <label id="closeNewgame" for="modalNewgame" class="modal-close"> | |
35 | </label> | |
36 | <h3 id="titleFenedit" class="section"> | |
37 | {{ translate("Game state (FEN):") }} | |
38 | </h3> | |
39 | <input id="input-fen" type="text"/> | |
40 | <p>TODO: cadence, adversaire (pre-filled if click on name)</p> | |
41 | <p>Note: leave FEN blank for random</p> | |
42 | <button @click="newGame">Launch game</button> | |
43 | </div> | |
44 | </div> | |
45 | <my-chat :conn="conn" :myname="myname" :people="people"></my-chat> | |
46 | <my-challenge-list :conn="conn"></my-challenge-list> | |
47 | <my-player-list :conn="conn"></my-player-list> | |
48 | <my-game-list :conn="conn" ........... my-local-game-list opposed to my-remote-ame-list ?! ...bof></my-game-list> | |
49 | onClick :: ask full game to remote player, and register as an observer in game | |
50 | (use gameId to communicate) | |
51 | on landing on game :: if gameId not found locally, check remotely | |
52 | ==> il manque un param dans game : "remoteId" | |
53 | </div> | |
54 | `, | |
55 | created: function() { | |
56 | const socketMessageListener = msg => { | |
57 | const data = JSON.parse(msg.data); | |
58 | switch (data.code) | |
59 | { | |
a3ab5fdb BA |
60 | case "newgame": //challenge accepted |
61 | // oppid: opponent socket ID (or DB id if registered) | |
81da2786 BA |
62 | this.newGame("human", data.fen, data.color, data.oppid, data.gameid); |
63 | break; | |
a6403027 BA |
64 | } |
65 | }; | |
66 | const socketCloseListener = () => { | |
67 | this.conn.addEventListener('message', socketMessageListener); | |
68 | this.conn.addEventListener('close', socketCloseListener); | |
69 | }; | |
70 | this.conn.onmessage = socketMessageListener; | |
71 | this.conn.onclose = socketCloseListener; | |
72 | }, | |
73 | methods: { | |
81da2786 | 74 | clickGameSeek: function(e) { |
81da2786 BA |
75 | if (this.mode == "human" && this.score == "*") |
76 | return; //no newgame while playing | |
77 | if (this.seek) | |
78 | { | |
79 | this.conn.send(JSON.stringify({code:"cancelnewgame"})); | |
80 | this.seek = false; | |
81 | } | |
82 | else | |
83 | this.newGame("human"); | |
84 | }, | |
81da2786 BA |
85 | newGame: function(mode, fenInit, color, oppId, gameId) { |
86 | const fen = fenInit || VariantRules.GenRandInitFen(); | |
87 | console.log(fen); //DEBUG | |
88 | if (mode=="human" && !oppId) | |
89 | { | |
90 | const storageVariant = localStorage.getItem("variant"); | |
8d7e2786 | 91 | if (!!storageVariant && storageVariant !== variant.name |
81da2786 BA |
92 | && localStorage["score"] == "*") |
93 | { | |
94 | return alert(translations["Finish your "] + | |
95 | storageVariant + translations[" game first!"]); | |
96 | } | |
97 | // Send game request and wait.. | |
98 | try { | |
99 | this.conn.send(JSON.stringify({code:"newgame", fen:fen, gameid: getRandString() })); | |
100 | } catch (INVALID_STATE_ERR) { | |
101 | return; //nothing achieved | |
102 | } | |
103 | this.seek = true; | |
104 | let modalBox = document.getElementById("modal-newgame"); | |
105 | modalBox.checked = true; | |
106 | setTimeout(() => { modalBox.checked = false; }, 2000); | |
107 | return; | |
108 | } | |
81da2786 BA |
109 | this.vr = new VariantRules(fen, []); |
110 | this.score = "*"; | |
111 | this.pgnTxt = ""; //redundant with this.score = "*", but cleaner | |
112 | this.mode = mode; | |
113 | this.incheck = []; | |
114 | this.fenStart = V.ParseFen(fen).position; //this is enough | |
115 | if (mode=="human") | |
116 | { | |
117 | // Opponent found! | |
118 | this.gameId = gameId; | |
119 | this.oppid = oppId; | |
120 | this.oppConnected = true; | |
121 | this.mycolor = color; | |
122 | this.seek = false; | |
123 | if (this.sound >= 1) | |
124 | new Audio("/sounds/newgame.mp3").play().catch(err => {}); | |
125 | document.getElementById("modal-newgame").checked = false; | |
126 | } | |
a6403027 | 127 | this.setStorage(); //store game state in case of interruptions |
81da2786 | 128 | }, |
a6403027 BA |
129 | continueGame: function() { |
130 | this.oppid = localStorage.getItem("oppid"); | |
131 | this.mycolor = localStorage.getItem("mycolor"); | |
132 | const moves = JSON.parse(localStorage.getItem("moves")); | |
133 | const fen = localStorage.getItem("fen"); | |
134 | const score = localStorage.getItem("score"); //always "*" ?! | |
135 | this.fenStart = localStorage.getItem("fenStart"); | |
136 | this.vr = new VariantRules(fen); | |
81da2786 | 137 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
a6403027 BA |
138 | this.gameId = localStorage.getItem("gameId"); |
139 | // Send ping to server (answer pong if opponent is connected) | |
140 | this.conn.send(JSON.stringify({ | |
141 | code:"ping",oppid:this.oppid,gameId:this.gameId})); | |
81da2786 | 142 | }, |
a6403027 BA |
143 | }, |
144 | }); |