a32438170e3c5663366850cde744027507b57ceb
[naja.git] / naja / gamestate.py
1 """
2 The current game state.
3 """
4
5 try:
6     import yaml
7 except ImportError:
8     yaml = None
9     import json
10
11 from naja.gameboard import GameBoard
12 from naja.resources import resources
13
14
15 def load_location_deck(name):
16     if yaml:
17         with resources.get_file('location_decks', '%s.yaml' % name) as deck_fp:
18             return yaml.safe_load(deck_fp)
19     else:
20         with resources.get_file('location_decks', '%s.json' % name) as deck_fp:
21             return json.load(deck_fp)
22
23
24 class GameState(object):
25     """
26     Naja game state.
27     """
28
29     def __init__(self, gameboard):
30         self.gameboard = gameboard
31
32     @classmethod
33     def new(cls, deck='standard', **kw):
34         deck = load_location_deck(deck)
35         return cls(GameBoard.new_game(deck, **kw))
36
37     @classmethod
38     def load(cls, data):
39         return cls(GameBoard.import_game(data))
40
41     @property
42     def player(self):
43         return self.gameboard.player
44
45     @property
46     def board_locations(self):
47         return self.gameboard.board_locations