Skip to content

Commit

Permalink
Merge pull request #105 from sturdy-robot/develop
Browse files Browse the repository at this point in the history
feat: introduce definitions
  • Loading branch information
sturdy-robot committed Mar 20, 2024
2 parents bf6f9de + f0e4884 commit 8fe8113
Show file tree
Hide file tree
Showing 31 changed files with 291 additions and 543 deletions.
42 changes: 7 additions & 35 deletions esm/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Tuple

from ..definitions import NAMES_FILE
from .esports.moba.generator import (
ChampionGenerator,
MobaPlayerGenerator,
TeamGenerator,
)
from .esports.moba.mobateam import MobaTeam
from .gamestate import GameState
from .settings import Settings
from .utils import load_list_from_file


class DB:
Expand All @@ -40,42 +43,11 @@ def players_file(self):
def champions_file(self):
return self.settings.champions_file

def get_moba_generators(
self,
) -> Tuple[MobaPlayerGenerator, TeamGenerator, ChampionGenerator]:
players = MobaPlayerGenerator()
teams = TeamGenerator()
champions = ChampionGenerator()
return players, teams, champions

def generate_moba_files(self):
players_gen, teams_gen, champions_gen = self.get_moba_generators()
champions_gen.generate_champions()
players_gen.champions_list = champions_gen.champions

players_gen.lane = 0
players_gen.generate_players(amount=self.settings.amount_players)

amount_teams = int(self.settings.amount_players / 5)

teams_gen.amount = amount_teams
teams_gen.player_list = players_gen.players
teams_gen.generate_teams()

teams_gen.generate_file()
champions_gen.generate_file()
players_gen.generate_file()

def load_moba_teams(self):
players_gen, teams_gen, champions_gen = self.get_moba_generators()

champions_gen.get_champions()
players_gen.champions_list = champions_gen.champions
players_gen.get_players_objects()
teams_gen.player_list = players_gen.players
teams_gen.get_teams_objects()
def generate_moba_files(self) -> None:
pass

return teams_gen.teams
def load_moba_teams(self) -> list[MobaTeam]:
pass

def get_gamestate(self) -> GameState:
pass
Expand Down
30 changes: 0 additions & 30 deletions esm/core/esmcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,6 @@ def __init__(self):
self.settings, self.db, self.settings.enable_auto_save
)

@property
def amount_players(self):
return self.settings.amount_players

@amount_players.setter
def amount_players(self, value):
try:
self.check_player_amount()
except AmountPlayersError:
self.settings.amount_players = 50
else:
self.settings.amount_players = value

def check_player_amount(self):
if self.amount_players > 300 or self.amount_players < 50:
error_message = dedent(
"""
Number of players is not supported! Ranges from 50 to 300 players! Defaulting to 50.
"""
).strip()
raise AmountPlayersError(error_message)

if self.amount_players % 5 != 0:
error_message = dedent(
"""
Number of players is not supported! Number should be a multiple of 5!
"""
)
raise AmountPlayersError(error_message)

def check_if_files_exist(self) -> None:
if (
not os.path.exists(self.settings.champions_file)
Expand Down
6 changes: 3 additions & 3 deletions esm/core/esports/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from datetime import date, datetime
from typing import Union

from esm.core.esports.moba.team import Team
from esm.core.esports.moba.mobateam import MobaTeam

from .player import Player

Expand All @@ -30,7 +30,7 @@ def __init__(
last_name: str,
birthday: Union[date, datetime, str],
nick_name: str,
team: Union[int, Team],
team: Union[int, MobaTeam],
is_player: bool,
quality: int,
):
Expand All @@ -42,7 +42,7 @@ def __init__(
self.quality = quality

def get_dict(self):
team = self.team.team_id if isinstance(self.team, Team) else self.team
team = self.team.team_id if isinstance(self.team, MobaTeam) else self.team
return {
"id": self.player_id.int,
"first_name": self.first_name,
Expand Down
6 changes: 3 additions & 3 deletions esm/core/esports/moba/generator/generate_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from esm.core.esports.moba.generator.generate_champions import Champion
from esm.core.esports.moba.generator.generate_players import MobaPlayerGenerator
from esm.core.esports.moba.generator.generator import GeneratorInterface
from esm.core.esports.moba.mobateam import MobaTeam
from esm.core.esports.moba.player import Lanes, MobaPlayer
from esm.core.esports.moba.team import Team


class TeamGeneratorError(Exception):
Expand Down Expand Up @@ -69,14 +69,14 @@ def generate_roster(
for lane in list(Lanes)
]

def generate(self, team_definition: dict[str, str | int]) -> Team:
def generate(self, team_definition: dict[str, str | int]) -> MobaTeam:
"""
Generates the team
"""
nationality = team_definition["nationality"]
mu = team_definition["mu"]
sigma = team_definition["sigma"]
return Team(
return MobaTeam(
team_id=self.generate_id(),
name=team_definition["name"],
nationality=nationality,
Expand Down
20 changes: 10 additions & 10 deletions esm/core/esports/moba/match_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def reset_values(self):
self.running_test = False

def _get_team_stats(self, i):
team = self.match.game.teams[i]
team = self.match.teams[i]
self.amount_team_kills[i].append(team.kills)
self.amount_team_deaths[i].append(team.deaths)
self.amount_team_assists[i].append(team.assists)
Expand All @@ -63,7 +63,7 @@ def _get_team_stats(self, i):
self.avg_team_assists[i] += team.assists

def _get_amount_wins(self):
if self.match.victorious_team == self.match.game.team1:
if self.match.victorious_team == self.match.team1:
self.amount_team_wins[0] += 1
else:
self.amount_team_wins[1] += 1
Expand Down Expand Up @@ -101,17 +101,17 @@ def run_match_test(self):
self.get_avg(self.avg_team_assists)

print("Average team kills:")
print(f"{self.match.game.team1.team.name}: {self.amount_team_kills[0]}")
print(f"{self.match.game.team2.team.name}: {self.amount_team_kills[1]}")
print(f"{self.match.team1.team.name}: {self.amount_team_kills[0]}")
print(f"{self.match.team2.team.name}: {self.amount_team_kills[1]}")
print("Average team deaths:")
print(f"{self.match.game.team1.team.name}: {self.amount_team_deaths[0]}")
print(f"{self.match.game.team2.team.name}: {self.amount_team_deaths[1]}")
print(f"{self.match.team1.team.name}: {self.amount_team_deaths[0]}")
print(f"{self.match.team2.team.name}: {self.amount_team_deaths[1]}")
print("Average team assists:")
print(f"{self.match.game.team1.team.name}: {self.amount_team_assists[0]}")
print(f"{self.match.game.team2.team.name}: {self.amount_team_assists[1]}")
print(f"{self.match.team1.team.name}: {self.amount_team_assists[0]}")
print(f"{self.match.team2.team.name}: {self.amount_team_assists[1]}")
print("Amount team wins:")
print(f"{self.match.game.team1.team.name}: {self.amount_team_wins[0]}")
print(f"{self.match.game.team2.team.name}: {self.amount_team_wins[1]}")
print(f"{self.match.team1.team.name}: {self.amount_team_wins[0]}")
print(f"{self.match.team2.team.name}: {self.amount_team_wins[1]}")

print("Average game time was", self.get_avg_game_time())
print("Max game time was", self.get_max_game_time())
Expand Down
21 changes: 10 additions & 11 deletions esm/core/esports/moba/team.py → esm/core/esports/moba/mobateam.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_players_from_list(


@dataclass
class Team(Serializable):
class MobaTeam(Serializable):
team_id: uuid.UUID
name: str
nationality: str
Expand Down Expand Up @@ -75,11 +75,11 @@ class TeamStats:
assists: int = 0


class TeamSimulation:
class MobaTeamSimulation:
def __init__(
self, team: Team, players: list[MobaPlayerSimulation], is_players_team: bool
self, team: MobaTeam, players: list[MobaPlayerSimulation], is_players_team: bool
):
self.team: Team = team
self.team: MobaTeam = team
self.towers: dict[str, int] = {
"top": 3,
"mid": 3,
Expand All @@ -91,6 +91,11 @@ def __init__(
"mid": 1,
"bot": 1,
}
self.inhibitors_cooldown: dict[str, float] = {
"top": 0.0,
"mid": 0.0,
"bot": 0.0,
}
self.is_players_team: bool = is_players_team
self.nexus: int = 1
self.players: list[MobaPlayerSimulation] = players
Expand All @@ -102,12 +107,6 @@ def __init__(
self._points: int = 0
self._bans: list[Champion] = []

def is_tower_up(self, lane: str) -> bool:
return self.towers[lane] != 0

def are_all_towers_up(self) -> bool:
return 0 not in self.towers.values()

def are_all_towers_down(self) -> bool:
return (
self.towers["top"] == 0
Expand Down Expand Up @@ -257,6 +256,6 @@ def __repr__(self):
def __eq__(self, other):
return (
self.team.team_id == other.team.team_id
if isinstance(other, TeamSimulation)
if isinstance(other, MobaTeamSimulation)
else NotImplemented
)
6 changes: 3 additions & 3 deletions esm/core/esports/moba/modules/match_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from esm.core.esports.moba.simulation.match_live import MatchLive

from ..mobamatch import MobaMatch
from ..team import TeamSimulation
from ..mobateam import MobaTeamSimulation


class MatchManager:
Expand All @@ -36,8 +36,8 @@ def initialize_game(
self,
game_id: uuid.UUID,
championship_id: uuid.UUID,
team1: TeamSimulation,
team2: TeamSimulation,
team1: MobaTeamSimulation,
team2: MobaTeamSimulation,
):
self.current_game = MobaMatch(game_id, championship_id, team1, team2)

Expand Down
4 changes: 2 additions & 2 deletions esm/core/esports/moba/progression.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from typing import Union
from uuid import UUID

from .mobateam import MobaTeam
from .player import MobaPlayer
from .team import Team


class Progression(ABC):
Expand All @@ -28,7 +28,7 @@ def progress(self, *args, **kwargs):


class PlayerProgression(Progression):
def progress(self, opp_team: Team, player: MobaPlayer):
def progress(self, opp_team: MobaTeam, player: MobaPlayer):
pass


Expand Down
20 changes: 6 additions & 14 deletions esm/core/esports/moba/simulation/events/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@
from queue import Queue
from typing import Union

from esm.core.esports.moba.mobateam import MobaTeamSimulation
from esm.core.esports.moba.simulation.commentaries import Commentaries
from esm.core.esports.moba.team import TeamSimulation

logger = logging.getLogger(__name__)


class EventCreator(ABC):
@abstractmethod
def factory_method(
self, event_chosen: dict, game_time: float, show_commentary: bool, queue: Queue
):
pass


class MobaEvent(ABC):
def __init__(
self,
Expand Down Expand Up @@ -71,8 +63,8 @@ def get_from_dict(

@staticmethod
def _get_probable_team(
team1: TeamSimulation, team2: TeamSimulation
) -> tuple[TeamSimulation, TeamSimulation]:
team1: MobaTeamSimulation, team2: MobaTeamSimulation
) -> tuple[MobaTeamSimulation, MobaTeamSimulation]:
"""
Gets the team with a higher probability to attack the other team
"""
Expand Down Expand Up @@ -131,9 +123,9 @@ def calculate_event(self, *args, **kwargs):
class NothingEvent(MobaEvent):
def calculate_event(
self,
team1: TeamSimulation,
team2: TeamSimulation,
which_nexus: Union[TeamSimulation, None],
team1: MobaTeamSimulation,
team2: MobaTeamSimulation,
which_nexus: Union[MobaTeamSimulation, None],
):
pass

Expand Down
25 changes: 5 additions & 20 deletions esm/core/esports/moba/simulation/events/inhib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,21 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
import random
from queue import Queue
from typing import Union

from esm.core.esports.moba.team import TeamSimulation
from esm.core.esports.moba.mobateam import MobaTeamSimulation

from .general import EventCreator, MobaEvent
from .general import MobaEvent

logger = logging.getLogger(__name__)


class InhibEventEventCreator(EventCreator):
def factory_method(
self, event_chosen: dict, game_time: float, show_commentary: bool, queue: Queue
):
return InhibEvent(
event_name=event_chosen["name"],
priority=event_chosen["priority"],
points=event_chosen["points"],
event_time=game_time,
show_commentary=show_commentary,
queue=queue,
)


class InhibEvent(MobaEvent):
def calculate_event(
self,
team1: TeamSimulation,
team2: TeamSimulation,
which_nexus: Union[TeamSimulation, None],
team1: MobaTeamSimulation,
team2: MobaTeamSimulation,
which_nexus: Union[MobaTeamSimulation, None],
):
if team1.get_exposed_inhibs():
attack_team = team2
Expand Down

0 comments on commit 8fe8113

Please sign in to comment.