def loadFromDB(name): """ Loads an attack from a Database """ connection = PkmnDBConnect() cursor = connection.cursor() attack = AttackFactory.buildAttackFromDB(cursor, name) connection.close() return attack
class DBAddAttackInUse(DBAdder): """ Adds an Attack In Use to the Database """ def __init__(self, id, connection=None, cursor=None): """ """ self.delegateType = "Attack in Use" self.commands = {"p": self.addParameters} self.vals = {"pkmn_id": id, "attack_id": None} # self.pkmn_id = id if connection is None: self.connection = PkmnDBConnect() self.cursor = self.connection.cursor() else: self.connection = connection self.cursor = cursor def execute(self, params, close=True): """ """ self.attack = params[0] self.cursor.execute("SELECT id from Attack where name = ?", (self.attack,)) self.vals["attack_id"] = self.cursor.fetchone()[0] self.addAttackInUse() self.connection.commit() if close: self.connection.close() def addAttackInUse(self): """ Adds the attack to the database """ params = [] toAdd = [] for key in self.vals.keys(): if self.vals[key] is None: continue params += [key] toAdd += [self.vals[key]] paramStr = self.GetStrFromList(params) print "Adding Attack In Use:", self.attack self.insertIntoDB("AttackInUse", paramStr, toAdd)
class DBAddAttackInUse(DBAdder): """ Adds an Attack In Use to the Database """ def __init__(self, id, connection = None, cursor = None): """ """ self.delegateType = "Attack in Use" self.commands = {'p':self.addParameters} self.vals = {'pkmn_id':id, 'attack_id':None} #self.pkmn_id = id if connection is None: self.connection = PkmnDBConnect() self.cursor = self.connection.cursor() else: self.connection = connection self.cursor = cursor def execute(self, params, close = True): """ """ self.attack = params[0] self.cursor.execute("SELECT id from Attack where name = ?", (self.attack,)) self.vals['attack_id'] = self.cursor.fetchone()[0] self.addAttackInUse() self.connection.commit() if (close): self.connection.close() def addAttackInUse(self): """ Adds the attack to the database """ params = [] toAdd = [] for key in self.vals.keys(): if self.vals[key] is None: continue params += [key] toAdd += [self.vals[key]] paramStr = self.GetStrFromList(params) print "Adding Attack In Use:", self.attack self.insertIntoDB("AttackInUse", paramStr, toAdd)
class DBAddSpecies(DBAdder): """ Adds a species to the Database """ def __init__(self): """ """ self.commands = {'a':self.addAbilities, 'd':self.addDreamAbility, 'p':self.addParameters, 's':self.addStats, 't':self.addTypes} self.vals = {'species':None, 'dex':None, 'HP':None, 'ATK':None, 'DEF':None, 'SPD':None, 'SATK':None, 'SDEF':None} self.abilities = [] self.types = [] self.dreamAbilities = [] self.connection = PkmnDBConnect() self.cursor = self.connection.cursor() def execute(self, params, close = True): """ """ for param in params: cmd = param[0] cmdParams = param[2:].split(':') self.commands[cmd](cmdParams) self.addSpecies() self.connection.commit() if (close): self.connection.close() def addSpecies(self): """ Adds the species to the database """ params = [] toAdd = [] for key in self.vals.keys(): if self.vals[key] is None: continue params += [key] toAdd += [self.vals[key]] paramStr = self.GetStrFromList(params) print "Adding Species:", self.vals['species'] self.insertIntoDB("Species", paramStr, toAdd) self.cursor.execute("SELECT id from Species where species = ?", (self.vals['species'],)) id = self.cursor.fetchone()[0] for type in self.types: self.insertIntoDB("SpeciesTypeJoin", "species_id, type_id", (id,)+(type,)) for ability in self.abilities: self.insertIntoDB("SpeciesAbilityJoin", "species_id, ability_id", (id,)+(type,)) for ability in self.dreamAbilities: self.insertIntoDB("SpeciesDreamAbilityJoin", "species_id, ability_id", (id,)+(type,)) def addAbilities(self, vals): """ Adds Ability parameters """ for ability in vals: if ability == "": continue ability = ability.strip() self.cursor.execute("SELECT id from Ability where name = ?", (ability,)) self.abilities.append(self.cursor.fetchone()[0]) def addDreamAbility(self, vals): """ Adds Dream Ability parameters """ for dreamAbility in vals: dreamAbility = dreamAbility.strip() self.cursor.execute("SELECT id from Ability where name = ?", (dreamAbility,)) self.dreamAbilities.append(self.cursor.fetchone()[0]) def addParameters(self, vals): """ Adds Species specific parameters """ self.vals['species'] = vals[0] self.vals['dex'] = vals[1] print "Building Species:", self.vals['species'] self.checkForSpeciesAlready() def addStats(self, vals): """ Adds stats """ self.vals['HP'] = vals[0] self.vals['ATK'] = vals[1] self.vals['DEF'] = vals[2] self.vals['SPD'] = vals[3] self.vals['SATK'] = vals[4] self.vals['SDEF'] = vals[5] def addTypes(self, vals): """ Adds types """ for type in vals: type = type.strip() if type == "": continue self.cursor.execute("SELECT id from Type where name = ?", (type,)) self.types.append(self.cursor.fetchone()[0]) def checkForSpeciesAlready(self): """ """ print "Checking for Species" self.cursor.execute("SELECT id from Species where species = ?", (self.vals['species'],)) t = self.cursor.fetchone() if not t is None: print "Species %s already exists" % self.vals['species'] raise Exception() print "Done checking Species"
class DBAddAbility(DBAdder): """ Adds an ability to the Database """ def __init__(self): """ """ self.delegateType = "Ability" self.commands = {'p':self.addParameters, 'a':self.addAbilityType} self.vals = {'name':None, 'ability_type_id':None, 'ability_id':None, 'effects':[]} self.getIDForType = {'ACC MOD':self.buildAccMod, 'BOOST STAB':self.buildAbilityWithNone, 'CANT LOWER STAT':self.buildCantLowerStat, 'EFFECT AFTER TURN':self.buildAbilityWithEffects, 'EFFECT ON CRIT':self.buildAbilityWithEffects, 'EFFECT ON STAT MOD':self.buildAbilityWithEffects, 'NO CRIT':self.buildAbilityWithNone, 'SKIP TURN':self.buildAbilityWithNone, 'SNIPER':self.buildAbilityWithNone, 'STAT MOD ON STATUS':self.buildStatModOnStatus} self.connection = PkmnDBConnect() self.cursor = self.connection.cursor() def execute(self, params, close = True): """ """ for param in params: cmd = param[0] cmdParams = param[2:].split(':') self.commands[cmd](cmdParams) self.addAbility() self.connection.commit() if (close): self.connection.close() def addAbility(self): """ Adds the attack to the database """ params = [] toAdd = [] for key in self.vals.keys(): if self.vals[key] is None: continue if key == 'effects': continue params += [key] toAdd += [self.vals[key]] paramStr = self.GetStrFromList(params) print "Adding Ability:", self.vals['name'] self.insertIntoDB("Ability", paramStr, toAdd) self.cursor.execute("SELECT id from Ability where name = ?", (self.vals['name'],)) id = self.cursor.fetchone()[0] self.connectToExtraEffects(id) def addParameters(self, vals): """ Adds Attack specific parameters """ self.vals['name'] = vals[0] print "Building Ability:", self.vals['name'] self.checkForAbilityAlready() def addAbilityType(self, vals): """ """ type = vals[0].strip() self.cursor.execute("SELECT id FROM AbilityVariants where type = ?", (type,)) val = self.cursor.fetchone() if val is None: print "%s ability variant does not exist" % type raise Exception() self.vals['ability_type_id'] = val[0] self.vals['ability_id'] = self.getIDForType[type](type, vals[1:]) def buildAbilityWithEffects(self, type, vals): """ """ table = "AbilityEffectsJoin" effects = vals[0].split('_')[1:] for effect in effects: effectAdder = DBAddEffect(self.connection, self.cursor) effect_type_id, effect_id = effectAdder.execute(effect[2:].split('%')) self.vals['effects'].append((effect_type_id, effect_id,)) def buildAbilityWithNone(self, type, vals): """ """ return None def buildAccMod(self, type, vals): """ """ table = "AccModAbility" mod = float(vals[0]) toAdd = (mod,) where = "mod = ?" params = "mod" return self.buildDelegate(type, table, where, toAdd, params) def buildCantLowerStat(self, type, vals): """ """ table = "CantLowerStatAbility" stat = vals[0] toAdd = (stat,) where = "stat = ?" params = "stat" return self.buildDelegate(type, table, where, toAdd, params) def buildStatModOnStatus(self, type, vals): """ """ table = "StatModOnStatusAbility" status = vals[0] stat = vals[1] mod = float(vals[2]) toAdd = (status, stat, mod,) where = "status = ? and stat = ? and mod = ?" params = "status, stat, mod" return self.buildDelegate(type, table, where, toAdd, params) def checkForAbilityAlready(self): """ """ self.cursor.execute("SELECT id from Ability where name = ?", (self.vals['name'],)) t = self.cursor.fetchone() if not t is None: print "Ability %s already exists" % self.vals['name'] raise Exception() def connectToExtraEffects(self, id): """ """ table = "AbilityEffectsJoin" paramStr = "ability_id, effect_type_id, effect_id" for effect in self.vals['effects']: self.insertIntoDB(table, paramStr, (id,)+effect)
class DBAddAttack(DBAdder): """ Adds an attack to the Database """ def __init__(self): """ """ self.commands = {'p':self.addParameters, 'h':self.addHit, 'd':self.addDamage, 'c':self.addCrit, 's':self.addSpeed, 'e':self.addEffect} self.vals = {'name':None, 'type_id':None, 'hit_type':None, 'hit_id':None, 'damage_type':None, 'damage_id':None, 'crit_type':None, 'crit_id':None, 'speed_type':None, 'speed_id':None, 'effects':[]} self.connection = PkmnDBConnect() self.cursor = self.connection.cursor() def execute(self, params, close = True): """ """ for param in params: cmd = param[0] cmdParams = param[2:].split(':') self.commands[cmd](cmdParams) self.addAttack() self.connection.commit() if (close): self.connection.close() def addAttack(self): """ Adds the attack to the database """ params = [] toAdd = [] for key in self.vals.keys(): if self.vals[key] is None: continue if key == 'effects': continue params += [key] toAdd += [self.vals[key]] paramStr = self.GetStrFromList(params) print "Adding Attack:", self.vals['name'] self.insertIntoDB("Attack", paramStr, toAdd) self.cursor.execute("SELECT id from Attack where name = ?", (self.vals['name'],)) id = self.cursor.fetchone()[0] for effect in self.vals['effects']: self.insertIntoDB("AttackEffectsJoin", "attack_id, effect_type, effect_id", (id,)+effect) def addParameters(self, vals): """ Adds Attack specific parameters """ self.vals['name'] = vals[0] type = vals[1].strip() print "Building Attack:", self.vals['name'] self.cursor.execute("SELECT id FROM Type where name = ?", (type,)) val = self.cursor.fetchone() if val is None: print "%s type does not exist" % type raise Exception() self.vals['type_id'] = val[0] self.checkForAttackAlready() def addHit(self, vals): """ """ hit = DBAddHit(self.connection, self.cursor) self.vals['hit_type'], self.vals['hit_id'] = hit.execute(vals) def addDamage(self, vals): """ """ damage = DBAddDamage(self.connection, self.cursor) self.vals['damage_type'], self.vals['damage_id'] = damage.execute(vals) def addCrit(self, vals): """ """ crit = DBAddCrit(self.connection, self.cursor) self.vals['crit_type'], self.vals['crit_id'] = crit.execute(vals) def addSpeed(self, vals): """ """ speed = DBAddSpeed(self.connection, self.cursor) self.vals['speed_type'], self.vals['speed_id'] = speed.execute(vals) def addEffect(self, vals): """ """ effect = DBAddEffect(self.connection, self.cursor) effect_type, effect_id = effect.execute(vals) self.vals['effects'].append((effect_type, effect_id,)) def checkForAttackAlready(self): """ """ self.cursor.execute("SELECT id from Attack where name = ?", (self.vals['name'],)) t = self.cursor.fetchone() if not t is None: print "Attack %s already exists" % self.vals['name'] raise Exception()
class DBAddSpecies(DBAdder): """ Adds a species to the Database """ def __init__(self): """ """ self.commands = { 'a': self.addAbilities, 'd': self.addDreamAbility, 'p': self.addParameters, 's': self.addStats, 't': self.addTypes } self.vals = { 'species': None, 'dex': None, 'HP': None, 'ATK': None, 'DEF': None, 'SPD': None, 'SATK': None, 'SDEF': None } self.abilities = [] self.types = [] self.dreamAbilities = [] self.connection = PkmnDBConnect() self.cursor = self.connection.cursor() def execute(self, params, close=True): """ """ for param in params: cmd = param[0] cmdParams = param[2:].split(':') self.commands[cmd](cmdParams) self.addSpecies() self.connection.commit() if (close): self.connection.close() def addSpecies(self): """ Adds the species to the database """ params = [] toAdd = [] for key in self.vals.keys(): if self.vals[key] is None: continue params += [key] toAdd += [self.vals[key]] paramStr = self.GetStrFromList(params) print "Adding Species:", self.vals['species'] self.insertIntoDB("Species", paramStr, toAdd) self.cursor.execute("SELECT id from Species where species = ?", (self.vals['species'], )) id = self.cursor.fetchone()[0] for type in self.types: self.insertIntoDB("SpeciesTypeJoin", "species_id, type_id", (id, ) + (type, )) for ability in self.abilities: self.insertIntoDB("SpeciesAbilityJoin", "species_id, ability_id", (id, ) + (type, )) for ability in self.dreamAbilities: self.insertIntoDB("SpeciesDreamAbilityJoin", "species_id, ability_id", (id, ) + (type, )) def addAbilities(self, vals): """ Adds Ability parameters """ for ability in vals: if ability == "": continue ability = ability.strip() self.cursor.execute("SELECT id from Ability where name = ?", (ability, )) self.abilities.append(self.cursor.fetchone()[0]) def addDreamAbility(self, vals): """ Adds Dream Ability parameters """ for dreamAbility in vals: dreamAbility = dreamAbility.strip() self.cursor.execute("SELECT id from Ability where name = ?", (dreamAbility, )) self.dreamAbilities.append(self.cursor.fetchone()[0]) def addParameters(self, vals): """ Adds Species specific parameters """ self.vals['species'] = vals[0] self.vals['dex'] = vals[1] print "Building Species:", self.vals['species'] self.checkForSpeciesAlready() def addStats(self, vals): """ Adds stats """ self.vals['HP'] = vals[0] self.vals['ATK'] = vals[1] self.vals['DEF'] = vals[2] self.vals['SPD'] = vals[3] self.vals['SATK'] = vals[4] self.vals['SDEF'] = vals[5] def addTypes(self, vals): """ Adds types """ for type in vals: type = type.strip() if type == "": continue self.cursor.execute("SELECT id from Type where name = ?", (type, )) self.types.append(self.cursor.fetchone()[0]) def checkForSpeciesAlready(self): """ """ print "Checking for Species" self.cursor.execute("SELECT id from Species where species = ?", (self.vals['species'], )) t = self.cursor.fetchone() if not t is None: print "Species %s already exists" % self.vals['species'] raise Exception() print "Done checking Species"