Example #1
0
def prepare(token):
    output = None
    conf_load(True)
    cur_game = Game.get_by_token(token)
    if cur_game and cur_game.participate(g.user) \
            and cur_game.get_attr('state') == cur_game.NEW:
            #and cur_game.state == cur_game.NEW:
        cur_game.state = cur_game.PLAYING
        cur_game.lock()
        waiting = cur_game.pop_waiting()
        if waiting:
            rounds = Rounds.get_rounds(conf('rounds_init'))
            cur_game.prepare(conf('score_init'), rounds)
            output = cur_game.new_round()
            if output.get("type") != 'done':
                output["type"] = 'new'
            cur_game.notice(output)
            cur_game.unlock()
        else:
            try:
                output = cur_game.declare_and_wait(g.user.id, conf('prepare_timeout'))
            except TimeoutError:
                output = cur_game.timeout()
    else:
        output = {"type": "exit",}
    output['state'] = 'new'
    return create_response(cur_game, output)
Example #2
0
def hand_in(token):
    cur_game = Game.get_by_token(token)
    if not cur_game or not cur_game.participate(g.user)\
            or cur_game.get_attr('state') != cur_game.PLAYING:
            #or cur_game.state != cur_game.PLAYING:
        if cur_game:
                    print cur_game.state
        abort(400)

    data = request.get_json()
    if not data:
        abort(400)

    if data['type'] == 'timeout':
        handin = { 
                "timeout": True,
                'time':conf('total_time'),
                'action':Log.create_action(g.user.id,'TIMEOUT')}
    else:
        handin = {
            "timeout": False,
            "time": data['time'],
            "choice": data['choice'],
            'action':Log.create_action(
                g.user.id,
                'NORMAL',
                data['choice'],
                data['time'])
        }
    cur_game.lock()
    actions = [handin['action']]
    mate = cur_game.pop_waiting()
    ack = None
    if mate:
        actions = [handin['action'],mate['action']]
        if handin['timeout'] or mate['timeout']:
            #cur_game.update_score("timeout")
            ack = cur_game.new_round()
            if ack.get("type") != 'done':
                ack['type'] = 'timeout'
        else:
            time = max(handin['time'], mate['time'])
            state = 'fail'
            if mate['choice'] == handin['choice']:
                state = 'match'
            #cur_game.update_score(state, time)
            ack = cur_game.new_round()
            ack['state'] = state
            if ack.get("type") != 'done':
                ack['type'] = state
            #else:
             #   ack = { "type": "unmatch" }
              #  count = cur_game.get_attr('submit_count')
               # count += 1
                #cur_game.set_attr('submit_count', count)
        cur_game.notice(ack)
        cur_game.unlock()
    else:
        try:
            wait_time = conf('timeout_time')
            wait_time += conf('total_time')-handin['time']
            ack = cur_game.declare_and_wait(handin,wait_time)
        except TimeoutError:
            ack = cur_game.timeout()
            ack['state'] = 'exit'
    ack['time'] = handin['time']
    return create_response(cur_game, ack,actions)