From: Christian Heller Date: Mon, 14 Dec 2020 22:32:55 +0000 (+0100) Subject: Only allow renaming and protection changing on thing player carries. X-Git-Url: https://plomlompom.com/repos/te"st.html?a=commitdiff_plain;h=feb0ffb7c7523ddee0cbd685cbc9c2e768a56cbd;p=plomrogue2 Only allow renaming and protection changing on thing player carries. --- diff --git a/plomrogue/commands.py b/plomrogue/commands.py index ca5c37c..24e991c 100644 --- a/plomrogue/commands.py +++ b/plomrogue/commands.py @@ -95,19 +95,18 @@ def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id): game.record_change((big_yx, little_yx), 'fov') cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char' -def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id): +def cmd_THING_PROTECTION(game, protection_char, connection_id): player = game.get_player(connection_id) if not player: raise GameError('need to be logged in for this') if not game.sessions[connection_id]['status'] == 'admin': raise GameError('need to be admin for this') - t = game.get_thing(thing_id) - if not t: - raise GameError('thing of ID %s not found' % thing_id) - t.protection = protection_char + if not player.carrying: + raise GameError('need to carry a thing to protect it') + player.carrying.protection = protection_char game.changed = True - game.record_change(t.position, 'other') -cmd_THING_PROTECTION.argtypes = 'int:pos char' + game.record_change(player.carrying.position, 'other') +cmd_THING_PROTECTION.argtypes = 'char' def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id): player = game.get_player(connection_id) @@ -118,7 +117,6 @@ def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id): if tile_class == '.': raise GameError('tile class "." must remain unprotected') game.map_control_passwords[tile_class] = password - #game.changed = True cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string' def cmd_NICK(game, nick, connection_id): @@ -246,26 +244,26 @@ def cmd_THING(game, big_yx, little_yx, thing_type, thing_id): raise GameError('illegal thing type %s' % thing_type) _ = game.get_map(big_yx) game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id) - # game.changed = True handled by add_thing cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg' -def cmd_THING_NAME(game, thing_id, name, pw, connection_id): - # TODO check if thing in FOV - t = game.get_thing(thing_id) - if not t: - raise GameError('thing of ID %s not found' % thing_id) - if not game.can_do_thing_with_pw(t, pw): +def cmd_THING_NAME(game, name, pw, connection_id): + player = game.get_player(connection_id) + if not player: + raise GameError('need to be logged in for this') + if not player.carrying: + raise GameError('need to carry a thing to rename it') + if not game.can_do_thing_with_pw(player.carrying, pw): raise GameError('wrong password for thing') if name == ' ': - if hasattr(t.__class__, 'name'): + if hasattr(player.carrying.__class__, 'name'): raise GameError('cannot un-name things of this type') - if hasattr(t, 'name'): - del t.name + if hasattr(player.carrying, 'name'): + del player.carrying.name else: - t.name = name + player.carrying.name = name game.changed = True - game.record_change(t.position, 'other') -cmd_THING_NAME.argtypes = 'int:pos string string' + game.record_change(player.carrying.position, 'other') +cmd_THING_NAME.argtypes = 'string string' def cmd_GOD_THING_NAME(game, thing_id, name): t = game.get_thing(thing_id) diff --git a/rogue_chat.html b/rogue_chat.html index 23626cf..35dc5d4 100644 --- a/rogue_chat.html +++ b/rogue_chat.html @@ -150,7 +150,7 @@ let mode_helps = { 'name_thing': { 'short': 'name thing', 'intro': '', - 'long': 'Give name to/change name of thing here.' + 'long': 'Give name to/change name of carried thing.' }, 'command_thing': { 'short': 'command', @@ -170,7 +170,7 @@ let mode_helps = { 'admin_thing_protect': { 'short': 'change thing protection', 'intro': '@ enter thing protection character:', - 'long': 'Change protection character for thing here.' + 'long': 'Change protection character for carried thing.' }, 'enter_face': { 'short': 'edit face', @@ -519,7 +519,7 @@ let server = { game.thing_types[tokens[1]] = tokens[2] } else if (tokens[0] === 'THING_CARRYING') { let t = game.get_thing_temp(tokens[1]); - t.carrying = game.get_thing(tokens[2], false); + t.carrying = game.get_thing_temp(tokens[2], false); } else if (tokens[0] === 'THING_INSTALLED') { let t = game.get_thing_temp(tokens[1]); t.installed = true; @@ -787,7 +787,7 @@ let tui = { }, switch_mode: function(mode_name) { - function fail(msg, return_mode) { + function fail(msg, return_mode='play') { tui.log_msg('? ' + msg); terminal.blink_screen(); tui.switch_mode(return_mode); @@ -800,34 +800,20 @@ let tui = { this.tile_draw = false; if (mode_name == 'command_thing' && (!game.player.carrying || !game.player.carrying.commandable)) { - return fail('not carrying anything commandable', 'play'); + return fail('not carrying anything commandable'); + } else if (mode_name == 'name_thing' && !game.player.carrying) { + return fail('not carrying anything to re-name'); + } else if (mode_name == 'admin_thing_protect' && !game.player.carrying) { + return fail('not carrying anything to protect') } else if (mode_name == 'take_thing' && game.player.carrying) { - return fail('already carrying something', 'play'); + return fail('already carrying something'); } else if (mode_name == 'drop_thing' && !game.player.carrying) { - return fail('not carrying anything droppable', 'play'); + return fail('not carrying anything droppable'); } else if (mode_name == 'enter_hat' && !game.player.hat) { return fail('not wearing hat to edit', 'edit'); } if (mode_name == 'admin_enter' && this.is_admin) { mode_name = 'admin'; - } else if (['name_thing', 'admin_thing_protect'].includes(mode_name)) { - let thing_id = null; - for (let t_id in game.things) { - if (t_id == game.player_id) { - continue; - } - let t = game.things[t_id]; - if (game.player.position[0] == t.position[0] - && game.player.position[1] == t.position[1]) { - thing_id = t_id; - break; - } - } - if (!thing_id) { - return fail('not standing over thing', 'fail'); - } else { - this.selected_thing_id = thing_id; - } }; this.mode = this['mode_' + mode_name]; if (["control_tile_draw", "control_tile_type", "control_pw_type"].includes(this.mode.name)) { @@ -950,14 +936,12 @@ let tui = { } else if (this.mode.name == 'password') { this.inputEl.value = this.password; } else if (this.mode.name == 'name_thing') { - let t = game.get_thing(this.selected_thing_id); - if (t && t.name_) { - this.inputEl.value = t.name_; + if (game.player.carrying && game.player.carrying.name_) { + this.inputEl.value = game.player.carrying.name_; } } else if (this.mode.name == 'admin_thing_protect') { - let t = game.get_thing(this.selected_thing_id); - if (t && t.protection) { - this.inputEl.value = t.protection; + if (game.player.carrying && game.player.carrying.protection) { + this.inputEl.value = game.player.carrying.protection; } } else if (['enter_face', 'enter_hat'].includes(this.mode.name)) { const start = this.ascii_draw_stage * 6; @@ -1606,8 +1590,7 @@ tui.inputEl.addEventListener('keydown', (event) => { if (tui.inputEl.value.length == 0) { tui.inputEl.value = " "; } - server.send(["THING_NAME", tui.selected_thing_id, tui.inputEl.value, - tui.password]); + server.send(["THING_NAME", tui.inputEl.value, tui.password]); tui.switch_mode('edit'); } else if (tui.mode.name == 'annotate' && event.key == 'Enter') { explorer.annotate(tui.inputEl.value); @@ -1641,7 +1624,7 @@ tui.inputEl.addEventListener('keydown', (event) => { if (tui.inputEl.value.length != 1) { tui.log_msg('@ entered non-single-char, therefore aborted'); } else { - server.send(['THING_PROTECTION', tui.selected_thing_id, tui.inputEl.value]) + server.send(['THING_PROTECTION', tui.inputEl.value]) tui.log_msg('@ sent new protection character for thing'); } tui.switch_mode('admin'); diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py index 07c8697..c26c380 100755 --- a/rogue_chat_curses.py +++ b/rogue_chat_curses.py @@ -29,7 +29,7 @@ mode_helps = { 'name_thing': { 'short': 'name thing', 'intro': '', - 'long': 'Give name to/change name of thing here.' + 'long': 'Give name to/change name of carried thing.' }, 'command_thing': { 'short': 'command', @@ -49,7 +49,7 @@ mode_helps = { 'admin_thing_protect': { 'short': 'change thing protection', 'intro': '@ enter thing protection character:', - 'long': 'Change protection character for thing here.' + 'long': 'Change protection character for carried thing.' }, 'enter_face': { 'short': 'edit face', @@ -342,7 +342,7 @@ def cmd_THING_INSTALLED(game, thing_id): cmd_THING_INSTALLED.argtypes = 'int:pos' def cmd_THING_CARRYING(game, thing_id, carried_id): - game.get_thing_temp(thing_id).carrying = game.get_thing(carried_id) + game.get_thing_temp(thing_id).carrying = game.get_thing_temp(carried_id) cmd_THING_CARRYING.argtypes = 'int:pos int:pos' def cmd_TERRAIN(game, terrain_char, terrain_desc): @@ -514,7 +514,8 @@ class TUI: self.mode_control_tile_draw.available_actions = ["move_explorer", "toggle_tile_draw"] self.mode_edit.available_modes = ["write", "annotate", "portal", - "name_thing", "enter_face", "enter_hat", "password", + "name_thing", "enter_face", "enter_hat", + "password", "chat", "study", "play", "admin_enter"] self.mode_edit.available_actions = ["move", "flatten", "install", "toggle_map_mode"] @@ -649,11 +650,11 @@ class TUI: elif self.mode.name == 'password': self.input_ = self.password elif self.mode.name == 'name_thing': - if hasattr(self.thing_selected, 'name'): - self.input_ = self.thing_selected.name + if hasattr(self.game.player.carrying, 'name'): + self.input_ = self.game.player.carrying.name elif self.mode.name == 'admin_thing_protect': - if hasattr(self.thing_selected, 'protection'): - self.input_ = self.thing_selected.protection + if hasattr(self.game.player.carrying, 'protection'): + self.input_ = self.game.player.carrying.protection elif self.mode.name in {'enter_face', 'enter_hat'}: start = self.ascii_draw_stage * 6 end = (self.ascii_draw_stage + 1) * 6 @@ -691,6 +692,10 @@ class TUI: (not self.game.player.carrying or not self.game.player.carrying.commandable): return fail('not carrying anything commandable') + if mode_name == 'name_thing' and not self.game.player.carrying: + return fail('not carrying anything to re-name') + if mode_name == 'admin_thing_protect' and not self.game.player.carrying: + return fail('not carrying anything to protect') if mode_name == 'take_thing' and self.game.player.carrying: return fail('already carrying something') if mode_name == 'drop_thing' and not self.game.player.carrying: @@ -699,17 +704,6 @@ class TUI: return fail('not wearing hat to edit', 'edit') if mode_name == 'admin_enter' and self.is_admin: mode_name = 'admin' - elif mode_name in {'name_thing', 'admin_thing_protect'}: - thing = None - for t in [t for t in self.game.things - if t.position == self.game.player.position - and t.id_ != self.game.player.id_]: - thing = t - break - if not thing: - return fail('not standing over thing', 'edit') - else: - self.thing_selected = thing self.mode = getattr(self, 'mode_' + mode_name) if self.mode.name in {'control_tile_draw', 'control_tile_type', 'control_pw_type'}: @@ -1250,8 +1244,7 @@ class TUI: if len(self.input_) != 1: self.log_msg('@ entered non-single-char, therefore aborted') else: - self.send('THING_PROTECTION %s %s' % (self.thing_selected.id_, - quote(self.input_))) + self.send('THING_PROTECTION %s' % (quote(self.input_))) self.log_msg('@ sent new protection character for thing') self.switch_mode('admin') elif self.mode.name == 'control_tile_type' and key == '\n': @@ -1279,9 +1272,8 @@ class TUI: elif self.mode.name == 'name_thing' and key == '\n': if self.input_ == '': self.input_ = ' ' - self.send('THING_NAME %s %s %s' % (self.thing_selected.id_, - quote(self.input_), - quote(self.password))) + self.send('THING_NAME %s %s' % (quote(self.input_), + quote(self.password))) self.switch_mode('edit') elif self.mode.name == 'annotate' and key == '\n': if self.input_ == '':