annotate rlgterm.js @ 122:6cfaf6d202be

Improve the probability of detecting numpad 5. Firefox now, in some circumstances, thinks numpad 5 is the clear key, with keycode 12. This makes it detectable.
author John "Elwin" Edwards <elwin@sdf.org>
date Sun, 12 Aug 2012 08:22:55 -0700
parents 54979d35611a
children e54018b26ed8
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 // A state machine that keeps track of polling the server.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 var ajaxstate = {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 state: 0,
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 timerID: null,
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 clear: function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
8 if (this.timerID != null) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
9 window.clearTimeout(this.timerID);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
10 this.timerID = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
12 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
13 set: function (ms) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14 this.clear();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15 this.timerID = window.setTimeout(getData, ms);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
16 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
17 gotdata: function () {
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
18 this.set(1000);
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
19 this.state = 1;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 gotnothing: function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
22 if (this.state == 0) {
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
23 this.set(1000);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
24 this.state = 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
25 }
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
26 else if (this.state < 4) {
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
27 this.set(4000);
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
28 this.state++;
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
29 }
63
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
30 else if (session.playing) {
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
31 if (this.state < 8) {
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
32 this.set(15000);
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
33 this.state++;
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
34 }
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
35 else {
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
36 /* It's been over a minute. Stop polling. */
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
37 this.clear();
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
38 }
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
39 }
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
40 else {
63
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
41 /* If watching, it can't stop polling entirely, because there
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
42 * are no POST events to start it up again. */
a077f9f84052 RLG-Web client: adjust polling for watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 61
diff changeset
43 this.set(10000);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
44 }
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
45 },
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
46 posted: function (wasdata) {
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
47 if (wasdata) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
48 this.set(1000);
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
49 this.state = 1;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
50 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
51 else {
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
52 this.set(200);
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
53 this.state = 0;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
54 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
55 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
56 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
57
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
58 /* Data on the available games. */
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
59 var games = {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
60 "rogue3": {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
61 "name": "Rogue V3",
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
62 "uname": "rogue3"
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
63 },
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
64 "rogue4": {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
65 "name": "Rogue V4",
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
66 "uname": "rogue4"
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
67 },
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
68 "rogue5": {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
69 "name": "Rogue V5",
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
70 "uname": "rogue5"
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
71 },
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
72 "srogue": {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
73 "name": "Super-Rogue",
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
74 "uname": "srogue"
120
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 118
diff changeset
75 },
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 118
diff changeset
76 "arogue5": {
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 118
diff changeset
77 "name": "Advanced Rogue 5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 118
diff changeset
78 "uname": "arogue5"
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
79 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
80 };
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
81
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
82 var session = {
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
83 /* The session id assigned by the server. */
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
84 id: null,
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
85 /* Login name and key */
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
86 lname: null,
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
87 lcred: null,
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
88 /* 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
89 playing: false,
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
90 /* WebSocket for communication */
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
91 sock: null
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
92 };
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
93
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
94 /* 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
95 var statInterval = null;
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
96 /* How frequently to check. */
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
97 var statDelta = 8000;
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
98 /* 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
99 var statsock = null;
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
100
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
101 function writeData(hexstr) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
102 var codenum;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
103 var codes = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
104 var nc;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105 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
106 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
107 /* UTF-8 translation. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 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
109 nc = Number("0x" + hexstr.substr(i, 2));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
110 if (nc < 0x7F) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
111 /* 0------- */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
112 codes.push(nc);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
113 /* Any incomplete sequence will be discarded. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
114 u8wait = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
115 expect = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
116 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 else if (nc < 0xC0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
118 /* 10------ : part of a multibyte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
119 if (expect > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
120 u8wait <<= 6;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
121 u8wait += (nc & 0x3F);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
122 expect--;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
123 if (expect == 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
124 codes.push(u8wait);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
125 u8wait = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
126 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
127 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
128 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
129 /* Assume an initial byte was missed. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
130 u8wait = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
131 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
132 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
133 /* These will all discard any incomplete sequence. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
134 else if (nc < 0xE0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
135 /* 110----- : introduces 2-byte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
136 u8wait = (nc & 0x1F);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
137 expect = 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
138 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
139 else if (nc < 0xF0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
140 /* 1110---- : introduces 3-byte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
141 u8wait = (nc & 0x0F);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
142 expect = 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
143 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
144 else if (nc < 0xF8) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
145 /* 11110--- : introduces 4-byte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
146 u8wait = (nc & 0x07);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
147 expect = 3;
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 (nc < 0xFC) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
150 /* 111110-- : introduces 5-byte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
151 u8wait = (nc & 0x03);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
152 expect = 4;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
153 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
154 else if (nc < 0xFE) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
155 /* 1111110- : introduces 6-byte sequence */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
156 u8wait = (nc & 0x01);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
157 expect = 5;
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 /* 1111111- : should never appear */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
161 u8wait = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
162 expect = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
163 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
164 /* Supporting all 31 bits is probably overkill... */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
165 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
166 termemu.write(codes);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
167 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
168 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
169
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
170 /* State for sending and receiving messages. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
171 var nsend = 0; // The number of the next packet to send.
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
172 var nrecv = 0; // The next packet expected.
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
173 var msgQ = []; // Queue for out-of-order messages.
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
174
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
175 /* Processes a message from the server, returning true or false if it was a
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
176 * data message with or without data, null if not data.
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
177 * All non-special responseTexts should be handed directly to this function.
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
178 */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
179 function processMsg(msg) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
180 var msgDicts;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
181 var havedata = null; // eventual return value
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
182 try {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
183 msgDicts = JSON.parse(msg);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
184 } catch (e) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
185 if (e instanceof SyntaxError)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
186 return null;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
187 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
188 if (msgDicts.length === 0)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
189 return false;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
190 for (var j = 0; j < msgDicts.length; j++) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
191 if (!msgDicts[j].t)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
192 continue;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
193 else if (msgDicts[j].t == "E") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
194 if (msgDicts[j].c == 1 || msgDicts[j].c == 6 || msgDicts[j].c == 7) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
195 gameover();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
196 if (msgDicts[j].c == 1) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
197 logout();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
198 }
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
199 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
200 debug(1, "Server error: " + msgDicts[j].s);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
201 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
202 // A data message
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
203 else if (msgDicts[j].t == "d") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
204 if (msgDicts[j].n === nrecv) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
205 writeData(msgDicts[j].d);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
206 nrecv++;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
207 /* Process anything in the queue that's now ready. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
208 var next;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
209 while ((next = msgQ.shift()) !== undefined) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
210 writeData(next.d);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
211 nrecv++;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
212 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
213 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
214 else if (msgDicts[j].n > nrecv) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
215 /* The current message comes after one still missing. Queue this one
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
216 * for later use. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
217 debug(1, "Got packet " + msgDicts[j].n + ", expected " + nrecv);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
218 msgQ[msgDicts[j].n - nrecv - 1] = msgDicts[j];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
219 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
220 else {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
221 /* This message's number was encountered previously. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
222 debug(1, "Discarding packet " + msgDicts[j].n + ", expected " + nrecv);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
223 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
224 havedata = true;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
225 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
226 else if (msgDicts[j].t == "T") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
227 setTitle(msgDicts[j].d);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
228 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
229 else if (msgDicts[j].t == "q") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
230 gameover();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
231 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
232 else {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
233 debug(1, "Unrecognized server message " + msg);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
234 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
235 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
236 return havedata;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
237 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
238
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
239 function getData() {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
240 if (session.id == null)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
241 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
242 var datareq = new XMLHttpRequest();
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
243 var msg = JSON.stringify({"id": session.id, "t": "n"});
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
244 datareq.onerror = errHandler;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
245 datareq.onreadystatechange = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
246 if (datareq.readyState == 4 && datareq.status == 200) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
247 var wasdata = processMsg(datareq.responseText);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
248 if (wasdata != null) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
249 if (wasdata)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
250 ajaxstate.gotdata();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
251 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
252 ajaxstate.gotnothing();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
253 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
254 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
255 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
256 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
257 datareq.open('POST', '/feed', true);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
258 datareq.send(msg);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
259 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
262 function postResponseHandler() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
263 if (this.readyState == 4 && this.status == 200) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
264 // We might want to do something with wasdata someday.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
265 var wasdata = processMsg(this.responseText);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
266 ajaxstate.posted();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
267 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
268 }
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
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
271 function errHandler() {
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
272 message("Unable to connect to the server.", "warn");
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
273 }
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
274
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
275 function sendback(str) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
276 /* For responding to terminal queries. */
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
277 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": str};
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
278 var datareq = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
279 datareq.onerror = errHandler;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
280 datareq.onreadystatechange = postResponseHandler;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
281 datareq.open('POST', '/feed', true);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
282 datareq.send(JSON.stringify(msgDict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
283 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
284 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
285
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
286 function sendkey(ev) {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
287 if (!session.playing)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
288 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
289 var keynum = ev.keyCode;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
290 var code;
70
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
291 if (keynum >= 65 && keynum <= 90) {
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
292 /* Letters. */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
293 if (ev.ctrlKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
294 keynum -= 64;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
295 else if (!ev.shiftKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
296 keynum += 32;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
297 code = keynum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
298 if (code.length < 2)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
299 code = "0" + code;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
300 }
70
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
301 else if (keynum >= 48 && keynum <= 57) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
302 /* The number row, NOT the numpad. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
303 if (ev.shiftKey) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
304 code = numShifts[keynum - 48].toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
305 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
306 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
307 code = keynum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
308 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
309 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
310 else if (keynum in keyHexCodes) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
311 if (ev.shiftKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
312 code = keyHexCodes[keynum][1];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
313 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
314 code = keyHexCodes[keynum][0];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
315 }
70
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
316 else if (keynum >= 16 && keynum <= 20) {
d7eb63cd7a16 Try to get some compatibility for keys.
John "Elwin" Edwards <elwin@sdf.org>
parents: 67
diff changeset
317 /* Shift, Cntl, Alt, CAPSLOCK */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
318 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
319 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
320 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
321 debug(1, "Ignoring keycode " + keynum);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
322 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
323 }
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
324 ev.preventDefault();
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
325 if (session.sock) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
326 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
327 }
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
328 else {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
329 var datareq = new XMLHttpRequest();
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
330 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": code};
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
331 datareq.onerror = errHandler;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
332 datareq.onreadystatechange = postResponseHandler;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
333 datareq.open('POST', '/feed', true);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
334 datareq.send(JSON.stringify(msgDict));
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
335 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
336 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
337 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
338
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
339 var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
340 ';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
341 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
342
76
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
343 var kpkeys = { "KP1": "1b4f46", "KP2": "1b4f42", "KP3": "1b5b367e",
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
344 "KP4": "1b4f44", "KP5": "1b5b45", "KP6": "1b4f43",
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
345 "KP7": "1b4f48", "KP8": "1b4f41", "KP9": "1b5b357e" };
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
346
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
347 function vkey(c) {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
348 if (!session.playing)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
349 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
350 var keystr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
351 if (c.match(/^[a-z]$/)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
352 if (termemu.ctrlp()) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
353 var n = c.charCodeAt(0) - 96;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
354 keystr = n.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
355 if (keystr.length < 2)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
356 keystr = "0" + keystr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
357 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
358 else if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
359 keystr = c.toUpperCase().charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
360 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
361 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
362 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
363 else if (c.match(/^[0-9]$/)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
364 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
365 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
366 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
367 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
368 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
369 else if (c == '\n')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
370 keystr = "0a";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
371 else if (c == '\t')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
372 keystr = "09";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
373 else if (c == '\b')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
374 keystr = "08";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
375 else if (c == ' ')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
376 keystr = "20";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
377 else if (c in charshifts) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
378 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
379 keystr = charshifts[c];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
380 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
381 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
382 }
76
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
383 else if (c in kpkeys) {
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
384 keystr = kpkeys[c];
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
385 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
386 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
387 return;
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
388 if (session.sock) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
389 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
390 }
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
391 else {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
392 var datareq = new XMLHttpRequest();
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
393 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": keystr};
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
394 datareq.onerror = errHandler;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
395 datareq.onreadystatechange = postResponseHandler;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
396 datareq.open('POST', '/feed', true);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
397 datareq.send(JSON.stringify(msgDict));
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
398 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
399 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
400 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
401
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
402 function setup() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
403 keyHexCodes.init();
4
ee22eb9ab009 Client: don't assume the terminal is 24x80.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
404 termemu.init("termwrap", 24, 80);
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
405 setmode("login");
78
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
406 /* Set up the text size. */
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
407 var cssSize = termemu.view.style.fontSize;
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
408 var match = cssSize.match(/\d*/);
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
409 if (!match) {
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
410 return;
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
411 }
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
412 var csize = Number(match[0]);
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
413 var allscreen = document.getElementById("termwrap");
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
414 while (csize > 9 && csize < 48) {
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
415 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
416 csize = textsize(false);
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
417 }
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
418 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
419 csize = textsize(true);
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
420 }
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
421 else
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
422 break;
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
423 }
116
d7d7cdcba3b4 Fix WebSocket support checking.
John "Elwin" Edwards <elwin@sdf.org>
parents: 112
diff changeset
424 if (!window.WebSocket) {
109
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
425 message("Your browser does not support WebSockets. You can still play, " +
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
426 "but it will be slower, and may not work in the future.", "warn");
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
427 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
428 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
429 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
430
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
431 function toggleshift() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
432 termemu.toggleshift();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
433 keydiv = document.getElementById("shiftkey");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
434 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
435 keydiv.className = "keysel";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
436 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
437 keydiv.className = "key";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
438 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
439 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
440
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
441 function togglectrl() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
442 termemu.togglectrl();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
443 keydiv = document.getElementById("ctrlkey");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
444 if (termemu.ctrlp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
445 keydiv.className = "keysel";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
446 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
447 keydiv.className = "key";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
448 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
449 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
450
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
451 function formlogin(ev) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
452 ev.preventDefault();
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
453 if (session.id != null)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
454 return;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
455 var loginmsg = {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
456 loginmsg["name"] = document.getElementById("input_name").value;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
457 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
458 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
459 req.onerror = errHandler;
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
460 req.onreadystatechange = function () {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
461 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
462 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
463 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
464 if (reply.t == 'l') {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
465 /* Success */
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
466 session.lcred = reply.k;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
467 session.lname = reply.u;
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
468 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
469 setmode("choose");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
470 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
471 else if (reply.t == 'E') {
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
472 var failmsg = "Logging in failed. ";
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
473 if (reply.c == 2)
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
474 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
475 else if (reply.c == 3)
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
476 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
477 else if (reply.c == 6)
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
478 failmsg += "The server is shutting down.";
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
479 message(failmsg, "warn");
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
480 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
481 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
482 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
483 };
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
484 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
485 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
486 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
487 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
488
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
489 function tableCurrent(gamelist) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
490 var gamediv = document.getElementById("gametable");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
491 while (gamediv.children.length > 2)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
492 gamediv.removeChild(gamediv.children[2]);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
493 if (gamelist.length === 0) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
494 gamediv.style.display = "none";
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
495 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
496 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
497 else {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
498 gamediv.style.display = "table";
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
499 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
500 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
501 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
502 var row = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
503 var cell1 = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
504 var cell2 = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
505 var cell3 = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
506 var cell4 = document.createElement("div");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
507 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
508 var uname = gamelist[i].g;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
509 if (uname in games)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
510 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
511 else {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
512 continue;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
513 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
514 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
515 var button = document.createElement("span");
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
516 button.appendChild(document.createTextNode("Watch"));
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
517 button.onclick = makeWatcher(gamelist[i].n);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
518 button.className = "ibutton";
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
519 cell4.appendChild(button);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
520 row.appendChild(cell1);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
521 row.appendChild(cell2);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
522 row.appendChild(cell3);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
523 row.appendChild(cell4);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
524 gamediv.appendChild(row);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
525 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
526 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
527
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
528 /* 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
529 function wsCurrent() {
116
d7d7cdcba3b4 Fix WebSocket support checking.
John "Elwin" Edwards <elwin@sdf.org>
parents: 112
diff changeset
530 if (!window.WebSocket)
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
531 return;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
532 if (session.id) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
533 /* 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
534 if (statsock) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
535 statsock.close();
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
536 statsock = null;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
537 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
538 return;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
539 }
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
540 if (session.lcred) {
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
541 /* 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
542 getchoices();
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
543 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
544 if (statsock)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
545 return;
106
dc1414faee19 RLG-Web client: determine the host for WebSocket connections.
John "Elwin" Edwards <elwin@sdf.org>
parents: 105
diff changeset
546 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
547 statsock.onmessage = function (ev) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
548 var msg;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
549 try {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
550 msg = JSON.parse(ev.data);
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
551 } catch (e) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
552 if (e instanceof SyntaxError)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
553 return;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
554 }
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
555 if (msg.t == "t") {
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
556 tableCurrent(msg.g);
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
557 }
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
558 else if ((msg.t == "b" || msg.t == "e") && msg.p == session.lname) {
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
559 getchoices();
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
560 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
561 };
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
562 statsock.onclose = function (ev) {
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
563 statsock = null;
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
564 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
565 }
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
566
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
567 function getcurrent(clear) {
116
d7d7cdcba3b4 Fix WebSocket support checking.
John "Elwin" Edwards <elwin@sdf.org>
parents: 112
diff changeset
568 if (window.WebSocket) {
112
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
569 return;
4f2b89e6fde2 RLG-Web: improvements to choices and status messaging.
John "Elwin" Edwards <elwin@sdf.org>
parents: 111
diff changeset
570 }
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
571 if (session.id || clear) {
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
572 if (statInterval) {
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
573 window.clearInterval(statInterval);
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
574 statInterval = null;
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
575 }
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
576 return;
73
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
577 }
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
578 if (!statInterval) {
d084610a89b5 Automatically update the lists of current games and actions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 70
diff changeset
579 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
580 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
581 if (session.lcred)
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
582 getchoices();
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
583 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
584 req.onerror = errHandler;
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
585 req.onreadystatechange = function () {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
586 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
587 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
588 var reply;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
589 try {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
590 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
591 } catch (e) {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
592 if (e instanceof SyntaxError)
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
593 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
594 }
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
595 if (!reply.s) {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
596 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
597 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
598 tableCurrent(reply.g);
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
599 };
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
600 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
601 req.send();
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
602 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
603 }
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
604
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
605 function getchoices() {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
606 if (session.id != null || !session.lcred)
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
607 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
608 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
609 req.onerror = errHandler;
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
610 req.onreadystatechange = function () {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
611 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
612 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
613 var reply;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
614 try {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
615 reply = JSON.parse(req.responseText);
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
616 } catch (e) {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
617 if (e instanceof SyntaxError)
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
618 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
619 }
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
620 if (!("name" in reply) || reply["name"] != session.lname ||
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
621 !("stat" in reply))
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
622 return;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
623 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
624 /* 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
625 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
626 optdiv.removeChild(optdiv.childNodes[1]);
64
e3082fd06520 Some status improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 63
diff changeset
627 for (var gname in games) {
e3082fd06520 Some status improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 63
diff changeset
628 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
629 continue;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
630 var acttext;
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
631 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
632 acttext = "Resume your game";
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
633 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
634 acttext = "Start a game";
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
635 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
636 acttext = "Force save";
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
637 else
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
638 continue;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
639 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
640 button.appendChild(document.createTextNode(acttext));
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
641 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
642 button.onclick = makeStarter(gname);
51
2eda3909f6a3 rlgterm.js: don't make nonfunctional button.
John "Elwin" Edwards <elwin@sdf.org>
parents: 47
diff changeset
643 button.className = "ibutton";
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
644 }
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
645 else {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
646 button.onclick = makeStopper(gname);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
647 button.className = "ibutton";
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
648 }
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
649 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
650 actdiv.appendChild(button);
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
651 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
652 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
653 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
654 rowdiv.appendChild(gamediv);
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
655 rowdiv.appendChild(actdiv);
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
656 optdiv.appendChild(rowdiv);
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
657 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
658 };
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
659 req.open('GET', '/pstatus/' + session.lname, true);
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
660 req.send();
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
661 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
662 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
663
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
664 /* 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
665 * 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
666 * being Super-Rogue.
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
667 */
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
668 function makeStarter(gname) {
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
669 if (!(gname in games))
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
670 return null;
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
671 var game = games[gname];
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
672 function starter(ev) {
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
673 startgame(game);
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
674 }
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
675 return starter;
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
676 }
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
677
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
678 function startgame(game) {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
679 if (session.id != null || !session.lcred)
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
680 return;
116
d7d7cdcba3b4 Fix WebSocket support checking.
John "Elwin" Edwards <elwin@sdf.org>
parents: 112
diff changeset
681 if (window.WebSocket) {
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
682 wsStart(game);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
683 return;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
684 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
685 var smsg = {};
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
686 smsg["key"] = session.lcred;
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
687 smsg["game"] = game.uname;
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
688 smsg["h"] = 24;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
689 smsg["w"] = 80;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
690 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
691 req.onerror = errHandler;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
692 req.onreadystatechange = function () {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
693 if (req.readyState != 4 || req.status != 200)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
694 return;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
695 var reply = JSON.parse(req.responseText);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
696 if (reply.t == 's') {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
697 /* Success */
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
698 session.id = reply.id;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
699 session.playing = true;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
700 termemu.resize(reply.h, reply.w);
91
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 90
diff changeset
701 message("You are now playing " + game.name + ".");
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
702 setmode("play");
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
703 getData();
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
704 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
705 else if (reply.t == 'E') {
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
706 if (reply.c == 1) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
707 logout();
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
708 message("The server forgot about you, please log in again.", "warn");
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 93
diff changeset
709 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 93
diff changeset
710 else if (reply.c == 4) {
109
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
711 if (reply.s == "dgamelaunch") {
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
712 message("You are already playing " + game.name + " over SSH.",
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
713 "warn");
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
714 }
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
715 else {
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
716 message("You are already playing " + game.name +
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
717 " in another browser window.", "warn");
67b393f10c2b RLG-Web: improve taking over sessions.
John "Elwin" Edwards <elwin@sdf.org>
parents: 108
diff changeset
718 }
94
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 93
diff changeset
719 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 93
diff changeset
720 else if (reply.c == 7) {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 93
diff changeset
721 message("The game is being saved, try again in a few seconds.");
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 93
diff changeset
722 }
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 93
diff changeset
723 else {
597e9477b8ae RLG-Web: Allow games to be taken over.
John "Elwin" Edwards <elwin@sdf.org>
parents: 93
diff changeset
724 message("The server says it can't start your game because \"" +
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
725 reply.s + "\". This is probably a bug.", "warn");
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
726 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
727 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
728 };
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
729 req.open('POST', '/play', true);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
730 req.send(JSON.stringify(smsg));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
731 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
732 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
733
111
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
734 function makeStopper(gname) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
735 if (!(gname in games))
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
736 return null;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
737 var game = games[gname];
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
738 function stopper(ev) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
739 stopgame(game);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
740 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
741 return stopper;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
742 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
743
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
744 function stopgame(game) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
745 if (!session.lcred)
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
746 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
747 var stopmsg = {"key": session.lcred, "g": game.uname};
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
748 var req = new XMLHttpRequest();
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
749 req.onerror = errHandler;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
750 req.onreadystatechange = function () {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
751 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
752 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
753 var reply = JSON.parse(req.responseText);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
754 if (reply.t == 'E') {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
755 if (reply.c == 7)
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
756 message("That game has already stopped.");
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
757 else if (reply.c == 1) {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
758 logout();
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
759 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
760 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
761 else {
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
762 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
763 "This might be a bug.", "warn");
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
764 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
765 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
766 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
767 req.open('POST', '/quit', true);
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
768 req.send(JSON.stringify(stopmsg));
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
769 return;
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
770 }
f56fdfeed01a Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents: 109
diff changeset
771
108
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
772 function wsStart(game) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
773 var sockurl = "ws://" + window.location.host + "/play/" + game.uname;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
774 sockurl += "?key=" + session.lcred + "&w=80&h=24";
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
775 ws = new WebSocket(sockurl);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
776 ws.onopen = function (event) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
777 session.id = true;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
778 session.playing = true;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
779 session.sock = ws;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
780 setmode("play");
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
781 };
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
782 ws.onmessage = function (event) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
783 var msgObject = JSON.parse(event.data);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
784 if (msgObject.t == 's') {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
785 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
786 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
787 }
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
788 else if (msgObject.t == 'd') {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
789 writeData(msgObject.d);
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
790 }
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
791 };
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
792 ws.onclose = function (event) {
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
793 session.sock = null;
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
794 gameover();
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
795 };
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
796 }
86a458080e80 RLG-Web client: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 106
diff changeset
797
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
798 function startwatching(gamenumber) {
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
799 if (session.id != null)
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
800 return;
116
d7d7cdcba3b4 Fix WebSocket support checking.
John "Elwin" Edwards <elwin@sdf.org>
parents: 112
diff changeset
801 if (window.WebSocket) {
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
802 wsWatch(gamenumber);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
803 return;
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
804 }
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
805 var wmsg = {"n": Number(gamenumber)};
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
806 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
807 req.onerror = errHandler;
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
808 req.onreadystatechange = function () {
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
809 if (req.readyState != 4 || req.status != 200)
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
810 return;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
811 var reply = JSON.parse(req.responseText);
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
812 if (reply.t == 'w') {
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
813 /* Success */
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
814 session.id = reply.id;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
815 session.playing = false;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
816 termemu.resize(reply.h, reply.w);
61
071ec6b1ec03 RLG-Web client: initialize and restore the terminal when watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 59
diff changeset
817 termemu.reset();
071ec6b1ec03 RLG-Web client: initialize and restore the terminal when watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 59
diff changeset
818 termemu.toAltBuf();
91
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 90
diff changeset
819 var pname = reply.p;
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 90
diff changeset
820 var gname = games[reply.g].name;
e07f98799120 RLG-Web: improve game-starting messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 90
diff changeset
821 message("You are now watching " + pname + " play " + gname + ".");
90
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
822 setmode("watch");
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
823 getData();
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
824 }
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
825 else if (reply.t == 'E') {
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
826 message("The game could not be watched: " + reply.s, "warn");
65
e63c26dc0497 RLG-Web client: refresh the current games list.
John "Elwin" Edwards <elwin@sdf.org>
parents: 64
diff changeset
827 getcurrent();
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
828 }
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
829 };
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
830 req.open('POST', '/watch', true);
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
831 req.send(JSON.stringify(wmsg));
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
832 return;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
833 }
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
834
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
835 function makeWatcher(n) {
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
836 function watcher(ev) {
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
837 startwatching(n);
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
838 }
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
839 return watcher;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
840 }
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
841
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
842 function wsWatch(gamenumber) {
106
dc1414faee19 RLG-Web client: determine the host for WebSocket connections.
John "Elwin" Edwards <elwin@sdf.org>
parents: 105
diff changeset
843 var sockurl = "ws://" + window.location.host + "/watch/" + String(gamenumber);
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
844 var ws = new WebSocket(sockurl);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
845 ws.onopen = function (event) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
846 session.id = true;
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
847 session.sock = ws;
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
848 setmode("watch");
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
849 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
850 ws.onmessage = function (event) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
851 var msgObject = JSON.parse(event.data);
101
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
852 if (msgObject.t == 'w') {
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
853 termemu.resize(msgObject.h, msgObject.w);
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
854 termemu.reset();
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
855 termemu.toAltBuf();
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
856 var pname = msgObject.p;
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
857 var gname = games[msgObject.g].name;
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
858 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
859 }
e59d68082664 RLG-Web: Complete the WebSocket watcher.
John "Elwin" Edwards <elwin@sdf.org>
parents: 100
diff changeset
860 else if (msgObject.t == 'd') {
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
861 writeData(msgObject.d);
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
862 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
863 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
864 ws.onclose = function (event) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
865 session.sock = null;
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
866 gameover();
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
867 };
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
868 }
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
869
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
870 function formreg(ev) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
871 ev.preventDefault();
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
872 if (session.id != null)
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
873 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
874 var regmsg = {};
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
875 regmsg["name"] = document.getElementById("regin_name").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
876 regmsg["pw"] = document.getElementById("regin_pw").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
877 regmsg["email"] = document.getElementById("regin_email").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
878 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
879 req.onerror = errHandler;
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
880 req.onreadystatechange = function () {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
881 if (req.readyState != 4 || req.status != 200)
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
882 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
883 var reply = JSON.parse(req.responseText);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
884 if (reply.t == 'r') {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
885 /* Success */
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
886 message("Welcome " + reply.u + ", you are now registered.");
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
887 session.lcred = reply.k;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
888 session.lname = reply.u;
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
889 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
890 setmode("choose");
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
891 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
892 else if (reply.t == 'E') {
93
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
893 var failmsg = "Registration failed.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
894 if (reply.c == 2) {
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
895 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
896 if (errdesc.match(/No name/))
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
897 failmsg += " You need to choose a name.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
898 else if (errdesc.match(/No password/))
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
899 failmsg += " You need to choose a password.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
900 else if (errdesc.match(/Invalid/)) {
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
901 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
902 "can also contain these characters: @.-_";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
903 }
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
904 else if (errdesc.match(/Username/))
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
905 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
906 else
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
907 failmsg += " This is probably a bug.";
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
908 }
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
909 else
104409bf5f03 rlgterm.js: improve registration failure messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 92
diff changeset
910 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
911 message(failmsg, "warn");
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
912 document.getElementById("regin_name").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
913 document.getElementById("regin_pw").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
914 document.getElementById("regin_email").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
915 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
916 };
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
917 req.open('POST', '/addacct', true);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
918 req.send(JSON.stringify(regmsg));
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
919 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
920 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
921
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
922 function gameover() {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
923 if (session.id == null)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
924 return;
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
925 /* 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
926 if (session.playing)
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
927 message("Finished playing.");
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
928 else
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
929 message("Finished watching.");
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
930 session.id = null;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
931 session.playing = false;
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
932 ajaxstate.clear();
61
071ec6b1ec03 RLG-Web client: initialize and restore the terminal when watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 59
diff changeset
933 termemu.toNormBuf();
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
934 nsend = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
935 nrecv = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
936 msgQ = [];
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
937 if (session.lcred != null)
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
938 setmode("choose");
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
939 else
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
940 setmode("login");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
941 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
942 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
943
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
944 function logout() {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
945 session.lcred = null;
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
946 session.lname = null;
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
947 setmode("login");
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
948 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
949
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
950 function stop() {
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
951 if (!session.id)
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
952 return;
100
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
953 if (session.sock) {
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
954 session.sock.close();
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
955 return;
3dbfdaf62623 RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents: 97
diff changeset
956 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
957 var req = new XMLHttpRequest();
75
2984604ce3e6 Begin handling XMLHttpRequest errors.
John "Elwin" Edwards <elwin@sdf.org>
parents: 73
diff changeset
958 req.onerror = errHandler;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
959 req.onreadystatechange = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
960 if (req.readyState == 4 && req.status == 200) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
961 processMsg(req.responseText);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
962 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
963 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
964 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
965 req.open('POST', '/feed', true);
59
00b985b8ba6a RLG-Web client: implement watching.
John "Elwin" Edwards <elwin@sdf.org>
parents: 58
diff changeset
966 req.send(JSON.stringify({"id": session.id, "t": "q"}));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
967 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
968 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
969
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
970 function setmode(mode, ev) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
971 if (ev)
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
972 ev.preventDefault();
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
973 if (mode == "play") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
974 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
975 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
976 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
977 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
978 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
979 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
980 }
90
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
981 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
982 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
983 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
984 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
985 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
986 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
987 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
988 }
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
989 else if (mode == "choose") {
6c6a6e4f1e5b RLG-Web client: distinguish playing and watching in UI.
John "Elwin" Edwards <elwin@sdf.org>
parents: 89
diff changeset
990 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
991 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
992 document.getElementById("startgame").style.display = "block";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
993 document.getElementById("login").style.display = "none";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
994 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
995 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
996 getcurrent();
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
997 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
998 else if (mode == "login") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
999 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
1000 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
1001 document.getElementById("startgame").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1002 document.getElementById("login").style.display = "block";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1003 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
1004 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
1005 getcurrent();
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1006 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1007 else if (mode == "register") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1008 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
1009 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
1010 document.getElementById("startgame").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1011 document.getElementById("login").style.display = "none";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1012 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
1013 document.getElementById("current").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1014 }
105
a9371002aecc RLG-Web client: use WebSockets for game lists.
John "Elwin" Edwards <elwin@sdf.org>
parents: 101
diff changeset
1015 wsCurrent();
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1016 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
1017
76
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
1018 function toggleBlock(id) {
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
1019 var element = document.getElementById(id);
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
1020 if (!element)
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
1021 return;
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
1022 if (element.style.display != "block")
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
1023 element.style.display = "block";
67
b6a3b26fe0dc RLG-Web client: some visual improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
1024 else
76
a497ecd116d9 Improvements to the keyboard.
John "Elwin" Edwards <elwin@sdf.org>
parents: 75
diff changeset
1025 element.style.display = "none";
67
b6a3b26fe0dc RLG-Web client: some visual improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
1026 }
b6a3b26fe0dc RLG-Web client: some visual improvements.
John "Elwin" Edwards <elwin@sdf.org>
parents: 66
diff changeset
1027
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1028 function message(msg, mtype) {
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
1029 var msgdiv = document.createElement("div");
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
1030 var msgtext = document.createTextNode(msg);
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
1031 msgdiv.appendChild(msgtext);
96
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1032 if (mtype) {
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1033 msgdiv.className = mtype;
7bb634f6995c RLG-Web client: improvements to UI messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 94
diff changeset
1034 }
89
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
1035 var msgcontainer = document.getElementById("messages");
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
1036 msgcontainer.insertBefore(msgdiv, msgcontainer.firstChild);
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
1037 }
6145e1ea7ace RLG-Web: add a new message framework.
John "Elwin" Edwards <elwin@sdf.org>
parents: 88
diff changeset
1038
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1039 function debug(level, msg) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1040 if (level < debugSuppress)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1041 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1042 var msgdiv = document.createElement("div");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1043 var msgtext = document.createTextNode(msg);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1044 msgdiv.appendChild(msgtext);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1045 document.getElementById("debug").appendChild(msgdiv);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1046 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1047 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1048
92
a4d3ecf188b7 RLG-Web client: replace setTitle() with message().
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
1049 function setTitle(tstr) {
a4d3ecf188b7 RLG-Web client: replace setTitle() with message().
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
1050 message(tstr);
a4d3ecf188b7 RLG-Web client: replace setTitle() with message().
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
1051 }
a4d3ecf188b7 RLG-Web client: replace setTitle() with message().
John "Elwin" Edwards <elwin@sdf.org>
parents: 91
diff changeset
1052
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1053 function textsize(larger) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1054 var cssSize = termemu.view.style.fontSize;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1055 if (!cssSize) {
78
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
1056 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1057 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1058 var match = cssSize.match(/\d*/);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1059 if (!match) {
78
06375a159188 Try to guess a good text size.
John "Elwin" Edwards <elwin@sdf.org>
parents: 77
diff changeset
1060 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1061 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1062 var csize = Number(match[0]);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1063 var nsize;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1064 if (larger) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1065 if (csize >= 48)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1066 nsize = 48;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1067 else if (csize >= 20)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1068 nsize = csize + 4;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1069 else if (csize >= 12)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1070 nsize = csize + 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1071 else if (csize >= 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1072 nsize = csize + 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1073 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1074 nsize = 8;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1075 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1076 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1077 if (csize <= 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1078 nsize = 8;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1079 else if (csize <= 12)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1080 nsize = csize - 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1081 else if (csize <= 20)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1082 nsize = csize - 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1083 else if (csize <= 48)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1084 nsize = csize - 4;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1085 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1086 nsize = 48;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1087 }
77
f8bb37f48d58 Make font resizing affect the keyboard too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 76
diff changeset
1088 var cssvalstr = nsize.toString() + "px";
f8bb37f48d58 Make font resizing affect the keyboard too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 76
diff changeset
1089 document.getElementById("term").style.fontSize = cssvalstr;
f8bb37f48d58 Make font resizing affect the keyboard too.
John "Elwin" Edwards <elwin@sdf.org>
parents: 76
diff changeset
1090 document.getElementById("keys").style.fontSize = cssvalstr;
9
826a7ced69f8 Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents: 6
diff changeset
1091 termemu.fixsize();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1092 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
1093 return nsize;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1094 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1095
66
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1096 function idlestr(ms) {
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1097 if (typeof(ms) != "number")
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1098 return "?";
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1099 var seconds = Math.round(ms / 1000);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1100 var ss = String(seconds % 60);
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1101 if (ss.length < 2)
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1102 ss = "0" + ss;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1103 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
1104 if (mm.length < 2)
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1105 mm = "0" + mm;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1106 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
1107 if (hh.length < 2)
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1108 hh = "0" + hh;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1109 return hh + ":" + mm + ":" + ss;
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1110 }
57bf0dcd080e Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents: 65
diff changeset
1111
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1112 function bell(on) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1113 var imgnode = document.getElementById("bell");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1114 if (on) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1115 imgnode.style.visibility = "visible";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1116 window.setTimeout(bell, 1500, false);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1117 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1118 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1119 imgnode.style.visibility = "hidden";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1120 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1121 }