let server = {
init: function(url) {
- this.websocket = new WebSocket(url);
+ this.url = url;
+ this.websocket = new WebSocket(this.url);
this.websocket.onopen = function(event) {
window.setInterval(function() { server.send(['PING']) }, 30000);
tui.log_msg("@ server connected! :)");
};
this.websocket.onclose = function(event) {
tui.log_msg('@ server disconnected :(');
- }
+ };
+ this.websocket.onmessage = this.handle_event;
},
send: function(tokens) {
this.websocket.send(unparser.untokenize(tokens));
+ },
+ handle_event: function(event) {
+ let tokens = parser.tokenize(event.data)[0];
+ if (tokens[0] === 'TURN') {
+ game.things = {}
+ game.turn = parseInt(tokens[1]);
+ } else if (tokens[0] === 'THING_POS') {
+ game.things[tokens[1]] = parser.parse_yx(tokens[2]);
+ } else if (tokens[0] === 'MAP') {
+ game.map_size = parser.parse_yx(tokens[1]);
+ game.map = tokens[2]
+ } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
+ explorer.empty_info_db();
+ if (tui.mode == mode_study) {
+ explorer.query_info();
+ }
+ tui.full_refresh();
+ } else if (tokens[0] === 'CHAT') {
+ tui.log_msg('# ' + tokens[1], 1);
+ } else if (tokens[0] === 'PLAYER_ID') {
+ game.player_id = parseInt(tokens[1]);
+ } else if (tokens[0] === 'LOGIN_OK') {
+ this.send(['GET_GAMESTATE']);
+ tui.log_help();
+ tui.switch_mode(mode_play);
+ } else if (tokens[0] === 'ANNOTATION') {
+ let position = parser.parse_yx(tokens[1]);
+ explorer.update_info_db(position, tokens[2]);
+ } else if (tokens[0] === 'UNHANDLED_INPUT') {
+ tui.log_msg('? unknown command');
+ } else if (tokens[0] === 'PLAY_ERROR') {
+ terminal.blink_screen();
+ } else if (tokens[0] === 'ARGUMENT_ERROR') {
+ tui.log_msg('? syntax error: ' + tokens[1]);
+ } else if (tokens[0] === 'GAME_ERROR') {
+ tui.log_msg('? game error: ' + tokens[1]);
+ } else if (tokens[0] === 'PONG') {
+ console.log('PONG');
+ } else {
+ tui.log_msg('? unhandled input: ' + event.data);
+ }
}
}
height_turn_line: 1,
height_mode_line: 1,
height_input: 1,
- key_up: 'w',
- key_down: 's',
- key_left: 'a',
- key_right: 'd',
- movement_keys_desc: 'w, a, s, d',
init: function() {
this.inputEl = document.getElementById("input");
this.inputEl.focus();
this.recalc_input_lines();
this.height_header = this.height_turn_line + this.height_mode_line;
this.log_msg("@ waiting for server connection ...");
+ this.init_wasd();
+ },
+ init_wasd: function() {
+ if (wasd_selector.value == 'w, a, s, d') {
+ tui.key_up = 'w';
+ tui.key_down = 's';
+ tui.key_left = 'a';
+ tui.key_right = 'd';
+ } else if (wasd_selector.value == 'arrow keys') {
+ tui.key_up = 'ArrowUp';
+ tui.key_down = 'ArrowDown';
+ tui.key_left = 'ArrowLeft';
+ tui.key_right = 'ArrowRight';
+ };
+ tui.movement_keys_desc = wasd_selector.value;
},
init_login: function() {
this.log_msg("@ please enter your username:");
tui.init();
tui.full_refresh();
-
server.init(websocket_location);
-server.websocket.onmessage = function (event) {
- let tokens = parser.tokenize(event.data)[0];
- if (tokens[0] === 'TURN') {
- game.things = {}
- game.turn = parseInt(tokens[1]);
- } else if (tokens[0] === 'THING_POS') {
- game.things[tokens[1]] = parser.parse_yx(tokens[2]);
- } else if (tokens[0] === 'MAP') {
- game.map_size = parser.parse_yx(tokens[1]);
- game.map = tokens[2]
- } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
- explorer.empty_info_db();
- if (tui.mode == mode_study) {
- explorer.query_info();
- }
- tui.full_refresh();
- } else if (tokens[0] === 'CHAT') {
- tui.log_msg('# ' + tokens[1], 1);
- } else if (tokens[0] === 'PLAYER_ID') {
- game.player_id = parseInt(tokens[1]);
- } else if (tokens[0] === 'LOGIN_OK') {
- server.send(['GET_GAMESTATE']);
- tui.log_help();
- tui.switch_mode(mode_play);
- } else if (tokens[0] === 'ANNOTATION') {
- let position = parser.parse_yx(tokens[1]);
- explorer.update_info_db(position, tokens[2]);
- } else if (tokens[0] === 'UNHANDLED_INPUT') {
- tui.log_msg('? unknown command');
- } else if (tokens[0] === 'PLAY_ERROR') {
- terminal.blink_screen();
- } else if (tokens[0] === 'ARGUMENT_ERROR') {
- tui.log_msg('? syntax error: ' + tokens[1]);
- } else if (tokens[0] === 'GAME_ERROR') {
- tui.log_msg('? game error: ' + tokens[1]);
- } else if (tokens[0] === 'PONG') {
- console.log('PONG');
- } else {
- tui.log_msg('? unhandled input: ' + event.data);
- }
-}
let explorer = {
position: [0,0],
}, false);
wasd_selector.addEventListener('input', function() {
- if (wasd_selector.value == 'w, a, s, d') {
- tui.key_up = 'w';
- tui.key_down = 's';
- tui.key_left = 'a';
- tui.key_right = 'd';
- } else if (wasd_selector.value == 'arrow keys') {
- tui.key_up = 'ArrowUp';
- tui.key_down = 'ArrowDown';
- tui.key_left = 'ArrowLeft';
- tui.key_right = 'ArrowRight';
- };
- tui.movement_keys_desc = wasd_selector.value;
+ tui.init_wasd();
}, false);
rows_selector.addEventListener('input', function() {
+ if (rows_selector.value % 2 != 0) {
+ return;
+ }
terminal.initialize();
tui.full_refresh();
}, false);
cols_selector.addEventListener('input', function() {
+ if (cols_selector.value % 4 != 0) {
+ return;
+ }
terminal.initialize();
tui.window_width = terminal.cols / 2,
tui.full_refresh();