1 // Javascript for index page: mostly counters updating
10 sortedCounts: function () {
11 // TODO: priorité aux parties corr où c'est à nous de jouer !
12 const variantsCounts
= variantArray
14 return v
.name
.startsWith(this.curPrefix
);
20 count: this.counts
[v
.name
] || 0,
23 return variantsCounts
.sort((a
,b
) => {
24 if (a
.count
!= b
.count
)
25 return b
.count
- a
.count
;
26 // Else, alphabetic ordering
27 return a
.name
.localeCompare(b
.name
);
32 const url
= socketUrl
;
33 const sid
= getRandString();
34 this.conn
= new WebSocket(url
+ "/?sid=" + sid
+ "&page=index");
35 const socketMessageListener
= msg
=> {
36 const data
= JSON
.parse(msg
.data
);
37 if (data
.code
== "counts")
38 this.counts
= data
.counts
;
39 else if (data
.code
== "increase")
40 this.counts
[data
.vname
]++;
41 else if (data
.code
== "decrease")
42 this.counts
[data
.vname
]--;
44 const socketCloseListener
= () => {
45 this.conn
= new WebSocket(url
+ "/?sid=" + sid
+ "&page=index");
46 this.conn
.addEventListener('message', socketMessageListener
);
47 this.conn
.addEventListener('close', socketCloseListener
);
49 this.conn
.onmessage
= socketMessageListener
;
50 this.conn
.onclose
= socketCloseListener
;
54 document
.onkeydown
= event
=> {
55 // Is it Back or Esc? If yes, apply action on current word
56 if (event
.keyCode
== 8) //Back
58 event
.preventDefault();
59 this.curPrefix
= this.curPrefix
.slice(0,-1);
61 else if (event
.keyCode
== 27) //Esc
63 event
.preventDefault();
66 // Is it alphanumeric? If yes, stack it
67 else if (_
.range(48,58).includes(event
.keyCode
)
68 || _
.range(65,91).includes(event
.keyCode
)
69 || _
.range(97,123).includes(event
.keyCode
))
71 let newChar
= String
.fromCharCode(event
.keyCode
);
72 this.curPrefix
+= this.curPrefix
.length
==0
73 ? newChar
.toUpperCase()
74 : newChar
.toLowerCase();
76 // ...ignore everything else
82 // si dernier lastMove sur serveur n'est pas le mien et nextColor == moi, alors background orange
83 // ==> background orange si à moi de jouer par corr (sur main index)
84 // (fonction "getNextCol()" dans base_rules.js ?)