Added Event classes to pyrisk.log
[pyrisk.git] / pyrisk / base.py
index 1cdd30f155f143560ee7f7183f93b5f91f238214..38d3b2345810d91dd307d109903ed54e8a26dcec 100644 (file)
@@ -19,7 +19,8 @@
 
 import random
 
-from .log import Logger
+from .log import Logger, BeginGame, EndGame, Killed, StartTurn, DealtCards, \
+    EarnsArmies, SelectTerritory, PlaceArmies, PlayCards, Attack, Conquer
 
 
 class PlayerError (Exception):
@@ -422,7 +423,7 @@ class Player (NameMixin, ID_CmpMixin):
         draw - another notification-only method
         """
         print 'Reporting for %s:\n  %s' \
-            % (self, '\n  '.join(log[self._message_index:]))
+            % (self, '\n  '.join([str(e) for e in log[self._message_index:]]))
         self._message_index = len(log)
     def draw(self, world, log, cards=[]):
         """Only called if you earned a new card (or cards).
@@ -510,7 +511,7 @@ class Engine (ID_CmpMixin):
         for p in self.players:
             p.alive = True
         random.shuffle(self.players)
-        self.log('Game started with %s' % self.players)
+        self.log(BeginGame(self.players))
         self.deck.shuffle()
         self.select_territories()
         self.place_initial_armies()
@@ -535,15 +536,13 @@ class Engine (ID_CmpMixin):
 
         Currently just a notification hook.
         """
-        self.log('Game over.')
+        self.log(EndGame(self.players))
         for p in self.players:
             p.report(self.world, self.log)
     def play_turn(self, player):
         """Work through the phases of player's turn.
         """
-        self.log("%s's turn (territory score: %s)"
-                 % (player, [(p,len(list(p.territories(self.world))))
-                             for p in self.players]))
+        self.log(StartTurn(player, self.players, self.world))
         self.play_cards_and_place_armies(player)
         captures = self.attack_and_fortify(player)
         self.end_of_turn_cards(player, captures)
@@ -567,7 +566,7 @@ class Engine (ID_CmpMixin):
                     break
                 except PlayerError, error:
                     continue
-            self.log('%s selects %s' % (p, t))
+            self.log(SelectTerritory(p, t))
             t.player = p
             t.armies = 1
         # last player has no choice.
@@ -611,7 +610,7 @@ class Engine (ID_CmpMixin):
                 break
             except PlayerError, error:
                 continue
-        self.log('%s places %s' % (player, placements))
+        self.log(PlaceArmies(player, placements))
         for terr_name,armies in placements.items():
             t = self.world.territory_by_name(terr_name)
             t.armies += armies
@@ -621,7 +620,7 @@ class Engine (ID_CmpMixin):
             cards.append(self.deck.pop())
         player.hand.extend(cards)
         player.draw(self.world, self.log, cards)
-        self.log('%s dealt %d cards' % (player, number))
+        self.log(DealtCards(player, number, len(self.deck)))
     def play_cards_and_place_armies(self, player, additional_armies=0):
         cards_required = len(player.hand) >= 5
         error = None
@@ -637,21 +636,17 @@ class Engine (ID_CmpMixin):
             except PlayerError, error:
                 continue
         w_prod,w_terr_prod = self.world.production(player)
-        self.log('%s earned %d armies from territories' % (player, w_prod))
-        if c_prod > 0:
-            self.log('%s played %s, earning %d armies'
-                     % (player, cards, c_prod+sum(c_terr_prod.values())))
+        self.log(EarnsArmies(player, w_prod, w_terr_prod))
         if cards != None:
             for c in cards:
                 player.hand.remove(c)
+            self.log(PlayCards(player, cards, c_prod, c_terr_prod))
         for terr,prod in c_terr_prod.items():
             if terr in w_terr_prod:
                 w_terr_prod[terr] += prod
             else:
                 w_terr_prod[terr] = prod
         self.world.place_territory_production(w_terr_prod)
-        if len(w_terr_prod) > 0:
-            self.log('%s was required to place %s' % (player, w_terr_prod))
         armies = w_prod + c_prod
         self.player_place_armies(player, armies, armies)
     def attack_and_fortify(self, player):
@@ -703,29 +698,25 @@ class Engine (ID_CmpMixin):
         if armies >= source.armies:
             raise PlayerError('%s attacking %s with %d armies, but only %d are available.'
                               % (source, target, armies, source.armies-1))
-        a_dice = sorted([random.randint(1, 6) for i in range(armies)],
+        s_dice = sorted([random.randint(1, 6) for i in range(armies)],
                         reverse=True)
         t_dice = sorted([random.randint(1, 6) for i in range(min(2, target.armies))],
                         reverse=True)
-        a_dead = 0
+        s_dead = 0
         t_dead = 0
-        for a,d in zip(a_dice, t_dice):
+        for a,d in zip(s_dice, t_dice):
             if d >= a:
-                a_dead += 1
+                s_dead += 1
             else:
                 t_dead += 1
-        source.armies -= a_dead
+        source.armies -= s_dead
         target.armies -= t_dead
         if target.armies == 0:
-            self.takeover(source, target, remaining_attackers=armies-a_dead)
-            self.log('%s conquered %s from %s with %d:%d.  Deaths %d:%d.  Remaining %d:%d'
-                     % (source.player, target, source, armies, len(t_dice),
-                        a_dead, t_dead, source.armies, target.armies))
+            self.takeover(source, target, remaining_attackers=armies-s_dead)
+            self.log(Conquer(source, target, s_dice, t_dice, s_dead, t_dead))
             assert target.armies > 0, target
             return True
-        self.log('%s attacked %s from %s with %d:%d.  Deaths %d:%d.  Remaining %d:%d' \
-                     % (source.player, target, source, armies, len(t_dice),
-                        a_dead, t_dead, source.armies, target.armies))
+        self.log(Attack(source, target, s_dice, t_dice, s_dead, t_dead))
         assert target.armies > 0, target
         return False
     def takeover(self, source, target, remaining_attackers):
@@ -753,7 +744,7 @@ class Engine (ID_CmpMixin):
         if len(self.living_players()) > 1:
             while len(killer.hand) > 5:
                 self.play_cards_and_place_armies(killer)
-        self.log('%s killed by %s' % (player, killer))
+        self.log(Killed(player, killer))
         if len(self.living_players()) > 1:
             player.report(self.world, self.log)
             # else the game is over, and killed will hear about this then.