def handle(self): room_resource = RoomResource(self.server, self.conn.token, self.session) if self.packet["enter"] == 0: room_resource.leave() self.send(models.Room.smo_list(self.session, self.active_users)) return try: room = room_resource.login(self.packet["room"], self.packet["password"]) except exceptions.Forbidden: self.send_message( "Unauthorized to enter in the room {name}".format( name=with_color(self.packet["room"]) ) ) return except exceptions.Unauthorized: self.send_message( "The room {name} is full".format( name=with_color(self.packet["room"]) ) ) return self.send(smpacket.SMPacketServerNSSMONL( packet=room.to_packet() )) self.send_room_resume(self.server, self.conn, room)
def request_launch_song(self, song): if not self.room.free and self.cannot(ability.Permissions.start_game, self.room.id): self.send_message("You don't have the permission to start a game", to="me") return if self.room.status == 2 and self.room.active_song_id: self.send_message( "Room %s is already playing %s." % ( with_color(self.room.name), with_color(self.room.active_song.fullname) ), to="me" ) return game = models.Game(room_id=self.room.id, song_id=song.id) self.session.add(game) self.session.commit() self.send_message("New game started: %s" % with_color(song.fullname)) self.room.status = 2 self.room.active_song = song self.sendplayers(self.room.id, smpacket.SMPacketServerNSCRSG( usage=2, song_title=song.title, song_subtitle=song.subtitle, song_artist=song.artist ))
def handle(self): room_resource = RoomResource(self.server, self.conn.token, self.session) try: room = room_resource.create( name=self.packet["title"], password=self.packet["password"], description=self.packet["description"], type_=1 ) except exceptions.Forbidden: self.send_message( "Unauthorized to create the room {name}".format( name=with_color(self.packet["title"]) ) ) return except exceptions.ValidationError: self.send_message( "The room {name} already exist!".format( name=with_color(self.packet["title"]) ) ) return room_resource.enter(room) self.send(smpacket.SMPacketServerNSSMONL( packet=room.to_packet() )) enter_room.EnterRoomController.send_room_resume(self.server, self.conn, room) self.send_message( "Welcome to your new room! Type /help for options", to="me" )
def handle(self): room_resource = RoomResource(self.server, self.conn.token, self.session) try: room = room_resource.create(name=self.packet["title"], password=self.packet["password"], description=self.packet["description"], type_=1) except exceptions.Forbidden: self.send_message("Unauthorized to create the room {name}".format( name=with_color(self.packet["title"]))) return except exceptions.ValidationError: self.send_message("The room {name} already exist!".format( name=with_color(self.packet["title"]))) return room_resource.enter(room) self.send(smpacket.SMPacketServerNSSMONL(packet=room.to_packet())) enter_room.EnterRoomController.send_room_resume( self.server, self.conn, room) self.send_message("Welcome to your new room! Type /help for options", to="me")
def request_launch_song(self, song): if not self.room.free and self.cannot(ability.Permissions.start_game, self.room.id): self.send_message("You don't have the permission to start a game", to="me") return if self.room.status == 2 and self.room.active_song_id: self.send_message("Room %s is already playing %s." % (with_color( self.room.name), with_color(self.room.active_song.fullname)), to="me") return game = models.Game(room_id=self.room.id, song_id=song.id) self.session.add(game) self.session.commit() self.send_message("New game started: %s" % with_color(song.fullname)) self.room.status = 2 self.room.active_song = song self.sendplayers( self.room.id, smpacket.SMPacketServerNSCRSG(usage=2, song_title=song.title, song_subtitle=song.subtitle, song_artist=song.artist))
def handle(self): room_resource = RoomResource(self.server, self.conn.token, self.session) if self.packet["enter"] == 0: room_resource.leave() self.send(models.Room.smo_list(self.session, self.active_users)) return try: room = room_resource.login(self.packet["room"], self.packet["password"]) except exceptions.Forbidden: self.send_message( "Unauthorized to enter in the room {name}".format( name=with_color(self.packet["room"]))) return except exceptions.Unauthorized: self.send_message("The room {name} is full".format( name=with_color(self.packet["room"]))) return self.send(smpacket.SMPacketServerNSSMONL(packet=room.to_packet())) self.send_room_resume(self.server, self.conn, room)
def start_game_request(self, song): with self.conn.mutex: self.conn.songs[song.id] = True if self.conn.song == song.id: self.request_launch_song(song) return self.send_message("%s select %s which have been played %s times.%s" % (self.colored_user_repr(self.room.id), with_color(song.fullname), song.time_played, " Best scores:" if song.time_played > 0 else "")) if song.time_played > 0: for song_stat in song.best_scores: self.send_message( song_stat.pretty_result(room_id=self.room.id, color=True)) with self.conn.mutex: self.conn.song = song.id self.conn.songs[song.id] = True self.sendplayers( self.room.id, smpacket.SMPacketServerNSCRSG(usage=1, song_title=song.title, song_subtitle=song.subtitle, song_artist=song.artist))
def start_game_request(self, song): with self.conn.mutex: self.conn.songs[song.id] = True if self.conn.song == song.id: self.request_launch_song(song) return self.send_message("%s select %s which have been played %s times.%s" % ( self.colored_user_repr(self.room.id), with_color(song.fullname), song.time_played, " Best scores:" if song.time_played > 0 else "" )) if song.time_played > 0: for song_stat in song.best_scores: self.send_message(song_stat.pretty_result(room_id=self.room.id, color=True)) with self.conn.mutex: self.conn.song = song.id self.conn.songs[song.id] = True self.sendplayers(self.room.id, smpacket.SMPacketServerNSCRSG( usage=1, song_title=song.title, song_subtitle=song.subtitle, song_artist=song.artist ))
def handle(self): if not self.room: return if "start_at" not in self.conn.songstats: return song_duration = datetime.datetime.now() - self.conn.songstats["start_at"] for user in self.active_users: with self.conn.mutex: songstat = self.create_stats(user, self.conn.songstats[user.pos], song_duration) xp = songstat.calc_xp(self.server.config.score.get("xpWeight")) user.xp += xp self.send_message("New result: %s" % songstat.pretty_result(room_id=self.room.id, color=True)) self.send_message( "%s gained %s XP!" % ( user.fullname_colored(user.room_id), with_color(xp, "aaaa00") ), to="me") with self.conn.mutex: self.conn.ingame = False self.conn.songstats = {0: {"data": []}, 1: {"data": []}} self.conn.song = None
def send_room_resume(server, conn, room): """ Send the room welcome information. :param server: Main server :param conn: Connection target :param room: Room to be resume :type server: smserver.server.StepmaniaServer :type conn: smserver.smutils.smconn.StepmaniaConn :type room: smserver.models.room.Room """ messages = [] messages.append("Room %s (%s), created at %s" % (with_color( room.name), room.mode if room.mode else "normal", room.created_at)) messages.append("%s/%s players online. Moderators: %s" % (room.nb_players, room.max_users, ", ".join( player.fullname_colored(room.id) for player in room.moderators))) if room.motd: messages.append(room.motd) for message in messages: server.send_message(message, conn=conn)
def handle(self): if not self.room: return if "start_at" not in self.conn.songstats: return song_duration = datetime.datetime.now( ) - self.conn.songstats["start_at"] for user in self.active_users: with self.conn.mutex: songstat = self.create_stats(user, self.conn.songstats[user.pos], song_duration) xp = songstat.calc_xp(self.server.config.score.get("xpWeight")) user.xp += xp self.send_message( "New result: %s" % songstat.pretty_result(room_id=self.room.id, color=True)) self.send_message("%s gained %s XP!" % (user.fullname_colored( user.room_id), with_color(xp, "aaaa00")), to="me") with self.conn.mutex: self.conn.ingame = False self.conn.songstats = {0: {"data": []}, 1: {"data": []}} self.conn.song = None
def send_message(self, message, room=None, conn=None, func=None): """ Send a chat message (default to all) :param str message: The message to send. :param room: If specified send to this room :param conn: If specified send to this connection :param func: If specified use this function to send the message :type room: smserver.models.room.Room :type conn: smserver.smutils.smconn.StepmaniaConn """ packet = smpacket.SMPacketServerNSCCM(message=message) if conn: conn.send(packet) return if not room: self.sendall(packet) return packet["message"] = "#%s %s" % (with_color(room.name), message) if not func: func = self.sendroom func(room.id, packet)
def handle(self): if not self.room: return song = models.Song.find_or_create(self.packet["song_title"], self.packet["song_subtitle"], self.packet["song_artist"], self.session) if self.packet["usage"] == 2: self.start_game_request(song) return have_song = self.check_song_presence(song) if not have_song: self.send_message( "%s does %s have the song (%s)!" % (self.colored_user_repr(self.room.id), with_color("not", "ff0000"), with_color(song.fullname)))
def fullname_colored(self, room_id=None): """ Retun user fullname with chat_color """ color = nick_color(self.name) if not self.online: color = "141414" if self.room_id != room_id: color = "313131" return with_color(message=self.fullname(room_id), color=color)
def fullname_colored(self, room_id=None): """ Retun user fullname with chat_color """ color = nick_color(self.name) if not self.online: color = "141414" if self.room_id != room_id: color = "313131" return with_color(message=self.fullname(room_id), color=color)
def __call__(self, resource, message): user = resource.session.query(models.User).filter_by(name=message).first() if not user: return ["Unknown user %s" % with_color(message)] connection = resource.connection models.Ban.unban(resource.session, user_id=user.id, room_id=connection.room_id) resource.send( "User {user} has been unban from this room".format( user=user.fullname_colored(resource.connection.room_id) ) )
def handle(self): if not self.room: return song = models.Song.find_or_create( self.packet["song_title"], self.packet["song_subtitle"], self.packet["song_artist"], self.session) if self.packet["usage"] == 2: self.start_game_request(song) return have_song = self.check_song_presence(song) if not have_song: self.send_message("%s does %s have the song (%s)!" % ( self.colored_user_repr(self.room.id), with_color("not", "ff0000"), with_color(song.fullname) ))
def __call__(self, resource, message): user = resource.session.query(models.User).filter_by(name=message).first() if not user: return ["Unknown user %s" % with_color(message)] connection = resource.connection if user.level(connection.room_id) > connection.level(connection.room_id): return ["Not authorize to voice %s" % user.fullname_colored(connection.room_id)] user.set_level(connection.room_id, 1) resource.send("%s give voice right to %s" % ( models.User.colored_users_repr(connection.active_users), user.fullname_colored(connection.room_id) ))
def check_end_game(self, session): for room in session.query(models.Room).filter_by(status=2): if self.room_still_in_game(room): continue self.server.log.info("Room %s finish is last song: %s" % (room.name, room.active_song_id)) room.status = 1 room.ingame = False game = room.last_game game.end_at = datetime.datetime.now() game.active = False self.server.sendplayers(room.id, game.scoreboard_packet) self.server.send_message( "Game ended %s" % with_color(room.active_song.fullname), room)
def send_message_room(self, message, room): """ Send a message to a room :param str message: Message to send :param room: Room to send the message :type room: smserver.models.room.Room """ if not room: self.log.warning("Trying to send message to unknown room: %r", message) return packet = smpacket.SMPacketServerNSCCM(message=message) packet["message"] = "#%s %s" % (with_color(room.name), message) self.server.sendroom(room.id, packet)
def send_message_room(self, message, room): """ Send a message to a room :param str message: Message to send :param room: Room to send the message :type room: smserver.models.room.Room """ if not room: self.log.warning("Trying to send message to unknown room: %r", message) return packet = smpacket.SMPacketServerNSCCM( message=message ) packet["message"] = "#%s %s" % (with_color(room.name), message) self.server.sendroom(room.id, packet)
def check_end_game(self, session): for room in session.query(models.Room).filter_by(status=2): if self.room_still_in_game(room): continue self.server.log.info("Room %s finish is last song: %s" % (room.name, room.active_song_id)) room.status = 1 room.ingame = False game = room.last_game game.end_at = datetime.datetime.now() game.active = False self.server.sendplayers(room.id, game.scoreboard_packet) self.server.send_message( "Game ended %s" % with_color(room.active_song.fullname), room )
def __call__(self, resource, message): user = resource.session.query(models.User).filter_by(name=message).first() connection = resource.connection if not user: return ["Unknown user %s" % with_color(message)] if user.level(connection.room_id) >= connection.level(connection.room_id): return ["Not authorize to kick %s" % user.fullname_colored(connection.room_id)] ChatKick.kick_user(self.server, user, connection.room) resource.send( "%s kick user %s" % ( models.User.colored_users_repr(connection.active_users), user.fullname_colored(connection.room_id), ) )
def __call__(self, resource, message): user = resource.session.query( models.User).filter_by(name=message).first() if not user: return ["Unknown user %s" % with_color(message)] connection = resource.connection if user.level(connection.room_id) > connection.level( connection.room_id): return [ "Not authorize to voice %s" % user.fullname_colored(connection.room_id) ] user.set_level(connection.room_id, 1) resource.send("%s give voice right to %s" % (models.User.colored_users_repr(connection.active_users), user.fullname_colored(connection.room_id)))
def send_room_resume(server, conn, room): """ Send the room welcome information. :param server: Main server :param conn: Connection target :param room: Room to be resume :type server: smserver.server.StepmaniaServer :type conn: smserver.smutils.smconn.StepmaniaConn :type room: smserver.models.room.Room """ messages = [] messages.append( "Room %s (%s), created at %s" % ( with_color(room.name), room.mode if room.mode else "normal", room.created_at ) ) messages.append( "%s/%s players online. Moderators: %s" % ( room.nb_players, room.max_users, ", ".join(player.fullname_colored(room.id) for player in room.moderators) ) ) if room.motd: messages.append(room.motd) for message in messages: server.send_message(message, conn=conn)