Progress?
[progcomp2013.git] / qchess / src / game.py
index 934c043..e3f1c7c 100644 (file)
@@ -1,4 +1,15 @@
 
+
+log_file = None
+
+def log(s):
+       if log_file != None:
+               import datetime
+               log_file.write(str(datetime.datetime.now()) + " : " + s + "\n")
+
+
+       
+
 # A thread that runs the game
 class GameThread(StoppableThread):
        def __init__(self, board, players):
@@ -36,6 +47,7 @@ class GameThread(StoppableThread):
                                                p2.update(result) # Inform players of what happened
 
 
+                                       log(result)
 
                                        target = self.board.grid[x][y]
                                        if isinstance(graphics, GraphicsThread):
@@ -64,9 +76,12 @@ class GameThread(StoppableThread):
                                        if self.stopped():
                                                break
 
-                                       result = self.board.update_move(x, y, x2, y2)
+                                       self.board.update_move(x, y, x2, y2)
+                                       result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
                                        for p2 in self.players:
-                                               p2.update(str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)) # Inform players of what happened
+                                               p2.update(result) # Inform players of what happened
+
+                                       log(result)
 
                                        if isinstance(graphics, GraphicsThread):
                                                with graphics.lock:
@@ -110,10 +125,80 @@ class GameThread(StoppableThread):
                for p2 in self.players:
                        p2.quit(self.final_result)
 
+               log(self.final_result)
+
                graphics.stop()
 
        
+# A thread that replays a log file
+class ReplayThread(GameThread):
+       def __init__(self, players, src):
+               self.board = Board(style="agent")
+               GameThread.__init__(self, self.board, players)
+               self.src = src
+
+               self.ended = False
+       
+       def run(self):
+               i = 0
+               phase = 0
+               for line in self.src:
+
+                       if self.stopped():
+                               self.ended = True
+                               break
+
+                       with self.lock:
+                               self.state["turn"] = self.players[i]
+
+                       line = line.split(":")
+                       result = line[len(line)-1].strip(" \r\n")
+                       log(result)
+
+                       try:
+                               self.board.update(result)
+                       except:
+                               self.ended = True
+                               self.final_result = result
+                               if isinstance(graphics, GraphicsThread):
+                                       graphics.stop()
+                               break
+
+                       [x,y] = map(int, result.split(" ")[0:2])
+                       target = self.board.grid[x][y]
+
+                       if isinstance(graphics, GraphicsThread):
+                               if phase == 0:
+                                       with graphics.lock:
+                                               graphics.state["moves"] = self.board.possible_moves(target)
+                                               graphics.state["select"] = target
+
+                                       time.sleep(turn_delay)
+
+                               elif phase == 1:
+                                       [x2,y2] = map(int, result.split(" ")[3:5])
+                                       with graphics.lock:
+                                               graphics.state["moves"] = [[x2,y2]]
+
+                                       time.sleep(turn_delay)
+
+                                       with graphics.lock:
+                                               graphics.state["select"] = None
+                                               graphics.state["dest"] = None
+                                               graphics.state["moves"] = None
+                                               
+
+
+                       
+
+                       for p in self.players:
+                               p.update(result)
+                       
+                       phase = (phase + 1) % 2
+                       if phase == 0:
+                               i = (i + 1) % 2
 
+               
 
 def opponent(colour):
        if colour == "white":

UCC git Repository :: git.ucc.asn.au