Beispiel #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 })
Beispiel #2
0
def new_game():
    """Form for submitting new game"""
    #name,players = None,None
    create_game_form = NewGameForm()
    if create_game_form.validate_on_submit():
        #split the string of player names
        #TODO: error handling if this is not formatted correctly
        players = create_game_form.players.data.split(',')
        #Instantiate class
        game  = Game(name=create_game_form.name.data)
        #add players
        game.add_players(player_names=players,num_players=len(players))
        #add to database
        game.archive(db)

    return render_template('new_game.html',form=create_game_form)
Beispiel #3
0
def new_game():
    """Form for submitting new game"""
    #name,players = None,None
    create_game_form = NewGameForm()
    if create_game_form.validate_on_submit():
        #split the string of player names
        #TODO: error handling if this is not formatted correctly
        players = create_game_form.players.data.split(',')
        #Instantiate class
        game = Game(name=create_game_form.name.data)
        #add players
        game.add_players(player_names=players, num_players=len(players))
        #add to database
        game.archive(db)

    return render_template('new_game.html', form=create_game_form)
Beispiel #4
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)
Beispiel #5
0
def enter_game_code_view():
    form = NewGameForm()
    if form.validate_on_submit():
        # get the id of the scenario corresponding to the code
        scenario = db.session.query(Scenario).filter(and_(Scenario.code == form.code.data,
                                                          Scenario.status == ACTIVE)).first()

        player = db.session.query(Player).get(current_user.get_id())

        if scenario:  # the code is valid
            # check if the scenario is ACTIVE or RUNNING
            # if so, redirect to the game
            if scenario in player.played_scenario and scenario.status in [ACTIVE, RUNNING]:
                session['scenario_code'] = scenario.code
                session['scenario_id'] = scenario.id
                return redirect(url_for('demand_game_dashboard'))


            player.played_scenario.append(scenario)
            db.session.commit()
            session['scenario_id'] = scenario.id
            session['scenario_code'] = scenario.code
            scenario = Scenario.query.get(scenario.id)

            #init the gameboard
            gameboard = GameBoard()
            gameboard.period = 1
            gameboard.table = cPickle.dumps(TableDict(scenario.duration)) # a dump of the table containing th game data

            player.gameboards.append(gameboard)
            scenario.gameboards.append(gameboard)

            db.session.add(gameboard)
            db.session.commit()

            return redirect(url_for('demand_game_dashboard'))
        else:
            form = NewGameForm()
            return render_template('new_game_form.html', form=form, message='Code not valid')

    return render_template('new_game_form.html', form=form)
Beispiel #6
0
def new_game():
    form = NewGameForm()
    if form.validate_on_submit():
        rows = form.rows.data
        columns = form.columns.data
        mines = form.mines.data
        if mines < rows * columns:
            session['game_board'] = ms.create_game_board(rows,
                                                         columns,
                                                         mines=mines)
            session['game_on'] = True
            return redirect(url_for('render_board'))
        else:
            flash(u"You can't have more mines than spaces!")
    else:
        for field, errors in form.errors.items():
            for error in errors:
                flash(u"Error in the %s field - %s" %
                      (getattr(form, field).label.text, error))

    return render_template('new_game.html', form=form)
Beispiel #7
0
def new_game():
    """Form for submitting new game"""
    #name,players = None,None
    create_game_form = NewGameForm()
    if create_game_form.validate_on_submit():
        #split the string of player names
        #TODO: error handling if this is not formatted correctly
        players = create_game_form.players.data.split(',')
        #Instantiate class
        game = scrabble.Game(name=create_game_form.name.data)
        #add players
        game.add_players(player_names=players, num_players=len(players))
        #add to database
        game_archive = pack_game(game)
        db.session.add(game_archive)
        db.session.commit()
        cur_game(game.name)
        #create player pages
        for p in players:
            player_view(game.name, p)

    return render_template('new_game.html', form=create_game_form)
Beispiel #8
0
def new_game():
    """Form for submitting new game"""
    #name,players = None,None
    create_game_form = NewGameForm()
    if create_game_form.validate_on_submit():
        #split the string of player names
        #TODO: error handling if this is not formatted correctly
        players = create_game_form.players.data.split(',')
        #Instantiate class
        game  = scrabble.Game(name=create_game_form.name.data)
        #add players
        game.add_players(player_names=players,num_players=len(players))
        #add to database
        game_archive = pack_game(game)
        db.session.add(game_archive)
        db.session.commit()
        cur_game(game.name)
        #create player pages
        for p in players:
            player_view(game.name,p)

    return render_template('new_game.html',form=create_game_form)