예제 #1
0
파일: game.py 프로젝트: ciappi/Yaranullin
class TestGame(unittest.TestCase):

    def setUp(self):
        self.game = Game()

    def test_create_board(self):
        board = self.game.create_board('Nasty Dungeon', (1000, 2000))
        self.assertIn(board.name, self.game.boards)
        self.assertIs(board, self.game.boards[board.name])

    def test_del_board(self):
        board = self.game.create_board('Nasty Dungeon', (1000, 2000))
        self.game.del_board(board.name)
        self.assertNotIn(board.name, self.game.boards)
예제 #2
0
 def __init__(self):
     self.game = Game()
     self.tmx_wrapper = TmxWrapper()
     connect('game-request-board-new', self.create_board)
     connect('game-request-board-del', self.del_board)
     connect('game-request-pawn-new', self.create_pawn)
     connect('game-request-pawn-move', self.move_pawn)
     connect('game-request-pawn-del', self.del_pawn)
     connect('game-request-update', self.request_update)
     LOGGER.debug("GameWrapper initialized")
예제 #3
0
파일: game.py 프로젝트: ciappi/Yaranullin
 def setUp(self):
     self.game = Game()
예제 #4
0
class GameWrapper(object):

    def __init__(self):
        self.game = Game()
        self.tmx_wrapper = TmxWrapper()
        connect('game-request-board-new', self.create_board)
        connect('game-request-board-del', self.del_board)
        connect('game-request-pawn-new', self.create_pawn)
        connect('game-request-pawn-move', self.move_pawn)
        connect('game-request-pawn-del', self.del_pawn)
        connect('game-request-update', self.request_update)
        LOGGER.debug("GameWrapper initialized")

    def request_update(self):
        boards = self._dump_game()
        post('game-event-update', tmxs=boards)

    def _dump_game(self):
        boards = {}
        for name in self.game.boards:
            board = self.tmx_wrapper.get_tmx_board(name)
            if board:
                boards[name] = board
            else:
                LOGGER.error("Unable to dump board '%s' to a string", name)
        return boards

    def load_from_files(self, files):
        ''' Load a board and its pawns from a tmx file '''
        for tmx in files:
            try:
                self.tmx_wrapper.load_board_from_file(tmx)
            except IOError:
                LOGGER.exception("Unable to open file '%s'", tmx)
            except ParseError:
                LOGGER.exception("Unable to parse file '%s'", tmx)
            else:
                LOGGER.info("Loaded board from file '%s'", tmx)

    def create_board(self, event_dict):
        name = event_dict['name']
        size = event_dict['size']
        board = self.game.create_board(name, size)
        if board:
            post('game-event-board-new', event_dict)

    def del_board(self, event_dict):
        board = self.game.del_board(event_dict['name'])
        if board:
            post('game-event-board-del', event_dict)

    def create_pawn(self, event_dict):
        bname = event_dict['bname']
        pname = event_dict['pname']
        initiative = event_dict['initiative']
        pos = event_dict['pos']
        size = event_dict['size']
        pawn = self.game.create_pawn(bname, pname, initiative, pos, size)
        if pawn:
            post('game-event-pawn-new', event_dict)

    def move_pawn(self, event_dict):
        bname = event_dict['bname']
        pname = event_dict['pname']
        pos = event_dict['pos']
        try:
            size = event_dict['size']
        except KeyError:
            size = None
        pawn = self.game.move_pawn(bname, pname, pos, size)
        if pawn:
            post('game-event-pawn-moved', event_dict)

    def del_pawn(self, event_dict):
        bname = event_dict['bname']
        pname = event_dict['pname']
        pawn = self.game.del_pawn(bname, pname)
        if pawn:
            post('game-event-pawn-del', event_dict)

    def clear(self):
        for bname in self.game.boards:
            self.game.del_board(bname)
            post('game-event-board-del', name=bname)