def __init__(self): self.clients = {} #organized {client_id: client_obj} self.total_clients = 0 self.world_lists = WordManager( "free") #a dict with the words preloaded self.lobbys = {} self.total_game_requests = 0 self.looping_call = LoopingCall(self.remote_post, e.CopyableEvent()) self.looping_call.start(5.0)
def notify(self, event): """ updates lobby accordingly if the event is relevant. Then posts to all players. If the game has started, funnels event into game. """ if self.game: self.game.post(event) #self.obs_post(event) else: if isinstance(event, e.QuitEvent): client_id = event.client_id self.players.remove(self.server.clients[client_id]) self.remove_client_from_waiting(client_id) self.organizer.delete_node(client_id) self.new_lineup_event() elif isinstance(event, e.ToHandoffToEvent): self.organizer.set_next(event.my_id, event.to_handoff_to) self.remove_client_from_waiting(event.my_id) self.new_lineup_event() elif isinstance(event, e.NumberSharingDeviceEvent): print "recieved: ", event.name self.server.clients[ event. client_id].number_sharing_device = event.number_sharing_device elif isinstance(event, e.StartGameRequestEvent): self.post(e.CopyableEvent()) #make sure everyone is still here #(post handles if someone left) if self.organizer.is_perfect_circle() and len( self.players) > 0: self.post(e.GameStartEvent(self.setup_teams())) #SEE circle_graph.client_id_lists() to understand why indexing & slicing player_id_list = self.organizer.client_id_lists()[0][0:-1] self._setup_list_for_multiple_players(player_id_list) self.game = setup_catch_phrase( self.players, self.word_list, player_id_list, self.game_over_callback, self.round_time, self.leeway_time, self.player_observers) elif self.waiting == []: #was not an acceptable circle. If waiting is empty we should #return cycles. If waiting is not empty, they should be able to #figure out the game can't start while we're still waiting on people. # #but seriously don't send out WrongOrderingEvent if it's not emtpy #because that takes the spot of the waiting_on_following_players_list #or at least at time of writing, it does. self.post( e.WrongOrderingEvent(self.organizer.visual_strings())) self.post(event) # sent to everyone!
def post(self, event): if not self.looping_call_clients: #setup client cleanup self.looping_call_clients = LoopingCall( self.post, e.CopyableEvent()) #check for pulse of clients self.looping_call_clients.start(15.0) #every 15 seconds is_tick_event = isinstance(event, e.TickEvent) if (not self.__in_loop) and is_tick_event: self.__in_loop = True while len(self.event_queue) > 0: event = self.event_queue.popleft() is_game_over = self._single_event_notify(event) if is_game_over: break self.event_queue.clear() self.__in_loop = False elif not is_tick_event: self.event_queue.append(event)
def dead_client(self, client): self.post(e.CopyableEvent()) #post takes care of dead clients