Exemplo n.º 1
0
def update_data_main_page():
    """return json with information about:

    - users who wait second player
    - games which does now

    the structure of dictionary is:
    { 'users':{ 'id1':'username1',
                'id2':'username2'},

      'games': { 'id':[user1, user2],
                 'id':[user1, user2]}}
    """
    if request.method == 'POST':
        current_user = "******"
        if request.cookies.has_key('session_id'):
            cookie_session = request.cookies.get('session_id')
            update_user(**{ 'session_id': cookie_session, 'time': True })
            user_status = get_user_status(cookie_session)
            if user_status == 1:
                return jsonify(user_status=user_status)
            if user_status == 0:
                user = get_user_by_session(cookie_session)
                if user:
                    current_user = user.user_name
        users = get_wait_users()
        list_username = {}
        for user in users:
            list_username[str(user.id)] = user.user_name
        games = get_begin_games()
        user_server = get_user_id(cookie_session)
        return jsonify(users=list_username, 
                       games=games, 
                       current_user=current_user, 
                       user_server=user_server)
Exemplo n.º 2
0
def move_battle():
    """move to battle
    """
    if request.method == 'POST':
        if request.cookies.has_key('session_id'):
            cookie_session = request.cookies.get('session_id')
            new_data_user = {'session_id':cookie_session,
                             'status': 4,
                             'time': True}
            update_user(**new_data_user)
            return jsonify(result=1)
Exemplo n.º 3
0
def get_state_field():
    """get data about field from js
    """
    if request.method == 'POST':
        if request.cookies.has_key('session_id'):
            cookie_session = request.cookies.get('session_id')
            update_user(**{ 'session_id': cookie_session, 'time': True })
            field = json.loads(request.form.keys()[0])
            if update_field(cookie_session, field):
                return jsonify(result='1')
            else:
                return jsonify(result='0')
Exemplo n.º 4
0
def check_shot():
    """check a shot and return result of shot
    """
    if request.method == 'POST':
        if request.cookies.has_key('session_id'):
            cookie_session = request.cookies.get('session_id')
            update_user(**{ 'session_id': cookie_session, 'time': True })
            coordinata = request.form['coordinata'].encode('utf8')
            result = get_value_coordinata(cookie_session, coordinata)
            field_opponent = get_field_opponent(cookie_session)
            return jsonify(result=result,
                           coordinata=coordinata,
                           field_opponent=field_opponent)
Exemplo n.º 5
0
def send_state_field():
    """return the current snapshot field from database
    """
    if request.method == 'POST':
        if request.cookies.has_key('session_id'):
            cookie_session = request.cookies.get('session_id')
            update_user(**{ 'session_id': cookie_session, 'time': True })
            field = get_field_dictionary(cookie_session)
            user_status = get_user_status(cookie_session)
            number_watch_user = get_watch_users(cookie_session)
            return jsonify(field=field,
                           status=user_status,
                           number_watch_user=number_watch_user)
Exemplo n.º 6
0
def add_new_user():
    """registration user in database for game

    jsonify: 
     - `username` : username from form 
     - `user_id` : set user.id for uniq
     - `new_user`: `1` - this new user
                   `0` - this old user, dont hand
    """
    if request.method == 'POST':
        username = request.form.values()[0].encode('utf8')
        # check session
        if request.cookies.has_key('session_id'):
            cookie_session = request.cookies.get('session_id')
        else:
            cookie_session = randstring()
        user = get_user_by_session(cookie_session)
        # if user exist
        if user:
            print 'test'
            update_user(**{'session_id': cookie_session, 'time': True })
            # if already have a server
            if user.status == 0:
                return jsonify(new_user=0)
            # if game was finished
            if user.status == 5 or user.status == 6 or user.status == 7:
                field = add_new_field()
                game = add_new_game(field)
                if add_user_in_db(cookie_session, username, game, field):
                    return jsonify(username=username,
                                   user_id=get_user_id(cookie_session),
                                   user_status=0, 
                                   new_user=1)
            # if in game play now
            if user.status == 3 or user.status == 4:
                return jsonify(new_user=2)
        else:
            field = add_new_field()
            game = add_new_game(field)
            if add_user_in_db(cookie_session, username, game, field):
                return jsonify(username=username,
                               user_id=get_user_id(cookie_session),
                               user_status=0, 
                               new_user=1)
Exemplo n.º 7
0
def reset_game():
    """reset current games
    """
    if request.method == 'POST':
        if request.cookies.has_key('session_id'):
            cookie_session = request.cookies.get('session_id')
            field = add_new_field()
            game = add_new_game(field)
            new_data_user = { 'game': game,
                              'field': field,
                              'session_id': cookie_session,
                              'status': 0,
                              'time': True}
            username = update_user(**new_data_user)
            if username:
                return jsonify(username=username,
                           user_id=get_user_id(cookie_session),
                           user_status=0, 
                           new_user=1)
Exemplo n.º 8
0
def add_second_user():
    """registration second user for play
    """
    if request.method == 'POST': 
        wait_user = request.form['user_id'].encode('utf8')
        username = request.form['username'].encode('utf8')
        if request.cookies.has_key('session_id'):
            cookie_session = request.cookies.get('session_id')
        # TODO: this is bottleneck
        # add exception here!
        current_user = get_user_by_session(cookie_session)
        if current_user and str(current_user.id) == wait_user:
            return jsonify(result="0")
        field = add_new_field()
        game = add_field_in_game(wait_user, field)
        add_user = add_user_in_db(cookie_session, username, game, field, status=1, status_first=2)
        session_wait_user = get_session_by_user_id(wait_user)
        new_data_user = { 'session_id' : session_wait_user,
                          'status': 1 }
        update_status = update_user(**new_data_user)
        if add_user and update_status:
            return jsonify(result="1")