Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
212 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,43 @@ | ||
from typing import Dict | ||
|
||
import sqlalchemy as sa | ||
import peewee | ||
|
||
from .. import db | ||
from ..db import DB as fsa | ||
|
||
CLEARANCE_PUBLIC = 0 # Anyone can view | ||
CLEARANCE_PLAYERS = 1 # Players in the game can view | ||
CLEARANCE_MODS = 2 # Mods, TOs, etc | ||
CLEARANCE_ADMIN = 3 # Debug info, developer eyes only. | ||
|
||
class Game(fsa.Model): # type: ignore | ||
__tablename__ = 'game' | ||
id = sa.Column(fsa.Integer, primary_key=True, autoincrement=False) | ||
match_id = sa.Column(sa.Integer, sa.ForeignKey('match.id'), nullable=False) | ||
winner_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'), nullable=True) | ||
log = fsa.Column(sa.Text) | ||
winner = fsa.relationship('User') | ||
class Game(db.BaseModel): # type: ignore | ||
id = peewee.IntegerField(primary_key=True) | ||
match_id = peewee.DeferredForeignKey('Match', backref='games') | ||
winner_id = peewee.DeferredForeignKey('User') | ||
|
||
log = peewee.TextField() | ||
|
||
|
||
def sanitized_log(self) -> str: | ||
if 'Exception:' in self.log: | ||
return 'Log is not visible at this time. Please contact Silasary.' | ||
# If we want to remove chat, or OOB messages, do that here. | ||
return self.log.strip() | ||
return str(self.log).strip() | ||
|
||
def to_dict(self) -> Dict: | ||
return { | ||
'id': self.id, | ||
'match_id': self.match_id, | ||
'winner': self.winner, | ||
'winner': self.winner_id, | ||
'log': self.log, | ||
} | ||
|
||
def insert_game(game_id: int, match_id: int, game_lines: str) -> None: | ||
local = Game(id=game_id, match_id=match_id, log=game_lines) | ||
db.merge(local) # This will replace an old version of the game, if one exists. | ||
db.commit() | ||
Game.insert({'id':game_id, 'match_id':match_id, 'log':game_lines}).on_conflict_replace().execute() | ||
|
||
def get_game(game_id: int) -> Game: | ||
return Game.query.filter_by(id=game_id).one_or_none() | ||
|
||
class Line(fsa.Model): #type: ignore | ||
id = sa.Column(fsa.Integer, primary_key=True, autoincrement=True) | ||
game_id = sa.Column(sa.Integer, sa.ForeignKey('game.id'), nullable=False) | ||
clearance = sa.Column(sa.Integer, nullable=True) | ||
class Line(db.BaseModel): #type: ignore | ||
id = peewee.AutoField() | ||
game_id = peewee.DeferredForeignKey('Game') | ||
clearance = peewee.IntegerField(null=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,71 @@ | ||
from typing import Any, Optional | ||
from typing import Optional | ||
|
||
import sqlalchemy as sa | ||
import peewee | ||
from flask import url_for | ||
from flask_migrate import Migrate | ||
from flask_sqlalchemy import SQLAlchemy | ||
from sqlalchemy.orm.exc import MultipleResultsFound | ||
from playhouse import db_url | ||
import peeweedbevolve | ||
|
||
from . import APP | ||
|
||
# pylint: disable=no-member | ||
|
||
DB = SQLAlchemy(APP) # type: ignore | ||
MIGRATE = Migrate(APP, DB) | ||
DB = db_url.connect(APP.config['DATABASE_URI']) | ||
|
||
MATCH_PLAYERS = DB.Table('match_players', | ||
DB.Column('match_id', DB.Integer, DB.ForeignKey('match.id'), primary_key=True), | ||
DB.Column('user_id', DB.Integer, DB.ForeignKey('user.id'), primary_key=True) | ||
) | ||
class BaseModel(peewee.Model): | ||
class Meta: | ||
database = DB | ||
|
||
MATCH_MODULES = DB.Table('match_modules', | ||
DB.Column('match_id', DB.Integer, DB.ForeignKey('match.id'), primary_key=True), | ||
DB.Column('module_id', DB.Integer, DB.ForeignKey('module.id'), primary_key=True) | ||
) | ||
class MatchPlayers(BaseModel): | ||
match_id = peewee.DeferredForeignKey('Match') | ||
user_id = peewee.DeferredForeignKey('User') | ||
|
||
class User(DB.Model): # type: ignore | ||
__tablename__ = 'user' | ||
id = sa.Column(sa.Integer, primary_key=True) | ||
name = sa.Column(sa.String(60)) | ||
discord_id = sa.Column(sa.String(200)) | ||
|
||
class MatchModules(BaseModel): | ||
match_id = peewee.DeferredForeignKey('Match') | ||
user_id = peewee.DeferredForeignKey('Module') | ||
|
||
class User(BaseModel): # type: ignore | ||
id = peewee.AutoField() | ||
name = peewee.CharField(max_length=60, unique=True) | ||
discord_id = peewee.CharField(max_length=200) | ||
|
||
def url(self) -> str: | ||
return url_for('show_person', person=self.name) | ||
|
||
class Format(DB.Model): # type: ignore | ||
__tablename__ = 'format' | ||
id = sa.Column(sa.Integer, primary_key=True) | ||
name = sa.Column(sa.String(40)) | ||
friendly_name = sa.Column(sa.String(40)) | ||
class Format(BaseModel): # type: ignore | ||
id = peewee.AutoField() | ||
name = peewee.CharField(max_length=40, unique=True) | ||
friendly_name = peewee.CharField(max_length=40) | ||
|
||
def get_name(self) -> str: | ||
if self.friendly_name: | ||
return self.friendly_name | ||
return self.name | ||
|
||
class Module(DB.Model): # type: ignore | ||
__tablename__ = 'module' | ||
id = sa.Column(sa.Integer, primary_key=True) | ||
name = sa.Column(sa.String(50)) | ||
class Module(BaseModel): # type: ignore | ||
id = peewee.AutoField() | ||
name = peewee.CharField(max_length=50) | ||
|
||
def commit() -> None: | ||
return DB.session.commit() | ||
# def commit() -> None: | ||
# return DB.session.commit() | ||
|
||
def add(item: Any) -> None: | ||
return DB.session.add(item) | ||
# def add(item: Any) -> None: | ||
# return DB.session.add(item) | ||
|
||
def merge(item: Any) -> None: | ||
return DB.session.merge(item) | ||
# def merge(item: Any) -> None: | ||
# return DB.session.merge(item) | ||
|
||
def delete(item: Any) -> None: | ||
return DB.session.delete(item) | ||
# def delete(item: Any) -> None: | ||
# return DB.session.delete(item) | ||
|
||
def get_format(name: str) -> Optional[Format]: | ||
return Format.query.filter_by(name=name).one_or_none() | ||
return Format.get_or_none(Format.name == name) | ||
|
||
def get_or_insert_format(name: str) -> Format: | ||
local = get_format(name) | ||
if local is not None: | ||
return local | ||
local = Format(name=name) | ||
add(local) | ||
commit() | ||
return local | ||
return Format.get_or_create(name=name)[0] | ||
|
||
def get_or_insert_module(name: str) -> Module: | ||
local = Module.query.filter_by(name=name).one_or_none() | ||
if local is not None: | ||
return local | ||
local = Module(name=name) | ||
add(local) | ||
commit() | ||
return local | ||
return Module.get_or_create(name=name)[0] | ||
|
||
def get_or_insert_user(name: str) -> User: | ||
try: | ||
local = User.query.filter_by(name=name).one_or_none() | ||
if local is not None: | ||
return local | ||
local = User(name=name) | ||
add(local) | ||
commit() | ||
return local | ||
except MultipleResultsFound: | ||
query = User.query.filter_by(name=name) | ||
return query.first() | ||
return User.get_or_create(name=name)[0] |
Oops, something went wrong.