// This can work for squared boards (2 or 4 players), with some adaptations (TODO)
// TODO: for 3 players, write a "board3.js"
+// TODO: current clicked square + moving square as parameters, + highlight
+
import { getSquareId, getSquareFromId } from "@/utils/squareId";
import { ArrayFun } from "@/utils/array";
computed: {
uniquePlayers: function() {
// Show e.g. "5 @nonymous", and do nothing on click on anonymous
- let playerList = [{id:0, name:"@nonymous", count:0}];
+ let anonymous = {id:0, name:"@nonymous", count:0};
+ let playerList = [];
this.players.forEach(p => {
if (p.id > 0)
playerList.push(p);
else
- playerList[0].count++;
+ anonymous.count++;
});
+ if (anonymous.count > 0)
+ playerList.push(anonymous);
return playerList;
},
},
- // TODO: this looks ugly... (use VueX ?!)
- watch: {
- "st.conn": function() {
- this.st.conn.onmessage = this.socketMessageListener;
- this.st.conn.onclose = this.socketCloseListener;
- // Ask server for for room composition:
- this.st.conn.send(JSON.stringify({code:"askplayers"}));
- },
- },
created: function() {
+ // Always add myself to players' list
+ this.players.push(this.st.user);
// TODO: ask server for current corr games (all but mines: names, ID, time control)
// also ask for corr challenges
- if (!!this.st.conn)
- {
- this.st.conn.onmessage = this.socketMessageListener;
- this.st.conn.onclose = this.socketCloseListener;
- // Ask server for for room composition:
+ // Ask server for for room composition:
+ const socketOpenListener = () => {
this.st.conn.send(JSON.stringify({code:"askplayers"}));
- }
+ };
+ this.st.conn.onopen = socketOpenListener;
+ this.st.conn.onmessage = this.socketMessageListener;
+ const socketCloseListener = () => {
+ // connexion is reinitialized in store.js
+ this.st.conn.addEventListener('message', this.socketMessageListener);
+ this.st.conn.addEventListener('close', socketCloseListener);
+ };
+ this.st.conn.onclose = socketCloseListener;
},
methods: {
socketMessageListener: function(msg) {
{
case "room":
// TODO: receive room composition (sids at least, id + names if registered)
- // Add myself to players
- this.players.push(this.st.user);
// TODO: also receive "askchallenges", "askgames"
// * - receive "new game": if live, store locally + redirect to game
// * If corr: notify "new game has started", give link, but do not redirect
break;
}
},
- socketCloseListener: function() {
- // connexion is reinitialized in store.js
- this.st.conn.addEventListener('message', this.socketMessageListener);
- this.st.conn.addEventListener('close', this.socketCloseListener);
- },
showGame: function(game) {
// NOTE: if we are an observer, the game will be found in main games list
// (sent by connected remote players)
//TODO: programmatic re-navigation on current game if we receive a move and are not there
module.exports = function(wss) {
- let clients = {}; //associative array client sid --> socket
- // No-op function as a callback when sending messages
- const noop = () => { };
+ let clients = {}; //associative array sid --> socket
wss.on("connection", (socket, req) => {
const query = getJsonFromUrl(req.url);
const sid = query["sid"];
return; //receiver not connected, nothing we can do
switch (obj.code)
{
- // Transmit chats and moves to current room
- // TODO: WebRTC instead in this case (most demanding?)
+ case "askplayers":
+ socket.send(JSON.stringify({code:"room", players:clients}));
+ break;
+ case "askchallenges":
+ // TODO: ask directly to people (webRTC)
+ break;
+ case "askgames":
+ // TODO: ask directly to people (webRTC)
+ break;
case "newchat":
- clients[obj.oppid].send(JSON.stringify({code:"newchat",msg:obj.msg}), noop);
+ clients[obj.oppid].send(JSON.stringify({code:"newchat",msg:obj.msg}));
break;
+ // Transmit chats and moves to current room
+ // TODO: WebRTC instead in this case (most demanding?)
case "newmove":
- clients[obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move}), noop);
+ clients[obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move}));
break;
// TODO: generalize that for several opponents
case "ping":
case "lastate":
const oppId = obj.oppid;
obj.oppid = sid; //I'm oppid for my opponent
- clients[oppId].send(JSON.stringify(obj), noop);
+ clients[oppId].send(JSON.stringify(obj));
break;
// TODO: moreover, here, game info should be sent (through challenge; not stored here)
case "newgame":
break;
// TODO: also other challenge events
case "resign":
- clients[obj.oppid].send(JSON.stringify({code:"resign"}), noop);
+ clients[obj.oppid].send(JSON.stringify({code:"resign"}));
break;
// TODO: case "challenge" (get ID) --> send to all, "acceptchallenge" (with ID) --> send to all, "cancelchallenge" --> send to all
// also, "sendgame" (give current game info, if any) --> to new connections, "sendchallenges" (same for challenges) --> to new connections
delete clients[sid];
// Notify every other connected client
Object.keys(clients).forEach( k => {
- clients[k].send(JSON.stringify({code:"disconnect",sid:sid}), noop);
+ clients[k].send(JSON.stringify({code:"disconnect",sid:sid}));
});
});
});