def render_motd(self, src_motd):
        # Wouldn't this be better to do with something like fuzzy?
        if self.scoreboard_type in ['kills', 'Kills', 'kill', 'Kill']:
            scores = self.server.database.top_kills()
        elif self.scoreboard_type in ['Dosh','dosh']:
            scores = self.server.database.top_dosh()
        else:
            logger.error("Bad configuration, scoreboard_type. "
                         "Options are: dosh, kills ({})"
                         .format(self.server.name))
            return

        for player in scores:
            name = player[0].replace("<", "&lt;")
            name = trim_string(name, 12)
            score = player[1]

            src_motd = src_motd.replace("%PLR", name, 1)
            src_motd = src_motd.replace("%SCR", millify(score), 1)

        if "%SRV_K" in src_motd:
            server_kills = self.server.database.server_kills()
            src_motd = src_motd.replace("%SRV_K", millify(server_kills), 1)

        if "%SRV_D" in src_motd:
            server_dosh = self.server.database.server_dosh()
            src_motd = src_motd.replace("%SRV_D", millify(server_dosh), 1)

        return src_motd
    def execute(self, username, args, admin):
        if not self.authorise(admin):
            return self.not_auth_message

        if len(args) > 1 and args[1] == '-w' and len(self.server.players) > 0:
            self.server.players.sort(key=lambda player: player.wave_kills,
                                     reverse=True)
            top_killer = self.server.players[0]
            return "Player {} killed the most zeds this wave: {} zeds"\
                .format(top_killer.username, top_killer.wave_kills)

        self.server.write_all_players()
        killers = self.server.database.top_kills()
        if len(killers) < 5:
            return "Not enough data."
        # [row][col]
        return "\n\nTop 5 players by total kills:\n" + \
            "\t"+str(millify(killers[0][1])) + "\t-\t" + trim_string(killers[0][0],20) + "\n" + \
            "\t"+str(millify(killers[1][1])) + "\t-\t" + trim_string(killers[1][0],20) + "\n" + \
            "\t"+str(millify(killers[2][1])) + "\t-\t" + trim_string(killers[2][0],20) + "\n" + \
            "\t"+str(millify(killers[3][1])) + "\t-\t" + trim_string(killers[3][0],20) + "\n" + \
            "\t"+str(millify(killers[4][1])) + "\t-\t" + trim_string(killers[4][0],20)
    def execute(self, username, args, admin):
        if not self.authorise(admin):
            return self.not_auth_message

        if len(args) > 1 and args[1] == '-w' and len(self.server.players) > 0:
            self.server.players.sort(key=lambda player: player.wave_dosh,
                                     reverse=True)
            top_dosh = self.server.players[0]
            return "Player {} earned the most this wave: £{}"\
                .format(top_dosh.username, millify(top_dosh.wave_dosh))\
                .encode("iso-8859-1", "ignore")

        self.server.write_all_players()
        doshers = self.server.database.top_dosh()
        if len(doshers) < 5:
            return "Not enough data."

        message = "\n\nTop 5 players by earnings:\n" + \
            "\t£"+str(millify(doshers[0][1])) + "\t-\t" + trim_string(doshers[0][0],20) + "\n" + \
            "\t£"+str(millify(doshers[1][1])) + "\t-\t" + trim_string(doshers[1][0],20) + "\n" + \
            "\t£"+str(millify(doshers[2][1])) + "\t-\t" + trim_string(doshers[2][0],20) + "\n" + \
            "\t£"+str(millify(doshers[3][1])) + "\t-\t" + trim_string(doshers[3][0],20) + "\n" + \
            "\t£"+str(millify(doshers[4][1])) + "\t-\t" + trim_string(doshers[4][0],20)
        return message.encode("iso-8859-1", "ignore")
    def execute(self, username, args, user_flags):
        args, err = self.parse_args(username, args, user_flags)
        if err:
            return err
        if args.help:
            return self.format_response(self.help_text, args)

        records = self.server.database.top_time()

        message = _("Top 5 players by play time:\n")

        for player in records[:5]:
            username = trim_string(player['username'], 20)
            time = seconds_to_hhmmss(player['score'])
            message += "\t{}\t-   {}\n".format(time, username)

        return self.format_response(message[:-1], args)
    def execute(self, username, args, user_flags):
        args, err = self.parse_args(username, args, user_flags)
        if err:
            return err
        if args.help:
            return self.format_response(self.help_text, args)

        records = self.server.database.top_dosh()

        message = _("Top 5 players by Dosh earned:\n")

        for player in records[:5]:
            username = trim_string(player['username'], 20)
            dosh = millify(player['score'])
            message += "\t£{}\t-   {}\n".format(dosh, username)

        return self.format_response(message[:-1], args)
    def execute(self, username, args, user_flags):
        args, err = self.parse_args(username, args, user_flags)
        if err:
            return err
        if args.help:
            return self.format_response(self.help_text, args)

        message = _("Scoreboard (name, kills, dosh):\n")

        self.server.players.sort(key=lambda player: player.kills, reverse=True)

        for player in self.server.players:
            username = trim_string(player.username, 20)
            dosh = millify(player.dosh)
            kills = player.kills
            message += _("{}\t- {} Kills, £{}\n").format(username, kills, dosh)

        return self.format_response(message[:-1], args)
Ejemplo n.º 7
0
def test_trim_string():
    tests = [{
        "str": "test",
        "len": 8,
        "expected": "test"
    }, {
        "str": "12345678",
        "len": 8,
        "expected": "12345678"
    }, {
        "str": "123456789",
        "len": 8,
        "expected": "123456.."
    }, {
        "str": "123456789",
        "len": 2,
        "expected": ".."
    }]

    for test in tests:
        result = trim_string(test["str"], test["len"])
        assert result == test["expected"]