annotate rlgterm.js @ 56:7f3ca16409fe

RLG-Web client: display a list of current games. Incorporate information from /status to create a list of ongoing games. Eventually these will be watchable.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 18 Jun 2012 14:30:05 -0700
parents 96815eae4ebe
children e7c864e80a79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 /* rlgterm.js: Roguelike Gallery's driver for termemu.js */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 // A state machine that keeps track of polling the server.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 var ajaxstate = {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 state: 0,
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 timerID: null,
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 clear: function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
8 if (this.timerID != null) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
9 window.clearTimeout(this.timerID);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
10 this.timerID = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
12 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
13 set: function (ms) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14 this.clear();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15 this.timerID = window.setTimeout(getData, ms);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
16 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
17 gotdata: function () {
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
18 this.set(1000);
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
19 this.state = 1;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 gotnothing: function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
22 if (this.state == 0) {
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
23 this.set(1000);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
24 this.state = 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
25 }
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
26 else if (this.state < 4) {
35
f15efa4818b4 rglterm.js: reduce the server polling.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
27 this.set(4000);
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
28 this.state++;
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
29 }
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) {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
154 var msgDicts;
16
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 {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
157 msgDicts = JSON.parse(msg);
16
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 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
162 if (msgDicts.length === 0)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
163 return false;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
164 for (var j = 0; j < msgDicts.length; j++) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
165 if (!msgDicts[j].t)
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
166 continue;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
167 else if (msgDicts[j].t == "E") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
168 if (msgDicts[j].c == 1 || msgDicts[j].c == 6 || msgDicts[j].c == 7) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
169 gameover();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
170 if (msgDicts[j].c == 1) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
171 logout();
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
172 }
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
173 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
174 debug(1, "Server error: " + msgDicts[j].s);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
175 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
176 // A data message
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
177 else if (msgDicts[j].t == "d") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
178 if (msgDicts[j].n === nrecv) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
179 writeData(msgDicts[j].d);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
180 nrecv++;
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
181 /* Process anything in the queue that's now ready. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
182 var next;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
183 while ((next = msgQ.shift()) !== undefined) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
184 writeData(next.d);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
185 nrecv++;
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
186 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
187 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
188 else if (msgDicts[j].n > nrecv) {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
189 /* The current message comes after one still missing. Queue this one
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
190 * for later use. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
191 debug(1, "Got packet " + msgDicts[j].n + ", expected " + nrecv);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
192 msgQ[msgDicts[j].n - nrecv - 1] = msgDicts[j];
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
193 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
194 else {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
195 /* This message's number was encountered previously. */
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
196 debug(1, "Discarding packet " + msgDicts[j].n + ", expected " + nrecv);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
197 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
198 havedata = true;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
199 }
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
200 else if (msgDicts[j].t == "T") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
201 setTitle(msgDicts[j].d);
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
202 }
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
203 else if (msgDicts[j].t == "q") {
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
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 {
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
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 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
210 return havedata;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
213 function getData() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
214 if (termemu.sessid == null)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
215 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
216 var datareq = new XMLHttpRequest();
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
217 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
218 datareq.onreadystatechange = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
219 if (datareq.readyState == 4 && datareq.status == 200) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
220 var wasdata = processMsg(datareq.responseText);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
221 if (wasdata != null) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
222 if (wasdata)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
223 ajaxstate.gotdata();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
224 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
225 ajaxstate.gotnothing();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
226 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
227 return;
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 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
230 datareq.open('POST', '/feed', true);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
231 datareq.send(msg);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
232 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
235 function postResponseHandler() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
236 if (this.readyState == 4 && this.status == 200) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
237 // 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
238 var wasdata = processMsg(this.responseText);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
239 ajaxstate.posted();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
240 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
244 function sendback(str) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
245 /* For responding to terminal queries. */
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
246 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
247 var datareq = new XMLHttpRequest();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
248 datareq.onreadystatechange = postResponseHandler;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
249 datareq.open('POST', '/feed', true);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
250 datareq.send(JSON.stringify(msgDict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
251 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
254 function sendkey(ev) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
255 if (termemu.sessid == null)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
256 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
257 var keynum = ev.keyCode;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
258 var code;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
259 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
260 /* 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
261 if (ev.ctrlKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
262 keynum -= 64;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
263 else if (!ev.shiftKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
264 keynum += 32;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
265 code = keynum.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
266 if (code.length < 2)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
267 code = "0" + code;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
268 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
269 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
270 /* The number row, NOT the numpad. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
271 if (ev.shiftKey) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
272 code = numShifts[keynum - 48].toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
273 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
274 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
275 code = keynum.toString(16);
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 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
278 else if (keynum in keyHexCodes) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
279 if (ev.shiftKey)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
280 code = keyHexCodes[keynum][1];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
281 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
282 code = keyHexCodes[keynum][0];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
283 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
284 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
285 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
286 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
287 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
288 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
289 debug(1, "Ignoring keycode " + keynum);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
290 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
291 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
292 // Isn't this check redundant?
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
293 if (termemu.sessid != null)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
294 ev.preventDefault();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
295 var datareq = new XMLHttpRequest();
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
296 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
297 datareq.onreadystatechange = postResponseHandler;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
298 datareq.open('POST', '/feed', true);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
299 datareq.send(JSON.stringify(msgDict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
300 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
303 var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
304 ';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e"
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
307 function vkey(c) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
308 if (termemu.sessid == null)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
309 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
310 var keystr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
311 if (c.match(/^[a-z]$/)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
312 if (termemu.ctrlp()) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
313 var n = c.charCodeAt(0) - 96;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
314 keystr = n.toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
315 if (keystr.length < 2)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
316 keystr = "0" + keystr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
317 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
318 else if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
319 keystr = c.toUpperCase().charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
320 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
321 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
322 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
323 else if (c.match(/^[0-9]$/)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
324 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
325 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
326 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
327 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
328 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
329 else if (c == '\n')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
330 keystr = "0a";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
331 else if (c == '\t')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
332 keystr = "09";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
333 else if (c == '\b')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
334 keystr = "08";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
335 else if (c == ' ')
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
336 keystr = "20";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
337 else if (c in charshifts) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
338 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
339 keystr = charshifts[c];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
340 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
341 keystr = c.charCodeAt(0).toString(16);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
342 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
343 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
344 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
345 var datareq = new XMLHttpRequest();
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
346 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
347 datareq.onreadystatechange = postResponseHandler;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
348 datareq.open('POST', '/feed', true);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
349 datareq.send(JSON.stringify(msgDict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
350 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
353 function setup() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
354 keyHexCodes.init();
4
ee22eb9ab009 Client: don't assume the terminal is 24x80.
John "Elwin" Edwards <elwin@sdf.org>
parents: 0
diff changeset
355 termemu.init("termwrap", 24, 80);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
356 setTitle("Not connected.");
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
357 setmode("login");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
358 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
361 function toggleshift() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
362 termemu.toggleshift();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
363 keydiv = document.getElementById("shiftkey");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
364 if (termemu.shiftp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
365 keydiv.className = "keysel";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
366 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
367 keydiv.className = "key";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
368 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
371 function togglectrl() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
372 termemu.togglectrl();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
373 keydiv = document.getElementById("ctrlkey");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
374 if (termemu.ctrlp())
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
375 keydiv.className = "keysel";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
376 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
377 keydiv.className = "key";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
378 return;
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
381 function formlogin(ev) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
382 ev.preventDefault();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
383 if (termemu.sessid != null)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
384 return;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
385 var loginmsg = {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
386 loginmsg["name"] = document.getElementById("input_name").value;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
387 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
388 var req = new XMLHttpRequest();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
389 req.onreadystatechange = function () {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
390 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
391 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
392 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
393 if (reply.t == 'l') {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
394 /* Success */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
395 lcred = reply.k;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
396 lname = reply.u;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
397 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
398 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
399 setmode("choose");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
400 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
401 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
402 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
403 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
404 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
405 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
406 };
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
407 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
408 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
409 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
410 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
411
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
412 function getcurrent() {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
413 if (termemu.sessid)
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
414 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
415 var req = new XMLHttpRequest();
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
416 req.onreadystatechange = function () {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
417 if (req.readyState != 4 || req.status != 200)
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
418 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
419 var reply;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
420 try {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
421 reply = JSON.parse(req.responseText);
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
422 } catch (e) {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
423 if (e instanceof SyntaxError)
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
424 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
425 }
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
426 if (!reply.s) {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
427 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
428 }
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
429 var gamediv = document.getElementById("gametable");
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
430 while (gamediv.children.length > 2)
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
431 gamediv.removeChild(optdiv.children[2]);
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
432 for (var i = 0; i < reply.g.length; i++) {
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
433 var row = document.createElement("div");
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
434 var cell1 = document.createElement("div");
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
435 var cell2 = document.createElement("div");
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
436 cell1.appendChild(document.createTextNode(reply.g[i].p));
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
437 cell2.appendChild(document.createTextNode(reply.g[i].g));
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
438 row.appendChild(cell1);
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
439 row.appendChild(cell2);
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
440 gamediv.appendChild(row);
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
441 }
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
442 };
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
443 req.open('GET', '/status', true);
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
444 req.send();
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
445 return;
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
446 }
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
447
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
448 function getchoices() {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
449 if (termemu.sessid != null || !lcred)
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
450 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
451 var req = new XMLHttpRequest();
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
452 req.onreadystatechange = function () {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
453 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
454 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
455 var reply;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
456 try {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
457 reply = JSON.parse(req.responseText);
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
458 } catch (e) {
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
459 if (e instanceof SyntaxError)
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
460 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
461 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
462 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
463 return;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
464 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
465 /* 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
466 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
467 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
468 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
469 if (!(gname in games))
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
470 continue;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
471 var acttext;
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
472 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
473 acttext = "Resume your game";
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
474 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
475 acttext = "Start a game";
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
476 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
477 acttext = "Game in progress";
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
478 else
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
479 continue;
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
480 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
481 button.appendChild(document.createTextNode(acttext));
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
482 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
483 button.onclick = makeStarter(gname);
51
2eda3909f6a3 rlgterm.js: don't make nonfunctional button.
John "Elwin" Edwards <elwin@sdf.org>
parents: 47
diff changeset
484 button.className = "ibutton";
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
485 }
45
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
486 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
487 actdiv.appendChild(button);
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
488 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
489 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
490 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
491 rowdiv.appendChild(gamediv);
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
492 rowdiv.appendChild(actdiv);
1bbd0e76ba92 RLG-Web: put the game options into a table.
John "Elwin" Edwards <elwin@sdf.org>
parents: 44
diff changeset
493 optdiv.appendChild(rowdiv);
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
494 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
495 };
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
496 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
497 req.send();
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
498 return;
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
499 }
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
500
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
501 /* 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
502 * 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
503 * being Super-Rogue.
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
504 */
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
505 function makeStarter(gname) {
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
506 if (!(gname in games))
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
507 return null;
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
508 var game = games[gname];
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
509 function starter(ev) {
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
510 startgame(game);
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
511 }
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
512 return starter;
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
513 }
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
514
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
515 function startgame(game) {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
516 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
517 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
518 var smsg = {};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
519 smsg["key"] = lcred;
46
59ecd99845eb rlgterm.js: make the options table functional.
John "Elwin" Edwards <elwin@sdf.org>
parents: 45
diff changeset
520 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
521 smsg["h"] = 24;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
522 smsg["w"] = 80;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
523 var req = new XMLHttpRequest();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
524 req.onreadystatechange = function () {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
525 if (req.readyState != 4 || req.status != 200)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
526 return;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
527 var reply = JSON.parse(req.responseText);
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 51
diff changeset
528 if (reply.t == 's') {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
529 /* Success */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
530 termemu.sessid = reply.id;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
531 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
532 setTitle("Playing as " + lname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
533 debug(1, "Playing with id " + termemu.sessid);
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
534 setmode("play");
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
535 getData();
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
536 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
537 else if (reply.t == 'E') {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
538 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
539 if (reply.c == 1) {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
540 logout();
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
541 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
542 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
543 };
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
544 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
545 req.send(JSON.stringify(smsg));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
546 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
547 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
548
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
549 function formreg(ev) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
550 ev.preventDefault();
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
551 if (termemu.sessid != null)
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
552 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
553 var regmsg = {};
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
554 regmsg["name"] = document.getElementById("regin_name").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
555 regmsg["pw"] = document.getElementById("regin_pw").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
556 regmsg["email"] = document.getElementById("regin_email").value;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
557 var req = new XMLHttpRequest();
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
558 req.onreadystatechange = function () {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
559 if (req.readyState != 4 || req.status != 200)
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
560 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
561 var reply = JSON.parse(req.responseText);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
562 if (reply.t == 'r') {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
563 /* Success */
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
564 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
565 lcred = reply.k;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
566 lname = reply.u;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
567 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
568 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
569 setmode("choose");
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
570 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
571 else if (reply.t == 'E') {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
572 debug(1, "Could not register: " + reply.s);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
573 document.getElementById("regin_name").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
574 document.getElementById("regin_pw").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
575 document.getElementById("regin_email").value = "";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
576 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
577 };
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
578 req.open('POST', '/addacct', true);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
579 req.send(JSON.stringify(regmsg));
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
580 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
581 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
582
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
583 function gameover() {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
584 if (termemu.sessid == null)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
585 return;
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
586 /* 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
587 termemu.sessid = null;
38
b06a14876645 RLG-Web: reduce polling further.
John "Elwin" Edwards <elwin@sdf.org>
parents: 35
diff changeset
588 ajaxstate.clear();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
589 setTitle("Game over.");
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
590 nsend = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
591 nrecv = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
592 msgQ = [];
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
593 setmode("choose");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
594 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
595 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
596
47
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
597 function logout() {
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
598 lcred = null;
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
599 lname = null;
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
600 setmode("login");
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
601 }
27b7f0c8b9f0 RLG-Web: make login sessions time out.
John "Elwin" Edwards <elwin@sdf.org>
parents: 46
diff changeset
602
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
603 function stop() {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
604 if (!termemu.sessid)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
605 return;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
606 var req = new XMLHttpRequest();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
607 req.onreadystatechange = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
608 if (req.readyState == 4 && req.status == 200) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
609 processMsg(req.responseText);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
610 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
611 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
612 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
613 req.open('POST', '/feed', true);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 9
diff changeset
614 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
615 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
616 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
617
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
618 function setmode(mode, ev) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
619 if (ev)
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
620 ev.preventDefault();
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
621 if (mode == "play") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
622 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
623 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
624 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
625 document.getElementById("register").style.display = "none";
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
626 document.getElementById("current").style.display = "none";
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
627 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
628 if (mode == "choose") {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 38
diff changeset
629 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
630 document.getElementById("startgame").style.display = "block";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
631 document.getElementById("login").style.display = "none";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
632 document.getElementById("register").style.display = "none";
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
633 document.getElementById("current").style.display = "block";
44
b848cb50cd69 rlgterm.js: present game options to the player.
John "Elwin" Edwards <elwin@sdf.org>
parents: 41
diff changeset
634 getchoices();
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
635 getcurrent();
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
636 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
637 else if (mode == "login") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
638 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
639 document.getElementById("startgame").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
640 document.getElementById("login").style.display = "block";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
641 document.getElementById("register").style.display = "none";
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
642 document.getElementById("current").style.display = "block";
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
643 getcurrent();
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
644 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
645 else if (mode == "register") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
646 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
647 document.getElementById("startgame").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
648 document.getElementById("login").style.display = "none";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
649 document.getElementById("register").style.display = "block";
56
7f3ca16409fe RLG-Web client: display a list of current games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 55
diff changeset
650 document.getElementById("current").style.display = "none";
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
651 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
652 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 16
diff changeset
653
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
654 function debug(level, msg) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
655 if (level < debugSuppress)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
656 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
657 var msgdiv = document.createElement("div");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
658 var msgtext = document.createTextNode(msg);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
659 msgdiv.appendChild(msgtext);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
660 document.getElementById("debug").appendChild(msgdiv);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
661 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
662 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
663
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
664 function textsize(larger) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
665 var cssSize = termemu.view.style.fontSize;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
666 if (!cssSize) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
667 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
668 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
669 var match = cssSize.match(/\d*/);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
670 if (!match) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
671 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
672 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
673 var csize = Number(match[0]);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
674 var nsize;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
675 if (larger) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
676 if (csize >= 48)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
677 nsize = 48;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
678 else if (csize >= 20)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
679 nsize = csize + 4;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
680 else if (csize >= 12)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
681 nsize = csize + 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
682 else if (csize >= 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
683 nsize = csize + 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
684 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
685 nsize = 8;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
686 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
687 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
688 if (csize <= 8)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
689 nsize = 8;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
690 else if (csize <= 12)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
691 nsize = csize - 1;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
692 else if (csize <= 20)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
693 nsize = csize - 2;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
694 else if (csize <= 48)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
695 nsize = csize - 4;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
696 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
697 nsize = 48;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
698 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
699 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
700 termemu.fixsize();
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
701 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
702 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
703 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
704
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
705 function bell(on) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
706 var imgnode = document.getElementById("bell");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
707 if (on) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
708 imgnode.style.visibility = "visible";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
709 window.setTimeout(bell, 1500, false);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
710 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
711 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
712 imgnode.style.visibility = "hidden";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
713 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
714 }