Example #1
0
def test_DB():

    player = Player()
    player.login = '******'
    scenario = Scenario()
    scenario.name = 'scenario 1'
    scenario.leadtime = 10
    scenario.duration = 20
    gameboard = GameBoard()
    gameboard.period = 1
#    T = TableDict(10)
#    T.set_cell('order', 5, 1000)
#    T.set_cell('forecast', 8, 10)

    gameboard.data = TableDict(10)
    gameboard.data.set_cell('order', 5, 1000)
    gameboard.data.set_cell('forecast', 8, 10)

    scenario_counter = ScenarioCounter()

    player.gameboards.append(gameboard)
    player.played_scenario.append(scenario)
    player.scenario_counters.append(scenario_counter)
    scenario.gameboards.append(gameboard)
    scenario.counters.append(scenario_counter)

    db.session.add(player)
    db.session.add(scenario)
    db.session.add(gameboard)
    db.session.add(scenario_counter)

    db.session.commit()

    pl = db.session.query(Player).first()
    assert_equal('Pippo', pl.login)
    assert_equal('scenario 1', pl.played_scenario[0].name)
    d = pl.gameboards.filter(GameBoard.scenario_id==scenario.id).first()

    assert_in('<table', d.data.get_HTML())
    assert_equal(d.data.get_cell('order', 5), 1000)
    assert_equal(d.data.get_cell('order', 8), 0)
    assert_equal(d.data.get_cell('forecast', 8), 10)
    assert_equal(d.data.get_cell('forecast', 1), 0)
    assert_equal(d.data.get_cell('forecast', 15), None)
Example #2
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)