Example #1
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)
Example #2
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
Example #3
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})
Example #4
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)