Exemplo n.º 1
0
def login():
    """SignIn existing user."""
    global logged_users
    form = SignupForm()
    if request.method == 'GET':
        return render_template('/auth/login.html',
                               form=form,
                               year=datetime.now().year)
    elif request.method == 'POST':
        if form.validate_on_submit():
            player = get_player_by_name(form.name.data)
            if player:
                if player in logged_users:
                    return render_template('/auth/login.html',
                                           form=form,
                                           title="Login",
                                           year=datetime.now().year,
                                           message="already signed?")
                if player.passwd == form.password.data:
                    login_user(player)
                    logged_users.append(player.name)
                    return redirect('/')
                else:
                    return render_template('/auth/login.html',
                                           form=form,
                                           title="Login",
                                           year=datetime.now().year,
                                           message="wrong credentials?")
            else:
                return redirect('/auth/signup')
        else:
            return render_template('/auth/login.html',
                                   form=form,
                                   title='login',
                                   message="user doesn't exist?")
Exemplo n.º 2
0
def signup():
    """SignUp new player."""
    global logged_users
    form = SignupForm()
    if request.method == 'GET':
        return render_template('/auth/signup.html',
                               form=form,
                               title="sign up",
                               year=datetime.now().year)
    elif request.method == 'POST':
        if form.validate_on_submit():
            if get_player_by_name(form.name.data):
                return render_template('/auth/signup.html',
                                       form=form,
                                       title="sign up",
                                       year=datetime.now().year,
                                       message="player already exists?")
            else:
                new_user = signup_new_player(form.name.data,
                                             form.password.data)
                if new_user:
                    login_user(new_user)
                    logged_users.append(new_user.name)
                    return redirect("/")
                else:
                    return render_template(
                        '/auth/signup.html',
                        form=form,
                        title="sign up",
                        year=datetime.now().year,
                        message="error registering new player?")
        else:
            return render_template('/auth/signup.html',
                                   form=form,
                                   title="sign up",
                                   year=datetime.now().year,
                                   message="invalid credentials?")
Exemplo n.º 3
0
def play():
    """POST the move, or GET opponent's move."""
    if request.method == "POST":
        data = request.get_json()
        current_game_board = current_game_get_board(int(data["gameID"]))
        if not current_game_update(int(data["gameID"]), data["move"], data["Iam"]):
            print("failed...")
            return jsonify({"response" : False})
        next_player = current_game_switch_players(int(data["gameID"]))
        return jsonify({"response" : {"board" : current_game_board, 
                                      "gameOver" : False, 
                                      "Won" : False, 
                                      "Draw" : False, 
                                      "toplay" : next_player}})
    elif request.method == "GET":
        data = request.args
        current_game = get_game(int(data["gameID"]))
        current_game_board = current_game_get_board(int(data["gameID"]))
        next_player = data["opponent"]
        game_won_flag = False
        game_over_flag = False
        game_draw_flag = False

        if current_game.ai_to_play:
            if play_winning_move(current_game) is not False:
                print("AI for win!!!")
            elif play_deffensive_move(current_game, data["Iam"]) is not False:
                print("AI in deffense!")
            elif play_offensive_move(current_game) is not False:
                print("AI offensive move?")
            elif play_free_move(current_game) is not False:
                print("AI free move...")
            else:
                print("AI: Oops?")
            next_player = current_game_switch_players(int(data["gameID"]))
            current_game_board = current_game_get_board(int(data["gameID"]))

        if check_if_game_won(current_game, data["Iam"]):
            game_over_flag = True
            game_won_flag = True
            game_draw_flag = False
            player = get_player_by_name(current_user.name)
            player.add_win()
            if current_game.vs_ai:
                ai = get_player_by_name("AI")
                ai.add_lose()
            update_standings()
        elif check_if_game_won(current_game, data["opponent"]):
            game_over_flag = True
            game_won_flag = False
            game_draw_flag = False
            player = get_player_by_name(current_user.name)
            player.add_lose()
            if current_game.vs_ai:
                ai = get_player_by_name("AI")
                ai.add_win()
            update_standings()
        elif current_game.no_more_free_fields:
            game_over_flag = True
            game_won_flag = False
            game_draw_flag = True
            player = get_player_by_name(current_user.name)
            player.add_draw()
            if current_game.vs_ai:
                ai = get_player_by_name("AI")
                ai.add_draw()
            update_standings()
        else:
            game_draw_flag = False
            game_over_flag = False
            game_won_flag = False
        return jsonify({"response" : {"board" : current_game_board, 
                                      "gameOver" : game_over_flag, 
                                      "Won" : game_won_flag, 
                                      "Draw" : game_draw_flag, 
                                      "toplay" : current_game.your_move}})
Exemplo n.º 4
0
def load_user(name):
    return get_player_by_name(name)
Exemplo n.º 5
0
 def test_get_player_by_nameid(self, mock_user):
     test_player = Player("Johnny", "DoeIsPass")
     mock_user.query.filter_by.return_value.first.return_value = test_player
     assert(get_player_by_name("Johnny") == test_player)