Refactor and documentation.
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
from dsst_sql import sql
|
||||
|
||||
|
||||
class DSSTCore:
|
||||
def __init__(self):
|
||||
# Set DB Connection
|
||||
sql.create_tables()
|
||||
|
||||
@staticmethod
|
||||
def insert_player(model: models.Player):
|
||||
return sql.Player.create(name=model.name, hex_id=model.hex_id)
|
||||
|
||||
@staticmethod
|
||||
def insert_enemy(enemy: models.Enemy):
|
||||
return sql.Enemy.create(name=enemy.name)
|
||||
|
||||
@staticmethod
|
||||
def insert_drink(model: models.Drink):
|
||||
return sql.Drink.create(name=model.name, vol=model.vol)
|
||||
|
||||
@staticmethod
|
||||
def insert_season(season: models.Season):
|
||||
return sql.Season.create(number=season.number, game_name=season.game_name, start_date=season.start_date,
|
||||
end_date=season.end_date)
|
||||
|
||||
@staticmethod
|
||||
def insert_death(death: models.Death):
|
||||
return sql.Death.create(info=death.info, player=death.player.id, enemy=death.enemy.id, penalty=death.penalty.id)
|
||||
|
||||
@staticmethod
|
||||
def insert_victory(victory: models.Victory):
|
||||
return sql.Death.create(info=victory.info, player=victory.player.id, enemy=victory.enemy.id)
|
||||
|
||||
@staticmethod
|
||||
def insert_episode(season_id: int, episode: models.Episode):
|
||||
with sql.connection.atomic():
|
||||
# Insert Episode Row
|
||||
new_ep = sql.Episode.create(seq_number=episode.seq_number, number=episode.number, date=episode.date,
|
||||
season=season_id)
|
||||
# Insert participating players
|
||||
for player in episode.players:
|
||||
sql.EpisodePlayer.insert(episode=new_ep.id, player=player.id)
|
||||
# Insert deaths in this episode
|
||||
if episode.deaths:
|
||||
for death in episode.deaths:
|
||||
new_death = DSSTCore.insert_death(death)
|
||||
sql.EpisodeDeath.create(death=new_death.id, episode=new_ep.id)
|
||||
# Insert victories in this episode
|
||||
if episode.victories:
|
||||
for victory in episode.victories:
|
||||
new_vic = DSSTCore.insert_victory(victory)
|
||||
sql.EpisodeVictory.create(victory=new_vic.id, episode=new_ep.id)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
core = DSSTCore()
|
||||
@@ -1,6 +1,14 @@
|
||||
"""
|
||||
This module contains the ORM class definitions for peewee.
|
||||
To access the database import this module an run queries on the classes
|
||||
Example:
|
||||
from sql import Episode
|
||||
query = Episode.select().where(Episode.name == 'MyName')
|
||||
"""
|
||||
|
||||
from peewee import *
|
||||
|
||||
connection = MySQLDatabase('dsst', user='dsst', password='dsst')
|
||||
db = MySQLDatabase(None)
|
||||
|
||||
|
||||
class Season(Model):
|
||||
@@ -11,7 +19,7 @@ class Season(Model):
|
||||
end_date = DateField(null=True)
|
||||
|
||||
class Meta:
|
||||
database = connection
|
||||
database = db
|
||||
|
||||
|
||||
class Player(Model):
|
||||
@@ -20,7 +28,7 @@ class Player(Model):
|
||||
hex_id = CharField(null=True)
|
||||
|
||||
class Meta:
|
||||
database = connection
|
||||
database = db
|
||||
|
||||
|
||||
class Episode(Model):
|
||||
@@ -32,7 +40,7 @@ class Episode(Model):
|
||||
players = ManyToManyField(Player, backref='episodes')
|
||||
|
||||
class Meta:
|
||||
database = connection
|
||||
database = db
|
||||
|
||||
|
||||
class Drink(Model):
|
||||
@@ -41,7 +49,7 @@ class Drink(Model):
|
||||
vol = DecimalField()
|
||||
|
||||
class Meta:
|
||||
database = connection
|
||||
database = db
|
||||
|
||||
|
||||
class Enemy(Model):
|
||||
@@ -50,7 +58,7 @@ class Enemy(Model):
|
||||
season = ForeignKeyField(Season, backref='enemies')
|
||||
|
||||
class Meta:
|
||||
database = connection
|
||||
database = db
|
||||
|
||||
|
||||
class Death(Model):
|
||||
@@ -61,7 +69,7 @@ class Death(Model):
|
||||
episode = ForeignKeyField(Episode, backref='deaths')
|
||||
|
||||
class Meta:
|
||||
database = connection
|
||||
database = db
|
||||
|
||||
|
||||
class Penalty(Model):
|
||||
@@ -72,7 +80,7 @@ class Penalty(Model):
|
||||
ForeignKeyField(Death, backref='penalties')
|
||||
|
||||
class Meta:
|
||||
database = connection
|
||||
database = db
|
||||
|
||||
|
||||
class Victory(Model):
|
||||
@@ -83,15 +91,4 @@ class Victory(Model):
|
||||
episode = ForeignKeyField(Episode, backref='victories')
|
||||
|
||||
class Meta:
|
||||
database = connection
|
||||
|
||||
|
||||
def create_tables():
|
||||
models = [Season, Episode, Player, Drink, Enemy, Death, Victory, Penalty, Episode.players.get_through_model()]
|
||||
for model in models:
|
||||
model.create_table()
|
||||
|
||||
|
||||
def drop_tables():
|
||||
models = [Season, Episode, Player, Drink, Enemy, Death, Victory, Penalty, Episode.players.get_through_model()]
|
||||
connection.drop_tables(models)
|
||||
database = db
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
"""
|
||||
This module contains shorthand functions for common queries to ease access from the UI
|
||||
"""
|
||||
from dsst_sql.sql import *
|
||||
|
||||
|
||||
def get_episodes_for_season(season_id):
|
||||
def get_episodes_for_season(season_id: int) -> list:
|
||||
"""Load list of episodes for a specific season
|
||||
:param season_id: ID of a season
|
||||
:return: List of sql.Episode or empty list
|
||||
"""
|
||||
try:
|
||||
return list(Season.get(Season.id == season_id).episodes)
|
||||
except Episode.DoesNotExist:
|
||||
@@ -9,6 +16,11 @@ def get_episodes_for_season(season_id):
|
||||
|
||||
|
||||
def get_player_deaths_for_season(season_id: int, player_id: int) -> int:
|
||||
"""Load all the aggregate count of all deaths for a player in a given season
|
||||
:param season_id: ID of a season
|
||||
:param player_id: ID of a player
|
||||
:return: Number of deaths of the player in the season
|
||||
"""
|
||||
deaths = 0
|
||||
for episode in list(Season.get(Season.id == season_id).episodes):
|
||||
deaths = deaths + len([death for death in list(episode.deaths) if death.player.id == player_id])
|
||||
@@ -16,7 +28,25 @@ def get_player_deaths_for_season(season_id: int, player_id: int) -> int:
|
||||
|
||||
|
||||
def get_player_victories_for_season(season_id: int, player_id: int) -> int:
|
||||
"""Load all the aggregate count of all victories for a player in a given season
|
||||
:param season_id: ID of a season
|
||||
:param player_id: ID of a player
|
||||
:return: Number of all victories of the player in the season
|
||||
"""
|
||||
victories = 0
|
||||
for episode in list(Season.get(Season.id == season_id).episodes):
|
||||
victories = victories + len([vic for vic in list(episode.victories) if vic.player.id == player_id])
|
||||
return victories
|
||||
|
||||
|
||||
def create_tables():
|
||||
"""Create all database tables"""
|
||||
models = [Season, Episode, Player, Drink, Enemy, Death, Victory, Penalty, Episode.players.get_through_model()]
|
||||
for model in models:
|
||||
model.create_table()
|
||||
|
||||
|
||||
def drop_tables():
|
||||
"""Drop all data in database"""
|
||||
models = [Season, Episode, Player, Drink, Enemy, Death, Victory, Penalty, Episode.players.get_through_model()]
|
||||
db.drop_tables(models)
|
||||
Reference in New Issue
Block a user