Final thoughts about presentation
[vchess.git] / public / javascripts / components / room.js
CommitLineData
b6487fb9
BA
1// TODO: main playing hall, chat + online players + current challenges + button "new game"
2/*
3input#modal-newgame.modal(type="checkbox")
4div(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/*
13Players + challenges : == "room" home of variant (surligner si nouveau défi perso et pas affichage courant)
14joueurs en ligne (dte),
15Nouvelle partie + défis en temps réel + parties en cours (milieu, tabs),
16chat 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
19quand je poste un lastMove corr, supprimer mon ancien lastMove le cas échéant (tlm l'a eu)
20fin 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
23Vue.component('my-room', {
24 props: ["conn","settings"],
25 data: {
3ca7a846
BA
26 remoteGames: [],
27 corrGames: [],
a3ab5fdb 28 },
a6403027
BA
29 // Modal new game, and then sub-components
30 template: `
31 <div>
32 <input id="modalNewgame" type="checkbox" class"="modal"/>
33 <div role="dialog" aria-labelledby="titleFenedit">
34 <div class="card smallpad">
35 <label id="closeNewgame" for="modalNewgame" class="modal-close">
36 </label>
37 <h3 id="titleFenedit" class="section">
38 {{ translate("Game state (FEN):") }}
39 </h3>
40 <input id="input-fen" type="text"/>
41 <p>TODO: cadence, adversaire (pre-filled if click on name)</p>
42 <p>Note: leave FEN blank for random</p>
43 <button @click="newGame">Launch game</button>
44 </div>
45 </div>
3ca7a846
BA
46 <div>
47 <my-chat :conn="conn" :myname="myname" :people="people"></my-chat>
48 <my-challenge-list :conn="conn"></my-challenge-list>
49 </div>
50 <div>
51 <my-player-list :conn="conn"></my-player-list>
52 // TODO: also corr games (of pther players)
53 // presentation ? table ?!
54 <my-game-summary v-for="g in remoteGames"
55 v-bind:vobj="g" v-bind:game="g" v-bind:key="g.id"
56 @click="() => showGame(g.id,g.uid)">
57 </my-game-summary>
58 </div>
a6403027
BA
59 </div>
60 `,
61 created: function() {
3ca7a846 62 // TODO: ask server for current corr games (all but mines)
a6403027
BA
63 const socketMessageListener = msg => {
64 const data = JSON.parse(msg.data);
65 switch (data.code)
66 {
3ca7a846
BA
67 // TODO: also receive remote games summaries (update)
68 // (just players names, time control, and ID + player ID)
a3ab5fdb
BA
69 case "newgame": //challenge accepted
70 // oppid: opponent socket ID (or DB id if registered)
81da2786
BA
71 this.newGame("human", data.fen, data.color, data.oppid, data.gameid);
72 break;
a6403027
BA
73 }
74 };
75 const socketCloseListener = () => {
76 this.conn.addEventListener('message', socketMessageListener);
77 this.conn.addEventListener('close', socketCloseListener);
78 };
79 this.conn.onmessage = socketMessageListener;
80 this.conn.onclose = socketCloseListener;
81 },
82 methods: {
81da2786 83 clickGameSeek: function(e) {
81da2786
BA
84 if (this.mode == "human" && this.score == "*")
85 return; //no newgame while playing
86 if (this.seek)
87 {
88 this.conn.send(JSON.stringify({code:"cancelnewgame"}));
89 this.seek = false;
90 }
91 else
92 this.newGame("human");
93 },
81da2786
BA
94 newGame: function(mode, fenInit, color, oppId, gameId) {
95 const fen = fenInit || VariantRules.GenRandInitFen();
96 console.log(fen); //DEBUG
97 if (mode=="human" && !oppId)
98 {
99 const storageVariant = localStorage.getItem("variant");
8d7e2786 100 if (!!storageVariant && storageVariant !== variant.name
81da2786
BA
101 && localStorage["score"] == "*")
102 {
103 return alert(translations["Finish your "] +
104 storageVariant + translations[" game first!"]);
105 }
106 // Send game request and wait..
107 try {
108 this.conn.send(JSON.stringify({code:"newgame", fen:fen, gameid: getRandString() }));
109 } catch (INVALID_STATE_ERR) {
110 return; //nothing achieved
111 }
112 this.seek = true;
113 let modalBox = document.getElementById("modal-newgame");
114 modalBox.checked = true;
115 setTimeout(() => { modalBox.checked = false; }, 2000);
116 return;
117 }
81da2786
BA
118 this.vr = new VariantRules(fen, []);
119 this.score = "*";
120 this.pgnTxt = ""; //redundant with this.score = "*", but cleaner
121 this.mode = mode;
122 this.incheck = [];
123 this.fenStart = V.ParseFen(fen).position; //this is enough
124 if (mode=="human")
125 {
126 // Opponent found!
127 this.gameId = gameId;
128 this.oppid = oppId;
129 this.oppConnected = true;
130 this.mycolor = color;
131 this.seek = false;
132 if (this.sound >= 1)
133 new Audio("/sounds/newgame.mp3").play().catch(err => {});
134 document.getElementById("modal-newgame").checked = false;
135 }
a6403027 136 this.setStorage(); //store game state in case of interruptions
81da2786 137 },
a6403027
BA
138 continueGame: function() {
139 this.oppid = localStorage.getItem("oppid");
140 this.mycolor = localStorage.getItem("mycolor");
141 const moves = JSON.parse(localStorage.getItem("moves"));
142 const fen = localStorage.getItem("fen");
143 const score = localStorage.getItem("score"); //always "*" ?!
144 this.fenStart = localStorage.getItem("fenStart");
145 this.vr = new VariantRules(fen);
81da2786 146 this.incheck = this.vr.getCheckSquares(this.vr.turn);
a6403027
BA
147 this.gameId = localStorage.getItem("gameId");
148 // Send ping to server (answer pong if opponent is connected)
149 this.conn.send(JSON.stringify({
150 code:"ping",oppid:this.oppid,gameId:this.gameId}));
81da2786 151 },
a6403027
BA
152 },
153});