def run(args): ''' Main loop for the server ''' GAME.load_from_files(args.board) stop = False while not stop: post('tick') stop = process_queue() asyncore.poll(0.01)
def run(args): ''' Main loop for the client ''' post('join', host=HOST, port=PORT) stop = False while not stop: post('tick') stop = process_queue() asyncore.poll(0.002)
def test_process_queue(self): connect('test', func_handler) event_dict = {'args1': 1, 'arg2':2} post('test', **event_dict) post('test', event_dict) process_queue() self.failUnlessEqual(event_dict, Q.popleft()) self.failUnlessEqual(event_dict, Q.popleft())
def join(self, event_dict): """Try to join a remote server.""" # XXX We should reconnect if the connection goes down but # prevent a reconnection if connection is ok. host = event_dict['host'] port = event_dict['port'] self.connect((host, port)) LOGGER.debug('Connecting to %s:%d', host, port) post('game-request-update')
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 process_queue(self): '''Process the event queue.''' # We trust we don't get stuck in this loop because the # network is much slower to fill the queue than we are able to # empty it. while True: data = _EndPoint._get_from_in_buffer(self) if not data: break event_dict = json.loads(data) if not self.check_in_event(event_dict): return LOGGER.debug("Got event dictionary") event = event_dict['event'] post(event, event_dict)
def _cache(resource_name, *args, **kargs): if not isinstance(resource_name, basestring): raise RuntimeError('cache._cache(): invalid name for a cached ' 'object') try: return _CACHE[hash(loader), resource_name, args, tuple(kargs.items())] except KeyError: try: cached_obj = loader(resource_name, *args, **kargs) except IOError as why: if why.errno == 2: # There is a missing file, ask the server... post('resource-request', name=resource_name) else: raise else: _CACHE[hash(loader), resource_name, args, tuple(kargs.items())] = cached_obj return cached_obj
def load_board_from_tmx(self, bname, tmx_map): ''' Load and return a board from a string ''' try: tmx_map = ElementTree.fromstring(tmx_map) except: raise ParseError("Error parsing '%s'" % bname) events = [] # Save basic board attribute size = int(tmx_map.attrib['width']), int(tmx_map.attrib['height']) tilewidth = int(tmx_map.attrib['tilewidth']) if tilewidth != int(tmx_map.attrib['tileheight']): raise ParseError("tilewidth != tileheight: tiles must be square") # Append a new board event board_event = ('game-request-board-new', dict(name=bname, size=size)) events.append(board_event) # Find pawn object group pawn_layer = _get_object_layer(tmx_map, 'pawns') if pawn_layer is not None: for pawn in pawn_layer.findall('object'): name = pawn.attrib['name'] # Minimum width and height must be 1 size = (max(int(pawn.attrib['width']) // tilewidth, 1), max(int(pawn.attrib['height']) // tilewidth, 1)) pos = (int(pawn.attrib['x']) // tilewidth, int(pawn.attrib['y']) // tilewidth) try: initiative = int(_get_property(pawn, 'initiative')) except KeyError: raise ParseError("Error parsing pawn '%s': missing " "initiative value" % name) new_pawn_event = ('game-request-pawn-new', dict(bname=bname, pname=name, initiative=initiative, pos=pos, size=size)) events.append(new_pawn_event) # Now add the board to _maps self._maps[bname] = tmx_map for event in events: post(event[0], event[1])
def clear(self): for bname in self.game.boards: self.game.del_board(bname) post('game-event-board-del', name=bname)
def test_post(self): # Post a single event connect('test', func_handler) event_dict = {'event': 'test', 'id': post('test')} self.failUnlessEqual(event_dict, _QUEUE.popleft())
def tick(self): ''' Get all the events from the in queue ''' while not self.in_queue.empty(): event_dict = self.in_queue.get() event = event_dict.pop('event') self.posted_events.add(post(event, event_dict))
def del_board(self, event_dict): self.boards.remove(event_dict['name']) LOGGER.info("Deleted board with name '%s'", event_dict['name']) post('game-event-board-del', event_dict)
def create_pawn(self, event_dict): LOGGER.info("Created pawn '%s' within board '%s'", event_dict['pname'], event_dict['bname']) post('game-event-pawn-new', event_dict)
def move_pawn(self, event_dict): LOGGER.info("Moved pawn '%s' at pos (%d, %d) within board '%s'", event_dict['pname'], event_dict['pos'][0], event_dict['pos'][1], event_dict['bname']) post('game-event-pawn-moved', event_dict)
def create_board(self, event_dict): self.boards.add(event_dict['name']) LOGGER.info("Created board with name '%s' and size (%d, %d)", event_dict['name'], event_dict['size'][0], event_dict['size'][1]) post('game-event-board-new', 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 del_pawn(self, event_dict): LOGGER.info("Removed pawn '%s' from board '%s'", event_dict['pname'], event_dict['bname']) post('game-event-pawn-del', 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_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 request_update(self): boards = self._dump_game() post('game-event-update', tmxs=boards)
def clear(self): for bname in self.boards: post('game-event-board-del', name=bname) self.boards.clear() LOGGER.info("All boards have been deleted")