async def change_location(sid, location): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] if room.creator != user: logger.warning(f"{user.name} attempted to change location") return old_location = room.get_active_location(dm=True) sio.leave_room(sid, old_location.get_path(), namespace="/planarally") room.dm_location = location new_location = room.get_active_location(dm=True) sio.enter_room(sid, new_location.get_path(), namespace="/planarally") await load_location(sid, new_location) room.player_location = location for room_player in room.players: for psid in state.get_sids(user=room_player.player, room=room): sio.leave_room(psid, old_location.get_path(), namespace="/planarally") sio.enter_room(psid, new_location.get_path(), namespace="/planarally") await load_location(psid, new_location)
async def change_location(sid: int, data: Dict[str, str]): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to change location") return # Send an anouncement to show loading state for room_player in pr.room.players: if not room_player.player.name in data["users"]: continue for psid in game_state.get_sids(player=room_player.player, room=pr.room): await sio.emit("Location.Change.Start", room=psid, namespace="/planarally") new_location = Location[data["location"]] for room_player in pr.room.players: if not room_player.player.name in data["users"]: continue for psid in game_state.get_sids(player=room_player.player, room=pr.room): sio.leave_room( psid, room_player.active_location.get_path(), namespace="/planarally" ) sio.enter_room(psid, new_location.get_path(), namespace="/planarally") await load_location(psid, new_location) room_player.active_location = new_location room_player.save()
async def connect(sid, environ): user = await authorized_userid(environ["aiohttp.request"]) if user is None: await sio.emit("redirect", "/", room=sid, namespace=GAME_NS) return ref = { k.split("=")[0]: k.split("=")[1] for k in unquote(environ["QUERY_STRING"]).strip().split("&") } try: room = ( Room.select().join(User).where((Room.name == ref["room"]) & (User.name == ref["user"]))[0]) except IndexError: return False else: for pr in room.players: if pr.player == user: if pr.role != Role.DM and room.is_locked: return False break else: return False pr = PlayerRoom.get(room=room, player=user) await game_state.add_sid(sid, pr) logger.info(f"User {user.name} connected with identifier {sid}") sio.enter_room(sid, pr.active_location.get_path(), namespace=GAME_NS)
def on_connect_to_game(self, sid, data): game = models.Game.query.filter_by(uuid=data['gameUUID']).first() members_user_id = [member.user_id for member in game.members] current_user = models.User.query.filter_by(id=data['userId']).first() game_data = { 'board_size': game.size, 'game_id': game.id, 'author': game.author, 'steps': get_steps(game.members) } not_finished_condition = game.finished_datetime is None if not_finished_condition: if current_user.id in members_user_id: self.enter_room(sid, room=data['gameUUID']) self.emit('game_info', game_data, room=sid) elif game.members.count() == 1: member = models.Member(game_id=game.id,user_id=current_user.id) member.save() sio.enter_room(sid, data['gameUUID']) self.emit('game_info', game_data, room=sid) else: self.emit('cant_connect', {'message': 'Create new game or choose another!'}, room=sid) else: game_data['finished'] = 'Game finished' self.emit('game_info', game_data, room=sid)
def create_or_join(sid, data): sio.enter_room(sid, data) try: connected_particpants[data].append(sid) except KeyError: connected_particpants[data] = [sid] numClients = len(connected_particpants[data]) if numClients == 1: sio.emit('created', data) elif numClients == 2: sio.emit('joined') sio.emit('join') print (sid, data, len(connected_particpants[data]))
async def connect(sid, environ): user = await authorized_userid(environ["aiohttp.request"]) if user is None: await sio.emit("redirect", "/", room=sid, namespace="/planarally") else: ref = unquote(environ["HTTP_REFERER"]).strip("/").split("/") room = (Room.select().join(User).where((Room.name == ref[-1]) & (User.name == ref[-2]))[0]) if room.creator == user: location = Location.get(room=room, name=room.dm_location) else: location = Location.get(room=room, name=room.player_location) state.add_sid(sid, user=user, room=room, location=location) logger.info(f"User {user.name} connected with identifier {sid}") sio.enter_room(sid, location.get_path(), namespace="/planarally") await sio.emit("Username.Set", user.name, room=sid, namespace="/planarally") await sio.emit( "Room.Info.Set", { "name": room.name, "creator": room.creator.name, "invitationCode": str(room.invitation_code), }, room=sid, namespace="/planarally", ) await sio.emit( "Notes.Set", [ note.as_dict() for note in Note.select().where((Note.user == user) & (Note.room == room)) ], room=sid, namespace="/planarally", ) await sio.emit( "Asset.List.Set", Asset.get_user_structure(user), room=sid, namespace="/planarally", ) await load_location(sid, location)
async def change_location(sid: str, data: LocationChangeData): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to change location") return # Send an anouncement to show loading state for room_player in pr.room.players: if not room_player.player.name in data["users"]: continue for psid in game_state.get_sids(player=room_player.player, room=pr.room): await sio.emit("Location.Change.Start", room=psid, namespace=GAME_NS) new_location = Location.get_by_id(data["location"]) for room_player in pr.room.players: if not room_player.player.name in data["users"]: continue for psid in game_state.get_sids(player=room_player.player, room=pr.room): try: sio.leave_room(psid, room_player.active_location.get_path(), namespace=GAME_NS) sio.enter_room(psid, new_location.get_path(), namespace=GAME_NS) except KeyError: await game_state.remove_sid(psid) continue await load_location(psid, new_location) # We could send this to all users in the new location, BUT # loading times might vary and we don't want to snap people back when they already move around # And it's possible that there are already users on the new location that don't want to be moved to this new position if "position" in data: await sio.emit( "Position.Set", data=data["position"], room=psid, namespace=GAME_NS, ) room_player.active_location = new_location room_player.save()
async def connect(sid, environ): user = await authorized_userid(environ["aiohttp.request"]) if user is None: await sio.emit("redirect", "/", room=sid, namespace="/planarally") else: # ref = unquote(environ["HTTP_REFERER"]).strip("/").split("/") ref = { k.split("=")[0]: k.split("=")[1] for k in unquote(environ["QUERY_STRING"]).strip().split("&") } try: room = (Room.select().join(User).where( (Room.name == ref["room"]) & (User.name == ref["user"]))[0]) except IndexError: return False if room.creator == user: location = Location.get(room=room, name=room.dm_location) else: location = Location.get(room=room, name=room.player_location) state.add_sid(sid, user=user, room=room, location=location) logger.info(f"User {user.name} connected with identifier {sid}") sio.enter_room(sid, location.get_path(), namespace="/planarally") await sio.emit("Username.Set", user.name, room=sid, namespace="/planarally") await sio.emit( "Room.Info.Set", { "name": room.name, "creator": room.creator.name, "invitationCode": str(room.invitation_code), }, room=sid, namespace="/planarally", ) await sio.emit( "Asset.List.Set", Asset.get_user_structure(user), room=sid, namespace="/planarally", ) await load_location(sid, location)
async def add_new_location(sid: str, location: str): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to add a new location") return new_location = Location.create(room=pr.room, name=location, index=pr.room.locations.count()) new_location.create_floor() for psid in game_state.get_sids(player=pr.player, active_location=pr.active_location): sio.leave_room(psid, pr.active_location.get_path(), namespace=GAME_NS) sio.enter_room(psid, new_location.get_path(), namespace=GAME_NS) await load_location(psid, new_location) pr.active_location = new_location pr.save()
async def handle_join_room(sid, json): if 'X-Token' not in json: return if not await Auth.is_user_authenticated(json['X-Token']): return org_team = get_room_id_by_json(json) sio.enter_room(sid, org_team) if 'task_id' not in json: return task_id = json['task_id'] if org_team in ROOM_MESSAGES and task_id in ROOM_MESSAGES[ org_team] and ROOM_MESSAGES[org_team][task_id]: await sio.emit('backlog', { 'task_id': task_id, 'message': ROOM_MESSAGES[org_team][task_id].getvalue() }, room=org_team)
async def connect(sid, environ): user = await authorized_userid(environ["aiohttp.request"]) if user is None: await sio.emit("redirect", "/", room=sid, namespace="/planarally") else: ref = { k.split("=")[0]: k.split("=")[1] for k in unquote(environ["QUERY_STRING"]).strip().split("&") } try: room = ( Room.select() .join(User) .where((Room.name == ref["room"]) & (User.name == ref["user"]))[0] ) except IndexError: return False else: for pr in room.players: if pr.player == user: if pr.role != Role.DM and room.is_locked: return False break else: return False pr = PlayerRoom.get(room=room, player=user) # todo: just store PlayerRoom as it has all the info await game_state.add_sid(sid, pr) logger.info(f"User {user.name} connected with identifier {sid}") labels = Label.select().where((Label.user == user) | (Label.visible == True)) label_filters = LabelSelection.select().where( (LabelSelection.user == user) & (LabelSelection.room == room) ) sio.enter_room(sid, pr.active_location.get_path(), namespace="/planarally") await sio.emit("Username.Set", user.name, room=sid, namespace="/planarally") await sio.emit( "Labels.Set", [l.as_dict() for l in labels], room=sid, namespace="/planarally", ) await sio.emit( "Labels.Filters.Set", [l.label.uuid for l in label_filters], room=sid, namespace="/planarally", ) await sio.emit( "Room.Info.Set", { "name": room.name, "creator": room.creator.name, "invitationCode": str(room.invitation_code), "isLocked": room.is_locked, "default_options": room.default_options.as_dict(), "players": [ { "id": rp.player.id, "name": rp.player.name, "location": rp.active_location.id, } for rp in room.players ], }, room=sid, namespace="/planarally", ) await sio.emit( "Asset.List.Set", Asset.get_user_structure(user), room=sid, namespace="/planarally", ) if pr.role == Role.DM: await sio.emit( "Locations.Settings.Set", { l.name: {} if l.options is None else l.options.as_dict() for l in pr.room.locations }, room=sid, namespace="/planarally", ) await load_location(sid, pr.active_location)
async def connect(sid, environ): user = await authorized_userid(environ["aiohttp.request"]) if user is None: await sio.emit("redirect", "/", room=sid, namespace="/planarally") else: ref = { k.split("=")[0]: k.split("=")[1] for k in unquote(environ["QUERY_STRING"]).strip().split("&") } try: room = (Room.select().join(User).where( (Room.name == ref["room"]) & (User.name == ref["user"]))[0]) except IndexError: return False else: if user != room.creator and ( user not in [pr.player for pr in room.players] or room.is_locked): return False if room.creator == user: location = Location.get(room=room, name=room.dm_location) else: location = Location.get(room=room, name=room.player_location) state.add_sid(sid, user=user, room=room, location=location) logger.info(f"User {user.name} connected with identifier {sid}") labels = Label.select().where((Label.user == user) | (Label.visible == True)) label_filters = LabelSelection.select().where( (LabelSelection.user == user) & (LabelSelection.room == room)) sio.enter_room(sid, location.get_path(), namespace="/planarally") await sio.emit("Username.Set", user.name, room=sid, namespace="/planarally") await sio.emit( "Labels.Set", [l.as_dict() for l in labels], room=sid, namespace="/planarally", ) await sio.emit( "Labels.Filters.Set", [l.label.uuid for l in label_filters], room=sid, namespace="/planarally", ) await sio.emit( "Room.Info.Set", { "name": room.name, "creator": room.creator.name, "invitationCode": str(room.invitation_code), "isLocked": room.is_locked, "players": [{ "id": rp.player.id, "name": rp.player.name } for rp in room.players], }, room=sid, namespace="/planarally", ) await sio.emit( "Asset.List.Set", Asset.get_user_structure(user), room=sid, namespace="/planarally", ) await load_location(sid, location)