Пример #1
0
    async def get_num_finishes(self, player):
        """
		Get the number of finishes.

		:param player: Player instance.
		:type player: pyplanet.apps.core.maniaplanet.models.Player
		:return: count or False when not possible to fetch.
		:rtype: int
		"""
        if self.app.instance.game.game == 'tm':
            return await Score.objects.count(
                Score.select(Score).where(Score.player == player))
        return False
Пример #2
0
    async def player_chat(self, player, text, cmd):
        if not cmd:
            if text == '++' or text == '--':
                if self.instance.game.game == 'tm':
                    finishes_required = await self.setting_finishes_before_voting.get_value(
                    )
                    player_finishes = await Score.objects.count(
                        Score.select().where(
                            Score.map_id == self.instance.map_manager.
                            current_map.get_id()).where(
                                Score.player_id == player.get_id()))
                    if player_finishes < finishes_required:
                        message = '$i$f00You have to finish this map at least $fff{}$f00 times before voting!'.format(
                            finishes_required)
                        await self.instance.chat(message, player)
                        return

                score = (1 if text == '++' else -1)
                player_votes = [
                    x for x in self.current_votes
                    if x.player_id == player.get_id()
                ]
                if len(player_votes) > 0:
                    player_vote = player_votes[0]
                    if player_vote.score != score:
                        player_vote.score = score
                        await player_vote.save()

                        message = '$ff0Successfully changed your karma vote to $fff{}$ff0!'.format(
                            text)
                        await self.calculate_karma()
                        await asyncio.gather(
                            self.instance.chat(message, player),
                            self.widget.display())
                else:
                    new_vote = KarmaModel(
                        map=self.instance.map_manager.current_map,
                        player=player,
                        score=score)
                    await new_vote.save()

                    self.current_votes.append(new_vote)
                    await self.calculate_karma()

                    message = '$ff0Successfully voted $fff{}$ff0!'.format(text)
                    await asyncio.gather(self.instance.chat(message, player),
                                         self.widget.display())
Пример #3
0
    async def player_chat(self, player, text, cmd):
        if not cmd:
            if text == '++' or text == '+' or text == '+-' or text == '-+' or text == '-' or text == '--':
                expanded_voting = await self.setting_expanded_voting.get_value(
                )
                if expanded_voting is False:
                    if text == '+' or text == '+-' or text == '-+' or text == '-':
                        return

                if self.instance.game.game == 'tm':
                    finishes_required = await self.setting_finishes_before_voting.get_value(
                    )
                    player_finishes = await Score.objects.count(
                        Score.select().where(
                            Score.map_id == self.instance.map_manager.
                            current_map.get_id()).where(
                                Score.player_id == player.get_id()))
                    if player_finishes < finishes_required:
                        message = '$i$f00You have to finish this map at least $fff{}$f00 times before voting!'.format(
                            finishes_required)
                        await self.instance.chat(message, player)
                        return

                score = -1
                if text == '++':
                    score = 1
                elif text == '+':
                    score = 0.5
                elif text == '+-' or text == '-+':
                    score = 0
                elif text == '-':
                    score = -0.5

                player_votes = [
                    x for x in self.current_votes
                    if x.player_id == player.get_id()
                ]
                if len(player_votes) > 0:
                    player_vote = player_votes[0]
                    if (player_vote.expanded_score is not None and player_vote.expanded_score != score) or \
                     (player_vote.expanded_score is None and player_vote.score != score):
                        player_vote.expanded_score = score
                        await player_vote.save()

                        map = next(
                            (m for m in self.instance.map_manager.maps if m.uid
                             == self.instance.map_manager.current_map.uid),
                            None)
                        if map is not None:
                            map.karma = await self.get_map_karma(
                                self.instance.map_manager.current_map)

                        message = '$ff0Successfully changed your karma vote to $fff{}$ff0!'.format(
                            text)
                        await self.calculate_karma()
                        await asyncio.gather(
                            self.instance.chat(message, player),
                            self.widget.display())
                else:
                    new_vote = KarmaModel(
                        map=self.instance.map_manager.current_map,
                        player=player,
                        score=score)
                    await new_vote.save()

                    self.current_votes.append(new_vote)
                    await self.calculate_karma()

                    map = next((
                        m for m in self.instance.map_manager.maps
                        if m.uid == self.instance.map_manager.current_map.uid),
                               None)
                    if map is not None:
                        map.karma = await self.get_map_karma(
                            self.instance.map_manager.current_map)

                    message = '$ff0Successfully voted $fff{}$ff0!'.format(text)
                    await asyncio.gather(self.instance.chat(message, player),
                                         self.widget.display())

                # Reload map referenced information
                asyncio.ensure_future(
                    self.load_map_votes(
                        map=self.instance.map_manager.current_map))
Пример #4
0
    async def get_data(self):
        score_list = await Score.objects.execute(
            Score.select(Score, Player).join(Player).where(
                Score.map ==
                self.app.instance.map_manager.current_map.get_id()).order_by(
                    Score.created_at.asc()))

        scores_list = list(score_list)

        personal_list = [
            s for s in scores_list if s.player.id == self.player.get_id()
        ]

        if not personal_list:
            message = '$i$f00There are no personal scores available for $fff{}$z$s$f00$i!'.format(
                self.app.instance.map_manager.current_map.name)
            await self.app.instance.chat(message, self.player)
            return

        local_record = min(s.score for s in scores_list)

        scores = []
        last_best = 0
        last_best_index = 1
        personal_best = min(s.score for s in personal_list)
        for score_in_list in personal_list:
            historical_local = min(s.score for s in scores_list
                                   if s.created_at <= score_in_list.created_at)

            score = {
                'index': '',
                'score': times.format_time(score_in_list.score)
            }
            score['created_at'] = score_in_list.created_at.strftime(
                '%d-%m-%Y %H:%M:%S')
            score['difference_to_pb'] = times.format_time(
                (score_in_list.score - personal_best))
            score['difference_to_prev'] = ''
            score['difference_to_local'] = times.format_time(
                (score_in_list.score - local_record))
            score['historical_local'] = times.format_time(historical_local)
            score['difference_to_hist_local'] = times.format_time(
                (score_in_list.score - historical_local))

            if last_best == 0:
                score['index'] = last_best_index
                last_best = score_in_list.score
                last_best_index += 1
            elif score_in_list.score < last_best:
                score['index'] = last_best_index
                score['difference_to_prev'] = '$00f- {}'.format(
                    times.format_time((last_best - score_in_list.score)))
                last_best = score_in_list.score
                last_best_index += 1
            else:
                score['difference_to_prev'] = '$f00+ {}'.format(
                    times.format_time((score_in_list.score - last_best)))

            if score_in_list.score == local_record:
                score['difference_to_local'] = ''
            else:
                score['difference_to_local'] = '$f00+ {}'.format(
                    score['difference_to_local'])

            if score_in_list.score == historical_local:
                score['difference_to_hist_local'] = ''
            else:
                score['difference_to_hist_local'] = '$f00+ {}'.format(
                    score['difference_to_hist_local'])

            if score_in_list.score == personal_best:
                score['index'] = 'PB'
                score['difference_to_pb'] = ''
            else:
                score['difference_to_pb'] = '$f00+ {}'.format(
                    score['difference_to_pb'])

            scores.append(score)

        return scores
Пример #5
0
    async def get_data(self):
        score_list = await Score.objects.execute(
            Score.select(Score, Player).join(Player).where(
                Score.map ==
                self.app.instance.map_manager.current_map.get_id()).order_by(
                    Score.score.asc()))

        scores_list = list(score_list)
        for score in scores_list:
            score_checkpoints = score.checkpoints.split(',')
            score.checkpoints = [
                int(y) - int(x)
                for x, y in zip(score_checkpoints, score_checkpoints[1:])
            ]  # TODO: Improve performance of this code block.
            score.checkpoints.insert(0, int(score_checkpoints[0]))

        personal_list = [
            s for s in scores_list if s.player.id == self.player.get_id()
        ]

        local_record = next(iter(scores_list or []), None)
        personal_best = next(iter(personal_list or []), None)

        checkpoints = []
        total_pb = 0
        total_local = 0
        total_ideal = 0

        for checkpoint_index in range(
                self.app.instance.map_manager.current_map.num_checkpoints):
            pb_checkpoint = personal_best.checkpoints[
                checkpoint_index] if personal_best is not None else None
            local_checkpoint = local_record.checkpoints[
                checkpoint_index] if local_record is not None else None
            ideal_checkpoint = (min(
                cp.checkpoints[checkpoint_index]
                for cp in scores_list) if scores_list is not None
                                and scores_list else None)
            ideal_record = ([
                score for score in scores_list
                if score.checkpoints[checkpoint_index] == ideal_checkpoint
            ][0] if scores_list is not None and scores_list else None)

            if pb_checkpoint is not None:
                total_pb += pb_checkpoint

            if local_checkpoint is not None:
                total_local += local_checkpoint
                total_ideal += ideal_checkpoint

            checkpoint = {
                'index':
                checkpoint_index + 1 if checkpoint_index <
                (self.app.instance.map_manager.current_map.num_checkpoints - 1)
                else 'Finish',
                'personal_best':
                '{} ({})'.format(times.format_time(total_pb),
                                 times.format_time(pb_checkpoint))
                if pb_checkpoint is not None else '-',
            }
            checkpoint['local_record'] = '{} ({})'.format(
                times.format_time(total_local),
                times.format_time(
                    local_checkpoint)) if local_checkpoint is not None else '-'
            checkpoint[
                'local_record_driver'] = local_record.player.nickname if local_checkpoint is not None else '-'
            checkpoint['difference_to_local'] = '-'
            checkpoint['ideal'] = '{} ({})'.format(
                times.format_time(total_ideal),
                times.format_time(
                    ideal_checkpoint)) if local_checkpoint is not None else '-'
            checkpoint[
                'ideal_driver'] = ideal_record.player.nickname if local_checkpoint is not None else '-'
            checkpoint['difference_to_ideal'] = '-'

            if pb_checkpoint is not None:
                if total_pb > total_local:
                    checkpoint['difference_to_local'] = '$f00+ {}'.format(
                        times.format_time((total_pb - total_local)))
                else:
                    checkpoint['difference_to_local'] = '$00f- {}'.format(
                        times.format_time((total_local - total_pb)))

                if total_pb > total_ideal:
                    checkpoint['difference_to_ideal'] = '$f00+ {}'.format(
                        times.format_time((total_pb - total_ideal)))
                else:
                    checkpoint['difference_to_ideal'] = '$00f- {}'.format(
                        times.format_time((total_ideal - total_pb)))

            checkpoints.append(checkpoint)

        return checkpoints