コード例 #1
0
    def get_context_data(self, **kwargs):
        context = super(GameView, self).get_context_data(**kwargs)
        user_utils.add_generic_context(context, request=self.request)

        game_id = kwargs['pk']
        game_logic = ChessLogic.ChessGame(user_id=self.request.user,
                                          game_id=game_id)
        if not game_logic:
            context['html_board'] = 'Game not found.'
            return {'context': context}
        else:
            context['user'] = self.request.user
            context['user_is_creator'] = False
            creator_id = int(
                game_logic.game_data.get_data('game_options/creator'))
            if creator_id == self.request.user.id:
                context['user_is_creator'] = True

            context['user_can_play'] = False
            side = game_logic.game_data.get_data('token/step/side')
            if side:
                player_id = game_logic.game_data.get_data('participants/%s/1' %
                                                          side)
                if player_id and player_id == self.request.user.id:
                    context['user_can_play'] = True

                # add contextual data
                if game_logic.board.is_kingchecked(side):
                    context['king_check'] = side
                html_board = game_logic.board.render(context)
                context['html_board'] = html_board
            else:
                rounds = game_logic.game_data.get_data('rounds')
                white_wins = 0
                black_wins = 0
                if rounds:
                    for round_k, round in rounds.items():
                        if round['winner'] == 'white':
                            white_wins += 1
                        elif round['winner'] == 'black':
                            black_wins += 1
                context['game_results'] = {
                    'white_wins': white_wins,
                    'black_wins': black_wins
                }
            context['json_data'] = json2html.convert(
                json=game_logic.game_data.data)
            context['game_logic'] = game_logic
            return {'context': context}
コード例 #2
0
    def get_context_data(self, *args, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        user_utils.add_generic_context(context, request=self.request)

        games = GamePersistentData.objects.all()
        opened_games = list()
        running_games = list()
        finished_games = list()
        for game in games:
            white_user_id = game.get_data('participants/white/1')
            black_user_id = game.get_data('participants/black/1')
            if not white_user_id or not black_user_id:
                opened_games.append(game)
                continue

            step = game.get_data('token/step/name')
            if not step:
                finished_games.append(game)
                continue

            rounds = game.get_data('rounds')
            if not rounds:
                running_games.append(game)
                continue

            white_wins = 0
            black_wins = 0
            for round_k, round in rounds.items():
                if round['winner'] == 'white':
                    white_wins += 1
                elif round['winner'] == 'black':
                    black_wins += 1
            winning_games = int(game.get_data('game_options/winning_games'))
            if white_wins >= winning_games or black_wins >= winning_games:
                finished_games.append(game)
                continue

            running_games.append(game)

        context['games'] = games
        context['opened_games'] = opened_games
        context['running_games'] = running_games
        context['finished_games'] = finished_games
        return {'context': context}
コード例 #3
0
    def get_context_data(self, *args, **kwargs):
        context = super(ProfileView, self).get_context_data(**kwargs)
        user_utils.add_generic_context(context, request=self.request)
        user_utils.add_theme_list(context)

        target_user_id = kwargs['pk']
        target_user = User.objects.filter(id=target_user_id).first()
        if not target_user:
            return False
        context['target_user'] = target_user

        # performances
        perfs = dict()
        user_ranking = UserRanking.objects.get_or_create(
            user=self.request.user)[0]
        user_elo = user_ranking.get_elo('chess')
        if user_elo:
            perfs['elo'] = user_elo
            perfs['level'] = user_ranking.get_user_level('chess')

        context['performances'] = perfs
        parsed_perf_data = RankingUtils().parse_history_data(
            'chess', target_user)
        context['performances_parsed'] = parsed_perf_data

        # search user games
        context['player_history'] = ProfileLoadData().get_player_history(
            target_user_id)

        user_colorset = UserColorSet.objects.filter(user=target_user).first()
        if not user_colorset:
            user_colorset = UserColorSet(user=target_user)
            default_colorset = ChessBoard.BoardColorSet().get_default_colorset(
            )
            user_colorset.set_data('chess', default_colorset)
        context['user_customization'] = user_colorset.get_data('')

        return {'context': context}