bf5df633f9fe5b062f57a0a7604909bf3e265f8d
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);
67 else if (Win
[Play
.oppmove
].includes(Play
.mymove
)) {
68 if (++Play
.oppoints
== MAX_POINTS
) Play
.endGame(false);
74 endGame: function(Iwin
) {
77 if (Iwin
) alert("Bravo t'as gagné !");
78 else alert("Pas de chance, t'as perdu...");
82 playMove: function(code
) {
83 if (Play
.mymove
|| Play
.gameover
)
84 // I already played, or game is over
95 if (Play
.oppmove
) Play
.compareMoves();
96 else Play
.oppselect
= "?";
101 `${getV("name")} [${Play.mypoints}] vs. ${Play.oppname} [${Play.oppoints}]`)]
104 m("img", {src: "assets/" + Choices
[Play
.myselect
] + ".png"}),
105 m("img", {src: "assets/" + Choices
[Play
.oppselect
] + ".png"})
108 {style: `opacity:${Play.mymove==''?'1':'0.5'}`},
109 ["r","p","s","l","k"].map((code
) => {
111 src: "assets/" + Choices
[code
] + ".png",
112 onclick: () => Play
.playMove(code
)
119 socket
.on("login", (msg
) => {
121 localStorage
.setItem("name", msg
.name
);
122 localStorage
.setItem("uid", msg
.uid
);
123 m
.route
.set("/seek");
127 socket
.on("play", (msg
) => {
128 Seek
.searching
= false;
129 if (msg
.oppid
== getV("uid")) {
130 alert("Cannot play against self!");
131 m
.redraw(); //TODO: because no DOM interaction... ?
135 m
.route
.set("/play");
138 socket
.on("move", (msg
) => {
139 Play
.oppmove
= msg
.choice
;
140 Play
.oppselect
= "?"; //not showing opponent selection yet!
141 if (Play
.mymove
) Play
.compareMoves();
142 else Play
.myselect
= "?";
143 m
.redraw(); //TODO... (because no DOM interactions)
146 m
.route(document
.body
, "/seek", {