From db06d34964a933a7af66fbf6bab2de2943ce0981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nitko=20Nitki=C4=87?= Date: Sun, 25 Feb 2024 16:02:41 +0100 Subject: [PATCH] Add tags + fixes --- backend/game/bots/bots.py | 6 ++++++ backend/routers/admin/admin.py | 2 +- backend/routers/admin/bot.py | 8 +++++--- backend/routers/admin/dataset.py | 6 ++++-- backend/routers/admin/game.py | 10 +++++----- backend/routers/admin/team.py | 6 +++--- backend/routers/users/game.py | 2 +- backend/routers/users/market.py | 12 ++++++------ backend/routers/users/player.py | 8 ++++---- backend/routers/users/power_plant.py | 8 ++++---- 10 files changed, 39 insertions(+), 29 deletions(-) diff --git a/backend/game/bots/bots.py b/backend/game/bots/bots.py index 0110ab0..d61d48e 100644 --- a/backend/game/bots/bots.py +++ b/backend/game/bots/bots.py @@ -12,6 +12,12 @@ class Bots: "resource_bot": ResourceBot } + def list(): + return [ + {"name": bot_name} + for bot_name, bot_id in Bots.bots.items() + ] + def exists(bot_id): return bot_id in Bots.bots diff --git a/backend/routers/admin/admin.py b/backend/routers/admin/admin.py index d4e54ee..3f98274 100644 --- a/backend/routers/admin/admin.py +++ b/backend/routers/admin/admin.py @@ -6,7 +6,7 @@ router = APIRouter() -@router.get("/migrate") +@router.get("/migrate", tags=["admin"]) async def migrate(): await migration.drop_tables() await migration.run_migrations() diff --git a/backend/routers/admin/bot.py b/backend/routers/admin/bot.py index 8c25d1c..195b828 100644 --- a/backend/routers/admin/bot.py +++ b/backend/routers/admin/bot.py @@ -1,5 +1,7 @@ from fastapi import APIRouter +from game.bots.bots import Bots + # BOT PATHS # GET /admin/bot/list - [{"id": [id], "name": [name]}, {}, {}, {}] @@ -7,6 +9,6 @@ router = APIRouter() -@router.get("/bot/list") -async def bot_list(): - return {"message": "Hello World"} +@router.get("/bot/list", tags=["admin"]) +def bot_list(): + return Bots.list() diff --git a/backend/routers/admin/dataset.py b/backend/routers/admin/dataset.py index 83aac1b..2cac29e 100644 --- a/backend/routers/admin/dataset.py +++ b/backend/routers/admin/dataset.py @@ -1,5 +1,7 @@ from fastapi import APIRouter +from model.datasets import Datasets + # DATASET PATHS # GET /admin/dataset/list - [{"id": [id], "name": [name]}, {}, {}, {}] @@ -7,6 +9,6 @@ router = APIRouter() -@router.get("/dataset/list") +@router.get("/dataset/list", tags=["admin"]) async def dataset_list(): - return {"message": "Hello World"} + return await Datasets.list() diff --git a/backend/routers/admin/game.py b/backend/routers/admin/game.py index 8ed417a..d46990c 100644 --- a/backend/routers/admin/game.py +++ b/backend/routers/admin/game.py @@ -37,7 +37,7 @@ class EditGameParams(BaseModel): tick_time: int -@router.post("/game/create") +@router.post("/game/create", tags=["admin"]) async def game_create(params: CreateGameParams): Bots.validate_string(params.bots) @@ -66,26 +66,26 @@ async def game_create(params: CreateGameParams): return {"message": "success"} -@router.get("/game/list") +@router.get("/game/list", tags=["admin"]) async def game_list(): games = await Game.list() return {"games": games} -@router.get("/game/{game_id}/player/list") +@router.get("/game/{game_id}/player/list", tags=["admin"]) async def player_list(game_id: int): players = await Player.list(game_id=game_id) return {"players": players} -@router.get("/game/{game_id}/delete") +@router.get("/game/{game_id}/delete", tags=["admin"]) async def game_delete(game_id: int): # TODO ne baca exception ako je vec zavrsena await Game.update(game_id=game_id, is_finished=True) return {"message": "success"} -@router.post("/game/{game_id}/edit") +@router.post("/game/{game_id}/edit", tags=["admin"]) async def game_edit(game_id: int, params: EditGameParams): if params.bots is not None: Bots.validate_string(params.bots) # a:10;b:10;c:10;d:10 diff --git a/backend/routers/admin/team.py b/backend/routers/admin/team.py index 8451111..194a46a 100644 --- a/backend/routers/admin/team.py +++ b/backend/routers/admin/team.py @@ -22,7 +22,7 @@ def id_generator(size=8, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) -@router.post("/team/create") +@router.post("/team/create", tags=["admin"]) async def team_create(params: CreateTeam): team_secret = id_generator() team_id = await Team.create(team_name=params.team_name, team_secret=team_secret) @@ -30,12 +30,12 @@ async def team_create(params: CreateTeam): return {"team_id": team_id, "team_secret": team_secret} -@router.get("/team/list") +@router.get("/team/list", tags=["admin"]) async def team_list(): return await Team.list() -@router.get("/team/{team_id}/delete") +@router.get("/team/{team_id}/delete", tags=["admin"]) async def team_delete(team_id: int): team_id = await Team.delete(team_id=team_id) diff --git a/backend/routers/users/game.py b/backend/routers/users/game.py index ee89da0..be07b41 100644 --- a/backend/routers/users/game.py +++ b/backend/routers/users/game.py @@ -10,7 +10,7 @@ router = APIRouter() -@router.get("/game/list") +@router.get("/game/list", tags=["users"]) async def game_list(): games = await Game.list() return {"games": games} diff --git a/backend/routers/users/market.py b/backend/routers/users/market.py index 68330b8..6c36fa4 100644 --- a/backend/routers/users/market.py +++ b/backend/routers/users/market.py @@ -15,7 +15,7 @@ router = APIRouter() -@router.get("/game/{game_id}/market/offer/list") +@router.get("/game/{game_id}/market/offer/list", tags=["users"]) async def offer_list(game_id: int = Depends(game_id)): return await Order.list( game_id=game_id, @@ -23,7 +23,7 @@ async def offer_list(game_id: int = Depends(game_id)): ) -@router.get("/game/{game_id}/market/offer/prices/from/{start_tick}/to/{end_tick}") +@router.get("/game/{game_id}/market/offer/prices/from/{start_tick}/to/{end_tick}", tags=["users"]) async def offer_list(game_id: int = Depends(game_id), start_tick: int = 0, end_tick: int = 0): if start_tick < 0 or end_tick < 0: raise HTTPException( @@ -41,7 +41,7 @@ class EnergyPrice(BaseModel): price: int -@router.post("/game/{game_id}/player/{player_id}/market/energy/set_price") +@router.post("/game/{game_id}/player/{player_id}/market/energy/set_price", tags=["users"]) async def energy_set_price_player(price: EnergyPrice, game_id: int = Depends(game_id), player: int = Depends(player)): if price <= 0: raise HTTPException( @@ -64,7 +64,7 @@ class UserOrder(BaseModel): type: OrderType -@router.post("/game/{game_id}/player/{player_id}/market/offer/create") +@router.post("/game/{game_id}/player/{player_id}/market/offer/create", tags=["users"]) async def offer_create_player(order: UserOrder, game_id: int = Depends(game_id), player: int = Depends(player)): if order.type == OrderType.ENERGY: raise Exception( @@ -92,7 +92,7 @@ async def offer_create_player(order: UserOrder, game_id: int = Depends(game_id), return {"success": True} -@router.post("/game/{game_id}/player/{player_id}/market/offer/list") +@router.post("/game/{game_id}/player/{player_id}/market/offer/list", tags=["users"]) async def offer_list_player(game_id: int = Depends(game_id), player: int = Depends(player)): return await Order.list( game_id=game_id, @@ -105,7 +105,7 @@ class OfferCancel(BaseModel): ids: List[int] -@router.post("/game/{game_id}/player/{player_id}/market/offer/cancel") +@router.post("/game/{game_id}/player/{player_id}/market/offer/cancel", tags=["users"]) async def offer_cancel_player(body: OfferCancel, game_id: int = Depends(game_id), player: int = Depends(player)): for order_id in body.ids: await Order.update( diff --git a/backend/routers/users/player.py b/backend/routers/users/player.py index df24ded..6872386 100644 --- a/backend/routers/users/player.py +++ b/backend/routers/users/player.py @@ -15,7 +15,7 @@ router = APIRouter() -@router.get("/game/{game_id}/player/list") +@router.get("/game/{game_id}/player/list", tags=["users"]) async def player_list(game_id: int, team_id: int = Depends(team_id)): players = await Player.list(game_id=game_id, team_id=team_id, is_active=True) return {"players": players} @@ -25,7 +25,7 @@ class PlayerCreate(BaseModel): player_name: str = None -@router.post("/game/{game_id}/player/create") +@router.post("/game/{game_id}/player/create", tags=["users"]) async def player_create(game_id: int = Depends(game_id), team_id: int = Depends(team_id), player_name: str = None): async with database.transaction(): if player_name is None: @@ -37,11 +37,11 @@ async def player_create(game_id: int = Depends(game_id), team_id: int = Depends( return {"player_id": player_id} -@router.get("/game/{game_id}/player/{player_id}") +@router.get("/game/{game_id}/player/{player_id}", tags=["users"]) async def player_get(player=Depends(player)): return await Player.get(player_id=player.player_id) -@router.get("/game/{game_id}/player/{player_id}/delete") +@router.get("/game/{game_id}/player/{player_id}/delete", tags=["users"]) async def player_delete(player=Depends(player)): return {"successfull": await Player.update(player_id=player.player_id, is_active=False)} diff --git a/backend/routers/users/power_plant.py b/backend/routers/users/power_plant.py index 5c9f029..b7df43d 100644 --- a/backend/routers/users/power_plant.py +++ b/backend/routers/users/power_plant.py @@ -21,7 +21,7 @@ router = APIRouter(dependencies=[Depends(player)]) -@router.get("/game/{game_id}/player/{player_id}/plant/list") +@router.get("/game/{game_id}/player/{player_id}/plant/list", tags=["users"]) async def list_plants(player_id: int): player = await Player.get(player_id=player_id) @@ -40,7 +40,7 @@ class PlantBuySell(BaseModel): type: PowerPlantType -@router.post("/game/{game_id}/player/{player_id}/plant/buy") +@router.post("/game/{game_id}/player/{player_id}/plant/buy", tags=["users"]) async def buy_plant(player_id: int, plant: PlantBuySell): type = PowerPlantType(plant.type) @@ -56,7 +56,7 @@ async def buy_plant(player_id: int, plant: PlantBuySell): await Player.update(player_id=player_id, **{type.name.lower() + "_plants_owned": plant_count + 1}) -@router.post("/game/{game_id}/player/{player_id}/plant/sell") +@router.post("/game/{game_id}/player/{player_id}/plant/sell", tags=["users"]) async def sell_plant(player_id: int, plant: PlantBuySell): type = PowerPlantType(plant.type) @@ -73,7 +73,7 @@ class PowerOn(BaseModel): number: int -@router.post("/game/{game_id}/player/{player_id}/plant/on") +@router.post("/game/{game_id}/player/{player_id}/plant/on", tags=["users"]) async def turn_on(player_id: int, plant: PowerOn): async with database.transaction(): player = await Player.get(player_id=player_id)