comparison rlgwebd.js @ 181:926b0780bc44

Print metadata at the beginning of ttyrec files.
author John "Elwin" Edwards
date Sat, 17 Jan 2015 15:48:41 -0500
parents 85fde763c7ed
children 3c0e7697bb30
comparison
equal deleted inserted replaced
180:85fde763c7ed 181:926b0780bc44
64 var sessions = {}; 64 var sessions = {};
65 var dglgames = {}; 65 var dglgames = {};
66 var allowlogin = true; 66 var allowlogin = true;
67 var gamemux = new events.EventEmitter(); 67 var gamemux = new events.EventEmitter();
68 68
69 /* TODO move TermSession and DglSession methods into the prototypes. */
70
69 /* Constructor. A TermSession handles a pty and the game running on it. 71 /* Constructor. A TermSession handles a pty and the game running on it.
70 * gname: (String) Name of the game to launch. 72 * gname: (String) Name of the game to launch.
71 * pname: (String) The player's name. 73 * pname: (String) The player's name.
72 * wsReq: (WebSocketRequest) The request from the client. 74 * wsReq: (WebSocketRequest) The request from the client.
73 * 75 *
126 this.frameoff = 0; 128 this.frameoff = 0;
127 this.playerconn = wsReq.accept(null, wsReq.origin); 129 this.playerconn = wsReq.accept(null, wsReq.origin);
128 /* Array for watcher connections. */ 130 /* Array for watcher connections. */
129 this.watchers = []; 131 this.watchers = [];
130 /* END setup */ 132 /* END setup */
133 this.framepush = function(chunk) {
134 /* If this chunk resets the screen, discard what preceded it. */
135 if (isclear(chunk)) {
136 this.framebuf = new Buffer(1024);
137 this.frameoff = 0;
138 }
139 /* Make sure there's space. */
140 while (this.framebuf.length < chunk.length + this.frameoff) {
141 var nbuf = new Buffer(this.framebuf.length * 2);
142 this.framebuf.copy(nbuf, 0, 0, this.frameoff);
143 this.framebuf = nbuf;
144 if (this.framebuf.length > 65536) {
145 tslog("Warning: Game %s frame buffer at %d bytes", this.tag(),
146 this.framebuf.length);
147 }
148 }
149 chunk.copy(this.framebuf, this.frameoff);
150 this.frameoff += chunk.length;
151 };
131 function ttyrec_chunk(datastr) { 152 function ttyrec_chunk(datastr) {
132 ss.lasttime = new Date(); 153 ss.lasttime = new Date();
133 var buf = new Buffer(datastr); 154 var buf = new Buffer(datastr);
134 var chunk = new Buffer(buf.length + 12); 155 var chunk = new Buffer(buf.length + 12);
135 /* TTYREC headers */ 156 /* TTYREC headers */
147 if (ss.watchers[i].connected) 168 if (ss.watchers[i].connected)
148 ss.watchers[i].sendUTF(msg); 169 ss.watchers[i].sendUTF(msg);
149 } 170 }
150 ss.emit('data', buf); 171 ss.emit('data', buf);
151 } 172 }
173 /* Begin the ttyrec with some metadata, like dgamelaunch does. */
174 var descstr = "\x1b[2J\x1b[1;1H\r\n";
175 descstr += "Player: " + this.pname + "\r\nGame: " + this.game.name + "\r\n";
176 descstr += "Server: Roguelike Gallery - rlgallery.org\r\n";
177 descstr += "Filename: " + ts + ".ttyrec\r\n";
178 descstr += "Time: (" + Math.floor(this.lasttime.getTime() / 1000) + ") ";
179 descstr += this.lasttime.toUTCString().slice(0, -4) + "\r\n";
180 descstr += "Size: " + this.w + "x" + this.h + "\r\n\x1b[2J";
181 ttyrec_chunk(descstr);
152 this.term.on("data", ttyrec_chunk); 182 this.term.on("data", ttyrec_chunk);
153 this.framepush = function(chunk) {
154 /* If this chunk resets the screen, discard what preceded it. */
155 if (isclear(chunk)) {
156 this.framebuf = new Buffer(1024);
157 this.frameoff = 0;
158 }
159 /* Make sure there's space. */
160 while (this.framebuf.length < chunk.length + this.frameoff) {
161 var nbuf = new Buffer(this.framebuf.length * 2);
162 this.framebuf.copy(nbuf, 0, 0, this.frameoff);
163 this.framebuf = nbuf;
164 if (this.framebuf.length > 65536) {
165 tslog("Warning: Game %s frame buffer at %d bytes", this.tag(),
166 this.framebuf.length);
167 }
168 }
169 chunk.copy(this.framebuf, this.frameoff);
170 this.frameoff += chunk.length;
171 };
172 this.write = function(data) { 183 this.write = function(data) {
173 this.term.write(data); 184 this.term.write(data);
174 }; 185 };
175 this.tag = function() { 186 this.tag = function() {
176 return this.game.uname + "/" + this.pname; 187 return this.game.uname + "/" + this.pname;