Skip to content

Commit

Permalink
Merge pull request #11 from The-Galley/fix/lobby-fix
Browse files Browse the repository at this point in the history
Mini fixes
  • Loading branch information
andy-takker committed Apr 4, 2024
2 parents bfc6e64 + 201b9e2 commit da66292
Show file tree
Hide file tree
Showing 35 changed files with 595 additions and 147 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ bandit: ##@Linting Run bandit

mypy: ##@Linting Run mypy
.venv/bin/mypy --config-file ./pyproject.toml $(PROJECT_PATH)

upgrade-head:
docker compose exec rest python -m industry_game.db upgrade head
27 changes: 16 additions & 11 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ services:
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
POSTGRES_DB: $POSTGRES_DB
volumes:
- postgres_data:/var/lib/postgresql/data

rest:
restart: on-failure
# image: andytakker/industry-game-rest:latest
build:
dockerfile: ./docker/rest.dockerfile
context: .
image: andytakker/industry-game-rest:latest
# build:
# dockerfile: ./docker/rest.dockerfile
# context: .
entrypoint: python -m industry_game
environment:
APP_API_ADDRESS: 0.0.0.0
Expand All @@ -30,18 +32,21 @@ services:

APP_SECRET: $APP_SECRET
APP_PRIVATE_KEY: $APP_PRIVATE_KEY
APP_PUBLIC_KEY: $APP_PUBLIC_KEY

frontend:
restart: on-failure
# image: andytakker/industry-game-frontend:latest
build:
dockerfile: ./docker/frontend.dockerfile
context: .
args:
BASE_URL: https://vk.com
image: andytakker/industry-game-frontend:latest
# # build:
# # dockerfile: ./docker/frontend.dockerfile
# # context: .
# args:
# BASE_URL: https://vk.com
ports:
- 80:80
- 443:443
volumes:
- ./ssl_keys:/etc/ssl_keys:ro

volumes:
postgres_data:
driver: local
5 changes: 0 additions & 5 deletions frontend/.idea/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions frontend/.idea/GitLink.xml

This file was deleted.

12 changes: 0 additions & 12 deletions frontend/.idea/frontend.iml

This file was deleted.

6 changes: 0 additions & 6 deletions frontend/.idea/inspectionProfiles/Project_Default.xml

This file was deleted.

6 changes: 0 additions & 6 deletions frontend/.idea/jsLinters/eslint.xml

This file was deleted.

8 changes: 0 additions & 8 deletions frontend/.idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions frontend/.idea/vcs.xml

This file was deleted.

26 changes: 0 additions & 26 deletions industry_game/handlers/games/create_game.py

This file was deleted.

26 changes: 26 additions & 0 deletions industry_game/handlers/games/game_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from http import HTTPStatus

from aiohttp.web import Response, View

from industry_game.utils.games.models import NewGameModel
from industry_game.utils.http.auth.base import (
AuthMixin,
require_admin_authorization,
)
from industry_game.utils.http.deps import DependenciesMixin
from industry_game.utils.http.params import parse_json_model
from industry_game.utils.http.response import msgspec_json_response


class CreateGameHandler(View, DependenciesMixin, AuthMixin):
@require_admin_authorization
async def post(self) -> Response:
body = await self.request.read()
new_game_data = parse_json_model(model=NewGameModel, data=body)

game = await self.game_storage.create(
name=new_game_data.name,
description=new_game_data.description,
created_by_id=self.user.id,
)
return msgspec_json_response(game, status=HTTPStatus.CREATED)
20 changes: 20 additions & 0 deletions industry_game/handlers/games/game_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from aiohttp.web import Response, View

from industry_game.utils.http.auth.base import (
AuthMixin,
require_admin_authorization,
)
from industry_game.utils.http.deps import DependenciesMixin
from industry_game.utils.http.models import StatusResponse
from industry_game.utils.http.params import parse_path_param
from industry_game.utils.http.response import msgspec_json_response


class StartGameHandler(View, DependenciesMixin, AuthMixin):
@require_admin_authorization
async def post(self) -> Response:
game_id = parse_path_param(self.request, "game_id", int)
await self.game_store.start_game(game_id=game_id)
return msgspec_json_response(
StatusResponse(message=f"Game {game_id} was started")
)
8 changes: 6 additions & 2 deletions industry_game/handlers/games/game_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
from aiohttp.web_response import Response

from industry_game.utils.games.models import UpdateGameModel
from industry_game.utils.http.auth.base import AuthMixin
from industry_game.utils.http.auth.base import (
AuthMixin,
require_admin_authorization,
)
from industry_game.utils.http.deps import DependenciesMixin
from industry_game.utils.http.params import parse_json_model, parse_path_param
from industry_game.utils.http.response import msgspec_json_response


class UpdateGameHandler(View, DependenciesMixin, AuthMixin):
@require_admin_authorization
async def post(self) -> Response:
body = await self.request.read()
update_game_data = parse_json_model(model=UpdateGameModel, data=body)
if not update_game_data.name and not update_game_data.description:
raise HTTPBadRequest
game_id = parse_path_param(self.request, "game_id", int)
game = await self.game_storage.update(
game = await self.game_storage.update_text(
game_id=game_id,
name=update_game_data.name,
description=update_game_data.description,
Expand Down
34 changes: 17 additions & 17 deletions industry_game/services/event.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import asyncio
import logging
from collections import deque
from contextlib import suppress
from datetime import datetime, timedelta
from datetime import UTC, datetime
from typing import Any

from aiomisc import Service

from industry_game.utils.events.base import MAX_EVENT_PROGRESS
from industry_game.utils.events.models import AbstractEvent
from industry_game.utils.events.processor import EventProcessor
from industry_game.utils.typed import not_none

log = logging.getLogger(__name__)


class EventService(Service):
event_queue: asyncio.Queue
event_queue: deque[AbstractEvent]
worker_task: asyncio.Task
processor: EventProcessor

Expand All @@ -31,20 +32,19 @@ async def stop(self, exception: Exception | None = None) -> Any:

async def work(self) -> None:
while True:
event: AbstractEvent = await self.event_queue.get()
if not self.event_queue:
continue
event = self.event_queue.popleft()
log.debug("Got event: %s", event)
if await self.is_rescheduled(event, datetime.now()):
await self.event_queue.put(event)
await self.process(event)
now = datetime.now(tz=UTC)
if event.is_active:
event.update_progress(now=now)
if event.progress >= MAX_EVENT_PROGRESS:
self.process(event)
else:
self.event_queue.append(event)
await asyncio.sleep(0.01)

async def is_rescheduled(self, event: AbstractEvent, now: datetime) -> bool:
if event.game.is_paused:
return True
return (
not_none(event.started_at) + timedelta(seconds=event.during_sec)
< now
)

async def process(self, event: AbstractEvent) -> None:
asyncio.shield(asyncio.create_task(self.processor.process_event(event)))
def process(self, event: AbstractEvent) -> None:
task = asyncio.shield(asyncio.create_task(event.execute()))
task.add_done_callback(event.post_hook())
12 changes: 7 additions & 5 deletions industry_game/services/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from yarl import URL

from industry_game.handlers.games.create_game import CreateGameHandler
from industry_game.handlers.games.game_create import CreateGameHandler
from industry_game.handlers.games.game_details import GameDetailsHandler
from industry_game.handlers.games.game_list import ListGameHandler
from industry_game.handlers.games.game_start import StartGameHandler
from industry_game.handlers.games.game_update import UpdateGameHandler
from industry_game.handlers.games.lobby.add_user_to_lobby import (
from industry_game.handlers.games.lobby.lobby_add_user import (
AddUserToGameLobbyHandler,
)
from industry_game.handlers.games.lobby.delete_user_from_lobby import (
from industry_game.handlers.games.lobby.lobby_delete_user import (
DeleteUserFromLobbyHandler,
)
from industry_game.handlers.games.lobby.list_lobby import ListGameLobbyHandler
from industry_game.handlers.games.lobby.read_lobby import (
from industry_game.handlers.games.lobby.lobby_list import ListGameLobbyHandler
from industry_game.handlers.games.lobby.lobby_read import (
ReadGameUserLobbyHandler,
)
from industry_game.handlers.ping import PingHandler
Expand Down Expand Up @@ -85,6 +86,7 @@ class REST(AIOHTTPService):
(hdrs.METH_POST, "/api/v1/games/", CreateGameHandler),
(hdrs.METH_GET, "/api/v1/games/{game_id}/", GameDetailsHandler),
(hdrs.METH_POST, "/api/v1/games/{game_id}/", UpdateGameHandler),
(hdrs.METH_POST, "/api/v1/games/{game_id}/start/", StartGameHandler),
# lobby handlers
(hdrs.METH_GET, "/api/v1/games/{game_id}/lobby/", ListGameLobbyHandler),
(
Expand Down
11 changes: 11 additions & 0 deletions industry_game/utils/buildings/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import msgspec

from industry_game.utils.games.models import Game


Expand All @@ -8,3 +10,12 @@ class BuildingType:
class Building:
type: BuildingType
game: Game


class BuildingTypeStruct(msgspec.Struct, frozen=True):
name: str


class BuildingStruct(msgspec.Struct, frozen=True):
type: BuildingType
game_id: int
Empty file.
2 changes: 2 additions & 0 deletions industry_game/utils/districts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class District:
pass
Loading

0 comments on commit da66292

Please sign in to comment.