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
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"
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
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
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>
parents:
diff changeset
84 u8wait = (nc & 0x0F);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
85 expect = 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
86 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
87 else if (nc < 0xF8) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
88 /* 11110--- : introduces 4-byte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
89 u8wait = (nc & 0x07);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
90 expect = 3;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
91 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
92 else if (nc < 0xFC) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
93 /* 111110-- : introduces 5-byte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
94 u8wait = (nc & 0x03);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
95 expect = 4;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
96 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
97 else if (nc < 0xFE) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
98 /* 1111110- : introduces 6-byte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
99 u8wait = (nc & 0x01);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
100 expect = 5;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
101 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
103 /* 1111111- : should never appear */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
104 u8wait = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105 expect = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
107 /* Supporting all 31 bits is probably overkill... */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
109 termemu.write(codes);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
110 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
111 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
112
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
113 function errHandler() {
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
114 message("Unable to connect to the server.", "warn");
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
115 }
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
116
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 function sendback(str) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
118 /* For responding to terminal queries. */
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
119 if (session.sock) {
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
120 session.sock.send(JSON.stringify({"t": "d", "d": str}));
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
121 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
122 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
123 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
124
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
125 function sendkey(ev) {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
126 if (!session.playing)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
127 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
128 var keynum = ev.keyCode;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
129 var code;
70
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
130 if (keynum >= 65 && keynum <= 90) {
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
131 /* Letters. */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
132 if (ev.ctrlKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
133 keynum -= 64;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
134 else if (!ev.shiftKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
135 keynum += 32;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
136 code = keynum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 if (code.length < 2)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
138 code = "0" + code;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
139 }
70
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
140 else if (keynum >= 48 && keynum <= 57) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 /* The number row, NOT the numpad. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 if (ev.shiftKey) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
143 code = numShifts[keynum - 48].toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
144 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
146 code = keynum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
148 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
149 else if (keynum in keyHexCodes) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 if (ev.shiftKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
151 code = keyHexCodes[keynum][1];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
152 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
153 code = keyHexCodes[keynum][0];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
154 }
70
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
155 else if (keynum >= 16 && keynum <= 20) {
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
156 /* Shift, Cntl, Alt, CAPSLOCK */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
157 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
158 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
159 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
160 debug(1, "Ignoring keycode " + keynum);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
161 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
162 }
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
163 ev.preventDefault();
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
164 if (session.sock) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
165 session.sock.send(JSON.stringify({"t": "d", "d": code}));
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
166 }
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
167 /* Otherwise it is disconnected */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
168 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
169 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
170
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
171 var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
172 ';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
173 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
174
76
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
175 var kpkeys = { "KP1": "1b4f46", "KP2": "1b4f42", "KP3": "1b5b367e",
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
176 "KP4": "1b4f44", "KP5": "1b5b45", "KP6": "1b4f43",
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
177 "KP7": "1b4f48", "KP8": "1b4f41", "KP9": "1b5b357e" };
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
178
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
179 function vkey(c) {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
180 if (!session.playing)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
181 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
182 var keystr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
183 if (c.match(/^[a-z]$/)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
184 if (termemu.ctrlp()) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
185 var n = c.charCodeAt(0) - 96;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
186 keystr = n.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
187 if (keystr.length < 2)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
188 keystr = "0" + keystr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
189 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
190 else if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
191 keystr = c.toUpperCase().charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
192 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
193 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
194 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
195 else if (c.match(/^[0-9]$/)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
196 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
197 keystr = numShifts[c.charCodeAt(0) - 48].toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
198 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
199 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
200 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
201 else if (c == '\n')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
202 keystr = "0a";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
203 else if (c == '\t')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
204 keystr = "09";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
205 else if (c == '\b')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
206 keystr = "08";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
207 else if (c == ' ')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
208 keystr = "20";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
209 else if (c in charshifts) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
210 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
211 keystr = charshifts[c];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
212 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
213 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
214 }
76
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
215 else if (c in kpkeys) {
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
216 keystr = kpkeys[c];
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
217 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
218 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
219 return;
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
220 if (session.sock) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
221 session.sock.send(JSON.stringify({"t": "d", "d": keystr}));
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
222 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
223 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
224 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
225
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
226 function setup() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
227 keyHexCodes.init();
4
ee22eb9ab009 Client: don't assume the terminal is 24x80.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
228 termemu.init("termwrap", 24, 80);
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
229 /* Is someone already logged in? */
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
230 if ("lcred" in sessionStorage) {
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
231 setmode("choose");
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
232 message("You are logged in as " + sessionStorage.getItem("lname") + ".");
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
233 }
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
234 else
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
235 setmode("login");
78
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
236 /* Set up the text size. */
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
237 var cssSize = termemu.view.style.fontSize;
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
238 var match = cssSize.match(/\d*/);
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
239 if (!match) {
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
240 return;
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
241 }
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
242 var csize = Number(match[0]);
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
243 var allscreen = document.getElementById("termwrap");
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
244 while (csize > 9 && csize < 48) {
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
245 if (allscreen.scrollWidth * 1.2 > window.innerWidth) {
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
246 csize = textsize(false);
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
247 }
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
248 else if (allscreen.scrollWidth * 2 < window.innerWidth) {
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
249 csize = textsize(true);
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
250 }
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
251 else
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
252 break;
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
253 }
116
d7d7cdcba3b4 Fix WebSocket support checking.
John "Elwin" Edwards <elwin@sdf.org>
parents: 112
diff changeset
254 if (!window.WebSocket) {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
255 message("Your browser does not support WebSockets. " +
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
256 "This Web app will not work.", "warn");
109
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
257 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
258 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
259 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
260
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
261 function toggleshift() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
262 termemu.toggleshift();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
263 keydiv = document.getElementById("shiftkey");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
264 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
265 keydiv.className = "keysel";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
266 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
267 keydiv.className = "key";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
268 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
269 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
270
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
271 function togglectrl() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
272 termemu.togglectrl();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
273 keydiv = document.getElementById("ctrlkey");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
274 if (termemu.ctrlp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
275 keydiv.className = "keysel";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
276 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
277 keydiv.className = "key";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
278 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
279 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
280
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
281 function formlogin(ev) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
282 ev.preventDefault();
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
283 /* What to do if logged in already? */
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
284 var loginmsg = {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
285 loginmsg["name"] = document.getElementById("input_name").value;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
286 loginmsg["pw"] = document.getElementById("input_pw").value;
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
287 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
288 req.onerror = errHandler;
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
289 req.onreadystatechange = function () {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
290 if (req.readyState != 4 || req.status != 200)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
291 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
292 var reply = JSON.parse(req.responseText);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
293 if (reply.t == 'l') {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
294 /* Success */
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
295 sessionStorage.setItem("lcred", reply.k);
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
296 sessionStorage.setItem("lname", reply.u);
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
297 message("You are now logged in as " + reply.u + ".");
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
298 setmode("choose");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
299 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
300 else if (reply.t == 'E') {
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
301 var failmsg = "Logging in failed. ";
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
302 if (reply.c == 2)
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
303 failmsg += reply.s.match(/Invalid data: (.*)/)[1];
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
304 else if (reply.c == 3)
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
305 failmsg += "The username or password was incorrect.";
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
306 else if (reply.c == 6)
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
307 failmsg += "The server is shutting down.";
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
308 message(failmsg, "warn");
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
309 document.getElementById("input_name").value = "";
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
310 document.getElementById("input_pw").value = "";
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
311 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
312 };
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
313 req.open('POST', '/login', true);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
314 req.send(JSON.stringify(loginmsg));
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
315 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
316 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
317
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
318 /* FIXME game list API has changed */
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
319 function tableCurrent(gamelist) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
320 var gamediv = document.getElementById("gametable");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
321 while (gamediv.children.length > 2)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
322 gamediv.removeChild(gamediv.children[2]);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
323 if (gamelist.length === 0) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
324 gamediv.style.display = "none";
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
325 document.getElementById("nogames").style.display = "block";
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
326 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
327 else {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
328 gamediv.style.display = "table";
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
329 document.getElementById("nogames").style.display = "none";
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
330 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
331 for (var i = 0; i < gamelist.length; i++) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
332 var row = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
333 var cell1 = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
334 var cell2 = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
335 var cell3 = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
336 var cell4 = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
337 cell1.appendChild(document.createTextNode(gamelist[i].p));
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
338 var uname = gamelist[i].g;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
339 if (uname in games)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
340 cell2.appendChild(document.createTextNode(games[uname].name));
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
341 else {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
342 continue;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
343 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
344 cell3.appendChild(document.createTextNode(idlestr(gamelist[i].i)));
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
345 var button = document.createElement("span");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
346 button.appendChild(document.createTextNode("Watch"));
161
a2a25b7631f1 RLGWebD client: fix watching.
John "Elwin" Edwards
parents: 159
diff changeset
347 button.onclick = makeWatcher(uname + "/" + gamelist[i].p);
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
348 button.className = "ibutton";
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
349 cell4.appendChild(button);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
350 row.appendChild(cell1);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
351 row.appendChild(cell2);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
352 row.appendChild(cell3);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
353 row.appendChild(cell4);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
354 gamediv.appendChild(row);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
355 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
356 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
357
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
358 /* Handles the status socket, opening and closing it when necessary. */
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
359 function wsCurrent() {
116
d7d7cdcba3b4 Fix WebSocket support checking.
John "Elwin" Edwards <elwin@sdf.org>
parents: 112
diff changeset
360 if (!window.WebSocket)
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
361 return;
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
362 if (session.connect) {
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
363 /* Don't bother with status if already playing/watching. */
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
364 if (statsock) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
365 statsock.close();
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
366 statsock = null;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
367 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
368 return;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
369 }
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
370 if ("lcred" in sessionStorage) {
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
371 /* When starting the socket, the choices list might not be initialized. */
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
372 getchoices();
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
373 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
374 if (statsock)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
375 return;
106
dc1414faee19 RLG-Web client: determine the host for WebSocket connections.
John "Elwin" Edwards <elwin@sdf.org>
parents: 105
diff changeset
376 statsock = new WebSocket("ws://" + window.location.host + "/status");
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
377 statsock.onmessage = function (ev) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
378 var msg;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
379 try {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
380 msg = JSON.parse(ev.data);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
381 } catch (e) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
382 if (e instanceof SyntaxError)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
383 return;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
384 }
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
385 if (msg.t == "t") {
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
386 tableCurrent(msg.g);
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
387 }
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
388 else if ((msg.t == "b" || msg.t == "e") &&
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
389 msg.p == sessionStorage.getItem("lname")) {
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
390 getchoices();
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
391 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
392 };
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
393 statsock.onclose = function (ev) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
394 statsock = null;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
395 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
396 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
397
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
398 /* FIXME gamelist API has changed */
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
399 function getcurrent(clear) {
116
d7d7cdcba3b4 Fix WebSocket support checking.
John "Elwin" Edwards <elwin@sdf.org>
parents: 112
diff changeset
400 if (window.WebSocket) {
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
401 return;
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
402 }
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
403 if (session.connect || clear) {
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
404 if (statInterval) {
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
405 window.clearInterval(statInterval);
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
406 statInterval = null;
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
407 }
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
408 return;
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
409 }
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
410 if (!statInterval) {
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
411 statInterval = window.setInterval(getcurrent, statDelta);
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
412 }
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
413 if ("lcred" in sessionStorage)
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
414 getchoices();
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
415 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
416 req.onerror = errHandler;
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
417 req.onreadystatechange = function () {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
418 if (req.readyState != 4 || req.status != 200)
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
419 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
420 var reply;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
421 try {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
422 reply = JSON.parse(req.responseText);
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
423 } catch (e) {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
424 if (e instanceof SyntaxError)
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
425 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
426 }
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
427 if (!reply.s) {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
428 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
429 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
430 tableCurrent(reply.g);
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
431 };
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
432 req.open('GET', '/status', true);
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
433 req.send();
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
434 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
435 }
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
436
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
437 function getchoices() {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
438 if (session.connect || !("lcred" in sessionStorage))
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
439 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
440 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
441 req.onerror = errHandler;
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
442 req.onreadystatechange = function () {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
443 if (req.readyState != 4 || req.status != 200)
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
444 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
445 var reply;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
446 try {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
447 reply = JSON.parse(req.responseText);
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
448 } catch (e) {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
449 if (e instanceof SyntaxError)
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
450 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
451 }
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
452 if (!("name" in reply) || reply["name"] != sessionStorage.getItem("lname")
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
453 || !("stat" in reply))
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
454 return;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
455 var optdiv = document.getElementById("opttable");
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
456 /* Don't remove the first child, it's the header. */
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
457 while (optdiv.childNodes.length > 1)
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
458 optdiv.removeChild(optdiv.childNodes[1]);
64
e3082fd06520 Some status improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 63
diff changeset
459 for (var gname in games) {
e3082fd06520 Some status improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 63
diff changeset
460 if (!(gname in reply.stat))
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
461 continue;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
462 var acttext;
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
463 if (reply.stat[gname] == "s")
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
464 acttext = "Resume your game";
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
465 else if (reply.stat[gname] == "0")
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
466 acttext = "Start a game";
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
467 else if (reply.stat[gname] == "p" || reply.stat[gname] == "d")
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
468 acttext = "Force save";
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
469 else
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
470 continue;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
471 var button = document.createElement("span");
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
472 button.appendChild(document.createTextNode(acttext));
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
473 if ("s0".indexOf(reply.stat[gname]) >= 0) {
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
474 button.onclick = makeStarter(gname);
51
2eda3909f6a3 rlgterm.js: don't make nonfunctional button.
John "Elwin" Edwards <elwin@sdf.org>
parents: 47
diff changeset
475 button.className = "ibutton";
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
476 }
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
477 else {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
478 button.onclick = makeStopper(gname);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
479 button.className = "ibutton";
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
480 }
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
481 var actdiv = document.createElement("div");
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
482 actdiv.appendChild(button);
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
483 var gamediv = document.createElement("div");
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
484 gamediv.appendChild(document.createTextNode(games[gname].name));
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
485 var rowdiv = document.createElement("div");
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
486 rowdiv.appendChild(gamediv);
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
487 rowdiv.appendChild(actdiv);
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
488 optdiv.appendChild(rowdiv);
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
489 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
490 };
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
491 req.open('GET', '/pstatus/' + sessionStorage.getItem("lname"), true);
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
492 req.send();
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
493 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
494 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
495
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
496 /* This can't be in the loop in getchoices(), or the closure's scope will
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
497 * get overwritten on the next iteration, and then all the games end up
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
498 * being Super-Rogue.
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
499 */
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
500 function makeStarter(gname) {
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
501 if (!(gname in games))
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
502 return null;
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
503 var game = games[gname];
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
504 function starter(ev) {
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
505 startgame(game);
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
506 }
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
507 return starter;
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
508 }
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
509
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
510 function makeStopper(gname) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
511 if (!(gname in games))
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
512 return null;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
513 var game = games[gname];
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
514 function stopper(ev) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
515 stopgame(game);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
516 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
517 return stopper;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
518 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
519
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
520 function stopgame(game) {
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
521 if (!("lcred" in sessionStorage))
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
522 return;
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
523 var stopmsg = {"key": sessionStorage.getItem("lcred"), "g": game.uname};
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
524 var req = new XMLHttpRequest();
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
525 req.onerror = errHandler;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
526 req.onreadystatechange = function () {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
527 if (req.readyState != 4 || req.status != 200)
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
528 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
529 var reply = JSON.parse(req.responseText);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
530 if (reply.t == 'E') {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
531 if (reply.c == 7)
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
532 message("That game has already stopped.");
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
533 else if (reply.c == 1) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
534 logout();
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
535 message("The server forgot about you, please log in again.", "warn");
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
536 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
537 else {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
538 message("That game could not be stopped because: " + reply.s +
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
539 "This might be a bug.", "warn");
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
540 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
541 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
542 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
543 req.open('POST', '/quit', true);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
544 req.send(JSON.stringify(stopmsg));
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
545 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
546 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
547
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
548 function startgame(game) {
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
549 if (!("lcred" in sessionStorage) || session.connect)
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
550 return;
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
551 if (!window.WebSocket) {
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
552 return;
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
553 }
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
554 var sockurl = "ws://" + window.location.host + "/play/" + game.uname;
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
555 sockurl += "?key=" + sessionStorage.getItem("lcred") + "&w=80&h=24";
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
556 ws = new WebSocket(sockurl);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
557 ws.onopen = function (event) {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
558 session.connect = true;
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
559 session.playing = true;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
560 session.sock = ws;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
561 setmode("play");
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
562 };
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
563 ws.onmessage = function (event) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
564 var msgObject = JSON.parse(event.data);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
565 if (msgObject.t == 's') {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
566 termemu.resize(msgObject.h, msgObject.w);
118
56a756695740 rlgterm.js: display the game's proper name when starting.
John "Elwin" Edwards <elwin@sdf.org>
parents: 116
diff changeset
567 message("You are now playing " + games[msgObject.g].name + ".");
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
568 }
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
569 else if (msgObject.t == 'd') {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
570 writeData(msgObject.d);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
571 }
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
572 };
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
573 ws.onclose = function (event) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
574 session.sock = null;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
575 gameover();
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
576 };
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
577 }
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
578
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
579 function makeWatcher(t) {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
580 function watcher(ev) {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
581 startwatching(t);
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
582 }
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
583 return watcher;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
584 }
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
585
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
586 function startwatching(tag) {
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
587 if (session.connect)
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
588 return;
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
589 var sockurl = "ws://" + window.location.host + "/watch/" + tag;
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
590 var ws = new WebSocket(sockurl);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
591 ws.onopen = function (event) {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
592 session.connect = true;
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
593 session.sock = ws;
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
594 setmode("watch");
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
595 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
596 ws.onmessage = function (event) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
597 var msgObject = JSON.parse(event.data);
101
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
598 if (msgObject.t == 'w') {
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
599 termemu.resize(msgObject.h, msgObject.w);
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
600 termemu.reset();
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
601 termemu.toAltBuf();
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
602 var pname = msgObject.p;
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
603 var gname = games[msgObject.g].name;
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
604 message("You are now watching " + pname + " play " + gname + ".");
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
605 }
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
606 else if (msgObject.t == 'd') {
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
607 writeData(msgObject.d);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
608 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
609 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
610 ws.onclose = function (event) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
611 session.sock = null;
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
612 gameover();
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
613 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
614 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
615
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
616 function formreg(ev) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
617 ev.preventDefault();
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
618 /* This ought to check for being logged in instead. */
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
619 if (session.connect)
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
620 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
621 var regmsg = {};
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
622 regmsg["name"] = document.getElementById("regin_name").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
623 regmsg["pw"] = document.getElementById("regin_pw").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
624 regmsg["email"] = document.getElementById("regin_email").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
625 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
626 req.onerror = errHandler;
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
627 req.onreadystatechange = function () {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
628 if (req.readyState != 4 || req.status != 200)
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
629 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
630 var reply = JSON.parse(req.responseText);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
631 if (reply.t == 'r') {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
632 /* Success */
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
633 message("Welcome " + reply.u + ", you are now registered.");
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
634 sessionStorage.setItem("lcred", reply.k);
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
635 sessionStorage.setItem("lname", reply.u);
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
636 message("You are now logged in as " + reply.u + ".");
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
637 setmode("choose");
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
638 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
639 else if (reply.t == 'E') {
93
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
640 var failmsg = "Registration failed.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
641 if (reply.c == 2) {
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
642 var errdesc = reply.s.match(/Invalid data: (.*)/)[1];
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
643 if (errdesc.match(/No name/))
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
644 failmsg += " You need to choose a name.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
645 else if (errdesc.match(/No password/))
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
646 failmsg += " You need to choose a password.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
647 else if (errdesc.match(/Invalid/)) {
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
648 failmsg += " Names must be letters and numbers. E-mail addresses " +
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
649 "can also contain these characters: @.-_";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
650 }
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
651 else if (errdesc.match(/Username/))
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
652 failmsg += " Someone else is already using that name.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
653 else
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
654 failmsg += " This is probably a bug.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
655 }
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
656 else
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
657 failmsg += " This is probably a bug.";
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
658 message(failmsg, "warn");
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
659 document.getElementById("regin_name").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
660 document.getElementById("regin_pw").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
661 document.getElementById("regin_email").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
662 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
663 };
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
664 req.open('POST', '/addacct', true);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
665 req.send(JSON.stringify(regmsg));
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
666 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
667 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
668
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
669 function gameover() {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
670 if (!session.connect)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
671 return;
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
672 /* TODO IFACE2 If the end was unexpected, tell player the game was saved. */
90
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
673 if (session.playing)
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
674 message("Finished playing.");
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
675 else
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
676 message("Finished watching.");
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
677 session.connect = false;
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
678 session.playing = false;
61
071ec6b1ec03 RLG-Web client: initialize and restore the terminal when watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 59
diff changeset
679 termemu.toNormBuf();
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
680 if ("lcred" in sessionStorage)
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
681 setmode("choose");
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
682 else
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
683 setmode("login");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
684 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
685 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
686
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
687 function logout() {
127
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
688 sessionStorage.removeItem("lcred");
e54018b26ed8 RLG-Web client: store login key in DOM Storage.
John "Elwin" Edwards <elwin@sdf.org>
parents: 120
diff changeset
689 sessionStorage.removeItem("lname");
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
690 setmode("login");
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
691 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
692
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
693 /* TODO determine whether this is needed */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
694 function stop() {
159
a613380ffdc2 RLGWebD: excise polling.
John "Elwin" Edwards
parents: 127
diff changeset
695 if (!session.connect)
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
696 return;
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
697 if (session.sock) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
698 session.sock.close();
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
699 return;
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
700 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
701 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
702
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
703 function setmode(mode, ev) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
704 if (ev)
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
705 ev.preventDefault();
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
706 if (mode == "play") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
707 document.getElementById("keyboard").style.display = "block";
90
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
708 document.getElementById("playctl").style.display = "block";
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
709 document.getElementById("startgame").style.display = "none";
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
710 document.getElementById("login").style.display = "none";
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
711 document.getElementById("register").style.display = "none";
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
712 document.getElementById("current").style.display = "none";
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
713 }
90
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
714 else if (mode == "watch") {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
715 document.getElementById("keyboard").style.display = "none";
90
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
716 document.getElementById("playctl").style.display = "block";
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
717 document.getElementById("startgame").style.display = "none";
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
718 document.getElementById("login").style.display = "none";
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
719 document.getElementById("register").style.display = "none";
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
720 document.getElementById("current").style.display = "none";
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
721 }
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
722 else if (mode == "choose") {
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
723 document.getElementById("keyboard").style.display = "none";
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
724 document.getElementById("playctl").style.display = "none";
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
725 document.getElementById("startgame").style.display = "block";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
726 document.getElementById("login").style.display = "none";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
727 document.getElementById("register").style.display = "none";
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
728 document.getElementById("current").style.display = "block";
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
729 getcurrent();
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
730 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
731 else if (mode == "login") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
732 document.getElementById("keyboard").style.display = "none";
90
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
733 document.getElementById("playctl").style.display = "none";
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
734 document.getElementById("startgame").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
735 document.getElementById("login").style.display = "block";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
736 document.getElementById("register").style.display = "none";
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
737 document.getElementById("current").style.display = "block";
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
738 getcurrent();
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
739 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
740 else if (mode == "register") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
741 document.getElementById("keyboard").style.display = "none";
90
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
742 document.getElementById("playctl").style.display = "none";
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
743 document.getElementById("startgame").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
744 document.getElementById("login").style.display = "none";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
745 document.getElementById("register").style.display = "block";
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
746 document.getElementById("current").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
747 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
748 wsCurrent();
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
749 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
750
76
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
751 function toggleBlock(id) {
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
752 var element = document.getElementById(id);
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
753 if (!element)
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
754 return;
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
755 if (element.style.display != "block")
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
756 element.style.display = "block";
67
b6a3b26fe0dc RLG-Web client: some visual improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
757 else
76
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
758 element.style.display = "none";
67
b6a3b26fe0dc RLG-Web client: some visual improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
759 }
b6a3b26fe0dc RLG-Web client: some visual improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
760
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
761 function message(msg, mtype) {
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
762 var msgdiv = document.createElement("div");
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
763 var msgtext = document.createTextNode(msg);
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
764 msgdiv.appendChild(msgtext);
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
765 if (mtype) {
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
766 msgdiv.className = mtype;
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
767 }
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
768 var msgcontainer = document.getElementById("messages");
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
769 msgcontainer.insertBefore(msgdiv, msgcontainer.firstChild);
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
770 }
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
771
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
772 function debug(level, msg) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
773 if (level < debugSuppress)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
774 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
775 var msgdiv = document.createElement("div");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
776 var msgtext = document.createTextNode(msg);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
777 msgdiv.appendChild(msgtext);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
778 document.getElementById("debug").appendChild(msgdiv);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
779 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
780 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
781
92
a4d3ecf188b7 RLG-Web client: replace setTitle() with message().
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
782 function setTitle(tstr) {
a4d3ecf188b7 RLG-Web client: replace setTitle() with message().
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
783 message(tstr);
a4d3ecf188b7 RLG-Web client: replace setTitle() with message().
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
784 }
a4d3ecf188b7 RLG-Web client: replace setTitle() with message().
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
785
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
786 function textsize(larger) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
787 var cssSize = termemu.view.style.fontSize;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
788 if (!cssSize) {
78
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
789 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
790 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
791 var match = cssSize.match(/\d*/);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
792 if (!match) {
78
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
793 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
794 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
795 var csize = Number(match[0]);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
796 var nsize;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
797 if (larger) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
798 if (csize >= 48)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
799 nsize = 48;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
800 else if (csize >= 20)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
801 nsize = csize + 4;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
802 else if (csize >= 12)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
803 nsize = csize + 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
804 else if (csize >= 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
805 nsize = csize + 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
806 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
807 nsize = 8;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
808 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
809 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
810 if (csize <= 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
811 nsize = 8;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
812 else if (csize <= 12)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
813 nsize = csize - 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
814 else if (csize <= 20)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
815 nsize = csize - 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
816 else if (csize <= 48)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
817 nsize = csize - 4;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
818 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
819 nsize = 48;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
820 }
77
f8bb37f48d58 Make font resizing affect the keyboard too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 76
diff changeset
821 var cssvalstr = nsize.toString() + "px";
f8bb37f48d58 Make font resizing affect the keyboard too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 76
diff changeset
822 document.getElementById("term").style.fontSize = cssvalstr;
f8bb37f48d58 Make font resizing affect the keyboard too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 76
diff changeset
823 document.getElementById("keys").style.fontSize = cssvalstr;
9
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 6
diff changeset
824 termemu.fixsize();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
825 debug(1, "Changing font size to " + nsize.toString());
78
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
826 return nsize;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
827 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
828
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
829 function idlestr(ms) {
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
830 if (typeof(ms) != "number")
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
831 return "?";
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
832 var seconds = Math.round(ms / 1000);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
833 var ss = String(seconds % 60);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
834 if (ss.length < 2)
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
835 ss = "0" + ss;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
836 var mm = String(Math.floor((seconds % 3600) / 60));
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
837 if (mm.length < 2)
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
838 mm = "0" + mm;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
839 var hh = String(Math.floor(seconds / 3600));
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
840 if (hh.length < 2)
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
841 hh = "0" + hh;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
842 return hh + ":" + mm + ":" + ss;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
843 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
844
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
845 function bell(on) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
846 var imgnode = document.getElementById("bell");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
847 if (on) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
848 imgnode.style.visibility = "visible";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
849 window.setTimeout(bell, 1500, false);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
850 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
851 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
852 imgnode.style.visibility = "hidden";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
853 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
854 }