home · contact · privacy
Refactor thing addition / removal.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 8 Dec 2020 20:56:23 +0000 (21:56 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 8 Dec 2020 20:56:23 +0000 (21:56 +0100)
plomrogue/commands.py
plomrogue/game.py
plomrogue/tasks.py
plomrogue/things.py

index dea07173c6181f2233260823977c95766732bd60..770fb2282d6a9f803bfe9210cda905340a041875 100644 (file)
@@ -40,9 +40,7 @@ def cmd_LOGIN(game, nick, connection_id):
         raise GameError('name already in use')
     if game.get_player(connection_id):
         raise GameError('cannot log in twice')
-    t = game.thing_types['Player'](game)
-    t.position = game.spawn_point
-    game.things += [t]  # TODO refactor into Thing.__init__?
+    t = game.add_thing('Player', game.spawn_point)
     t.thing_char = game.get_next_player_char()
     game.sessions[connection_id] = {
         'thing_id': t.id_,
@@ -57,7 +55,6 @@ def cmd_LOGIN(game, nick, connection_id):
         t.position = s.position
         break
     game.changed = True
-    game.changed_fovs = True
 cmd_LOGIN.argtypes = 'string'
 
 def cmd_BECOME_ADMIN(game, password, connection_id):
@@ -230,17 +227,8 @@ def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
     if thing_type not in game.thing_types:
         raise GameError('illegal thing type %s' % thing_type)
     _ = game.get_map(big_yx)
-    t_old = None
-    if thing_id > 0:
-        t_old = game.get_thing(thing_id)
-    t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
-                                                                       little_yx))
-    if t_old:
-        game.things[game.things.index(t_old)] = t_new
-    else:
-        game.things += [t_new]
+    game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
     game.changed = True
-    game.changed_fovs = True
 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
 
 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
index 6e8afdf83182dded8a25c735b94dc7cdc2a7095a..10fcb12b19f3c7f644d11eb8376368d99c15028c 100755 (executable)
@@ -210,6 +210,22 @@ class Game(GameBase):
                 return '/O  O\\' + '| oo |' + '\\>--</'
         return None
 
+    def remove_thing(self, t):
+        self.things.remove(t)
+        self.changed_fovs = True
+
+    def add_thing(self, type_, position, id_=0):
+        t_old = None
+        if id_ > 0:
+            t_old = self.get_thing(id_)
+        t = self.thing_types[type_](self, id_=id_, position=position)
+        if t_old:
+            self.things[self.things.index(t_old)] = t
+        else:
+            self.things += [t]
+        self.changed_fovs = True
+        return t
+
     def send_gamestate(self, connection_id=None):
         """Send out game state data relevant to clients."""
 
@@ -294,8 +310,7 @@ class Game(GameBase):
                 t = self.get_player(connection_id)
                 if hasattr(t, 'name'):
                     self.io.send('CHAT ' + quote(t.name + ' left the map.'))
-                self.things.remove(t)
-                self.changed_fovs = True
+                self.remove_thing(t)
                 to_delete += [connection_id]
         for connection_id in to_delete:
             del self.sessions[connection_id]
index ef5fc5cda730be8c60d8b33f1c67252ddc4136ed..08a4a4f58119d2818a89a18a5b5e2577f230b945 100644 (file)
@@ -123,15 +123,13 @@ class Task_DROP(Task):
                       if t.type_ == 'BottleDeposit'
                       and t.position == self.thing.position]:
                 t.accept()
-                self.thing.game.things.remove(self.thing.carrying)
-                self.thing.game.changed_fovs = True
+                self.thing.game.remove_thing(self.thing.carrying)
                 break
         elif self.thing.carrying.type_ == 'Hat':
             for t in [t for t in self.thing.game.things
                       if t.type_ == 'HatRemixer'
                       and t.position == self.thing.position]:
                 t.accept(self.thing.carrying)
-                self.thing.game.changed_fovs = True
                 break
         self.thing.carrying = None
 
@@ -232,13 +230,10 @@ class Task_WEAR(Task):
 
     def do(self):
         if self.thing.name in self.thing.game.hats:
-            t = self.thing.game.thing_types['Hat'](self.thing.game,
-                                                   position=self.thing.position)
-            self.thing.game.things += [t]
+            t = self.thing.game.add_thing('Hat', self.thing.position)
             t.design = self.thing.game.hats[self.thing.name]
             del self.thing.game.hats[self.thing.name]
             self.thing.send_msg('CHAT "You drop your hat."')
-            self.game.changed_fovs = True  # thing addition
             for remixer in [t for t in self.thing.game.things
                             if t.type_ == 'HatRemixer'
                             and t.position == self.thing.position]:
index 23f66ad4a5bb25b5fe297554992116a338879556..e1a9bd3a2d18b0d8b1b64943b6f055dfd11508e6 100644 (file)
@@ -96,11 +96,8 @@ class ThingSpawner(Thing):
         for t in [t for t in self.game.things
                   if t != self and t.position == self.position]:
             return
-        t = self.game.thing_types[self.child_type](self.game,
-                                                   position=self.position)
-        self.game.things += [t]
+        self.game.add_thing(self.child_type, self.position)
         self.game.changed = True
-        self.game.changed_fovs = True
 
 
 
@@ -302,8 +299,7 @@ class Thing_BottleDeposit(Thing):
         if self.bottle_counter >= 3:
             self.bottle_counter = 0
             choice = random.choice(['MusicPlayer', 'Hat'])
-            t = self.game.thing_types[choice](self.game, position=self.position)
-            self.game.things += [t]
+            self.game.add_thing(choice, self.position)
             msg = 'here is a gift as a reward for ecological consciousness –'
             if choice == 'MusicPlayer':
                 msg += 'pick it up and then use "command thing" on it!'
@@ -311,7 +307,6 @@ class Thing_BottleDeposit(Thing):
                 msg += 'pick it up and then use "(un-)wear" on it!'
             self.sound('BOTTLE DEPOSITOR', msg)
             self.game.changed = True
-            self.game.changed_fovs = True
 
     def accept(self):
         self.bottle_counter += 1