Пример #1
0
def open_or_new_games(request):
    if not request.COOKIES.get('nickname', ''):
        return HttpResponseRedirect(reverse(set_nickname))

    if request.method == 'POST':
        form = NewGameForm(request.POST)
        if form.is_valid():
            nickname = re.sub(r'\W', '', request.COOKIES.get('nickname', ''))
            num_players = form.cleaned_data['number_of_players']
            game = Game(name=nickname, num_players=num_players)
            game.save()
            try:
                player = game.add_player(nickname, True)
                request.session['player_id'] = player.id
                request.session['game_id'] = game.id
            except Game.InvalidPlayer as x:
                pass
                #TODO: add session error message

            return HttpResponseRedirect(reverse(current_game))
    else:
        form = NewGameForm()

    return direct_to_template(request, 
            'open_or_new_games.html', {'form': form })
Пример #2
0
def new_game(request):
    form = NewGameForm()
    if request.method == 'POST':
        form = NewGameForm(request.POST)
        if form.is_valid():
            player_number = int(form.cleaned_data['player_number'])
            port_number = int(form.cleaned_data['magic_number'])
            game = Game.objects.create(player_number=player_number, port_number=port_number, connected_number=1)
            game.players.add(request.user)
            game.save()
            # Popen(['./../game/pyciv.py', 'save' + '.json', str(port_number), str(player_number)])
            return HttpResponseRedirect('/enter')
    context = {}
    context.update(csrf(request))
    context['form'] = form

    return render_to_response('main/account/new_game.html', context)