def set_class_color(self, class_number: int, color: QColor): color_array = np.array( [color.red(), color.green(), color.blue(), color.alpha()], np.uint8) self.set_class_color_array(class_number, color_array)
def __init__(self, name, color0=QColor("black"), color1=QColor("white"), alpha=(1, 1)): QObject.__init__(self) self.name = name # color is either specified as one argument (which should then be a [3,n] or [4,n] array), # or as two QColors orstring names. if isinstance(color0, (list, tuple)): self._rgb = numpy.array(color0) if self._rgb.shape[1] != 3 or self._rgb.shape[0] < 2: raise TypeError("expected [N,3] (N>=2) array as first argument") else: if isinstance(color0, str): color0 = QColor(color0) if isinstance(color1, str): color1 = QColor(color1) self._rgb = numpy.array([[color0.red(), color0.green(), color0.blue()], [color1.red(), color1.green(), color1.blue()]]) / 255. self._rgb_arg = numpy.arange(self._rgb.shape[0]) / (self._rgb.shape[0] - 1.0) # alpha array self._alpha = numpy.array(alpha).astype(float) self._alpha_arg = numpy.arange(len(alpha)) / (len(alpha) - 1.0) # background brush self._brush = None
class Kingdom : def __init__(self, id_i, name,attribs,parent): self.id = id_i self.name = name self.attribs = attribs self.color=QColor(attribs["red"],attribs["green"],attribs["blue"],attribs["alpha"]) self.groupes = {} self.temples = [] self.parent = parent self.settings = Config().instance.settings def addGroupe (self, groupe): self.groupes[groupe.name] = groupe def addTemple (self,temple): self.temples.append(temple) def delete (self): while (len(self.groupes)!= 0): for groupe in self.groupes.values(): groupe.delete() break self.model().database._delete("gm_kingdom","ID="+str(self.id)) self.empire().kingdoms.pop(self.name) def createGroupe (self,name, default_values={}): result = self.model().database.select("ID+1","gm_groupe",False,"ID + 1 not in (select ID from gm_groupe)","ID") result.first() if 'groupe' in default_values : params = default_values['groupe'] else: params = {} params ['rank']=1 params ['description']='' params ['color']='saphir' groupe = Groupe(result.value("ID+1"), name,params,self) self.addGroupe(groupe) attribs = groupe.attribs attribs['parent']=0 attribs['name']=groupe.name attribs['ID'] = groupe.id attribs['ID_kingdom']=self.id self.model().database.insert("gm_groupe",attribs) groupe.updateFromDisk(default_values) def updateFromDisk (self,default={}): path = os.path.join(Config().instance.path_to_pic(),self.faction().name,self.empire().name) currentPath = os.path.join(path,self.name) print ('current path',currentPath) if os.path.exists(currentPath) and os.path.exists(os.path.join(currentPath,'Picture')): currentPath = os.path.join(currentPath,'Picture') list_group = list(filter(SqliteModel.isValid,os.listdir(currentPath))) #on supprime les groupe qui n existe plus list_groupe_to_delete = [] for groupe in self.groupes.values() : if (groupe.name in list_group) == False : print ('groupe demande delete',groupe.name) list_groupe_to_delete.append(groupe) for g in list_groupe_to_delete: g.delete() for groupe_name in list_group : # on met a jours les groupes existant if groupe_name in self.groupes: print ('groupe demande update',groupe_name) self.groupes[groupe_name].updateFromDisk(default) else: #on cree un nouveau groupe print ('kingodm demande delete',groupe_name) self.createGroupe(groupe_name, default) else: print ('kingdom suppresion',self.name) self.delete() # def getAllGroupes (self): # dict_groupes = {} # for groupe in self.groupes.values(): # dict_groupes[groupe.id] = groupe # if groupe.isSub() == True : # for sg in groupe.sub_groupes : # dict_groupes[sg.id] = sg # return dict_groupes # # def getAllWarriors (self): # dict_heros = {} # for groupe in self.groupes.values(): # dict_heros.update(groupe.getAllWarriors()) # if groupe.isSub() == True : # for sg in groupe.sub_groupes : # dict_heros.update(sg.getAllWarriors()) # return dict_heros def getDictAttributes (self): attribs = {} attribs['armee'] = self.attribs['armee'] attribs['description'] = self.attribs["description"] attribs['couleur'] = str(self.color.red())+','+str(self.color.green())+','+str(self.color.blue())+','+str(self.color.alpha()) attribs['name'] = self.name attribs['ID'] = self.id attribs['ID_empire'] = self.parent.id temples = None for t_id in self.attribs['temples']: if temples == None: temples=str(t_id) else: temples=temples+","+str(t_id) print ('kingdom temples',temples) attribs['temples'] = temples return attribs def getWarriorList(self, func=None): warrior_list = [] for groupe in self.groupes.values(): warrior_list+=groupe.getWarriorList(func) return warrior_list def removeGroupe(self,groupe_name): print ('groupes',self.groupes) for groupe in self.groupes.values(): if groupe.name == groupe_name: self.model().database._delete("gm_groupe","ID="+str(groupe.id)) self.groupes.pop(groupe.name) return else: for sub in groupe.sub_groupes: if sub.name == groupe_name : groupe.removeSubGroupe(sub) print ('ooo',len(groupe.sub_groupes)) if len(groupe.sub_groupes)== 0: print ('suppresion du groupe parent') self.model().database._delete("gm_groupe","ID="+str(groupe.id)) self.groupes.pop(groupe.name) return def faction (self): return self.empire().parent def model (self): return self.faction().parent def empire (self): return self.parent def findGroupeFromName(self,name): for groupe in self.groupes.values(): print ('name ;',groupe.name) if groupe.name == name: return groupe for sub in groupe.sub_groupes: if sub.name == name: return sub return None def avancement (self): warrior_list = self.getWarriorList() nb_complete = 0 nb_alive= 0 for w in warrior_list : if w.attribs['complete']==2: nb_complete +=1 if w.attribs['HP'] > 0: nb_alive += 1 return len(warrior_list),nb_complete,nb_alive
class Empire: def __init__ (self, id_i, name, attrib,parent): self.id = id_i self.name = name self.attrib = attrib self.attrib['icon'] = self.name+".png" self.kingdoms = {} self.color = QColor(int(self.attrib['color'].split(',')[0]),int(self.attrib['color'].split(',')[1]),int(self.attrib['color'].split(',')[2])) self.parent= parent self.geometry = self.loadGeom() def loadGeom (self): geometry = {'polygon':[]} filename = self.name+"_geometry.txt" filename = filename.lower() filename = os.path.join(Config().instance.path_to_icons(),"empire","32x32",filename) file = QFile(filename) if file.open(QIODevice.ReadOnly): stream = QTextStream(file) while (not stream.atEnd()): line = stream.readLine() if line[0]!= "#": elts = line.split(' ') l = [] print ('line',line) for i in range (1,len(elts)): try : l.append(QPoint(int(elts[i].split(',')[0]),int(elts[i].split(',')[1]))) except IndexError : qDebug("Warning : empire.loadgeom, probleme lecture paire de point") if (elts[0] == "p"): print ('add polygon') geometry['polygon'].append( l ) else: pass else : print ('not able to load file',filename) return geometry def getDictAttributes (self): attribs = {} attribs['name'] = self.name attribs['color']=str(self.color.red())+','+str(self.color.green())+','+str(self.color.blue()) attribs['icon']=self.attrib['icon'] attribs['ID']=self.id attribs['ID_faction']=self.parent.id return attribs def faction (self): return self.parent def model (self): return self.faction().parent def createKingdom (self,name, default_values = {}): result = self.model().database.select("ID+1","gm_kingdom",False,"ID + 1 not in (select ID from gm_kingdom)","ID") result.first() if 'kingdom' in default_values : params = default_values['kingdom'] else: params = {} params ['armee']='' params ['description']='' params ['red']=255 params ['green']=255 params ['blue']=0 params ['alpha']=255 params['temples']=[] kingdom = Kingdom(result.value("ID+1"), name,params,self) self.addKingdom(kingdom) attribs = kingdom.getDictAttributes() self.model().database.insert("gm_kingdom",attribs) kingdom.updateFromDisk(default_values) def delete (self): while (len(self.kingdoms)!= 0): for kingdom in self.kingdoms.values(): kingdom.delete() break self.model().database._delete("gm_empire","ID="+str(self.id)) self.faction().empires.pop(self.name) def updateFromDisk (self,default={}): path = os.path.join(Config().instance.path_to_pic(),self.faction().name) currentPath = os.path.join(path,self.name) print ('current path',currentPath) if os.path.exists(currentPath): list_kingdoms = list(filter(SqliteModel.isValid,os.listdir(currentPath))) #on supprime les groupe qui n existe plus for kingdom in self.kingdoms.values(): if (kingdom.name in list_kingdoms) == False : print ('kingodm demande delete') kingdom.delete() for kingdom_name in list_kingdoms: # on met a jours les groupes existant if kingdom_name in self.kingdoms: print ('kingodm update') self.kingdoms[kingdom_name].updateFromDisk(default) else: print ('kingodm creation') #on cree un nouveau groupe self.createKingdom(kingdom_name, default) else: self.delete() # def getAllGroupes (self): # dict_groupes = {} # for kingdom in self.kingdoms.values(): # dict_groupes.update(kingdom.getAllGroupes()) # return dict_groupes # # def getAllWarriors (self): # dict_heros= {} # for kingdom in self.kingdoms.values(): # dict_heros.update(kingdom.getAllWarriors()) # return dict_heros def addKingdom(self, kingdom): self.kingdoms[kingdom.name] = kingdom def getKingdomFromName (self,kingdom_name): for kingdom in self.kingdoms.values(): if kingdom.name == kingdom_name: return kingdom return None def getWarriorList(self,func=None): warrior_list = [] for kingdom in self.kingdoms.values(): warrior_list+=kingdom.getWarriorList(func) return warrior_list