home · contact · privacy
Some more code style improvements after flake8.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 26 Nov 2020 20:47:31 +0000 (21:47 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 26 Nov 2020 20:47:31 +0000 (21:47 +0100)
plomrogue/commands.py
plomrogue/game.py
plomrogue/io.py
plomrogue/io_tcp.py
plomrogue/io_websocket.py
plomrogue/mapping.py
plomrogue/parser.py
plomrogue/tasks.py
plomrogue/things.py
rogue_chat_curses.py

index 0c3da0ada3b9e49dc784dbd9000c674f0a86b142..65b054019b0f38dbb6d492803ec72e821baff28f 100644 (file)
@@ -146,20 +146,20 @@ def cmd_GET_GAMESTATE(game, connection_id):
     game.send_gamestate(connection_id)
 cmd_GET_GAMESTATE.argtypes = ''
 
-#def cmd_QUERY(game, target_nick, msg, connection_id):
-#    if not connection_id in game.sessions:
-#        raise GameError('can only query when logged in')
-#    t = game.get_thing(game.sessions[connection_id], False)
-#    source_nick = t.name
-#    for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
-#        for c_id in game.sessions:
-#            if game.sessions[c_id] == t.id_:
-#                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
-#                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
-#                return
-#        raise GameError('target user offline')
-#    raise GameError('can only query with registered nicknames')
-#cmd_QUERY.argtypes = 'string string'
+# def cmd_QUERY(game, target_nick, msg, connection_id):
+#     if not connection_id in game.sessions:
+#         raise GameError('can only query when logged in')
+#     t = game.get_thing(game.sessions[connection_id], False)
+#     source_nick = t.name
+#     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
+#         for c_id in game.sessions:
+#             if game.sessions[c_id] == t.id_:
+#                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
+#                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
+#                 return
+#         raise GameError('target user offline')
+#     raise GameError('can only query with registered nicknames')
+# cmd_QUERY.argtypes = 'string string'
 
 def cmd_PING(game, connection_id):
     game.io.send('PONG', connection_id)
@@ -181,7 +181,7 @@ def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
             if little_yx in game.annotations[big_yx]:
                 del game.annotations[big_yx][little_yx]
     else:
-        if not big_yx in game.annotations:
+        if big_yx not in game.annotations:
             game.annotations[big_yx] = {}
         game.annotations[big_yx][little_yx] = msg
     game.changed = True
@@ -199,21 +199,21 @@ def cmd_PORTAL(game, yx, msg, pw, connection_id):
             if little_yx in game.portals[big_yx]:
                 del game.portals[big_yx][little_yx]
     else:
-        if not big_yx in game.portals:
+        if big_yx not in game.portals:
             game.portals[big_yx] = {}
         game.portals[big_yx][little_yx] = msg
     game.changed = True
 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
 
 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
-    if not big_yx in game.annotations:
+    if big_yx not in game.annotations:
         game.annotations[big_yx] = {}
     game.annotations[big_yx][little_yx] = msg
     game.changed = True
 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
 
 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
-    if not big_yx in game.portals:
+    if big_yx not in game.portals:
         game.portals[big_yx] = {}
     game.portals[big_yx][little_yx] = msg
     game.changed = True
@@ -222,9 +222,9 @@ cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
 def cmd_GET_ANNOTATION(game, yx, connection_id):
     player = game.get_player(connection_id)
     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
-    annotation = '(unknown)';
+    annotation = '(unknown)'
     if player.fov_test(big_yx, little_yx):
-        annotation = '(none)';
+        annotation = '(none)'
         if big_yx in game.annotations:
             if little_yx in game.annotations[big_yx]:
                 annotation = game.annotations[big_yx][little_yx]
@@ -251,7 +251,7 @@ def cmd_MAP_CONTROL_PW(game, tile_class, password):
 cmd_MAP_CONTROL_PW.argtypes = 'char string'
 
 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
-    if not thing_type in game.thing_types:
+    if thing_type not in game.thing_types:
         raise GameError('illegal thing type %s' % thing_type)
     _ = game.get_map(big_yx)
     t_old = None
index 7936842ae6ce892ae9a27e7a4aa28593de5196b8..45e23e1a759e53d366b6a7894883d8772a4744ec 100755 (executable)
@@ -58,15 +58,15 @@ class SaveableMap(Map):
             self.set_line(3, 'X' * self.geometry.size.x)
             self.set_line(4, 'X' * self.geometry.size.x)
             for y in range(self.geometry.size.y):
-                self[YX(y,0)] = 'X'
-                self[YX(y,1)] = 'X'
-                self[YX(y,2)] = 'X'
-                self[YX(y,3)] = 'X'
-                self[YX(y,4)] = 'X'
+                self[YX(y, 0)] = 'X'
+                self[YX(y, 1)] = 'X'
+                self[YX(y, 2)] = 'X'
+                self[YX(y, 3)] = 'X'
+                self[YX(y, 4)] = 'X'
         elif type(self.geometry) == MapGeometryHex:
             # TODO: for this to work we need a map side length divisible by 6.
 
-            def draw_grid(offset=YX(0,0)):
+            def draw_grid(offset=YX(0, 0)):
                 dirs = ('DOWNRIGHT', 'RIGHT', 'UPRIGHT', 'RIGHT')
 
                 def draw_snake(start):
@@ -89,20 +89,23 @@ class SaveableMap(Map):
 
                 if alternate_hex:
                     draw_snake(offset + YX(0, 0))
-                draw_snake(offset + YX((0 + alternate_hex) * distance, -int(1.5*distance)))
-                draw_snake(offset + YX((1 + alternate_hex) * distance, 0))
-                draw_snake(offset + YX((2 + alternate_hex) * distance, -int(1.5*distance)))
+                draw_snake(offset + YX((0 + alternate_hex) * distance,
+                           -int(1.5 * distance)))
+                draw_snake(offset + YX((1 + alternate_hex) * distance,
+                           0))
+                draw_snake(offset + YX((2 + alternate_hex) * distance,
+                           -int(1.5 * distance)))
 
             distance = self.geometry.size.y // 3
             draw_grid()
-            draw_grid(YX(2,0))
-            draw_grid(YX(0,2))
-            draw_grid(YX(1,0))
-            draw_grid(YX(0,1))
-            draw_grid(YX(-1,0))
-            draw_grid(YX(0,-1))
-            draw_grid(YX(-2,0))
-            draw_grid(YX(0,-2))
+            draw_grid(YX(2, 0))
+            draw_grid(YX(0, 2))
+            draw_grid(YX(1, 0))
+            draw_grid(YX(0, 1))
+            draw_grid(YX(-1, 0))
+            draw_grid(YX(0, -1))
+            draw_grid(YX(-2, 0))
+            draw_grid(YX(0, -2))
         self.modified = old_modified
 
 
@@ -121,7 +124,7 @@ class Game(GameBase):
         self.map_controls = {}
         self.map_control_passwords = {}
         self.annotations = {}
-        self.spawn_point = YX(0,0), YX(0,0)
+        self.spawn_point = YX(0, 0), YX(0, 0)
         self.portals = {}
         self.player_chars = string.digits + string.ascii_letters
         self.player_char_i = -1
@@ -181,7 +184,7 @@ class Game(GameBase):
         return self.map_geometry.__class__.__name__[len('MapGeometry'):]
 
     def get_player(self, connection_id):
-        if not connection_id in self.sessions:
+        if connection_id not in self.sessions:
             return None
         player = self.get_thing(self.sessions[connection_id]['thing_id'])
         return player
@@ -300,47 +303,47 @@ class Game(GameBase):
 
     def save(self):
 
-      def write(f, msg):
-          f.write(msg + '\n')
-
-      with open(self.io.save_file, 'w') as f:
-          write(f, 'TURN %s' % self.turn)
-          map_geometry_shape = self.get_map_geometry_shape()
-          write(f, 'MAP %s %s' % (map_geometry_shape, self.map_geometry.size,))
-          for big_yx in [yx for yx in self.maps if self.maps[yx].modified]:
-              for y, line in self.maps[big_yx].lines():
-                  write(f, 'MAP_LINE %s %5s %s' % (big_yx, y, quote(line)))
-          for big_yx in self.annotations:
-              for little_yx in self.annotations[big_yx]:
-                  write(f, 'GOD_ANNOTATE %s %s %s' %
-                        (big_yx, little_yx, quote(self.annotations[big_yx][little_yx])))
-          for big_yx in self.portals:
-              for little_yx in self.portals[big_yx]:
-                  write(f, 'GOD_PORTAL %s %s %s' % (big_yx, little_yx,
-                                                    quote(self.portals[big_yx][little_yx])))
-          for big_yx in [yx for yx in self.map_controls
-                         if self.map_controls[yx].modified]:
-              for y, line in self.map_controls[big_yx].lines():
-                  write(f, 'MAP_CONTROL_LINE %s %5s %s' % (big_yx, y, quote(line)))
-          for tile_class in self.map_control_passwords:
-              write(f, 'MAP_CONTROL_PW %s %s' % (tile_class,
-                                                 self.map_control_passwords[tile_class]))
-          for pw in self.admin_passwords:
-                  write(f, 'ADMIN_PASSWORD %s' % pw)
-          for t in [t for t in self.things if not t.type_ == 'Player']:
-              write(f, 'THING %s %s %s %s' % (t.position[0],
-                                              t.position[1], t.type_, t.id_))
-              if hasattr(t, 'name'):
-                  write(f, 'THING_NAME %s %s' % (t.id_, quote(t.name)))
-          write(f, 'SPAWN_POINT %s %s' % (self.spawn_point[0],
-                                          self.spawn_point[1]))
+        def write(f, msg):
+            f.write(msg + '\n')
+
+        with open(self.io.save_file, 'w') as f:
+            write(f, 'TURN %s' % self.turn)
+            map_geometry_shape = self.get_map_geometry_shape()
+            write(f, 'MAP %s %s' % (map_geometry_shape, self.map_geometry.size,))
+            for big_yx in [yx for yx in self.maps if self.maps[yx].modified]:
+                for y, line in self.maps[big_yx].lines():
+                    write(f, 'MAP_LINE %s %5s %s' % (big_yx, y, quote(line)))
+            for big_yx in self.annotations:
+                for little_yx in self.annotations[big_yx]:
+                    write(f, 'GOD_ANNOTATE %s %s %s' %
+                          (big_yx, little_yx, quote(self.annotations[big_yx][little_yx])))
+            for big_yx in self.portals:
+                for little_yx in self.portals[big_yx]:
+                    write(f, 'GOD_PORTAL %s %s %s' % (big_yx, little_yx,
+                                                      quote(self.portals[big_yx][little_yx])))
+            for big_yx in [yx for yx in self.map_controls
+                           if self.map_controls[yx].modified]:
+                for y, line in self.map_controls[big_yx].lines():
+                    write(f, 'MAP_CONTROL_LINE %s %5s %s' % (big_yx, y, quote(line)))
+            for tile_class in self.map_control_passwords:
+                write(f, 'MAP_CONTROL_PW %s %s' % (tile_class,
+                                                   self.map_control_passwords[tile_class]))
+            for pw in self.admin_passwords:
+                write(f, 'ADMIN_PASSWORD %s' % pw)
+            for t in [t for t in self.things if not t.type_ == 'Player']:
+                write(f, 'THING %s %s %s %s' % (t.position[0],
+                                                t.position[1], t.type_, t.id_))
+                if hasattr(t, 'name'):
+                    write(f, 'THING_NAME %s %s' % (t.id_, quote(t.name)))
+            write(f, 'SPAWN_POINT %s %s' % (self.spawn_point[0],
+                                            self.spawn_point[1]))
 
     def get_map(self, big_yx, type_='normal'):
         if type_ == 'normal':
             maps = self.maps
         elif type_ == 'control':
             maps = self.map_controls
-        if not big_yx in maps:
+        if big_yx not in maps:
             maps[big_yx] = SaveableMap(self.map_geometry)
             if type_ == 'control':
                 maps[big_yx].draw_presets(big_yx.y % 2)
@@ -352,9 +355,9 @@ class Game(GameBase):
         self.annotations = {}
         self.portals = {}
         self.admin_passwords = []
-        self.spawn_point = YX(0,0), YX(0,0)
+        self.spawn_point = YX(0, 0), YX(0, 0)
         self.map_geometry = map_geometry
         self.map_control_passwords = {'X': 'secret'}
-        self.get_map(YX(0,0))
-        self.get_map(YX(0,0), 'control')
+        self.get_map(YX(0, 0))
+        self.get_map(YX(0, 0), 'control')
         self.annotations = {}
index 68f580ba3fa689b78075a175ed4a2e880128235f..252fac18d3c3fba5a5f6308f535e165746f20b7d 100644 (file)
@@ -80,9 +80,9 @@ class GameIO():
                     command(*args, connection_id=connection_id)
                 elif god_mode:
                     command(*args)
-                    #if store and not hasattr(command, 'dont_save'):
-                    #    with open(self.game_file_name, 'a') as f:
-                    #        f.write(input_ + '\n')
+                    # if store and not hasattr(command, 'dont_save'):
+                    #     with open(self.game_file_name, 'a') as f:
+                    #         f.write(input_ + '\n')
         except ArgError as e:
             answer(connection_id, 'ARGUMENT_ERROR ' + quote(str(e)))
         except PlayError as e:
@@ -99,7 +99,7 @@ class GameIO():
         """
         if connection_id:
             for server in self.servers:
-                 if connection_id in server.clients:
+                if connection_id in server.clients:
                     client = server.clients[connection_id]
                     client.put(msg)
         else:
index 09b9db1bbf3d0f47539a2c4aaea27aacd9c0c18c..93fa68e7f604d7f50887a91c0673104f3731e30c 100644 (file)
@@ -15,7 +15,7 @@ class PlomSocket:
     def send(self, message, silent_connection_break=False):
         """Send via self.socket, encoded/delimited as way recv() expects.
 
-        In detail, all \ and $ in message are escaped with prefixed \,
+        In detail, all \\ and $ in message are escaped with prefixed \\,
         and an unescaped $ is appended as a message delimiter. Then,
         socket.send() is called as often as necessary to ensure
         message is sent fully, as socket.send() due to buffering may
@@ -60,7 +60,7 @@ class PlomSocket:
 
         In detail, socket.recv() is looped over for sequences of bytes
         that can be decoded as a Unicode string delimited by an
-        unescaped $, with \ and $ escapable by \. If a sequence of
+        unescaped $, with \\ and $ escapable by \\. If a sequence of
         characters that ends in an unescaped $ cannot be decoded as
         Unicode, None is returned as its representation. Stop once
         socket.recv() returns nothing.
index 0a74f4e8a4ef8c2397fa99497cce16dc10a1d929..bf3766e416555a65aad4f515b36dd3fa8ff28fbe 100644 (file)
@@ -43,4 +43,3 @@ class PlomWebSocketServer(SimpleWebSocketServer):
 
     def server_close(self):
         self.close()
-
index 97f788bf59c52d6b992b25b083f22a5259afc5bd..5822dada4371170542923c1c63e894e95bc3e1df 100644 (file)
@@ -195,7 +195,7 @@ class Map():
         if width_line != width_map:
             raise ArgError('map line width %s unequal map width %s' % (width_line, width_map))
         self.terrain = self.terrain[:y * width_map] + line +\
-                       self.terrain[(y + 1) * width_map:]
+            self.terrain[(y + 1) * width_map:]
 
     def get_position_index(self, yx):
         return yx.y * self.geometry.size.x + yx.x
@@ -212,7 +212,7 @@ class SourcedMap(Map):
     def __init__(self, source_maps, source_center, radius, get_map):
         self.source_maps = source_maps
         self.radius = radius
-        example_map = get_map(YX(0,0))
+        example_map = get_map(YX(0, 0))
         self.source_geometry = example_map.geometry
         size, self.offset, self.center = \
             self.source_geometry.define_segment(source_center, radius)
@@ -262,16 +262,16 @@ class DijkstraMap(SourcedMap):
                     if self.terrain[j] < self.terrain[i] - 1:
                         self.terrain[i] = self.terrain[j] + 1
                         shrunk = True
-        #print('DEBUG Dijkstra')
-        #line_to_print = []
-        #x = 0
-        #for n in self.terrain:
-        #    line_to_print += ['%3s' % n]
-        #    x += 1
-        #    if x >= self.size.x:
-        #        x = 0
-        #        print(' '.join(line_to_print))
-        #        line_to_print = []
+        # print('DEBUG Dijkstra')
+        # line_to_print = []
+        # x = 0
+        # for n in self.terrain:
+        #     line_to_print += ['%3s' % n]
+        #     x += 1
+        #     if x >= self.size.x:
+        #         x = 0
+        #         print(' '.join(line_to_print))
+        #         line_to_print = []
 
 
 
@@ -281,7 +281,7 @@ class FovMap(SourcedMap):
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
-        self.terrain = '?' * self.size_i #self.size.y * self.size.x
+        self.terrain = '?' * self.size_i
         self[self.center] = '.'
         self.shadow_cones = []
         self.circle_out(self.center, self.shadow_process)
@@ -302,7 +302,7 @@ class FovMap(SourcedMap):
         def in_shadow_cone(new_cone):
             for old_cone in self.shadow_cones:
                 if old_cone[0] <= new_cone[0] and \
-                    new_cone[1] <= old_cone[1]:
+                   new_cone[1] <= old_cone[1]:
                     return True
                 # We might want to also shade tiles whose middle arm is inside a
                 # shadow cone for a darker FOV. Note that we then could not for
@@ -336,9 +336,9 @@ class FovMap(SourcedMap):
                 if unmerged:
                     self.shadow_cones += [cone]
 
-        step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center
+        step_size = (CIRCLE / len(self.circle_out_directions)) / distance_to_center
         number_steps = dir_i * distance_to_center + dir_progress
-        left_arm = correct_arm(step_size/2 + step_size*number_steps)
+        left_arm = correct_arm(step_size / 2 + step_size * number_steps)
         right_arm = correct_arm(left_arm + step_size)
 
         # Optimization potential: left cone could be derived from previous
index 2a87e7d10ea221336cde9dae391b14d5b774b024..a043bde4615f80474aaab11b974ddb2a87a48b0b 100644 (file)
@@ -101,8 +101,8 @@ class Parser:
         tmpl_tokens = signature.split()
         if len(tmpl_tokens) != len(args_tokens):
             raise ArgError('Number of arguments (' + str(len(args_tokens)) +
-                           ') not expected number (' + str(len(tmpl_tokens))
-                           ').')
+                           ') not expected number (' + str(len(tmpl_tokens)) +
+                           ').')
         args = []
         string_string = 'string'
         for i in range(len(tmpl_tokens)):
index 2e12e5a2ec3028f66b1ea7b8b8efbfb7e9196dcf..558b1782f3067b70ca033dd2e3426578a8425772 100644 (file)
@@ -70,8 +70,9 @@ class Task_FLATTEN_SURROUNDINGS(Task):
         pass
 
     def do(self):
-        for yxyx in[self.thing.position] + \
-            list(self.thing.game.map_geometry.get_neighbors_yxyx(self.thing.position).values()):
+        for yxyx in [self.thing.position] + \
+                list(self.thing.game.map_geometry.get_neighbors_yxyx(
+                    self.thing.position).values()):
             if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
                 continue
             self.thing.game.maps[yxyx[0]][yxyx[1]] = '.'
@@ -86,8 +87,8 @@ class Task_PICK_UP(Task):
             raise PlayError('already carrying something')
         nothing_to_pick_up = True
         for t in [t for t in self.thing.game.things
-                  if t != self.thing and t.position == self.thing.position
-                  and t.type_ != 'Player']:
+                  if t != self.thing and t.position == self.thing.position and
+                  t.type_ != 'Player']:
             nothing_to_pick_up = False
             break
         if nothing_to_pick_up:
index a99460974f7dbc8f435f262b3caf8d14be5a9b79..0f058f2af68822cf30650f667e5931a3e1f83562 100644 (file)
@@ -6,7 +6,7 @@ from plomrogue.mapping import YX
 class ThingBase:
     type_ = '?'
 
-    def __init__(self, game, id_=0, position=(YX(0,0),YX(0,0))):
+    def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
         self.game = game
         if id_ == 0:
             self.id_ = self.game.new_thing_id()
index 80a167eea6d5c042c401bf4d93a9482f8ab887f3..466c9b7d72a69f25ed999cf18bf13b2bd1ab5da3 100755 (executable)
@@ -467,8 +467,8 @@ class TUI:
     def reconnect(self):
         self.log_msg('@ attempting reconnect')
         self.send('QUIT')
-        time.sleep(0.1)  # FIXME necessitated by some some strange SSL race
-                         # conditions with ws4py, find out what exactly
+        # necessitated by some strange SSL race conditions with ws4py
+        time.sleep(0.1)  # FIXME find out why exactly necessary
         self.switch_mode('waiting_for_server')
         self.connect()
 
@@ -525,7 +525,7 @@ class TUI:
         if self.mode.name in {'control_tile_draw', 'control_tile_type',
                               'control_pw_type'}:
             self.map_mode = 'protections'
-        elif self.mode.name!= 'edit':
+        elif self.mode.name != 'edit':
             self.map_mode = 'terrain + things'
         if self.mode.shows_info or self.mode.name == 'control_tile_draw':
             player = self.game.get_thing(self.game.player_id)
@@ -724,7 +724,7 @@ class TUI:
             if type(self.game.map_geometry) == MapGeometryHex:
                 indent = 0
                 for line in map_lines_split:
-                    map_lines += [indent*' ' + ''.join(line)]
+                    map_lines += [indent * ' ' + ''.join(line)]
                     indent = 0 if indent else 1
             else:
                 for line in map_lines_split:
@@ -788,7 +788,7 @@ class TUI:
             for i in range(self.size.y):
                 safe_addstr(i,
                             self.window_width * (not self.mode.has_input_prompt),
-                            ' '*self.window_width)
+                            ' ' * self.window_width)
             lines = []
             for line in content.split('\n'):
                 lines += msg_into_lines_of_width(line, self.window_width)
@@ -816,7 +816,7 @@ class TUI:
                 draw_help()
 
         curses.curs_set(False)  # hide cursor
-        curses.use_default_colors();
+        curses.use_default_colors()
         stdscr.timeout(10)
         reset_screen_size()
         self.explorer = YX(0, 0)
@@ -977,7 +977,7 @@ class TUI:
                 if self.mode.mode_switch_on_key(self, key):
                     continue
                 elif key == self.keys['flatten'] and\
-                     'FLATTEN_SURROUNDINGS' in self.game.tasks:
+                    'FLATTEN_SURROUNDINGS' in self.game.tasks:
                     self.send('TASK:FLATTEN_SURROUNDINGS ' + quote(self.password))
                 elif key == self.keys['toggle_map_mode']:
                     self.toggle_map_mode()