77213edf77c6a56f7e16b2f0492996e1752e3aba
3 const getV
= (field
) => localStorage
.getItem(field
);
7 return m("input[type=text][placeholder=User name]", {
9 onchange(e
) {Login
.username
= e
.currentTarget
.value
},
10 onkeyup: Login
.process
13 process: function(e
) {
14 if (e
.keyCode
== 13) socket
.emit("login", e
.target
.value
);
20 seekGame: function() {
21 // If not logged in, redirect
22 if (!getV("name")) m
.route
.set("/login");
24 socket
.emit("seek", {uid: getV("uid"), name: getV("name")});
25 Seek
.searching
= true;
28 view: function(vnode
) {
30 return m("img.animated", {src: "assets/searching.gif"});
31 return m("button", {onclick: Seek
.seekGame
}, "Play");
36 {'r':"Rock",'p':"Paper",'s':"Scissors",'l':"Lizard",'k':"Spock",'?':"what"};
38 {'r':['s','l'], 'p':['r','k'], 's':['p','l'], 'l':['p','k'], 'k':['r','s']};
53 initGame: function(msg
) {
55 Play
.oppid
= msg
.oppid
;
56 Play
.oppname
= msg
.oppname
;
57 Play
.gameover
= false;
58 for (const v
of ["mymove","oppmove"]) Play
[v
] = "";
59 for (const v
of ["myselect","oppselect"]) Play
[v
] = "?";
60 for (const v
of ["mnum","mypoints","oppoints"]) Play
[v
] = 0;
62 compareMoves: function() {
63 Play
.oppselect
= Play
.oppmove
; //reveal opponent's move only now
64 if (Win
[Play
.mymove
].includes(Play
.oppmove
)) {
65 if (++Play
.mypoints
== MAX_POINTS
) Play
.endGame(true);
66 socket
.emit("inc_pts", {uid: getV("uid"), gid:Play
.gid
});
68 else if (Win
[Play
.oppmove
].includes(Play
.mymove
)) {
69 if (++Play
.oppoints
== MAX_POINTS
) Play
.endGame(false);
75 endGame: function(Iwin
) {
78 if (Iwin
) alert("Bravo t'as gagné !");
79 else alert("Pas de chance, t'as perdu...");
83 playMove: function(code
) {
84 if (Play
.mymove
|| Play
.gameover
)
85 // I already played, or game is over
96 if (Play
.oppmove
) Play
.compareMoves();
97 else Play
.oppselect
= "?";
102 `${getV("name")} [${Play.mypoints}] vs. ${Play.oppname} [${Play.oppoints}]`)]
105 m("img", {src: "assets/" + Choices
[Play
.myselect
] + ".png"}),
106 m("img", {src: "assets/" + Choices
[Play
.oppselect
] + ".png"})
109 {style: `opacity:${Play.mymove==''?'1':'0.5'}`},
110 ["r","p","s","l","k"].map((code
) => {
112 src: "assets/" + Choices
[code
] + ".png",
113 onclick: () => Play
.playMove(code
)
120 socket
.on("login", (msg
) => {
122 localStorage
.setItem("name", msg
.name
);
123 localStorage
.setItem("uid", msg
.uid
);
124 m
.route
.set("/seek");
128 socket
.on("play", (msg
) => {
129 Seek
.searching
= false;
130 if (msg
.oppid
== getV("uid")) {
131 alert("Cannot play against self!");
132 m
.redraw(); //TODO: because no DOM interaction... ?
136 m
.route
.set("/play");
139 socket
.on("move", (msg
) => {
140 Play
.oppmove
= msg
.choice
;
141 Play
.oppselect
= "?"; //not showing opponent selection yet!
142 if (Play
.mymove
) Play
.compareMoves();
143 else Play
.myselect
= "?";
144 m
.redraw(); //TODO... (because no DOM interactions)
147 m
.route(document
.body
, "/seek", {