From 0f1e35f5f1a318d6e3e610085babd46946ad3600 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sun, 6 Dec 2020 22:53:35 +0100 Subject: [PATCH] In pick_up selection view, use list indexes instead of internal .id_. --- rogue_chat.html | 18 ++++++++++++------ rogue_chat_curses.py | 22 +++++++++++++++------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/rogue_chat.html b/rogue_chat.html index 3c6fe00..0e20ebc 100644 --- a/rogue_chat.html +++ b/rogue_chat.html @@ -664,6 +664,7 @@ let tui = { }, offset: [0,0], map_lines: [], + selectables: [], init: function() { this.mode_chat.available_modes = ["play", "study", "edit", "admin_enter"] this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter", @@ -802,20 +803,20 @@ let tui = { } else if (this.mode.name == 'take_thing') { this.log_msg("selectable things:"); const player = game.things[game.player_id]; - let selectables = []; + this.selectables = []; for (const t_id in game.things) { const t = game.things[t_id]; if (t.position[0] == player.position[0] && t.position[1] == player.position[1] && t != player && t.type_ != 'Player') { - selectables.push([t_id, t]); + this.selectables.push([t_id, t]); } }; - if (selectables.length == 0) { + if (this.selectables.length == 0) { this.log_msg('none') } else { - for (const t of selectables) { - this.log_msg(t[0] + ' ' + explorer.get_thing_info(t[1])); + for (let [i, t] of this.selectables.entries()) { + this.log_msg(i + ': ' + explorer.get_thing_info(t[1])); } } } else if (this.mode.name == 'command_thing') { @@ -1400,7 +1401,12 @@ tui.inputEl.addEventListener('keydown', (event) => { if (tui.inputEl.value.length == 0) { tui.log_msg('@ aborted'); } else { - server.send(['TASK:PICK_UP', tui.inputEl.value]); + const i = parseInt(tui.inputEl.value); + if (isNaN(i) || i < 0 || i >= tui.selectables.length) { + tui.log_msg('? invalid index, aborted'); + } else { + server.send(['TASK:PICK_UP', tui.selectables[i][0]]); + } } tui.inputEl.value = ""; tui.switch_mode('play'); diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py index db5f993..1f93dd8 100755 --- a/rogue_chat_curses.py +++ b/rogue_chat_curses.py @@ -625,14 +625,15 @@ class TUI: elif self.mode.name == 'take_thing': self.log_msg('selectable things:') player = self.game.get_thing(self.game.player_id) - selectables = [t for t in self.game.things - if t != player and t.type_ != 'Player' - and t.position == player.position] - if len(selectables) == 0: + self.selectables = [t for t in self.game.things + if t != player and t.type_ != 'Player' + and t.position == player.position] + if len(self.selectables) == 0: self.log_msg('none') else: - for t in selectables: - self.log_msg(str(t.id_) + ' ' + self.get_thing_info(t)) + for i in range(len(self.selectables)): + t = self.selectables[i] + self.log_msg(str(i) + ': ' + self.get_thing_info(t)) elif self.mode.name == 'command_thing': self.send('TASK:COMMAND ' + quote('HELP')) elif self.mode.name == 'control_pw_pw': @@ -1013,7 +1014,14 @@ class TUI: if self.input_ == '': self.log_msg('@ aborted') else: - self.send('TASK:PICK_UP ' + quote(self.input_)) + try: + i = int(self.input_) + if i < 0 or i >= len(self.selectables): + self.log_msg('? invalid index, aborted') + else: + self.send('TASK:PICK_UP %s' % self.selectables[i].id_) + except ValueError: + self.log_msg('? invalid index, aborted') self.input_ = '' self.switch_mode('play') elif self.mode.name == 'command_thing' and key == '\n': -- 2.30.2