Mercurial > hg > rlgwebd
annotate rlgterm.js @ 52:ab82ae1ba793 beta1
RLG-Web: indicate beta status.
Due to the various improvements and testing, the project's status has
been promoted.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Mon, 11 Jun 2012 14:09:54 -0700 |
parents | 2eda3909f6a3 |
children | 96815eae4ebe |
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 } |
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
30 else if (this.state < 8) { |
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
31 this.set(15000); |
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
32 this.state++; |
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
33 } |
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
34 else { |
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
35 /* It's been over a minute. Stop polling. */ |
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
36 this.clear(); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
37 } |
35
f15efa4818b4
rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
38 }, |
f15efa4818b4
rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
39 posted: function (wasdata) { |
f15efa4818b4
rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
40 if (wasdata) { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
41 this.set(1000); |
35
f15efa4818b4
rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
42 this.state = 1; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
43 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
44 else { |
35
f15efa4818b4
rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
45 this.set(200); |
f15efa4818b4
rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
46 this.state = 0; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
47 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
48 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
50 |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
51 /* Data on the available games. */ |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
52 var games = { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
53 "rogue3": { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
54 "name": "Rogue V3", |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
55 "uname": "rogue3" |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
56 }, |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
57 "rogue4": { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
58 "name": "Rogue V4", |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
59 "uname": "rogue4" |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
60 }, |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
61 "rogue5": { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
62 "name": "Rogue V5", |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
63 "uname": "rogue5" |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
64 }, |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
65 "srogue": { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
66 "name": "Super-Rogue", |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
67 "uname": "srogue" |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
68 } |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
69 }; |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
70 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
71 /* Login name and key */ |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
72 var lname = null; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
73 var lcred = null; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
74 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
75 function writeData(hexstr) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
76 var codenum; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
77 var codes = []; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
78 var nc; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
79 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
|
80 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
|
81 /* UTF-8 translation. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
82 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
|
83 nc = Number("0x" + hexstr.substr(i, 2)); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
84 if (nc < 0x7F) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
85 /* 0------- */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
86 codes.push(nc); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
87 /* Any incomplete sequence will be discarded. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
88 u8wait = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
89 expect = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
90 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
91 else if (nc < 0xC0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
92 /* 10------ : part of a multibyte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
93 if (expect > 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
94 u8wait <<= 6; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
95 u8wait += (nc & 0x3F); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
96 expect--; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
97 if (expect == 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
98 codes.push(u8wait); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
99 u8wait = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
100 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
101 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
102 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
103 /* Assume an initial byte was missed. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
104 u8wait = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
105 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
106 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
107 /* These will all discard any incomplete sequence. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
108 else if (nc < 0xE0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
109 /* 110----- : introduces 2-byte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
110 u8wait = (nc & 0x1F); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
111 expect = 1; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
112 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
113 else if (nc < 0xF0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
114 /* 1110---- : introduces 3-byte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
115 u8wait = (nc & 0x0F); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
116 expect = 2; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
117 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
118 else if (nc < 0xF8) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
119 /* 11110--- : introduces 4-byte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
120 u8wait = (nc & 0x07); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
121 expect = 3; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
122 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
123 else if (nc < 0xFC) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
124 /* 111110-- : introduces 5-byte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
125 u8wait = (nc & 0x03); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
126 expect = 4; |
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 if (nc < 0xFE) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
129 /* 1111110- : introduces 6-byte sequence */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
130 u8wait = (nc & 0x01); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
131 expect = 5; |
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 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
134 /* 1111111- : should never appear */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
135 u8wait = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
136 expect = 0; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
137 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
138 /* Supporting all 31 bits is probably overkill... */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
139 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
140 termemu.write(codes); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
141 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
142 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
143 |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
144 /* State for sending and receiving messages. */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
145 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
|
146 var nrecv = 0; // The next packet expected. |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
147 var msgQ = []; // Queue for out-of-order messages. |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
148 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
149 /* 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
|
150 * 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
|
151 * 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
|
152 */ |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
153 function processMsg(msg) { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
154 var msgDict; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
155 var havedata = null; // eventual return value |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
156 try { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
157 msgDict = JSON.parse(msg); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
158 } catch (e) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
159 if (e instanceof SyntaxError) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
160 return null; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
161 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
162 if (!msgDict.t) |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
163 return null; |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
164 else if (msgDict.t == "E") { |
41
ea3b7775009d
rlgterm.js: fix failure to recognize end of game
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
165 if (msgDict.c == 1 || msgDict.c == 6 || msgDict.c == 7) { |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
166 gameover(); |
47
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
167 if (msgDict.c == 1) { |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
168 logout(); |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
169 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
170 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
171 debug(1, "Server error: " + msgDict.s); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
172 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
173 else if (msgDict.t == "n") { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
174 havedata = false; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
175 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
176 // A data message |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
177 else if (msgDict.t == "d"){ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
178 if (msgDict.n === nrecv) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
179 writeData(msgDict.d); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
180 nrecv++; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
181 /* Process anything in the queue that's now ready. */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
182 var next; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
183 while ((next = msgQ.shift()) !== undefined) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
184 writeData(next.d); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
185 nrecv++; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
186 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
187 } |
22 | 188 else if (msgDict.n > nrecv) { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
189 /* The current message comes after one still missing. Queue this one |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
190 * for later use. */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
191 debug(1, "Got packet " + msgDict.n + ", expected " + nrecv); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
192 msgQ[msgDict.n - nrecv - 1] = msgDict; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
193 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
194 else { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
195 /* This message's number was encountered previously. */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
196 debug(1, "Discarding packet " + msgDict.n + ", expected " + nrecv); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
197 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
198 havedata = true; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
199 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
200 else if (msgDict.t == "T") { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
201 setTitle(msgDict.d); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
202 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
203 else if (msgDict.t == "q") { |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
204 gameover(); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
205 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
206 else { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
207 debug(1, "Unrecognized server message " + msg); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
208 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
209 return havedata; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
210 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
211 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
212 function getData() { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
213 if (termemu.sessid == null) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
214 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
215 var datareq = new XMLHttpRequest(); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
216 var msg = JSON.stringify({"id": termemu.sessid, "t": "n"}); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
217 datareq.onreadystatechange = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
218 if (datareq.readyState == 4 && datareq.status == 200) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
219 var wasdata = processMsg(datareq.responseText); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
220 if (wasdata != null) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
221 if (wasdata) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
222 ajaxstate.gotdata(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
223 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
224 ajaxstate.gotnothing(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
225 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
226 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
227 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
228 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
229 datareq.open('POST', '/feed', true); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
230 datareq.send(msg); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
231 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
232 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
233 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
234 function postResponseHandler() { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
235 if (this.readyState == 4 && this.status == 200) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
236 // 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
|
237 var wasdata = processMsg(this.responseText); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
238 ajaxstate.posted(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
239 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
240 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
241 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
242 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
243 function sendback(str) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
244 /* For responding to terminal queries. */ |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
245 var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": str}; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
246 var datareq = new XMLHttpRequest(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
247 datareq.onreadystatechange = postResponseHandler; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
248 datareq.open('POST', '/feed', true); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
249 datareq.send(JSON.stringify(msgDict)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
250 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
251 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
252 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
253 function sendkey(ev) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
254 if (termemu.sessid == null) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
255 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
256 var keynum = ev.keyCode; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
257 var code; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
258 if (keynum >= ev.DOM_VK_A && keynum <= ev.DOM_VK_Z) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
259 /* Letters. This assumes the codes are 65-90. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
260 if (ev.ctrlKey) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
261 keynum -= 64; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
262 else if (!ev.shiftKey) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
263 keynum += 32; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
264 code = keynum.toString(16); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
265 if (code.length < 2) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
266 code = "0" + code; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
267 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
268 else if (keynum >= ev.DOM_VK_0 && keynum <= ev.DOM_VK_9) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
269 /* The number row, NOT the numpad. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
270 if (ev.shiftKey) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
271 code = numShifts[keynum - 48].toString(16); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
272 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
273 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
274 code = keynum.toString(16); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
275 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
276 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
277 else if (keynum in keyHexCodes) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
278 if (ev.shiftKey) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
279 code = keyHexCodes[keynum][1]; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
280 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
281 code = keyHexCodes[keynum][0]; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
282 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
283 else if (keynum == ev.DOM_VK_SHIFT || keynum == ev.DOM_VK_CONTROL || |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
284 keynum == ev.DOM_VK_ALT || keynum == ev.DOM_VK_CAPS_LOCK) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
285 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
286 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
287 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
288 debug(1, "Ignoring keycode " + keynum); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
289 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
290 } |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
291 // Isn't this check redundant? |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
292 if (termemu.sessid != null) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
293 ev.preventDefault(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
294 var datareq = new XMLHttpRequest(); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
295 var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": code}; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
296 datareq.onreadystatechange = postResponseHandler; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
297 datareq.open('POST', '/feed', true); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
298 datareq.send(JSON.stringify(msgDict)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
299 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
300 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
301 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
302 var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
303 ';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e" |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
304 } |
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 function vkey(c) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
307 if (termemu.sessid == null) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
308 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
309 var keystr; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
310 if (c.match(/^[a-z]$/)) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
311 if (termemu.ctrlp()) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
312 var n = c.charCodeAt(0) - 96; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
313 keystr = n.toString(16); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
314 if (keystr.length < 2) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
315 keystr = "0" + keystr; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
316 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
317 else if (termemu.shiftp()) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
318 keystr = c.toUpperCase().charCodeAt(0).toString(16); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
319 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
320 keystr = c.charCodeAt(0).toString(16); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
321 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
322 else if (c.match(/^[0-9]$/)) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
323 if (termemu.shiftp()) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
324 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
|
325 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
326 keystr = c.charCodeAt(0).toString(16); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
327 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
328 else if (c == '\n') |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
329 keystr = "0a"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
330 else if (c == '\t') |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
331 keystr = "09"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
332 else if (c == '\b') |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
333 keystr = "08"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
334 else if (c == ' ') |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
335 keystr = "20"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
336 else if (c in charshifts) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
337 if (termemu.shiftp()) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
338 keystr = charshifts[c]; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
339 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
340 keystr = c.charCodeAt(0).toString(16); |
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 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
343 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
344 var datareq = new XMLHttpRequest(); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
345 var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": keystr}; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
346 datareq.onreadystatechange = postResponseHandler; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
347 datareq.open('POST', '/feed', true); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
348 datareq.send(JSON.stringify(msgDict)); |
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 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
351 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
352 function setup() { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
353 keyHexCodes.init(); |
4
ee22eb9ab009
Client: don't assume the terminal is 24x80.
John "Elwin" Edwards <elwin@sdf.org>
parents:
0
diff
changeset
|
354 termemu.init("termwrap", 24, 80); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
355 setTitle("Not connected."); |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
356 setmode("login"); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
357 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
358 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
359 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
360 function toggleshift() { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
361 termemu.toggleshift(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
362 keydiv = document.getElementById("shiftkey"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
363 if (termemu.shiftp()) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
364 keydiv.className = "keysel"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
365 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
366 keydiv.className = "key"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
367 return; |
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 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
370 function togglectrl() { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
371 termemu.togglectrl(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
372 keydiv = document.getElementById("ctrlkey"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
373 if (termemu.ctrlp()) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
374 keydiv.className = "keysel"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
375 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
376 keydiv.className = "key"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
377 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
378 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
379 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
380 function formlogin(ev) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
381 ev.preventDefault(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
382 if (termemu.sessid != null) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
383 return; |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
384 var loginmsg = {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
385 loginmsg["name"] = document.getElementById("input_name").value; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
386 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
|
387 var req = new XMLHttpRequest(); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
388 req.onreadystatechange = function () { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
389 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
|
390 return; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
391 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
|
392 if (reply.t == 'l') { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
393 /* Success */ |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
394 lcred = reply.k; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
395 lname = reply.u; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
396 setTitle("Logged in as " + reply.u); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
397 debug(1, "Logged in as " + reply.u + " with id " + reply.k); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
398 setmode("choose"); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
399 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
400 else if (reply.t == 'E') { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
401 debug(1, "Could not log in: " + reply.s); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
402 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
|
403 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
|
404 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
405 }; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
406 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
|
407 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
|
408 return; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
409 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
410 |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
411 function getchoices() { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
412 if (termemu.sessid != null || !lcred) |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
413 return; |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
414 var req = new XMLHttpRequest(); |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
415 req.onreadystatechange = function () { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
416 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
|
417 return; |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
418 var reply; |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
419 try { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
420 reply = JSON.parse(req.responseText); |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
421 } catch (e) { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
422 if (e instanceof SyntaxError) |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
423 return; |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
424 } |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
425 if (!("name" in reply) || reply["name"] != lname || !("stat" in reply)) |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
426 return; |
45
1bbd0e76ba92
RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents:
44
diff
changeset
|
427 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
|
428 /* 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
|
429 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
|
430 optdiv.removeChild(optdiv.childNodes[1]); |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
431 for (var gname in reply.stat) { |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
432 if (!(gname in games)) |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
433 continue; |
45
1bbd0e76ba92
RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents:
44
diff
changeset
|
434 var acttext; |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
435 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
|
436 acttext = "Resume your game"; |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
437 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
|
438 acttext = "Start a game"; |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
439 else if (reply.stat[gname] == "p") |
45
1bbd0e76ba92
RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents:
44
diff
changeset
|
440 acttext = "Game in progress"; |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
441 else |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
442 continue; |
45
1bbd0e76ba92
RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents:
44
diff
changeset
|
443 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
|
444 button.appendChild(document.createTextNode(acttext)); |
46
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
445 if ("s0".indexOf(reply.stat[gname]) >= 0) { |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
446 button.onclick = makeStarter(gname); |
51
2eda3909f6a3
rlgterm.js: don't make nonfunctional button.
John "Elwin" Edwards <elwin@sdf.org>
parents:
47
diff
changeset
|
447 button.className = "ibutton"; |
46
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
448 } |
45
1bbd0e76ba92
RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents:
44
diff
changeset
|
449 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
|
450 actdiv.appendChild(button); |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
451 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
|
452 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
|
453 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
|
454 rowdiv.appendChild(gamediv); |
1bbd0e76ba92
RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents:
44
diff
changeset
|
455 rowdiv.appendChild(actdiv); |
1bbd0e76ba92
RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents:
44
diff
changeset
|
456 optdiv.appendChild(rowdiv); |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
457 } |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
458 }; |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
459 req.open('GET', '/pstatus/' + lname, true); |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
460 req.send(); |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
461 return; |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
462 } |
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
463 |
46
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
464 /* 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
|
465 * 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
|
466 * being Super-Rogue. |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
467 */ |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
468 function makeStarter(gname) { |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
469 if (!(gname in games)) |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
470 return null; |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
471 var game = games[gname]; |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
472 function starter(ev) { |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
473 startgame(game); |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
474 } |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
475 return starter; |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
476 } |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
477 |
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
478 function startgame(game) { |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
479 if (termemu.sessid != null || !lcred) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
480 return; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
481 var smsg = {}; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
482 smsg["key"] = lcred; |
46
59ecd99845eb
rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents:
45
diff
changeset
|
483 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
|
484 smsg["h"] = 24; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
485 smsg["w"] = 80; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
486 var req = new XMLHttpRequest(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
487 req.onreadystatechange = function () { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
488 if (req.readyState != 4 || req.status != 200) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
489 return; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
490 var reply = JSON.parse(req.responseText); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
491 if (reply.t == 'l') { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
492 /* Success */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
493 termemu.sessid = reply.id; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
494 termemu.resize(reply.h, reply.w); |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
495 setTitle("Playing as " + lname); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
496 debug(1, "Playing with id " + termemu.sessid); |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
497 setmode("play"); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
498 getData(); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
499 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
500 else if (reply.t == 'E') { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
501 debug(1, "Could not start game: " + reply.s); |
47
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
502 if (reply.c == 1) { |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
503 logout(); |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
504 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
505 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
506 }; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
507 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
|
508 req.send(JSON.stringify(smsg)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
509 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
510 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
511 |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
512 function formreg(ev) { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
513 ev.preventDefault(); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
514 if (termemu.sessid != null) |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
515 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
516 var regmsg = {}; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
517 regmsg["name"] = document.getElementById("regin_name").value; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
518 regmsg["pw"] = document.getElementById("regin_pw").value; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
519 regmsg["email"] = document.getElementById("regin_email").value; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
520 var req = new XMLHttpRequest(); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
521 req.onreadystatechange = function () { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
522 if (req.readyState != 4 || req.status != 200) |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
523 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
524 var reply = JSON.parse(req.responseText); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
525 if (reply.t == 'r') { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
526 /* Success */ |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
527 debug(1, "Registered account: " + reply.d); |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
528 lcred = reply.k; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
529 lname = reply.u; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
530 setTitle("Logged in as " + lname); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
531 debug(1, "Logged in as " + lname + "with id " + lcred); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
532 setmode("choose"); |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
533 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
534 else if (reply.t == 'E') { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
535 debug(1, "Could not register: " + reply.s); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
536 document.getElementById("regin_name").value = ""; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
537 document.getElementById("regin_pw").value = ""; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
538 document.getElementById("regin_email").value = ""; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
539 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
540 }; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
541 req.open('POST', '/addacct', true); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
542 req.send(JSON.stringify(regmsg)); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
543 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
544 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
545 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
546 function gameover() { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
547 if (termemu.sessid == null) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
548 return; |
38
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
549 /* TODO IFACE2 If the end was unexpected, tell player the game was saved. */ |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
550 termemu.sessid = null; |
38
b06a14876645
RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents:
35
diff
changeset
|
551 ajaxstate.clear(); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
552 setTitle("Game over."); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
553 nsend = 0; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
554 nrecv = 0; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
555 msgQ = []; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
556 setmode("choose"); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
557 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
558 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
559 |
47
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
560 function logout() { |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
561 lcred = null; |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
562 lname = null; |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
563 setmode("login"); |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
564 } |
27b7f0c8b9f0
RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents:
46
diff
changeset
|
565 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
566 function stop() { |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
567 if (!termemu.sessid) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
568 return; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
569 var req = new XMLHttpRequest(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
570 req.onreadystatechange = function () { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
571 if (req.readyState == 4 && req.status == 200) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
572 processMsg(req.responseText); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
573 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
574 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
575 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
576 req.open('POST', '/feed', true); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
9
diff
changeset
|
577 req.send(JSON.stringify({"id": termemu.sessid, "t": "q"})); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
578 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
579 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
580 |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
581 function setmode(mode, ev) { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
582 if (ev) |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
583 ev.preventDefault(); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
584 if (mode == "play") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
585 document.getElementById("keyboard").style.display = "block"; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
586 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
|
587 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
|
588 document.getElementById("register").style.display = "none"; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
589 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
590 if (mode == "choose") { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
591 document.getElementById("keyboard").style.display = "none"; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
592 document.getElementById("startgame").style.display = "block"; |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
593 document.getElementById("login").style.display = "none"; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
594 document.getElementById("register").style.display = "none"; |
44
b848cb50cd69
rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents:
41
diff
changeset
|
595 getchoices(); |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
596 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
597 else if (mode == "login") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
598 document.getElementById("keyboard").style.display = "none"; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
599 document.getElementById("startgame").style.display = "none"; |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
600 document.getElementById("login").style.display = "block"; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
601 document.getElementById("register").style.display = "none"; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
602 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
603 else if (mode == "register") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
604 document.getElementById("keyboard").style.display = "none"; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
38
diff
changeset
|
605 document.getElementById("startgame").style.display = "none"; |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
606 document.getElementById("login").style.display = "none"; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
607 document.getElementById("register").style.display = "block"; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
608 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
609 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
16
diff
changeset
|
610 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
611 function debug(level, msg) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
612 if (level < debugSuppress) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
613 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
614 var msgdiv = document.createElement("div"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
615 var msgtext = document.createTextNode(msg); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
616 msgdiv.appendChild(msgtext); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
617 document.getElementById("debug").appendChild(msgdiv); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
618 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
619 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
620 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
621 function textsize(larger) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
622 var cssSize = termemu.view.style.fontSize; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
623 if (!cssSize) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
624 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
625 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
626 var match = cssSize.match(/\d*/); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
627 if (!match) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
628 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
629 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
630 var csize = Number(match[0]); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
631 var nsize; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
632 if (larger) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
633 if (csize >= 48) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
634 nsize = 48; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
635 else if (csize >= 20) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
636 nsize = csize + 4; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
637 else if (csize >= 12) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
638 nsize = csize + 2; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
639 else if (csize >= 8) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
640 nsize = csize + 1; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
641 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
642 nsize = 8; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
643 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
644 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
645 if (csize <= 8) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
646 nsize = 8; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
647 else if (csize <= 12) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
648 nsize = csize - 1; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
649 else if (csize <= 20) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
650 nsize = csize - 2; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
651 else if (csize <= 48) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
652 nsize = csize - 4; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
653 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
654 nsize = 48; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
655 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
656 document.getElementById("term").style.fontSize = nsize.toString() + "px"; |
9
826a7ced69f8
Make the emulator screen resizable.
John "Elwin" Edwards <elwin@sdf.org>
parents:
6
diff
changeset
|
657 termemu.fixsize(); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
658 debug(1, "Changing font size to " + nsize.toString()); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
659 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
660 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
661 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
662 function bell(on) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
663 var imgnode = document.getElementById("bell"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
664 if (on) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
665 imgnode.style.visibility = "visible"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
666 window.setTimeout(bell, 1500, false); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
667 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
668 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
669 imgnode.style.visibility = "hidden"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
670 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
671 } |