home · contact · privacy
In web client, fix study mode explorer positioning bug.
[plomrogue2] / rogue_chat_curses.py
index 54499daf2551ac335dc93ff9f274d1dcc09bd136..182a1e7f763827ec2a1437990ed1e2c7f3ecf686 100755 (executable)
@@ -10,6 +10,22 @@ from plomrogue.things import ThingBase
 from plomrogue.misc import quote
 from plomrogue.errors import BrokenSocketConnection
 
+mode_helps = {
+    'play': 'This mode allows you to interact with the map.',
+    'study': 'This mode allows you to study the map and its tiles in detail.  Move the question mark over a tile, and the right half of the screen will show detailed information on it.',
+    'edit': 'This mode allows you to change the map tile you currently stand on (if your map editing password authorizes you so).  Just enter any printable ASCII character to imprint it on the ground below you.',
+    'control_pw_type': 'This mode is the first of two steps to change the password for a tile control character.  First enter the tile control character for which you want to change the password!',
+    'control_pw_pw': 'This mode is the second of two steps to change the password for a tile control character.  Enter the new password for the tile control character you chose.',
+    'annotate': 'This mode allows you to add/edit a comment on the tile you are currently standing on (provided your map editing password authorizes you so).  Hit Return to leave.',
+    'portal': 'This mode allows you to imprint/edit/remove a teleportation target on the ground you are currently standing on (provided your map editing password authorizes you so).  Enter or edit a URL to imprint a teleportation target; enter emptiness to remove a pre-existing teleportation target.  Hit Return to leave.',
+    'chat': 'This mode allows you to engage in chit-chat with other users.  Any line you enter into the input prompt that does not start with a "/" will be sent out to nearby players – but barriers and distance will reduce what they can read, so stand close to them to ensure they get your message.  Lines that start with a "/" are used for commands like:',
+    'login': 'Pick your player name.',
+    'waiting_for_server': 'Waiting for a server response.',
+    'post_login_wait': 'Waiting for a server response.',
+    'password': 'This mode allows you to change the password that you send to authorize yourself for editing password-protected map tiles.  Hit return to confirm and leave.',
+    'admin': 'This mode allows you to become admin if you know an admin password.'
+}
+
 from ws4py.client import WebSocketBaseClient
 class WebSocketClient(WebSocketBaseClient):
 
@@ -233,28 +249,40 @@ class TUI:
 
     class Mode:
 
-        def __init__(self, name, help_intro, has_input_prompt=False,
-                     shows_info=False, is_intro = False):
+        def __init__(self, name, has_input_prompt=False,
+                     shows_info=False, is_intro = False,
+                     is_single_char_entry=False):
             self.name = name
             self.has_input_prompt = has_input_prompt
             self.shows_info = shows_info
             self.is_intro = is_intro
-            self.help_intro = help_intro
+            self.help_intro = mode_helps[name]
+            self.is_single_char_entry = is_single_char_entry
 
     def __init__(self, host):
         import os
         import json
         self.host = host
-        self.mode_play = self.Mode('play', 'This mode allows you to interact with the map.')
-        self.mode_study = self.Mode('study', 'This mode allows you to study the map and its tiles in detail.  Move the question mark over a tile, and the right half of the screen will show detailed information on it.', shows_info=True)
-        self.mode_edit = self.Mode('edit', 'This mode allows you to change the map tile you currently stand on (if your map editing password authorizes you so).  Just enter any printable ASCII character to imprint it on the ground below you.')
-        self.mode_annotate = self.Mode('annotate', 'This mode allows you to add/edit a comment on the tile you are currently standing on (provided your map editing password authorizes you so).  Hit Return to leave.', has_input_prompt=True, shows_info=True)
-        self.mode_portal = self.Mode('portal', 'This mode allows you to imprint/edit/remove a teleportation target on the ground you are currently standing on (provided your map editing password authorizes you so).  Enter or edit a URL to imprint a teleportation target; enter emptiness to remove a pre-existing teleportation target.  Hit Return to leave.', has_input_prompt=True, shows_info=True)
-        self.mode_chat = self.Mode('chat', 'This mode allows you to engage in chit-chat with other users.  Any line you enter into the input prompt that does not start with a "/" will be sent out to nearby players – but barriers and distance will reduce what they can read, so stand close to them to ensure they get your message.  Lines that start with a "/" are used for commands like:', has_input_prompt=True)
-        self.mode_waiting_for_server = self.Mode('waiting_for_server', 'Waiting for a server response.', is_intro=True)
-        self.mode_login = self.Mode('login', 'Pick your player name.', has_input_prompt=True, is_intro=True)
-        self.mode_post_login_wait = self.Mode('post_login_wait', 'Waiting for a server response.', is_intro=True)
-        self.mode_password = self.Mode('password', 'This mode allows you to change the password that you send to authorize yourself for editing password-protected map tiles.  Hit return to confirm and leave.', has_input_prompt=True)
+        self.mode_play = self.Mode('play')
+        self.mode_study = self.Mode('study', shows_info=True)
+        self.mode_edit = self.Mode('edit', is_single_char_entry=True)
+        self.mode_control_pw_type = self.Mode('control_pw_type',
+                                              is_single_char_entry=True)
+        self.mode_control_pw_pw = self.Mode('control_pw_pw',
+                                            has_input_prompt=True)
+        self.mode_annotate = self.Mode('annotate', has_input_prompt=True,
+                                       shows_info=True)
+        self.mode_portal = self.Mode('portal', has_input_prompt=True,
+                                     shows_info=True)
+        self.mode_chat = self.Mode('chat', has_input_prompt=True)
+        self.mode_waiting_for_server = self.Mode('waiting_for_server',
+                                                 is_intro=True)
+        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_password = self.Mode('password', has_input_prompt=True)
+        self.mode_admin = self.Mode('admin', has_input_prompt=True)
         self.game = Game()
         self.game.tui = self
         self.parser = Parser(self.game)
@@ -273,10 +301,13 @@ class TUI:
             'switch_to_portal': 'T',
             'switch_to_study': '?',
             'switch_to_edit': 'm',
+            'switch_to_admin': 'A',
+            'switch_to_control_pw': 'C',
             'flatten': 'F',
             'take_thing': 'z',
             'drop_thing': 'u',
             'teleport': 'p',
+            'help': 'h',
             'toggle_map_mode': 'M',
             'hex_move_upleft': 'w',
             'hex_move_upright': 'e',
@@ -375,15 +406,19 @@ class TUI:
             player = self.game.get_thing(self.game.player_id)
             self.explorer = YX(player.position.y, player.position.x)
             self.query_info()
+        if self.mode.is_single_char_entry:
+            self.show_help = True
         if self.mode.name == 'waiting_for_server':
             self.log_msg('@ waiting for server …')
-        if self.mode.name == 'edit':
-            self.show_help = True
         elif self.mode.name == 'login':
             if self.login_name:
                 self.send('LOGIN ' + quote(self.login_name))
             else:
                 self.log_msg('@ enter username')
+        elif self.mode.name == 'admin':
+            self.log_msg('@ enter admin password:')
+        elif self.mode.name == 'control_pw_pw':
+            self.log_msg('@ enter tile control password for "%s":' % self.tile_control_char)
         self.restore_input_values()
 
     def loop(self, stdscr):
@@ -463,6 +498,10 @@ class TUI:
                 if terrain_char in self.game.terrains:
                     terrain_desc = self.game.terrains[terrain_char]
                 info = 'TERRAIN: "%s" / %s\n' % (terrain_char, terrain_desc)
+                protection = self.game.map_control_content[pos_i]
+                if protection == '.':
+                    protection = 'unprotected'
+                info = 'PROTECTION: %s\n' % protection
                 for t in self.game.things:
                     if t.position == self.explorer:
                         info += 'THING: %s / %s' % (t.type_,
@@ -566,7 +605,7 @@ class TUI:
         def draw_help():
             content = "%s mode help\n\n%s\n\n" % (self.mode.name,
                                                   self.mode.help_intro)
-            if self.mode == self.mode_play:
+            if self.mode.name == 'play':
                 content += "Available actions:\n"
                 if 'MOVE' in self.game.tasks:
                     content += "[%s] – move player\n" % ','.join(self.movement_keys)
@@ -584,14 +623,17 @@ class TUI:
                 content += '[%s] – portal edit mode\n' % self.keys['switch_to_portal']
                 content += '[%s] – annotation mode\n' % self.keys['switch_to_annotate']
                 content += '[%s] – password input mode\n' % self.keys['switch_to_password']
-            elif self.mode == self.mode_study:
+                content += '[%s] – become admin\n' % self.keys['switch_to_admin']
+                content += '[%s] – change tile control password' % self.keys['switch_to_control_pw']
+
+            elif self.mode.name == 'study':
                 content += 'Available actions:\n'
                 content += '[%s] – move question mark\n' % ','.join(self.movement_keys)
                 content += '[%s] – toggle view between terrain, annotations, and password protection areas\n' % self.keys['toggle_map_mode']
                 content += '\n\nOther modes available from here:'
                 content += '[%s] – chat mode\n' % self.keys['switch_to_chat']
                 content += '[%s] – play mode\n' % self.keys['switch_to_play']
-            elif self.mode == self.mode_chat:
+            elif self.mode.name == 'chat':
                 content += '/nick NAME – re-name yourself to NAME\n'
                 content += '/%s or /play – switch to play mode\n' % self.keys['switch_to_play']
                 content += '/%s or /study – switch to study mode\n' % self.keys['switch_to_study']
@@ -676,19 +718,30 @@ class TUI:
                 max_length = self.window_width * self.size.y - len(input_prompt) - 1
                 if len(self.input_) > max_length:
                     self.input_ = self.input_[:max_length]
-            elif key == self.keys['help'] and self.mode != self.mode_edit:
+            elif key == self.keys['help'] and not self.mode.is_single_char_entry:
                 self.show_help = True
-            elif self.mode == self.mode_login and key == '\n':
+            elif self.mode.name == 'login' and key == '\n':
                 self.login_name = self.input_
                 self.send('LOGIN ' + quote(self.input_))
                 self.input_ = ""
-            elif self.mode == self.mode_password and key == '\n':
+            elif self.mode.name == 'control_pw_pw' and key == '\n':
+                if self.input_ == '':
+                    self.log_msg('@ aborted')
+                else:
+                    self.send('SET_MAP_CONTROL_PASSWORD ' + quote(self.tile_control_char) + ' ' + quote(self.input_))
+                    self.input_ = ""
+                self.switch_mode('play')
+            elif self.mode.name == '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':
+            elif self.mode.name == 'admin' and key == '\n':
+                self.send('BECOME_ADMIN ' + quote(self.input_))
+                self.input_ = ""
+                self.switch_mode('play')
+            elif self.mode.name == 'chat' and key == '\n':
                 if self.input_ == '':
                     continue
                 if self.input_[0] == '/':  # FIXME fails on empty input
@@ -707,21 +760,21 @@ class TUI:
                 else:
                     self.send('ALL ' + quote(self.input_))
                 self.input_ = ""
-            elif self.mode == self.mode_annotate and key == '\n':
+            elif self.mode.name == 'annotate' and key == '\n':
                 if self.input_ == '':
                     self.input_ = ' '
                 self.send('ANNOTATE %s %s %s' % (self.explorer, quote(self.input_),
                                                  quote(self.password)))
                 self.input_ = ""
                 self.switch_mode('play')
-            elif self.mode == self.mode_portal and key == '\n':
+            elif self.mode.name == 'portal' and key == '\n':
                 if self.input_ == '':
                     self.input_ = ' '
                 self.send('PORTAL %s %s %s' % (self.explorer, quote(self.input_),
                                                quote(self.password)))
                 self.input_ = ""
                 self.switch_mode('play')
-            elif self.mode == self.mode_study:
+            elif self.mode.name == 'study':
                 if key == self.keys['switch_to_chat']:
                     self.switch_mode('chat')
                 elif key == self.keys['switch_to_play']:
@@ -735,7 +788,7 @@ class TUI:
                         self.map_mode = 'terrain'
                 elif key in self.movement_keys:
                     move_explorer(self.movement_keys[key])
-            elif self.mode == self.mode_play:
+            elif self.mode.name == 'play':
                 if key == self.keys['switch_to_chat']:
                     self.switch_mode('chat')
                 elif key == self.keys['switch_to_study']:
@@ -746,6 +799,10 @@ class TUI:
                     self.switch_mode('portal')
                 elif key == self.keys['switch_to_password']:
                     self.switch_mode('password')
+                elif key == self.keys['switch_to_admin']:
+                    self.switch_mode('admin')
+                elif key == self.keys['switch_to_control_pw']:
+                    self.switch_mode('control_pw_type')
                 if key == self.keys['switch_to_edit'] and\
                    'WRITE' in self.game.tasks:
                     self.switch_mode('edit')
@@ -766,9 +823,12 @@ class TUI:
                         self.log_msg('? not standing on portal')
                 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:
+            elif self.mode.name == 'edit':
                 self.send('TASK:WRITE %s %s' % (key, quote(self.password)))
                 self.switch_mode('play')
+            elif self.mode.name == 'control_pw_type':
+                self.tile_control_char = key
+                self.switch_mode('control_pw_pw')
 
 #TUI('localhost:5000')
 TUI('wss://plomlompom.com/rogue_chat/')