Пример #1
0
def round_inline_handle(_bot: telegram.Bot, job_queue: JobQueue,
                        query: telegram.CallbackQuery, gm: bool,
                        game_round: Round):
    language_code = get_language(query.from_user)

    def _(x):
        return get(x, language_code)

    method = str(query.data)
    actors = game_round.get_actors()
    if method == 'round:next':
        next_count = game_round.counter + 1
        if next_count >= len(actors):
            next_count = 0
            game_round.round_counter += 1
        game_round.counter = next_count
        game_round.save()
        answer_callback_query(job_queue, query.id)
        update_round_message(job_queue, game_round, language_code)
    elif method == 'round:prev':
        prev_count = game_round.counter - 1
        if prev_count < 0:
            if game_round.round_counter <= 1:
                answer_callback_query(job_queue, query.id,
                                      _(Text.ALREADY_FIRST_TURN))
                return
            else:
                prev_count = len(actors) - 1
                game_round.round_counter -= 1
        game_round.counter = prev_count
        answer_callback_query(job_queue, query.id)
        update_round_message(job_queue,
                             game_round,
                             language_code,
                             refresh=True)
        game_round.save()
    elif method == 'round:remove':
        if not gm:
            raise NotGm()

        actors = game_round.get_actors()
        if len(actors) > 1:
            current = actors[game_round.counter % len(actors)]
            current.delete()
            answer_callback_query(job_queue, query.id)
            update_round_message(job_queue,
                                 game_round,
                                 language_code,
                                 refresh=True)
        else:
            answer_callback_query(query.id,
                                  _(Text.AT_LEAST_ONE_ACTOR),
                                  show_alert=True)
    elif method == 'round:finish':
        if not gm:
            raise NotGm()
        message: telegram.Message = query.message
        edit_message(job_queue, message.chat_id, message.message_id,
                     _(Text.ROUND_ALREADY_FINISHED))
        remove_round(job_queue, game_round.chat_id)
Пример #2
0
def refresh_round_message(bot: telegram.Bot,
                          game_round: Round,
                          get_text,
                          refresh=False):
    reply_markup = InlineKeyboardMarkup([
        [
            InlineKeyboardButton(get_text(Text.ROUND_REMOVE),
                                 callback_data='round:remove'),
            InlineKeyboardButton(get_text(Text.ROUND_FINISH),
                                 callback_data='round:finish'),
            InlineKeyboardButton("←", callback_data='round:prev'),
            InlineKeyboardButton("→", callback_data='round:next'),
        ],
    ])

    actors = game_round.get_actors()
    if not actors:
        return
    game_round.counter = game_round.counter % len(actors)
    counter = game_round.counter
    state = ''
    if game_round.hide:
        state = '[{}]'.format(get_text(Text.HIDED_ROUND_LIST))
    round_counter = get_text(
        Text.ROUND_COUNTER).format(round_number=game_round.round_counter)
    text = '<b>{}</b> {state} #round\n\n{round_number}   [{counter}/{total}]\n\n'.format(
        get_text(Text.ROUND_INDICATOR),
        state=state,
        round_number=round_counter,
        counter=counter + 1,
        total=len(actors),
    )
    for index, actor in enumerate(actors):
        is_current = counter == index
        if is_current:
            text += '• {} ({}) ← {}\n'.format(actor.name, actor.value,
                                              get_text(Text.CURRENT))
        elif not game_round.hide:
            text += '◦ {} ({})\n'.format(actor.name, actor.value)

    if refresh:
        try:
            bot.edit_message_text(
                text,
                chat_id=game_round.chat_id,
                message_id=game_round.message_id,
                parse_mode='HTML',
                reply_markup=reply_markup,
            )
        except TelegramError:
            pass
    else:
        bot.delete_message(game_round.chat_id, game_round.message_id)
        message = bot.send_message(game_round.chat_id,
                                   text,
                                   parse_mode='HTML',
                                   reply_markup=reply_markup)
        game_round.message_id = message.message_id
        game_round.save()
Пример #3
0
    def _populate_rounds(self, game, winning_team_players, loose_team_players, winning_team_score, loose_team_score,
                         winning_team_id):
        winning_team_round_score = self._split_a_total(winning_team_score, 4, [i for i in range(1, 5)])
        loose_team_round_score = self._split_a_total(loose_team_score, 4, [i for i in range(1, 5)])
        for i in range(1, 5):
            game_players = winning_team_players + loose_team_players
            round = Round(game=game, round_number=i)
            round.save()
            round.players.set(game_players)

            self._populate_player_round_score(round, game_players, winning_team_round_score[i],
                                              loose_team_round_score[i], winning_team_players, loose_team_players,
                                              winning_team_id)
            self._populate_team_round_scores(round, game.teams)
Пример #4
0
def round_inline_handle(bot: telegram.Bot, query: telegram.CallbackQuery,
                        gm: bool, game_round: Round):
    _ = partial(get_by_user, user=query.from_user)

    method = str(query.data)
    actors = game_round.get_actors()
    if method == 'round:next':
        next_count = game_round.counter + 1
        if next_count >= len(actors):
            next_count = 0
            game_round.round_counter += 1
        game_round.counter = next_count
        game_round.save()
        query.answer()
        refresh_round_message(bot, game_round, _)
    elif method == 'round:prev':
        prev_count = game_round.counter - 1
        if prev_count < 0:
            if game_round.round_counter <= 1:
                query.answer(text=_(Text.ALREADY_FIRST_TURN))
                return
            else:
                prev_count = len(actors) - 1
                game_round.round_counter -= 1
        game_round.counter = prev_count
        query.answer()
        refresh_round_message(bot, game_round, _, refresh=True)
        game_round.save()
    elif method == 'round:remove':
        if not gm:
            raise NotGm()

        actors = game_round.get_actors()
        if len(actors) > 1:
            current = actors[game_round.counter % len(actors)]
            current.delete()
            query.answer()
            refresh_round_message(bot, game_round, _, refresh=True)
        else:
            query.answer(show_alert=True, text=_(Text.AT_LEAST_ONE_ACTOR))
    elif method == 'round:finish':
        if not gm:
            raise NotGm()
        query.edit_message_text(_(Text.ROUND_ALREADY_FINISHED))
        remove_round(bot, game_round.chat_id)
Пример #5
0
    def create(self, validated_data):
        # First we check if the action_1 won
        try:
            WinCondition.objects.get(killer=validated_data['action_1'],
                                     victim=validated_data['action_2'])
        except WinCondition.DoesNotExist:
            # Then we check if action_2 won
            try:
                WinCondition.objects.get(killer=validated_data['action_2'],
                                         victim=validated_data['action_1'])
            except WinCondition.DoesNotExist:
                # If there is no winner the field remain null, and because finished date will stopped being null we
                # know that the round was a tie
                winner = None
            else:
                winner = Game.objects.get(
                    id=validated_data['game'].id).player_2
        else:
            winner = Game.objects.get(id=validated_data['game'].id).player_1

        # Here we create the instance of the round
        round = Round(
            # Here we assign the winner
            winner=winner,
            game=validated_data['game'],
            action_1=validated_data['action_1'],
            action_2=validated_data['action_2'])
        round.save()

        # Here we check if the amount of round required is
        rounds_amount = Round.objects.filter(game=round.game).exclude(
            winner=None).count()
        # Todo: Make amount of rounds required dynamic
        if rounds_amount >= 3:
            round.game.is_finished = True
            round.game.finished_at = timezone.now()
            round.game.winner = winner
            round.game.save()

        return round
Пример #6
0
def roomjoin_view(request,gameid):
	game = OnlineGame.objects.get(id = gameid)
	if not game.players.filter(user = request.user):
		game.players.add(Profile.objects.get(user = request.user))
		game.save()
	if (game.players.all().count() >= 2) and game.players.all()[0].user == request.user and not game.isStarted:
		game.isStarted = True
		for index,pl in enumerate(game.players.all()):

			ground = Round()
			ground.player = pl
			ground.save()
			nphrase = Phrase()
			nphrase.gamesCount = -1
			nphrase.name = game.phrasePack.phrases.all()[index].name
			nphrase.author = pl
			nphrase.save()
			ground.phrases.add(nphrase)
			ground.save()
			game.rounds.add(ground)
			game.save()
	
	return render(request, 'game/room.html', context={'game':game,'phrases':game.phrasePack.phrases.all(), 'players':game.players.all(), 'name':Profile.objects.get(user = request.user).name})
Пример #7
0
def create_rounds(room_id, ending_t, trial):
    class Pvp:
        n_real_players = 1 if trial else 2
        name = "pvp"

    class Pve:
        n_real_players = 1
        name = "pve"

    # noinspection PyTypeChecker
    round_types = (Pve, ) * 2 + (Pvp, )

    for rt in round_types:

        # create round and its composition
        round_id = secrets.token_hex(10)

        while Round.objects.filter(round_id=round_id).first():
            round_id = secrets.token_hex(10)

        rd = Round(
            round_id=round_id,
            room_id=room_id,
            real_players=rt.n_real_players,
            missing_players=rt.n_real_players,
            ending_t=ending_t,
            state=rt.name,
            opened=1,
            t=0,
        )

        rd.save()

        composition.create(round_id=round_id, n_real_players=rt.n_real_players)

        data.init(round_id=round_id)
        state.init(round_id=round_id, ending_t=ending_t)