1bc9c38e3d35c31937b3e8ca60605dbef9639b27
[r15k.git] / r15k / engine / game.py
1 # -*- coding: utf-8 -*-
2
3 import logging
4
5 from r15k.engine.ids import new_id
6 from r15k.engine.turn import Turn
7
8 logger = logging.getLogger('r15k.game')
9
10 class Game:
11 def __init__(self, world=None):
12 self.world = world
13 self.oldTurns = []
14 self.currentTurn = Turn()
15 self.isRunning = True
16 self.uid = new_id()
17 self.players = {}
18
19 def update(self):
20 logger.info("Updating.")
21 self.currentTurn.resolve(self.world)
22 self.oldTurns.append(self.currentTurn)
23 self.currentTurn = Turn()
24 return self.currentTurn
25
26 def dump(self):
27 data = {}
28 data["turn"] = self.currentTurn.dump()
29 data["world"] = self.world.dump()
30 data["players"] = dict([(k, v.dump()) for k, v in self.players.iteritems()])
31 data["uid"] = self.uid
32 return data
33
34 def restore(self, data):
35 self.currentTurn.restore(data["turn"])
36 self.world.restore(data["world"])
37 self.players = {}
38 for k, d in data.get("players", {}).iteritems():
39 p = Player()
40 p.restore(d)
41 self.players[k] = p
42 self.uid = data["uid"]
43
44 class Player:
45 def __init__(self, name, user):
46 self.name = name
47 self.user = user
48 self.uid = new_id()
49
50 def dump(self):
51 data = {}
52 data['name'] = self.name
53 data['uid'] = self.uid
54 data['user'] = self.user
55 return data
56
57 def restore(self, data):
58 self.name = data['name']
59 self.uid = data['uid']
60 self.user = data['user']
61