home · contact · privacy
Replace annotation polling with annotation push, cache info display.
[plomrogue2] / rogue_chat_curses.py
index 420c858190b6ab51dbb1329274bee2bd89fd432d..70d60b27f1c6ee8abfccf31f6b3d0a0f73d54d2e 100755 (executable)
@@ -134,12 +134,12 @@ class PlomSocketClient(PlomSocket):
             pass  # we assume socket will be known as dead by now
 
 def cmd_TURN(game, n):
-    game.info_db = {}
-    game.info_hints = []
+    game.annotations = {}
     game.turn = n
     game.things = []
     game.portals = {}
     game.turn_complete = False
+    game.fov = ''
 cmd_TURN.argtypes = 'int:nonneg'
 
 def cmd_LOGIN_OK(game):
@@ -224,10 +224,9 @@ cmd_MAP_CONTROL.argtypes = 'string'
 def cmd_GAME_STATE_COMPLETE(game):
     if game.tui.mode.name == 'post_login_wait':
         game.tui.switch_mode('play')
-    if game.tui.mode.shows_info:
-        game.tui.query_info()
     game.turn_complete = True
     game.tui.do_refresh = True
+    game.tui.info_cached = None
 cmd_GAME_STATE_COMPLETE.argtypes = ''
 
 def cmd_PORTAL(game, position, msg):
@@ -250,12 +249,8 @@ def cmd_ARGUMENT_ERROR(game, msg):
     game.tui.do_refresh = True
 cmd_ARGUMENT_ERROR.argtypes = 'string'
 
-def cmd_ANNOTATION_HINT(game, position):
-    game.info_hints += [position]
-cmd_ANNOTATION_HINT.argtypes = 'yx_tuple:nonneg'
-
 def cmd_ANNOTATION(game, position, msg):
-    game.info_db[position] = msg
+    game.annotations[position] = msg
     if game.tui.mode.shows_info:
         game.tui.do_refresh = True
 cmd_ANNOTATION.argtypes = 'yx_tuple:nonneg string'
@@ -309,7 +304,6 @@ class Game(GameBase):
         self.register_command(cmd_MAP_CONTROL)
         self.register_command(cmd_PORTAL)
         self.register_command(cmd_ANNOTATION)
-        self.register_command(cmd_ANNOTATION_HINT)
         self.register_command(cmd_GAME_STATE_COMPLETE)
         self.register_command(cmd_ARGUMENT_ERROR)
         self.register_command(cmd_GAME_ERROR)
@@ -320,8 +314,7 @@ class Game(GameBase):
         self.register_command(cmd_RANDOM_COLORS)
         self.map_content = ''
         self.player_id = -1
-        self.info_db = {}
-        self.info_hints = []
+        self.annotations = {}
         self.portals = {}
         self.terrains = {}
 
@@ -536,14 +529,9 @@ class TUI:
         if len(self.log) > 100:
             self.log = self.log[-100:]
 
-    def query_info(self):
-        self.send('GET_ANNOTATION ' + str(self.explorer))
-
     def restore_input_values(self):
-        if self.mode.name == 'annotate' and self.explorer in self.game.info_db:
-            info = self.game.info_db[self.explorer]
-            if info != '(none)':
-                self.input_ = info
+        if self.mode.name == 'annotate' and self.explorer in self.game.annotations:
+            self.input_ = self.game.annotations[self.explorer]
         elif self.mode.name == 'portal' and self.explorer in self.game.portals:
             self.input_ = self.game.portals[self.explorer]
         elif self.mode.name == 'password':
@@ -597,8 +585,6 @@ class TUI:
         if self.mode.shows_info or self.mode.name == 'control_tile_draw':
             player = self.game.get_thing(self.game.player_id)
             self.explorer = YX(player.position.y, player.position.x)
-            if self.mode.shows_info:
-                self.query_info()
         if self.mode.is_single_char_entry:
             self.show_help = True
         if self.mode.name == 'waiting_for_server':
@@ -640,6 +626,47 @@ class TUI:
         curses.init_color(2, rand(0), rand(0), rand(0))
         self.do_refresh = True
 
+    def get_info(self):
+        if self.info_cached:
+            return self.info_cached
+        pos_i = self.explorer.y * self.game.map_geometry.size.x + self.explorer.x
+        info_to_cache = ''
+        if len(self.game.fov) > pos_i and self.game.fov[pos_i] != '.':
+            info_to_cache += 'outside field of view'
+        else:
+            terrain_char = self.game.map_content[pos_i]
+            terrain_desc = '?'
+            if terrain_char in self.game.terrains:
+                terrain_desc = self.game.terrains[terrain_char]
+            info_to_cache += 'TERRAIN: "%s" / %s\n' % (terrain_char,
+                                                       terrain_desc)
+            protection = self.game.map_control_content[pos_i]
+            if protection == '.':
+                protection = 'unprotected'
+            info_to_cache += 'PROTECTION: %s\n' % protection
+            for t in self.game.things:
+                if t.position == self.explorer:
+                    protection = t.protection
+                    if protection == '.':
+                        protection = 'none'
+                    info_to_cache += 'THING: %s / %s' %\
+                        (t.type_, self.game.thing_types[t.type_])
+                    if hasattr(t, 'thing_char'):
+                        info_to_cache += t.thing_char
+                    if hasattr(t, 'name'):
+                        info_to_cache += ' (%s)' % t.name
+                    info_to_cache += ' / protection: %s\n' % protection
+            if self.explorer in self.game.portals:
+                info_to_cache += 'PORTAL: ' +\
+                    self.game.portals[self.explorer] + '\n'
+            else:
+                info_to_cache += 'PORTAL: (none)\n'
+            if self.explorer in self.game.annotations:
+                info_to_cache += 'ANNOTATION: ' +\
+                    self.game.annotations[self.explorer]
+        self.info_cached = info_to_cache
+        return self.info_cached
+
     def loop(self, stdscr):
         import datetime
 
@@ -694,9 +721,8 @@ class TUI:
         def move_explorer(direction):
             target = self.game.map_geometry.move_yx(self.explorer, direction)
             if target:
+                self.info_cached = None
                 self.explorer = target
-                if self.mode.shows_info:
-                    self.query_info()
                 if self.tile_draw:
                     self.send_tile_control_command()
             else:
@@ -715,42 +741,7 @@ class TUI:
                 safe_addstr(max_y - i - 1, self.window_width, lines[i])
 
         def draw_info():
-            if not self.game.turn_complete:
-                return
-            pos_i = self.explorer.y * self.game.map_geometry.size.x + self.explorer.x
-            info = 'MAP VIEW: %s\n' % self.map_mode
-            if self.game.fov[pos_i] != '.':
-                info += 'outside field of view'
-            else:
-                terrain_char = self.game.map_content[pos_i]
-                terrain_desc = '?'
-                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:
-                        protection = t.protection
-                        if protection == '.':
-                            protection = 'none'
-                        info += 'THING: %s / %s' % (t.type_,
-                                                    self.game.thing_types[t.type_])
-                        if hasattr(t, 'thing_char'):
-                            info += t.thing_char
-                        if hasattr(t, 'name'):
-                            info += ' (%s)' % t.name
-                        info += ' / protection: %s\n' % protection
-                if self.explorer in self.game.portals:
-                    info += 'PORTAL: ' + self.game.portals[self.explorer] + '\n'
-                else:
-                    info += 'PORTAL: (none)\n'
-                if self.explorer in self.game.info_db:
-                    info += 'ANNOTATION: ' + self.game.info_db[self.explorer]
-                else:
-                    info += 'ANNOTATION: waiting …'
+            info = 'MAP VIEW: %s\n%s' % (self.map_mode, self.get_info())
             lines = msg_into_lines_of_width(info, self.window_width)
             height_header = 2
             for i in range(len(lines)):
@@ -792,7 +783,7 @@ class TUI:
                         map_lines_split += [[c + ' ' for c
                                              in self.game.map_content[start:end]]]
                 if self.map_mode == 'terrain + annotations':
-                    for p in self.game.info_hints:
+                    for p in self.game.annotations:
                         map_lines_split[p.y][p.x] = 'A '
                 elif self.map_mode == 'terrain + things':
                     for p in self.game.portals.keys():