From: Christian Heller Date: Sat, 31 Oct 2020 23:42:07 +0000 (+0100) Subject: Refactor client's server interaction code. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/template?a=commitdiff_plain;h=b6547c8d843a9e0d6b341fc17e6fb6eebededd18;p=plomrogue2-experiments Refactor client's server interaction code. --- diff --git a/new2/rogue_chat_nocanvas_monochrome.html b/new2/rogue_chat_nocanvas_monochrome.html index 96ad054..de79177 100644 --- a/new2/rogue_chat_nocanvas_monochrome.html +++ b/new2/rogue_chat_nocanvas_monochrome.html @@ -135,7 +135,8 @@ let parser = { 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! :)"); @@ -143,10 +144,52 @@ let server = { }; 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); + } } } @@ -391,49 +434,7 @@ let game = { 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],