Skip to content

Commit

Permalink
Merge pull request #32 from YousefEZ/trade
Browse files Browse the repository at this point in the history
✨ Introducing Trade
  • Loading branch information
YousefEZ authored Oct 5, 2024
2 parents a3bda29 + ac53fee commit 78ec4d6
Show file tree
Hide file tree
Showing 37 changed files with 2,641 additions and 1,089 deletions.
4 changes: 4 additions & 0 deletions host/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
UserId = NewType("UserId", int)


def as_user_id(value: str | int) -> UserId:
return UserId(int(value))


def variable_guard(func):
def wrapper(*args, **kwargs):
if any(isinstance(arg, jinja2.runtime.Undefined) for arg in args):
Expand Down
17 changes: 16 additions & 1 deletion host/gameplay_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from pydantic import BaseModel


class MetadataGameplaySettings(BaseModel):
minimum_nation_name_length: int
maximum_nation_name_length: int


class ForeignGameplaySettings(BaseModel):
maximum_aid_slots: int
maximum_aid_amount: float
Expand All @@ -23,10 +28,20 @@ class InteriorGameplaySettings(BaseModel):
starter_technology: int


class TradeGameplaySettings(BaseModel):
maximum_active_agreements: int
resources_per_nation: int
offer_expire_days: float
maximum_number_of_offers_sent: int
maximum_number_of_offers_received: int


class GameplaySettingsModel(BaseModel):
bank: BankGameplaySettings
interior: InteriorGameplaySettings
foreign: ForeignGameplaySettings
interior: InteriorGameplaySettings
trade: TradeGameplaySettings
metadata: MetadataGameplaySettings


with open("settings/gameplay_settings.json", "r") as gameplay_settings_file:
Expand Down
40 changes: 33 additions & 7 deletions host/nation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from datetime import UTC, datetime
from enum import IntEnum, auto
from functools import cached_property
from typing import List, Optional, get_args

Expand All @@ -21,17 +22,28 @@
from sqlalchemy.orm import Session


class StartResponses(IntEnum):
SUCCESS = 0
ALREADY_EXISTS = auto()
NAME_TAKEN = auto()
NAME_TOO_SHORT = auto()
NAME_TOO_LONG = auto()
NON_ASCII = auto()


def user_exists(identifier: base_types.UserId, session: Session) -> bool:
metadata = session.query(models.MetadataModel).filter_by(user_id=identifier).first()
return metadata is not None


class Nation:
def __init__(self, identifier: base_types.UserId, session: Session):
self._identifier: base_types.UserId = identifier
self._session: Session = session

@property
def exists(self) -> bool:
metadata = (
self._session.query(models.MetadataModel).filter_by(user_id=self._identifier).first()
)
return metadata is not None
return user_exists(self._identifier, self._session)

@staticmethod
def search_for_nations(
Expand Down Expand Up @@ -95,8 +107,22 @@ def ministries(self) -> List[Ministry]:
for ministry_object in get_args(types.ministries.Ministries)
]

@classmethod
def start(cls, identifier: base_types.UserId, name: str, session: Session) -> Nation:
@staticmethod
def start(identifier: base_types.UserId, name: str, session: Session) -> StartResponses:
if not name.isascii():
return StartResponses.NON_ASCII

if len(name) < GameplaySettings.metadata.minimum_nation_name_length:
return StartResponses.NAME_TOO_SHORT

if len(name) > GameplaySettings.metadata.maximum_nation_name_length:
return StartResponses.NAME_TOO_LONG

if user_exists(identifier, session):
return StartResponses.ALREADY_EXISTS

if Nation.fetch_from_name(name, session) is not None:
return StartResponses.NAME_TAKEN
metadata = models.MetadataModel(
user_id=identifier,
nation=name,
Expand All @@ -108,7 +134,7 @@ def start(cls, identifier: base_types.UserId, name: str, session: Session) -> Na
session.add(metadata)
session.commit()

return cls(identifier, session)
return StartResponses.SUCCESS

def find_player(self, identifier: base_types.UserId) -> Nation:
return Nation(identifier, self._session)
Expand Down
19 changes: 8 additions & 11 deletions host/nation/foreign.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def sponsorships(self) -> List[AidRequest]:
}
)

def _send(self, recipient: base_types.UserId, amount: Price, reason: str) -> AidRequest:
def _send(self, recipient: base_types.UserId, amount: Price, reason: str) -> None:
request = models.AidRequestModel(
aid_id=str(uuid.uuid4()),
sponsor=self._player.identifier,
Expand All @@ -195,7 +195,6 @@ def _send(self, recipient: base_types.UserId, amount: Price, reason: str) -> Aid
self._player.bank.deduct(amount)
self._session.add(request)
self._session.commit()
return AidRequest(request)

def _remove_expired_requests(self, requests: Set[AidRequest]) -> List[AidRequest]:
removed_requests = set()
Expand Down Expand Up @@ -240,23 +239,21 @@ def _verify_send_request(

return AidRequestCode.SUCCESS

def send(
self, recipient: base_types.UserId, amount: Price, reason: str
) -> Tuple[AidRequestCode, Optional[AidRequest]]:
def send(self, recipient: base_types.UserId, amount: Price, reason: str) -> AidRequestCode:
code = self._verify_send_request(recipient, amount, reason)
if code != AidRequestCode.SUCCESS:
return code, None
if code is AidRequestCode.SUCCESS:
return code

request = self._send(recipient, amount, reason)
return AidRequestCode.SUCCESS, request
self._send(recipient, amount, reason)
return AidRequestCode.SUCCESS

def _cancel_request(self, request: AidRequest) -> None:
model_request = (
self._session.query(models.AidRequestModel).filter_by(aid_id=request.id).first()
)
if model_request is None:
return
self._player.find_player(request.sponsor).bank.add(request.amount)
self._player.find_player(request.sponsor).bank.receive(request.amount)
self._session.delete(model_request)
self._session.commit()

Expand All @@ -279,7 +276,7 @@ def _accept(self, request: AidRequest) -> AidAgreement:
)
self._session.delete(request.model)
self._session.add(agreement)
self._player.bank.add(request.amount)
self._player.bank.receive(request.amount)
return AidAgreement(agreement)

def _verify_accept_request(self, request: AidRequest) -> AidAcceptCode:
Expand Down
14 changes: 5 additions & 9 deletions host/nation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
class TradeRequestModel(Base):
__tablename__ = "TradeRequests"

trade_id: Mapped[str] = mapped_column(primary_key=True)
date: Mapped[datetime]
expires: Mapped[datetime]
sponsor: Mapped[int]
recipient: Mapped[int]
sponsor: Mapped[int] = mapped_column(primary_key=True)
recipient: Mapped[int] = mapped_column(primary_key=True)


class TradeModel(Base):
__tablename__ = "Trades"

trade_id: Mapped[str] = mapped_column(primary_key=True)
date: Mapped[datetime]
sponsor: Mapped[int]
recipient: Mapped[int]
sponsor: Mapped[int] = mapped_column(primary_key=True)
recipient: Mapped[int] = mapped_column(primary_key=True)


class AidRequestModel(Base):
Expand Down Expand Up @@ -54,8 +51,7 @@ class ResourcesModel(Base):
__tablename__ = "Resources"

user_id: Mapped[int] = mapped_column(primary_key=True)
primary: Mapped[str]
secondary: Mapped[str]
resource: Mapped[str] = mapped_column(primary_key=True)


class MetadataModel(Base):
Expand Down
Loading

0 comments on commit 78ec4d6

Please sign in to comment.