annotate rlgterm.js @ 213:08665c56c6a0 default tip

Fix race condition related to watching DGL games. It's possible for a dgamelaunch game to end and cause rlgwebd to stop watching it before rlgwebd has started watching it.
author John "Elwin" Edwards
date Sun, 16 Aug 2020 20:56:18 -0400
parents f3843245a35e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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"
191
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 187
diff changeset
24 },
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 187
diff changeset
25 "arogue7": {
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 187
diff changeset
26 "name": "Advanced Rogue 7",
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 187
diff changeset
27 "uname": "arogue7"
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 187
diff changeset
28 },
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 187
diff changeset
29 "xrogue": {
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 187
diff changeset
30 "name": "XRogue",
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 187
diff changeset
31 "uname": "xrogue"
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
32 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
33 };
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
34
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
35 var session = {
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
36 /* The session id assigned by the server. */
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
37 connect: false,
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
38 /* 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
39 /* 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
40 playing: false,
186
674e8899703b Client: improve messages at the end of watching a game.
John "Elwin" Edwards
parents: 176
diff changeset
41 /* If watching, the name of the player. */
674e8899703b Client: improve messages at the end of watching a game.
John "Elwin" Edwards
parents: 176
diff changeset
42 playername: null,
674e8899703b Client: improve messages at the end of watching a game.
John "Elwin" Edwards
parents: 176
diff changeset
43 /* Whether the Stop button was pushed. */
674e8899703b Client: improve messages at the end of watching a game.
John "Elwin" Edwards
parents: 176
diff changeset
44 leaving: false,
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
45 /* WebSocket for communication */
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
46 sock: null
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
47 };
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
48
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
49 /* 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
50 var statInterval = null;
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
51 /* How frequently to check. */
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
52 var statDelta = 8000;
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
53 /* 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
54 var statsock = null;
163
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 161
diff changeset
55 /* List of currently active games. */
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 161
diff changeset
56 var currentList = [];
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 161
diff changeset
57 /* Last time the list was updated. */
0f6da35b27a0 RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents: 161
diff changeset
58 var currentTS = null;
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
59
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
60 function writeData(hexstr) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
61 var codenum;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
62 var codes = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
63 var nc;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
64 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
65 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
66 /* UTF-8 translation. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
67 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
68 nc = Number("0x" + hexstr.substr(i, 2));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
69 if (nc < 0x7F) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
70 /* 0------- */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
71 codes.push(nc);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
72 /* Any incomplete sequence will be discarded. */
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 expect = 0;
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 else if (nc < 0xC0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
77 /* 10------ : part of a multibyte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
78 if (expect > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
79 u8wait <<= 6;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
80 u8wait += (nc & 0x3F);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
81 expect--;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
82 if (expect == 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
83 codes.push(u8wait);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
84 u8wait = 0;