def on_leave(self): """Leave lobby""" assert self.lobby_id assert self.listener_job lobby = Lobby.query.get(self.lobby_id) # If we're auth'd do actual leave, otherwise just kill job and lobby_id if g.player: # If we're owner delete, if not just leave if lobby.owner is g.player: db.session.remove(lobby) db.session.commit() # Broadcast delete to lobby and lobby listing self.broadcast_event('/lobby/', 'delete', self.lobby_id) self.broadcast_event('/lobby/%d' % self.lobby_id, 'delete') else: lobby.leave(g.player) db.session.commit() # Broadcast leave to lobby and lobby listing self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) # Update ACL self.del_acl_method('on_set_team') if 'on_set_class' in self.allowed_methods: self.del_acl_method('on_set_class') self.del_acl_method('on_toggle_ready') self.del_acl_method('on_leave') self.add_acl_method('on_join') self.add_acl_method('on_create_lobby') self.unsubscribe('/lobby/%d' % self.lobby_id) current_app.logger.info('Player %s left Lobby %d', (g.player.id if g.player else 'Anonymous', self.lobby_id)) self.lobby_id = None return True
def on_get_lobby_listing(self): """Get full lobby listing""" lobbies = Lobby.query.all() lobby_listing = [make_lobby_item_dict(l) for l in lobbies] current_app.logger.info('Player: %s got lobby listing to LobbiesNamespace' % g.player.id if g.player else 'Anonymous') return True, lobby_listing
def on_join(self, lobby_id): """Join lobby""" lobby = Lobby.query.get(lobby_id) if g.player: # Leave or delete old lobbies lobby_deletes = leave_or_delete_all_lobbies(g.player) lobby.join(g.player) db.session.commit() # Broadcast update to the joined lobby and lobby listing self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d' % lobby_id, 'update', make_lobby_dict(lobby)) # Send leave or deletes [self.broadcast_leave_or_delete(*l_d) for l_d in lobby_deletes] # Update ACL self.add_acl_method('on_set_team') self.del_acl_method('on_create_lobby') self.del_acl_method('on_join') if lobby.owner == g.player: self.add_acl_method('on_kick') self.add_acl_method('on_set_lobby_name') self.add_acl_method('on_set_team_name') self.add_acl_method('on_leave') # Set lobby id and start listening on redis self.lobby_id = lobby_id self.subscribe('/lobby/%d' % lobby_id) current_app.logger.info('Player %s joined Lobby %d', (g.player.id if g.player else 'Anonymous', lobby_id)) return True, make_lobby_dict(lobby)
def on_subscribe(self): """Subscribe to lobby CUD listener""" lobby_listing = [make_lobby_item_dict(l) for l in Lobby.query.all()] self.subscribe('/lobby/') current_app.logger.info('Player: %s subscribed to LobbiesNamespace' % g.player.id if g.player else 'Anonymous') return True, lobby_listing
def on_leave(self): """Leave lobby""" assert self.lobby_id lobby = Lobby.query.get(self.lobby_id) # If we're auth'd do actual leave, otherwise just kill job and lobby_id if g.player: # If we're owner delete, if not just leave if lobby.owner is g.player: db.session.remove(lobby) db.session.commit() # Broadcast delete to lobby and lobby listing self.broadcast_event('/lobby/', 'delete', self.lobby_id) self.broadcast_event('/lobby/%d' % self.lobby_id, 'delete') else: lobby.leave(g.player) db.session.commit() # Broadcast leave to lobby and lobby listing self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) # Update ACL self.del_acl_method('on_set_team') if 'on_set_class' in self.allowed_methods: self.del_acl_method('on_set_class') self.del_acl_method('on_toggle_ready') self.del_acl_method('on_leave') self.add_acl_method('on_join') self.add_acl_method('on_create_lobby') self.unsubscribe('/lobby/%d' % self.lobby_id) current_app.logger.info('Player %s left Lobby %d', (g.player.id if g.player else 'Anonymous', self.lobby_id)) self.lobby_id = None return True
def on_join(self, lobby_id): """Join lobby""" lobby_id = int(lobby_id) lobby = Lobby.query.get(lobby_id) if g.player: # Leave or delete old lobbies lobby_deletes = leave_or_delete_all_lobbies(g.player) lobby.join(g.player) db.session.commit() # Broadcast update to the joined lobby and lobby listing self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d' % lobby_id, 'update', make_lobby_dict(lobby)) # Send leave or deletes [self.broadcast_leave_or_delete(*l_d) for l_d in lobby_deletes] # Update ACL self.add_acl_method('on_set_team') self.del_acl_method('on_create_lobby') self.del_acl_method('on_join') if lobby.owner == g.player: self.add_acl_method('on_kick') self.add_acl_method('on_set_lobby_name') self.add_acl_method('on_set_team_name') self.add_acl_method('on_leave') # Set lobby id and start listening on redis self.lobby_id = lobby_id self.subscribe('/lobby/%d' % lobby_id) current_app.logger.info('Player %s joined Lobby %d', (g.player.id if g.player else 'Anonymous', lobby_id)) return True, make_lobby_dict(lobby)
def on_get_lobby_listing(self): """Get full lobby listing""" lobbies = Lobby.query.all() lobby_listing = [make_lobby_item_dict(l) for l in lobbies] current_app.logger.info( 'Player: %s got lobby listing to LobbiesNamespace' % g.player.id if g.player else 'Anonymous') return True, lobby_listing
def broadcast_leave_or_delete(self, lobby, is_delete): """Broadcast leave or delete to redis""" if is_delete: self.broadcast_event('/lobby/', 'delete', lobby.id) self.broadcast_event('/lobby/%d', 'delete', lobby.id) else: self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby))
def on_create_lobby(self, name, server_address, rcon_password, game_map, game_type): """Create and join lobby""" assert g.player assert not self.lobby_id # Check server if current_app.config.get('RCON_CHECK_SERVER', True): try: sr = connect_rcon(server_address, rcon_password) sq = connect_query(server_address) except RconException: return False, 'bad_rcon_pass' except Exception: return False, 'server_error' else: if not check_map(sr): return False, 'map_dne' # TODO: ask if you want to kick players if not check_players(sq): return False, 'players' sr.close() sq.close() # Leave or delete old lobbies lobby_deletes = leave_or_delete_all_lobbies(g.player) # TODO: pull/generate password from list if game_type == 'highlander': lobby = HighlanderLobby(name, g.player, server_address, game_map, 'password') elif game_type == 'sixes': lobby = SixesLobby(name, g.player, server_address, game_map, 'password') else: return False, 'bad_game_type' lobby.join(g.player) db.session.add(lobby) db.session.commit() # Send event to redis self.broadcast_event('/lobby/', 'create', make_lobby_item_dict(lobby)) # Send leave or deletes [self.broadcast_leave_or_delete(*l_d) for l_d in lobby_deletes] # Update ACL self.add_acl_method('on_set_team') self.add_acl_method('on_leave') self.del_acl_method('on_create_lobby') self.del_acl_method('on_join') self.add_acl_method('on_kick') self.add_acl_method('on_set_lobby_name') self.add_acl_method('on_set_team_name') # Set lobby id and start listening on redis self.lobby_id = lobby.id self.subscribe('/lobby/%d' % lobby.id) current_app.logger.info('Player: %d created Lobby %d' % (g.player.id, lobby.id)) return True, lobby.id
def on_set_lobby_name(self, name): assert g.player assert self.lobby_id lobby = Lobby.query.get(self.lobby_id) assert lobby.owner == g.player if name is None or len(name) == 0: return False, 'name_none' lobby.name = name db.session.commit() self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby))
def on_set_class(self, class_id): """Set class in lobby""" assert self.lobby_id assert g.player lobby = Lobby.query.get(self.lobby_id) lobby.set_class(g.player, class_id) db.session.commit() # Broadcast redis update self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) current_app.logger.info('Player %d set class to %s', (g.player.id, class_id if class_id else 'Random')) return True
def on_set_class(self, class_id): """Set class in lobby""" assert self.lobby_id assert g.player lobby = Lobby.query.get(self.lobby_id) lobby.set_class(g.player, class_id) db.session.commit() # Broadcast redis update self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) current_app.logger.info('Player: %d set class to %s', (g.player.id, class_id if class_id else 'Random')) return True
def on_set_lobby_name(self, name): assert g.player assert self.lobby_id lobby = Lobby.query.get(self.lobby_id) assert lobby.owner == g.player if name is None or len(name) == 0: return False, 'name_none' lobby.name = name db.session.commit() self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) current_app.logger.info('Player: %d set Lobby: %d\'s name to `%s`' % (g.player.id, self.lobby_id, name)) return True
def on_kick(self, player_id): """Kick a player from the lobby""" assert g.player assert self.lobby_id lobby = Lobby.query.get(self.lobby_id) assert lobby.owner == g.player player = Player.query.get(player_id) if player is None: return False, 'player_dne' if not lobby.has_player(player): return False, 'player_dne_lobby' lobby.leave(player) db.session.commit() self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) return True
def on_kick(self, player_id): """Kick a player from the lobby""" assert g.player assert self.lobby_id lobby = Lobby.query.get(self.lobby_id) assert lobby.owner == g.player player = Player.query.get(player_id) if player is None: return False, 'player_dne' if not player in lobby: return False, 'player_dne_lobby' lobby.leave(player) db.session.commit() self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) current_app.logger.info('Player: %d kicked Player: %d from Lobby: %d' % (g.player.id, player_id, self.lobby_id)) return True
def on_set_team(self, team_id): """Set team in lobby""" assert self.lobby_id assert g.player lobby = Lobby.query.get(self.lobby_id) lobby.set_team(g.player, team_id) db.session.commit() # Broadcast redis update self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) # Update ACL if team_id is not None: self.add_acl_method('on_set_class') self.add_acl_method('on_toggle_ready') else: self.del_acl_method('on_set_class') self.del_acl_method('on_toggle_ready') current_app.logger.info('Player %d set team to %s', (g.player.id, team_id if team_id else 'Spectator')) return True
def on_set_team(self, team_id): """Set team in lobby""" assert self.lobby_id assert g.player lobby = Lobby.query.get(self.lobby_id) lobby.set_team(g.player, team_id) db.session.commit() # Broadcast redis update self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) # Update ACL if team_id is not None: self.add_acl_method('on_set_class') self.add_acl_method('on_toggle_ready') else: self.del_acl_method('on_set_class') self.del_acl_method('on_toggle_ready') current_app.logger.info('Player: %d set team to %s', (g.player.id, team_id if team_id else 'Spectator')) return True
def disconnect(self, *args, **kwargs): """Disconnect handling""" # If auth'd and in a lobby, leave the lobby if g.player and self.lobby_id: lobby = Lobby.query.get(self.lobby_id) # If owner, this means delete, otherwise just leave if lobby.owner is g.player: db.session.remove(lobby) db.session.commit() self.broadcast_event('/lobby/', 'delete', lobby_id) self.broadcast_event('/lobby/%d' % lobby_id, 'delete') else: lobby.leave(g.player) db.session.commit() self.broadcast_event('/lobby/', 'update', make_lobby_item_dict(lobby)) self.broadcast_event('/lobby/%d', 'update', make_lobby_dict(lobby)) current_app.logger.info('Player: %s disconnected from LobbyNamespace' % g.player.id if g.player else 'Anonymous') # Do real disconnect logic super(LobbyNamespace, self).disconnect(*args, **kwargs)