Beispiel #1
0
 def test_player_invalid(self):
     form = NewGameForm({'player1': '', 'player2': 'foobar'})
     self.assertFalse(form.is_valid())
     self.assertEqual(
         form.errors, {
             'player1': ['This field is required.'],
             'player2': ['Unknown player type: foobar']
         })
Beispiel #2
0
 def test_player_invalid(self):
     form = NewGameForm({
         'player1': '',
         'player2': 'foobar'
     })
     self.assertFalse(form.is_valid())
     self.assertEqual(form.errors, {
         'player1': ['This field is required.'],
         'player2': ['Unknown player type: foobar']
     })
Beispiel #3
0
    def test_create(self):
        "Tests that create returns a model with the players assigned."
        random.seed(1)

        form = NewGameForm({
            'player1': 'game.players.RandomPlayer',
            'player2': 'human'
        })
        self.assertTrue(form.is_valid())
        game = form.create()
        self.assertEqual(game.board, "         ")
        self.assertEqual(game.player_x, "human")
        self.assertEqual(game.player_o, "game.players.RandomPlayer")
Beispiel #4
0
    def test_create(self):
        "Tests that create returns a model with the players assigned."
        random.seed(1)

        form = NewGameForm({
            'player1': 'game.players.AIPlayer',
            'player2': 'human'
        })
        self.assertTrue(form.is_valid())
        game = form.create()
        self.assertEqual(game.board, "         ")
        self.assertEqual(game.player_x, "human")
        self.assertEqual(game.player_o, "game.players.AIPlayer")
Beispiel #5
0
def new():
    form = NewGameForm()
    if request.method == 'POST' and form.validate_on_submit():
        game_settings = GameSettings(generations_per_round=form.generations_per_round.data,
                                     rounds_number=form.rounds_number.data,
                                     new_cells_per_round=form.new_cells_per_round.data)
        game = GameOfLife(settings=game_settings)
        game_db = Game(first_player_id=current_user.id,
                        settings=game_settings.ToJSON(),
                        state=game._GameOfLife__state.ToJSON())
        db.session.add(game_db)
        db.session.commit()
        return redirect('/game/%d' % game_db.id)
    return render_template('new_game.html', form=form)
Beispiel #6
0
def new_game(request):
    if request.method == 'POST':
        if request.POST['act'] == 'setting':
            form = NewSettingForm(request.POST)
            if form.is_valid():
                setting = form.save(commit=False)
                setting.owner = request.user
                setting.save()
                return redirect(reverse('new_game'))
        elif request.POST['act'] == 'game':
            form = NewGameForm(request.POST)
            if form.is_valid():
                form.save()
                return redirect(
                    reverse('edit_setting') + '?setting=' +
                    str(form.cleaned_data.get('setting').pk))

    settings = Setting.objects.filter(owner=request.user).all()

    if settings.__len__() == 0 or 'setting' in request.GET.keys():
        if request.method != 'POST':
            form = NewSettingForm(None)
        title = 'Создайте Сеттинг'
        action = 'setting'
    else:
        if request.method != 'POST':
            form = NewGameForm(user=request.user)  # , user=request.user)
        title = 'Создайте Игру'
        action = 'game'

    return render(
        request, 'main/newgame.html', {
            'form': form,
            'title': title,
            'action': action,
            'menu': generate_menu(request)
        })
Beispiel #7
0
def new_game(request):
    context = dict()
    if request.method == 'POST':
        form = NewGameForm(request.POST)
        if form.is_valid():
            try:
                game_check = Game.objects.get(name__iexact=form.cleaned_data['name'])
                if game_check:
                    messages.error(request,
                                   'Game name already taken. Try again.')
                    if mobileBrowser(request):
                        return render(request, 'mobile/new_game.html', context)
                    else:
                        return render(request, 'desktop/new_game.html', context)
            except:
                print("All good")

            game = Game.objects.create(
                name=form.cleaned_data['name'].strip()
            )
            user = request.user
            game.players.add(user)
            game.host = user
            game.save()
            Score.objects.create(
                player=user,
                game=game
            )
            return HttpResponseRedirect(game.get_url())

    form = NewGameForm()
    context['form'] = form
    if mobileBrowser(request):
        return render(request, 'mobile/new_game.html', context)
    else:
        return render(request, 'desktop/new_game.html', context)
Beispiel #8
0
 def test_player_valid(self):
     form = NewGameForm({
         'player1': 'game.players.RandomPlayer',
         'player2': 'human'
     })
     self.assertTrue(form.is_valid())
Beispiel #9
0
 def test_player_valid(self):
     form = NewGameForm({
         'player1': 'game.players.AIPlayer',
         'player2': 'human'
     })
     self.assertTrue(form.is_valid())