Exemple #1
0
def submit_turn(request, game_id):
    statsd.incr("event.play_turn")
    game = get_object_or_404(Game, id=game_id)

    if not game.viewable(request.user):
        return forbidden()
    state = game.deserialize(game.current_state())
    variables, coefficients = state['variables'], state['coefficients']

    while '' in variables.owned_items:
        variables.owned_items.remove('')
    while '' in variables.user_messages:
        variables.user_messages.remove('')

    from engine.simple_controller import adjust_submission
    kwargs = {}
    for key in request.POST:
        kwargs[key] = request.POST.get(key)
    kwargs = adjust_submission(kwargs, variables.names)

    user_submission = game.user_input.schema().deserialize(kwargs)

    for key in user_submission:
        variables[key] = user_submission[key]

    tc = ChainedRandom()
    turn = logic.Turn(variables, coefficients, tc)
    alive, variables = turn.go()

    del variables.people

    old_state = game.current_state()
    new_state = State(name=old_state.name, game=old_state.game)
    new_state.state = json.dumps(dict(
        variables=variables,
        coefficients=coefficients))
    new_state.save()

    if not alive:
        game.mark_finished()
        game.save()

    game.score = game.calculate_score()
    game.save()
    return redirect(game.show_game_url())
Exemple #2
0
def clone_state(request, state_id):
    if not request.user.is_superuser:
        return forbidden("Forbidden")

    state = get_object_or_404(State, id=state_id)

    new_state = State(name=request.POST.get('state_name') or state.name)
    new_state.state = state.state
    new_state.visible = request.POST.get('visible', False) == "True"
    new_state.save()

    # Add new sections
    posted_sections = request.POST.getlist('associated_sections')
    for section_id in posted_sections:
        section = get_object_or_404(CourseSection, id=section_id)
        section.starting_states.add(new_state)
        section.save()

    return redirect(new_state.view_state_url())