Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
0
 def test_add_game(self, new_field):
     print add_new_game(new_field)
     self.count += 1