class Attack(db.Model): id = db.Column(db.Integer, primary_key=True) ability_id = db.Column(db.Integer, db.ForeignKey('ability.id')) ability = db.relationship("Ability", back_populates="attacks") damageFormula = db.Column(db.String(40), nullable=False) damagetype_id = db.Column(db.Integer, db.ForeignKey('damage_type.id')) damageType = db.relationship("DamageType", back_populates="attacksoftype") def __init__(self, damageFormula, damagetype_id): self.damageFormula = damageFormula self.damagetype_id = damagetype_id
class User(db.Model): __tablename__ = "account" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(144), nullable=False) username = db.Column(db.String(144), nullable=False, unique=True) password = db.Column(db.String(144), nullable=False) roles = db.relationship("Role", secondary="account_role", backref='role', lazy=True) creatures = db.relationship("Creature", secondary="account_creature", backref='creature', lazy=True) def __init__(self, name, username, password): self.name = name self.username = username self.password = password self.roles = [] self.creatures = [] def get_id(self): return self.id def is_active(self): return True def is_anonymous(self): return False def is_authenticated(self): return True @staticmethod def initialize_admin(): try: stmt = text( "INSERT INTO Account (name, username, password) VALUES ('Admin', 'Admin', 'Admin')" ) db.engine.execute(stmt) stmt = text( "INSERT INTO Account_Role (user_id, role_id) VALUES (1, 1)") db.engine.execute(stmt) stmt = text( "INSERT INTO Account_Role (user_id, role_id) VALUES (1, 2)") db.engine.execute(stmt) except: pass
class Role(db.Model): __tablename__ = "role" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(40), nullable=False, unique=True) accounts = db.relationship("User", secondary="account_role") def __init__(self, name): self.name = name @staticmethod def initialize_roles(): try: stmt = text("INSERT INTO Role (name) VALUES ('ADMIN')") db.engine.execute(stmt) stmt = text("INSERT INTO Role (name) VALUES ('USER')") db.engine.execute(stmt) except: pass @staticmethod def find_role_by_name(name): stmt = text("SELECT role.id, role.name FROM role" " WHERE role.name = :name").params(name=name) res = db.engine.execute(stmt) response = [] for row in res: response.append({"id": row[0], "name": row[1]}) return response
class Ability(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) description = db.Column(db.String(10000), nullable=False) attacks = db.relationship("Attack") toHit = db.Column(db.Integer, nullable=False) def __init__(self, name, description, toHit=0): self.name = name self.description = description self.toHit = toHit self.attacks = []
class DamageType(db.Model): id = db.Column(db.Integer, primary_key=True) type = db.Column(db.String(40), nullable=False, unique=True) attacksoftype = db.relationship("Attack", back_populates="damageType") @staticmethod def initialize_damagetypes(): try: stmt = text("INSERT INTO Damage_Type (type) VALUES ('Acid')") db.engine.execute(stmt) stmt = text( "INSERT INTO Damage_Type (type) VALUES ('Bludgeoning')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Cold')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Fire')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Force')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Lightning')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Necrotic')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Piercing')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Poison')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Psychic')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Radiant')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Slashing')") db.engine.execute(stmt) stmt = text("INSERT INTO Damage_Type (type) VALUES ('Thunder')") db.engine.execute(stmt) except: pass