diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml new file mode 100644 index 0000000..da0791c --- /dev/null +++ b/.github/workflows/python-app.yml @@ -0,0 +1,39 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python application + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: "3.8" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest diff --git a/src/blueprints/attack.py b/src/blueprints/attack.py index f397c73..ca89f34 100644 --- a/src/blueprints/attack.py +++ b/src/blueprints/attack.py @@ -1,14 +1,16 @@ from flask import Blueprint , jsonify , current_app from flask import request import random +from src.tools.check_token import token_required +from src.tools.check_player import check_player attack = Blueprint('attack',__name__) main_game = current_app.config['main_game'] @attack.route('/attack',methods=['POST']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def attack_func(player_id): # this API used to attack a node from another node diff --git a/src/blueprints/fort.py b/src/blueprints/fort.py index 7ac1a08..3919c76 100644 --- a/src/blueprints/fort.py +++ b/src/blueprints/fort.py @@ -1,13 +1,15 @@ from flask import Blueprint , jsonify , current_app from flask import request +from src.tools.check_token import token_required +from src.tools.check_player import check_player fort = Blueprint('fort',__name__) main_game = current_app.config['main_game'] @fort.route('/fort',methods=['POST']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def fort_func(player_id): # this API used to apply the fortification ability of the player diff --git a/src/blueprints/get_adj.py b/src/blueprints/get_adj.py index db2ddd8..6b6447f 100644 --- a/src/blueprints/get_adj.py +++ b/src/blueprints/get_adj.py @@ -1,13 +1,14 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_adj = Blueprint('get_adj',__name__) main_game = current_app.config['main_game'] @get_adj.route('/get_adj',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_adj_func(player_id): # this API used to the list of the adjacent nodes of each node output_dict = {} diff --git a/src/blueprints/get_number_of_fort_troops.py b/src/blueprints/get_number_of_fort_troops.py index 4dbfadb..f995662 100644 --- a/src/blueprints/get_number_of_fort_troops.py +++ b/src/blueprints/get_number_of_fort_troops.py @@ -1,13 +1,14 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_number_of_fort_troops = Blueprint('get_number_of_fort_troops',__name__) main_game = current_app.config['main_game'] @get_number_of_fort_troops.route('/get_number_of_fort_troops',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_number_of_fort_troops_func(player_id): # this API used to get the number of fort troops on each node output_dict = {} diff --git a/src/blueprints/get_number_of_troops_to_put.py b/src/blueprints/get_number_of_troops_to_put.py index e5506ac..7536509 100644 --- a/src/blueprints/get_number_of_troops_to_put.py +++ b/src/blueprints/get_number_of_troops_to_put.py @@ -1,5 +1,6 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_number_of_troops_to_put = Blueprint('get_number_of_troops_to_put',__name__) @@ -7,8 +8,8 @@ main_game = current_app.config['main_game'] @get_number_of_troops_to_put.route('/get_number_of_troops_to_put',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_number_of_troops_to_put_func(player_id): # return the number of troops that the player can put on the map output_dict={"number_of_troops": main_game.player_turn.number_of_troops_to_place} diff --git a/src/blueprints/get_owners.py b/src/blueprints/get_owners.py index ddff211..9b07639 100644 --- a/src/blueprints/get_owners.py +++ b/src/blueprints/get_owners.py @@ -1,13 +1,14 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_owners = Blueprint('get_owners',__name__) main_game = current_app.config['main_game'] @get_owners.route('/get_owners',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_owners_func(player_id): output_dict = {} for node in main_game.nodes.values(): diff --git a/src/blueprints/get_player_id.py b/src/blueprints/get_player_id.py index 6e8b3b3..d181952 100644 --- a/src/blueprints/get_player_id.py +++ b/src/blueprints/get_player_id.py @@ -1,5 +1,6 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_player_id = Blueprint('get_player_id',__name__) @@ -7,8 +8,8 @@ main_game = current_app.config['main_game'] @get_player_id.route('/get_player_id',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_player_number_func(player_id): output_dict={'player_id': player_id} return jsonify(output_dict),200 \ No newline at end of file diff --git a/src/blueprints/get_reachable.py b/src/blueprints/get_reachable.py index c809770..18af18e 100644 --- a/src/blueprints/get_reachable.py +++ b/src/blueprints/get_reachable.py @@ -1,14 +1,16 @@ from flask import Blueprint , jsonify , current_app from flask import request from src.tools.find_reachable import find_reachable +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_reachable = Blueprint('get_reachable',__name__) main_game = current_app.config['main_game'] @get_reachable.route('/get_reachable',methods=['POST']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_reachable_func(player_id): # this API used to find all the nodes that the owner can move it's troops from node_id to them # body of the request should be like this: diff --git a/src/blueprints/get_state.py b/src/blueprints/get_state.py index f9814ab..ebfd729 100644 --- a/src/blueprints/get_state.py +++ b/src/blueprints/get_state.py @@ -1,5 +1,6 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_state = Blueprint('get_state',__name__) @@ -7,8 +8,8 @@ main_game = current_app.config['main_game'] @get_state.route('/get_state',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_state_func(player_id): output_dict={'state': main_game.state} return jsonify(output_dict),200 \ No newline at end of file diff --git a/src/blueprints/get_strategic_nodes.py b/src/blueprints/get_strategic_nodes.py index 7cf996f..d38dd74 100644 --- a/src/blueprints/get_strategic_nodes.py +++ b/src/blueprints/get_strategic_nodes.py @@ -1,13 +1,14 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_strategic_nodes = Blueprint('get_strategic_nodes',__name__) main_game = current_app.config['main_game'] @get_strategic_nodes.route('/get_strategic_nodes',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_strategic_nodes_func(player_id): output_dict={'strategic_nodes': [i.id for i in main_game.nodes.values() if i.is_strategic], 'score': [i.score_of_strategic for i in main_game.nodes.values() if i.is_strategic]} diff --git a/src/blueprints/get_troops_count.py b/src/blueprints/get_troops_count.py index cf08632..48fe015 100644 --- a/src/blueprints/get_troops_count.py +++ b/src/blueprints/get_troops_count.py @@ -1,13 +1,14 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_troops_count = Blueprint('get_troops_count',__name__) main_game = current_app.config['main_game'] @get_troops_count.route('/get_troops_count',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_troops_count_func(player_id): output_dict = {} for node in main_game.nodes.values(): diff --git a/src/blueprints/get_turn_number.py b/src/blueprints/get_turn_number.py index 895d6e2..4b25be7 100644 --- a/src/blueprints/get_turn_number.py +++ b/src/blueprints/get_turn_number.py @@ -1,13 +1,14 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player get_turn_number = Blueprint('get_turn_number',__name__) main_game = current_app.config['main_game'] @get_turn_number.route('/get_turn_number',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def get_turn_number_func(player_id): output_dict={'turn_number': main_game.turn_number} return jsonify(output_dict),200 \ No newline at end of file diff --git a/src/blueprints/move_troop.py b/src/blueprints/move_troop.py index a90c5b5..e715eb1 100644 --- a/src/blueprints/move_troop.py +++ b/src/blueprints/move_troop.py @@ -1,14 +1,16 @@ from flask import Blueprint , jsonify , current_app from flask import request from src.tools.find_path import find_path +from src.tools.check_token import token_required +from src.tools.check_player import check_player move_troop = Blueprint('move_troop',__name__) main_game = current_app.config['main_game'] @move_troop.route('/move_troop',methods=['POST']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def move_troop_func(player_id): # this API used to move troops from source to destination diff --git a/src/blueprints/next_state.py b/src/blueprints/next_state.py index 58b270f..cbe969b 100644 --- a/src/blueprints/next_state.py +++ b/src/blueprints/next_state.py @@ -1,13 +1,14 @@ from flask import Blueprint , jsonify , current_app - +from src.tools.check_token import token_required +from src.tools.check_player import check_player next_state = Blueprint('next_state',__name__) main_game = current_app.config['main_game'] @next_state.route('/next_state',methods=['GET']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def next_state_func(player_id): ''' This function is used to change the state of the game to the next state diff --git a/src/blueprints/put_one_troop.py b/src/blueprints/put_one_troop.py index e3cc817..56710d5 100644 --- a/src/blueprints/put_one_troop.py +++ b/src/blueprints/put_one_troop.py @@ -1,13 +1,15 @@ from flask import Blueprint , jsonify , current_app from flask import request +from src.tools.check_token import token_required +from src.tools.check_player import check_player put_one_troop = Blueprint('put_one_troop',__name__) main_game = current_app.config['main_game'] @put_one_troop.route('/put_one_troop',methods=['POST']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def put_one_troop_func(player_id): # this API is used to put one troop on the map in the initial troop state of the game diff --git a/src/blueprints/put_troop.py b/src/blueprints/put_troop.py index 49bb346..ae8e1c6 100644 --- a/src/blueprints/put_troop.py +++ b/src/blueprints/put_troop.py @@ -1,13 +1,15 @@ from flask import Blueprint , jsonify , current_app from flask import request +from src.tools.check_token import token_required +from src.tools.check_player import check_player put_troop = Blueprint('put_troop',__name__) main_game = current_app.config['main_game'] @put_troop.route('/put_troop',methods=['POST']) -@current_app.config['token_required'] -@current_app.config['check_player'] +@token_required +@check_player def put_troop_func(player_id): # this API used to put troops in the map in the put troop state diff --git a/src/blueprints/ready.py b/src/blueprints/ready.py index 964a3f6..6be176f 100644 --- a/src/blueprints/ready.py +++ b/src/blueprints/ready.py @@ -7,6 +7,7 @@ from flask import current_app from flask import jsonify import os +from src.tools.check_token import token_required # get the main_game instance from the flask global variable main_game = current_app.config['main_game'] @@ -17,7 +18,7 @@ @ready.route('/ready', methods=['GET']) -@current_app.config['token_required'] +@token_required def ready_func(player_id): try: main_game.players[player_id].is_ready = True