Mercurial > hg > rlgwebd
annotate rlgterm.js @ 161:a2a25b7631f1
RLGWebD client: fix watching.
author | John "Elwin" Edwards |
---|---|
date | Sat, 03 Jan 2015 20:03:49 -0500 |
parents | a613380ffdc2 |
children | 0f6da35b27a0 |
rev | line source |
---|---|
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1 /* rlgterm.js: Roguelike Gallery's driver for termemu.js */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
2 |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
3 /* Data on the available games. */ |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
4 var games = { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
5 "rogue3": { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
6 "name": "Rogue V3", |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
7 "uname": "rogue3" |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
8 }, |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
9 "rogue4": { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
10 "name": "Rogue V4", |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
11 "uname": "rogue4" |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
12 }, |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
13 "rogue5": { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
14 "name": "Rogue V5", |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
15 "uname": "rogue5" |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
16 }, |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
17 "srogue": { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
18 "name": "Super-Rogue", |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
19 "uname": "srogue" |
120
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
118
diff
changeset
|
20 }, |
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
118
diff
changeset
|
21 "arogue5": { |
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
118
diff
changeset
|
22 "name": "Advanced Rogue 5", |
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
118
diff
changeset
|
23 "uname": "arogue5" |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
24 } |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
25 }; |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
26 |
59
00b985b8ba6a
RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents:
58
diff
changeset
|
27 var session = { |
00b985b8ba6a
RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents:
58
diff
changeset
|
28 /* The session id assigned by the server. */ |
159 | 29 connect: false, |
127
e54018b26ed8
RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents:
120
diff
changeset
|
30 /* Login name and key are now in sessionStorage. */ |
59
00b985b8ba6a
RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents:
58
diff
changeset
|
31 /* Whether the game is being played or just watched. */ |
100
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
97
diff
changeset
|
32 playing: false, |
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
97
diff
changeset
|
33 /* WebSocket for communication */ |
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
97
diff
changeset
|
34 sock: null |
59
00b985b8ba6a
RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents:
58
diff
changeset
|
35 }; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
36 |
73
d084610a89b5
Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents:
70
diff
changeset
|
37 /* The interval ID for checking the status of current games. */ |
d084610a89b5
Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents:
70
diff
changeset
|
38 var statInterval = null; |
d084610a89b5
Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents:
70
diff
changeset
|
39 /* How frequently to check. */ |
d084610a89b5
Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents:
70
diff
changeset
|
40 var statDelta = 8000; |
105
a9371002aecc
RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents:
101
diff
changeset
|
41 /* A WebSocket to listen for status events. */ |
a9371002aecc
RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents:
101
diff
changeset
|
42 var statsock = null; |
73
d084610a89b5
Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents:
70
diff
changeset
|
43 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
44 function writeData(hexstr) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
45 var codenum; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
46 var codes = []; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
47 var nc; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
48 var u8wait = 0; /* Stores bits from previous bytes of multibyte sequences. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 var expect = 0; /* The number of 10------ bytes expected. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
50 /* UTF-8 translation. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
51 for (var i = 0; i < hexstr.length; i += 2) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
52 nc = Number("0x" + hexstr.substr(i, 2)); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
53 if (nc < 0x7F) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
54 /* 0------- */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
55 codes.push(nc); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
56 /* Any incomplete sequence will be discarded. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
57 u8wait = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
58 expect = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
59 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
60 else if (nc < 0xC0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
61 /* 10------ : part of a multibyte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
62 if (expect > 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
63 u8wait <<= 6; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
64 u8wait += (nc & 0x3F); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
65 expect--; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
66 if (expect == 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
67 codes.push(u8wait); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
68 u8wait = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
69 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
70 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
71 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
72 /* Assume an initial byte was missed. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
73 u8wait = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
74 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
75 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
76 /* These will all discard any incomplete sequence. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
77 else if (nc < 0xE0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
78 /* 110----- : introduces 2-byte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
79 u8wait = (nc & 0x1F); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
80 expect = 1; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
81 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
82 else if (nc < 0xF0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
83 /* 1110---- : introduces 3-byte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org> |