<style>
</style>
</head><body>
-<pre id="terminal" style="display: inline-block; color: white; background-color: black;"></pre>
<div>
movement: <select id="WASD_selector" name="WASD_selector" >
<option value="w, a, s, d" selected>w, a, s, d</option>
<option value="arrow keys">arrow keys</option>
</select>
+rows: <input id="n_rows" type="number" step=2 min=10 value=24 />
+cols: <input id="n_cols" type="number" step=4 min=20 value=80 />
</div>
+<pre id="terminal" style="display: inline-block;"></pre>
+<textarea id="input" style="opacity: 0; width: 0px;"></textarea>
<script>
"use strict";
let websocket_location = "ws://localhost:8000";
this.websocket = new WebSocket(url);
this.websocket.onopen = function(event) {
window.setInterval(function() { server.send(['PING']) }, 30000);
- tui.log_msg("@ server connected!");
+ tui.log_msg("@ server connected! :)");
tui.init_login();
};
+ this.websocket.onclose = function(event) {
+ tui.log_msg('@ server disconnected :(');
+ }
},
send: function(tokens) {
- if (this.websocket.readyState !== WebSocket.OPEN) {
- tui.log_msg('server disconnected :(');
- } else {
- this.websocket.send(unparser.untokenize(tokens));
- }
+ this.websocket.send(unparser.untokenize(tokens));
}
}
let quoted = ['"'];
for (let i = 0; i < str.length; i++) {
let c = str[i];
- if (c in ['"', '\\']) {
+ if (['"', '\\'].includes(c)) {
quoted.push('\\');
};
quoted.push(c);
mode: mode_waiting_for_server,
log: [],
input_prompt: '> ',
- input: '',
input_lines: [],
window_width: terminal.cols / 2,
height_turn_line: 1,
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.wasd_selector = document.getElementById("WASD_selector");
- this.wasd_selector.addEventListener('input', function() {
- if (tui.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 (tui.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 = tui.wasd_selector.value;
- }, false);
},
init_login: function() {
this.log_msg("@ please enter your username:");
if (mode == mode_annotate && explorer.position in explorer.info_db) {
let info = explorer.info_db[explorer.position];
if (info != "(none)") {
- this.add_to_input(explorer.info_db[explorer.position]);
+ this.inputEl.value = explorer.info_db[explorer.position];
+ this.recalc_input_lines();
}
}
this.full_refresh();
},
empty_input: function(str) {
- this.input = "";
+ this.inputEl.value = "";
if (this.mode.has_input_prompt) {
this.recalc_input_lines();
} else {
this.height_input = 0;
}
},
- add_to_input: function(str) {
- if (this.input_prompt.length + this.input.length + str.length > this.window_width * terminal.rows) {
- return;
- }
- this.input += str;
- this.recalc_input_lines();
- this.full_refresh();
- },
recalc_input_lines: function() {
- this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.input, this.window_width);
+ this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
this.height_input = this.input_lines.length;
},
- shorten_input: function() {
- if (this.input.length == 0) {
- terminal.blink_screen();
- } else {
- this.input = tui.input.slice(0, -1);
- this.recalc_input_lines();
- this.full_refresh();
- }
- },
msg_into_lines_of_width: function(msg, width) {
let chunk = "";
let lines = [];
return lines;
},
log_msg: function(msg) {
- let lines = this.msg_into_lines_of_width(msg, this.window_width);
- this.log = this.log.concat(lines);
- while (this.log.length > terminal.rows) {
+ this.log.push(msg);
+ while (this.log.length > terminal.rows * 4) {
this.log.shift();
};
this.full_refresh();
terminal.write(1, this.window_width, 'TURN: ' + game.turn);
},
draw_history: function() {
- if (terminal.rows <= this.height_header + this.height_input) {
- return;
- }
+ let log_display_lines = [];
+ for (let line of this.log) {
+ log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
+ };
for (let y = terminal.rows - 1 - this.height_input,
- i = this.log.length - 1;
+ i = log_display_lines.length - 1;
y >= this.height_header && i >= 0;
y--, i--) {
- terminal.write(y, this.window_width, this.log[i]);
+ terminal.write(y, this.window_width, log_display_lines[i]);
}
},
draw_info: function() {
tui.log_msg('# ' + tokens[1], 1);
} else if (tokens[0] === 'PLAYER_ID') {
game.player_id = parseInt(tokens[1]);
- } else if (tokens[0] === 'META') {
- tui.log_msg('@ ' + tokens[1]);
} else if (tokens[0] === 'LOGIN_OK') {
server.send(['GET_GAMESTATE']);
- tui.log_msg('@ ' + tokens[1]);
tui.log_help();
- tui.switch_mode(mode_chat);
+ tui.switch_mode(mode_play);
} else if (tokens[0] === 'ANNOTATION') {
let position = parser.parse_yx(tokens[1]);
explorer.update_info_db(position, tokens[2]);
}
}
-document.addEventListener('keydown', (event) => {
- if (tui.mode.has_input_prompt && event.key.length === 1) {
- tui.add_to_input(event.key);
- } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
- tui.shorten_input();
- } else if (tui.mode == mode_login && event.key == 'Enter') {
- server.send(['LOGIN', tui.input]);
+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.input);
+ 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.input);
+ 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') {
}
} else if (tokens[0] == ':msg') {
if (tokens.length > 2) {
- let msg = tui.input.slice(token_starts[2]);
+ let msg = tui.inputEl.value.slice(token_starts[2]);
server.send(['QUERY', tokens[1], msg]);
} else {
tui.log_msg('? need message target and message');
} else {
tui.log_msg('? unknown command');
}
- } else {
- server.send(['ALL', tui.input]);
+ } else {
+ server.send(['ALL', tui.inputEl.value]);
}
+ } else if (tui.inputEl.valuelength > 0) {
+ server.send(['ALL', tui.inputEl.value]);
}
tui.empty_input();
tui.full_refresh();
} else if (tui.mode == mode_play) {
if (event.key === 'c') {
+ event.preventDefault();
tui.switch_mode(mode_chat);
} else if (event.key === 'e') {
+ event.preventDefault();
tui.switch_mode(mode_edit);
} else if (event.key === '?') {
tui.switch_mode(mode_study);
} else if (event.key === tui.key_down) {
server.send(['TASK:MOVE', 'DOWN']);
};
- } else if (tui.mode == mode_edit) {
- if (event.key != "Shift" && event.key.length == 1) {
- server.send(["TASK:WRITE", event.key]);
- tui.switch_mode(mode_play);
- }
} else if (tui.mode == mode_study) {
if (event.key === 'c') {
tui.switch_mode(mode_chat);
} else if (event.key == 'p') {
tui.switch_mode(mode_play);
- } else if (event.key === 'a') {
- explorer.move('left');
} else if (event.key === tui.key_left) {
- explorer.move('right');
+ explorer.move('left');
} else if (event.key === tui.key_right) {
- explorer.move('up');
+ explorer.move('right');
} else if (event.key === tui.key_up) {
- explorer.move('down');
+ explorer.move('up');
} else if (event.key === tui.key_down) {
+ explorer.move('down');
+ } else if (event.key === 'e') {
+ event.preventDefault();
tui.switch_mode(mode_annotate);
};
}
}, false);
+
+let wasd_selector = document.getElementById("WASD_selector");
+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;
+}, false);
+let rows_selector = document.getElementById("n_rows");
+rows_selector.addEventListener('input', function() {
+ terminal.rows = rows_selector.value;
+ terminal.initialize();
+ tui.full_refresh();
+}, false);
+let cols_selector = document.getElementById("n_cols");
+cols_selector.addEventListener('input', function() {
+ terminal.cols = cols_selector.value;
+ terminal.initialize();
+ tui.window_width = terminal.cols / 2,
+ tui.full_refresh();
+}, false);
+window.setInterval(function() {
+ if (!(['input', 'n_cols', 'n_rows', 'WASD_selector'].includes(document.activeElement.id))) {
+ tui.inputEl.focus();
+ }
+}, 100);
</script>
</body></html>