home · contact · privacy
Add terrain editing access control via passwords.
[plomrogue2] / rogue_chat_curses.py
index 64eaf03959436cc768af00c0b323a7ab66f41259..bf5ad1f65fcb103d7dbad0e5b4a84a913e2a5b55 100755 (executable)
@@ -103,6 +103,10 @@ def cmd_MAP(game, geometry, size, content):
         }
 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos string'
 
+def cmd_MAP_CONTROL(game, content):
+    game.map_control_content = content
+cmd_MAP_CONTROL.argtypes = 'string'
+
 def cmd_GAME_STATE_COMPLETE(game):
     game.info_db = {}
     if game.tui.mode.name == 'post_login_wait':
@@ -166,6 +170,7 @@ class Game(GameBase):
         self.register_command(cmd_THING_POS)
         self.register_command(cmd_THING_NAME)
         self.register_command(cmd_MAP)
+        self.register_command(cmd_MAP_CONTROL)
         self.register_command(cmd_PORTAL)
         self.register_command(cmd_ANNOTATION)
         self.register_command(cmd_GAME_STATE_COMPLETE)
@@ -214,6 +219,7 @@ class TUI:
         self.mode_login = self.Mode('login', has_input_prompt=True, is_intro=True)
         self.mode_post_login_wait = self.Mode('post_login_wait', is_intro=True)
         self.mode_teleport = self.Mode('teleport', has_input_prompt=True)
+        self.mode_password = self.Mode('password', has_input_prompt=True)
         self.game = Game()
         self.game.tui = self
         self.parser = Parser(self.game)
@@ -221,15 +227,19 @@ class TUI:
         self.do_refresh = True
         self.queue = queue.Queue()
         self.login_name = None
+        self.map_mode = 'terrain'
+        self.password = 'foo'
         self.switch_mode('waiting_for_server')
         self.keys = {
             'switch_to_chat': 't',
             'switch_to_play': 'p',
+            'switch_to_password': 'p',
             'switch_to_annotate': 'm',
             'switch_to_portal': 'P',
             'switch_to_study': '?',
             'switch_to_edit': 'm',
             'flatten': 'F',
+            'toggle_map_mode': 'M',
             'hex_move_upleft': 'w',
             'hex_move_upright': 'e',
             'hex_move_right': 'd',
@@ -269,6 +279,7 @@ class TUI:
         self.send('GET_ANNOTATION ' + str(self.explorer))
 
     def switch_mode(self, mode_name, keep_position = False):
+        self.map_mode = 'terrain'
         self.mode = getattr(self, 'mode_' + mode_name)
         if self.mode.shows_info and not keep_position:
             player = self.game.get_thing(self.game.player_id, False)
@@ -289,6 +300,8 @@ class TUI:
                 self.input_ = info
         elif self.mode.name == 'portal' and self.explorer in self.game.portals:
             self.input_ = self.game.portals[self.explorer]
+        elif self.mode.name == 'password':
+            self.input_ = self.password
 
     def help(self):
         self.log_msg("HELP:");
@@ -303,16 +316,18 @@ class TUI:
             self.log_msg("  %s - move" % ','.join(self.movement_keys));
         self.log_msg("  %s - switch to chat mode" % self.keys['switch_to_chat']);
         self.log_msg("commands specific to play mode:");
+        self.log_msg("  %s - enter terrain password" % self.keys['switch_to_password']);
         if 'WRITE' in self.game.tasks:
             self.log_msg("  %s - write following ASCII character" % self.keys['switch_to_edit']);
         if 'FLATTEN_SURROUNDINGS' in self.game.tasks:
             self.log_msg("  %s - flatten surroundings" % self.keys['flatten']);
         self.log_msg("  %s - switch to study mode" % self.keys['switch_to_study']);
         self.log_msg("commands specific to study mode:");
+        self.log_msg("  %s - switch to play mode" % self.keys['switch_to_play']);
         if 'MOVE' not in self.game.tasks:
             self.log_msg("  %s - move" % ','.join(self.movement_keys));
         self.log_msg("  %s - annotate terrain" % self.keys['switch_to_annotate']);
-        self.log_msg("  %s - switch to play mode" % self.keys['switch_to_play']);
+        self.log_msg("  %s - toggle terrain/control view" % self.keys['toggle_map_mode']);
 
     def loop(self, stdscr):
         import time
@@ -455,12 +470,16 @@ class TUI:
             if not self.game.turn_complete:
                 return
             map_lines_split = []
+            map_content = self.game.map_content
+            if self.map_mode == 'control':
+                map_content = self.game.map_control_content
             for y in range(self.game.map_geometry.size.y):
                 start = self.game.map_geometry.size.x * y
                 end = start + self.game.map_geometry.size.x
-                map_lines_split += [list(self.game.map_content[start:end])]
-            for t in self.game.things:
-                map_lines_split[t.position.y][t.position.x] = '@'
+                map_lines_split += [list(map_content[start:end])]
+            if self.map_mode == 'terrain':
+                for t in self.game.things:
+                    map_lines_split[t.position.y][t.position.x] = '@'
             if self.mode.shows_info:
                 map_lines_split[self.explorer.y][self.explorer.x] = '?'
             map_lines = []
@@ -548,6 +567,12 @@ class TUI:
                 self.login_name = self.input_
                 self.send('LOGIN ' + quote(self.input_))
                 self.input_ = ""
+            elif self.mode == self.mode_password and key == '\n':
+                if self.input_ == '':
+                    self.input_ = ' '
+                self.password = self.input_
+                self.input_ = ""
+                self.switch_mode('play')
             elif self.mode == self.mode_chat and key == '\n':
                 if self.input_[0] == '/':
                     if self.input_ in {'/' + self.keys['switch_to_play'], '/play'}:
@@ -605,6 +630,11 @@ class TUI:
                     self.switch_mode('annotate', keep_position=True)
                 elif key == self.keys['switch_to_portal']:
                     self.switch_mode('portal', keep_position=True)
+                elif key == self.keys['toggle_map_mode']:
+                    if self.map_mode == 'terrain':
+                        self.map_mode = 'control'
+                    else:
+                        self.map_mode = 'terrain'
                 elif key in self.movement_keys:
                     move_explorer(self.movement_keys[key])
             elif self.mode == self.mode_play:
@@ -612,6 +642,8 @@ class TUI:
                     self.switch_mode('chat')
                 elif key == self.keys['switch_to_study']:
                     self.switch_mode('study')
+                elif key == self.keys['switch_to_password']:
+                    self.switch_mode('password')
                 if key == self.keys['switch_to_edit'] and\
                    'WRITE' in self.game.tasks:
                     self.switch_mode('edit')
@@ -621,7 +653,7 @@ class TUI:
                 elif key in self.movement_keys and 'MOVE' in self.game.tasks:
                     self.send('TASK:MOVE ' + self.movement_keys[key])
             elif self.mode == self.mode_edit:
-                self.send('TASK:WRITE ' + key)
+                self.send('TASK:WRITE %s %s' % (key, quote(self.password)))
                 self.switch_mode('play')
 
 TUI('localhost:5000')