Exemple #1
0
def start_game_req_response(message):
    game_object = None
    username = message['user']['username'].encode("utf8")
    opponent_name = message['opponent'].encode("utf8")
    lobby = sf.get_lobby()
    user_in_lobby = False
    opponent_in_lobby = False
    for user in lobby:
        if user['username'] == username:
            user_in_lobby = True
        if user['username'] == opponent_name:
            opponent_in_lobby = True
    if user_in_lobby and opponent_in_lobby:
        sf.update_user_attribute(username, 'in_game', True)
        game_object = sf.add_game(username, opponent_name)
    else:
        print 'Error: could not start game, a user was not in the lobby'
    return_obj = {
                  'type': 'start_game_req_resp',
                  'id'  : message['id'],
                  'game' : game_object
                 }
    return return_obj
Exemple #2
0
def start_game_resp_response(message):
    game_object = None
    username = message['user']['username'].encode("utf8")
    game = sf.get_game(username)
    if game != None and message['response'] is True:
        sf.update_game_attribute(game['user1'], game['user2'], 'responded', True)
        sf.update_game_attribute(game['user1'], game['user2'], 'response', True)
        game_object = sf.get_game(username)
        sf.update_user_attribute(username, 'in_game', True)
    elif game != None:
        sf.update_game_attribute(game['user1'], game['user2'], 'responded', True)
        sf.update_game_attribute(game['user1'], game['user2'], 'response', False)
        game_object = sf.get_game(username)
        sf.remove_game(game_object['user1'], game_object['user2'])
        sf.update_user_attribute(game_object['user1'], 'in_game', False)
        sf.update_user_attribute(game_object['user2'], 'in_game', False)
    return_obj = {
                  'type': 'start_game_resp_resp',
                  'id'  : message['id'],
                  'game' : game_object
                 }
    return return_obj
Exemple #3
0
def game_object_reqst_response(message):
    game_object = sf.get_game(message['user']['username'])
    if game_object != None and game_object['game_over'] is True:
        username1 = game_object['user1']
        username2 = game_object['user2']
        winner = game_object['winner']
        if winner == username1:
            sf.update_user_attribute(username1, 'win_value', sf.get_user(username1)['win_value']+1)
            sf.update_user_attribute(username2, 'lose_value', sf.get_user(username2)['lose_value']+1)
        elif winner == username2:
            sf.update_user_attribute(username1, 'lose_value', sf.get_user(username1)['lose_value']+1)
            sf.update_user_attribute(username2, 'win_value', sf.get_user(username2)['win_value']+1)
        else:
            sf.update_user_attribute(username1, 'draw_value', sf.get_user(username1)['draw_value']+1)
            sf.update_user_attribute(username2, 'draw_value', sf.get_user(username2)['draw_value']+1)
        sf.update_user_attribute(username1, 'in_game', False)
        sf.update_user_attribute(username2, 'in_game', False)
        sf.remove_game(game_object['user1'], game_object['user2'])
    return_obj = {
                  'type': 'game_object_reqst_resp',
                  'id'  : message['id'],
                  'game' : game_object
                 }
    return return_obj