# -*- coding: utf-8 -*- ''' Fonctions communes ''' import os from random import randint import time from PyQt4.QtGui import QApplication import cPickle as pickle ppal = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) def rep(nature): """retourne le chemin du repertoire demande, le cree s'il n'existe pas (!!!) ne retourne que les chemins fixes (ne comprenant pas de part variable)""" #premier niveau de rep if nature == "app": retour = ppal elif nature == "rsc": retour = os.path.join(ppal, "rsc\\") elif nature in ["mat", "jeu"]: retour = os.path.join(ppal, "mat\\") elif nature in ["parties", "partie", "mde", "grp", "cbt"]: retour = os.path.join(ppal, "parties\\") elif nature in ["profils", "profil", "reception"]: retour = os.path.join(ppal, "profils\\") elif nature == "tmp": retour = os.path.join(ppal, "tmp\\") if not os.path.isdir(retour): os.mkdir(retour) #2e niveau if nature in ["parties", "mde", "grp", "cbt"]: s = sessionEnCours() partie = s.partie() retour = os.path.join(retour, "{}\\".format(partie)) elif nature in ["profil", "reception"]: s = sessionEnCours() partie = s.util() retour = os.path.join(retour, "{}\\".format(partie)) elif nature == "jeu": s = sessionEnCours() jeu = s.jeu() retour = os.path.join(retour, "{}\\".format(jeu)) if not os.path.isdir(retour): os.mkdir(retour) #3e niveau if nature in ["reception", "mde", "grp", "cbt"]: retour = os.path.join(retour, "{}\\".format(nature)) if not os.path.isdir(retour): os.mkdir(retour) return retour def uid(prefixe = ""): """construit un identifiant unique de 10 caracteres""" #representation de la date (annee/mois/jour/heure/min/seconde) base = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n", \ "o","p","q","r","s","t","u","v","w","x","y","z", \ "A","B","C","D","E","F","G","H","I","J","K","L", \ "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", \ "2","3","4","5","6","7","8","9"] dat = time.gmtime() a = dat[0] ; m = dat[1] ; j = dat[2] ; h = dat[3] ; mn = dat[4] ; s = dat[5] a_, m_, j_, h_, mn_, s_ = base[(int(a)-2000)], base[int(m)], base[int(j)], base[int(h)], base[int(mn)], base[int(s)] #ajout de 2 caracteres aleatoires c1 = base[randint(0,59)] ; c2 = base[randint(0,59)] #concatenation if len(prefixe) >= 2: p_ = prefixe[0:2] else: while len(prefixe) < 2: prefixe += "-" p_ = prefixe retour = "{}{}{}{}{}{}{}{}{}".format(p_, a_, m_, j_, h_, mn_, s_, c1, c2) return retour def enregistrerSous(objet, cible): """enregistre l'objet dans le fichier cible""" with open(cible, 'wb') as f: pickle.dump(objet, f, protocol=pickle.HIGHEST_PROTOCOL) f.close() def charger(fichier): """charge l'objet depuis le fichier demande""" fichier = "{}".format(fichier) #on recupere l'objet try: with open(fichier, 'rb') as f: objet = pickle.load(f) f.close() except IOError: objet = None return objet def sessionEnCours(): """retourne la donnee demandee de la session idS""" arg = QApplication.argv() idS = arg[1] s = charger(os.path.join(os.path.join(ppal, "tmp\\"), "{}.tmp".format(idS))) return s class Session(object): def __init__(self): self._idS = uid("se") self._idU = "defaut" #id utilisateur self._idP = "defaut" #id partie self._idJ = "defaut" #id du jeu (materiels) self.enregistrer() def idS(self): return self._idS def fichier(self): return os.path.join(os.path.join(ppal, "tmp\\") , "{}.tmp".format(self._idS)) def enregistrer(self): enregistrerSous(self, self.fichier()) def util(self): return self._idU def majUtil(self, idU): self._idU = idU self.enregistrer() def partie(self): return self._idP def majPartie(self, idP): self._idP = idP self.enregistrer() def jeu(self): return self._idJ def majJeu(self, idJ): self._idJ = idJ self.enregistrer() def fin(self): try: os.remove(self.fichier()) except: pass ### balancer ces trucs dans les règles de jeu? def lstLibEtats(typMat): """liste des noms des statuts qui peuvent affecter les combattants et decors""" if typMat == "cb": return {0: "Indemne", 1: "Légèrement blessé", 2: "Blessé", 3:"Grievement blessé", 4: "Hors-combat"} else: return {0: "Indemne", 1: "Abîmé", 2: "Endommagé", 3:"Très endommagé", 4: "Détruit"} def lstLibStatuts(): """liste des noms des statuts qui peuvent affecter les combattants et decors""" return {0: "Etourdi", 1: "Gelé", 2: "Paralysé", 3:"Empoisonné", 4: "Brûlé", 5:"Verrouillé"}