-let mode = 'chat';
-document.addEventListener('keydown', (event) => {
- if (mode == 'chat') {
- if (event.key.length === 1) {
- chat.input_line += event.key;
- tui.draw_input_line();
- tui.refresh();
- } else if (event.key == 'Backspace') {
- chat.input_line = chat.input_line.slice(0, -1);
- tui.draw_input_line();
- tui.refresh();
- } else if (event.key == 'Enter') {
- let tokens = parser.tokenize(chat.input_line);
- console.log(tokens);
- if (tokens.length > 0 && tokens[0].length > 0) {
- if (tokens[0][0] == '/') {
- if (tokens[0] == '/game') {
- mode = 'game';
- } else if (tokens[0] == '/?') {
- tui.log_help();
- tui.refresh();
- } else if (tokens[0] == '/login') {
- if (tokens.length > 1) {
- websocket.send('LOGIN ' + quote(tokens[1]));
- } else {
- tui.log_msg('need login name');
- }
- } else if (tokens[0] == '/msg') {
- if (tokens.length > 2) {
- websocket.send('QUERY ' + quote(tokens[1]) + ' ' + quote(tokens[2]));
- } else {
- tui.log_msg('need message target and message');
- }
+let explorer = {
+ position: [0,0],
+ info_db: {},
+ move: function(direction) {
+ let try_pos = [0,0];
+ try_pos[0] = this.position[0];
+ try_pos[1] = this.position[1];
+ if (direction == 'left') {
+ try_pos[1] -= 1;
+ } else if (direction == 'right') {
+ try_pos[1] += 1;
+ } else if (direction == 'up') {
+ try_pos[0] -= 1;
+ } else if (direction == 'down') {
+ try_pos[0] += 1;
+ };
+ if (!(try_pos[0] < 0) &&
+ !(try_pos[1] < 0) &&
+ !(try_pos[0] >= game.map_size[0])
+ && !(try_pos[1] >= game.map_size[1])) {
+ this.position = try_pos;
+ this.query_info();
+ tui.full_refresh();
+ }
+ },
+ update_info_db: function(yx, str) {
+ this.info_db[yx] = str;
+ if (tui.mode == mode_study) {
+ tui.full_refresh();
+ }
+ },
+ empty_info_db: function() {
+ this.info_db = {};
+ if (tui.mode == mode_study) {
+ tui.full_refresh();
+ }
+ },
+ query_info: function() {
+ server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
+ },
+ get_info: function() {
+ if (this.position in this.info_db) {
+ return this.info_db[this.position];
+ } else {
+ return 'waiting …';
+ }
+ },
+ annotate: function(msg) {
+ if (msg.length == 0) {
+ msg = " "; // triggers annotation deletion
+ }
+ server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
+ }
+}
+
+tui.inputEl.addEventListener('input', (event) => {
+ if (tui.mode.has_input_prompt) {
+ let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
+ if (tui.inputEl.value.length > max_length) {
+ tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
+ };
+ tui.recalc_input_lines();
+ tui.full_refresh();
+ } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
+ server.send(["TASK:WRITE", tui.inputEl.value[0]]);
+ tui.switch_mode(mode_play);
+ }
+}, false);
+tui.inputEl.addEventListener('keydown', (event) => {
+ if (event.key == 'Enter') {
+ event.preventDefault();
+ }
+ if (tui.mode == mode_login && event.key == 'Enter') {
+ server.send(['LOGIN', tui.inputEl.value]);
+ tui.switch_mode(mode_login);
+ } else if (tui.mode == mode_annotate && event.key == 'Enter') {
+ explorer.annotate(tui.inputEl.value);
+ tui.switch_mode(mode_study, true);
+ } else if (tui.mode == mode_chat && event.key == 'Enter') {
+ let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
+ if (tokens.length > 0 && tokens[0].length > 0) {
+ if (tokens[0][0] == ':') {
+ if (tokens[0] == ':play' || tokens[0] == ':p') {
+ tui.switch_mode(mode_play);
+ } else if (tokens[0] == ':study' || tokens[0] == ':?') {
+ tui.switch_mode(mode_study);
+ } else if (tokens[0] == ':help') {
+ tui.log_help();
+ } else if (tokens[0] == ':nick') {
+ if (tokens.length > 1) {
+ server.send(['LOGIN', tokens[1]]);
+ } else {
+ tui.log_msg('? need login name');
+ }
+ } else if (tokens[0] == ':msg') {
+ if (tokens.length > 2) {
+ let msg = tui.inputEl.value.slice(token_starts[2]);
+ server.send(['QUERY', tokens[1], msg]);