Move to basic client/server structure.

This commit is contained in:
luxick
2018-03-03 14:36:41 +01:00
parent cb129eddd1
commit 8516650af4
25 changed files with 207 additions and 18 deletions

View File

View File

View File

@@ -0,0 +1,96 @@
"""
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 *
db = MySQLDatabase(None)
class Season(Model):
id = AutoField()
number = IntegerField()
game_name = CharField()
start_date = DateField(null=True)
end_date = DateField(null=True)
class Meta:
database = db
class Player(Model):
id = AutoField()
name = CharField()
hex_id = CharField(null=True)
class Meta:
database = db
class Episode(Model):
id = AutoField()
seq_number = IntegerField()
number = CharField()
name = CharField(null=True)
date = DateField(null=True)
season = ForeignKeyField(Season, backref='episodes')
players = ManyToManyField(Player, backref='episodes')
class Meta:
database = db
class Drink(Model):
id = AutoField()
name = CharField()
vol = DecimalField()
class Meta:
database = db
class Enemy(Model):
id = AutoField()
name = CharField()
optional = BooleanField()
season = ForeignKeyField(Season, backref='enemies')
class Meta:
database = db
class Death(Model):
id = AutoField()
info = CharField(null=True)
player = ForeignKeyField(Player)
enemy = ForeignKeyField(Enemy)
episode = ForeignKeyField(Episode, backref='deaths')
class Meta:
database = db
class Penalty(Model):
id = AutoField()
size = DecimalField()
drink = ForeignKeyField(Drink)
player = ForeignKeyField(Player, backref='penalties')
death = ForeignKeyField(Death, backref='penalties')
class Meta:
database = db
class Victory(Model):
id = AutoField()
info = CharField(null=True)
player = ForeignKeyField(Player)
enemy = ForeignKeyField(Enemy)
episode = ForeignKeyField(Episode, backref='victories')
class Meta:
database = db

View File

@@ -0,0 +1,52 @@
"""
This module contains shorthand functions for common queries to ease access from the UI
"""
from data_access.sql import *
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:
return []
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])
return deaths
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)

View File

@@ -0,0 +1,42 @@
import pickle
import socket
from data_access import sql
from common import util, models
PORT = 12345
HOST = socket.gethostname()
BUFFER_SIZE = 1024
class DsstServer:
def __init__(self):
self.socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Created socket')
self.socket_server.bind((HOST, PORT))
print(f'Bound socket to {PORT} on host {HOST}')
def run(self):
self.socket_server.listen(5)
print('Socket is listening')
while True:
client, address = self.socket_server.accept()
try:
print(f'Connection from {address}')
data = util.recv_msg(client)
request = pickle.loads(data)
print(f'Received data: {request}')
dummy = models.Player()
dummy.name = 'Player 1'
dummy.hex_id = '0xC2'
dummy.deaths = [1, 2, 3]
util.send_msg(client, pickle.dumps(dummy))
finally:
client.close()
print('Connection to client closed')
if __name__ == '__main__':
server = DsstServer()
server.run()