commun.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Fonctions communes
  4. '''
  5. import os
  6. from random import randint
  7. import time
  8. from PyQt4.QtCore import SIGNAL, Qt, QString
  9. from PyQt4.QtGui import QDialog, QApplication
  10. import cPickle as pickle
  11. from ui.ecran_confirmation import Ui_cfrm_fenetre
  12. from ui.ecran_saisie import Ui_saisi_fenetre
  13. from ui.ecran_message import Ui_msg_fenetre
  14. ppal = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
  15. def rep(nature):
  16. """retourne le chemin du repertoire demande, le cree s'il n'existe pas
  17. (!!!) ne retourne que les chemins fixes (ne comprenant pas de part variable)"""
  18. #premier niveau de rep
  19. if nature == "app":
  20. retour = ppal
  21. elif nature == "rsc":
  22. retour = os.path.join(ppal, "rsc\\")
  23. elif nature in ["mat", "jeu"]:
  24. retour = os.path.join(ppal, "mat\\")
  25. elif nature in ["parties", "partie", "mde", "grp", "cbt"]:
  26. retour = os.path.join(ppal, "parties\\")
  27. elif nature in ["profils", "profil", "reception"]:
  28. retour = os.path.join(ppal, "profils\\")
  29. elif nature == "tmp":
  30. retour = os.path.join(ppal, "tmp\\")
  31. if not os.path.isdir(retour): os.mkdir(retour)
  32. #2e niveau
  33. if nature in ["partie", "mde", "grp", "cbt"]:
  34. s = sessionEnCours()
  35. partie = s.partie()
  36. retour = os.path.join(retour, "{}\\".format(partie))
  37. elif nature in ["profil", "reception"]:
  38. s = sessionEnCours()
  39. partie = s.util()
  40. retour = os.path.join(retour, "{}\\".format(partie))
  41. elif nature == "jeu":
  42. s = sessionEnCours()
  43. jeu = s.jeu()
  44. retour = os.path.join(retour, "{}\\".format(jeu))
  45. if not os.path.isdir(retour): os.mkdir(retour)
  46. #3e niveau
  47. if nature in ["reception", "mde", "grp", "cbt"]:
  48. retour = os.path.join(retour, "{}\\".format(nature))
  49. if not os.path.isdir(retour): os.mkdir(retour)
  50. return retour
  51. def uid(prefixe = ""):
  52. """construit un identifiant unique de 10 caracteres"""
  53. #representation de la date (annee/mois/jour/heure/min/seconde)
  54. base = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n", \
  55. "o","p","q","r","s","t","u","v","w","x","y","z", \
  56. "A","B","C","D","E","F","G","H","I","J","K","L", \
  57. "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", \
  58. "2","3","4","5","6","7","8","9"]
  59. dat = time.gmtime()
  60. a = dat[0] ; m = dat[1] ; j = dat[2] ; h = dat[3] ; mn = dat[4] ; s = dat[5]
  61. 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)]
  62. #ajout de 2 caracteres aleatoires
  63. c1 = base[randint(0,59)] ; c2 = base[randint(0,59)]
  64. #concatenation
  65. if len(prefixe) >= 2:
  66. p_ = prefixe[0:2]
  67. else:
  68. while len(prefixe) < 2:
  69. prefixe += "-"
  70. p_ = prefixe
  71. retour = "{}{}{}{}{}{}{}{}{}".format(p_, a_, m_, j_, h_, mn_, s_, c1, c2)
  72. return retour
  73. def enregistrerSous(objet, cible):
  74. """enregistre l'objet dans le fichier cible"""
  75. with open(cible, 'wb') as f:
  76. pickle.dump(objet, f, protocol=pickle.HIGHEST_PROTOCOL)
  77. f.close()
  78. def charger(fichier):
  79. """charge l'objet depuis le fichier demande"""
  80. fichier = "{}".format(fichier)
  81. #on recupere l'objet
  82. # try:
  83. with open(fichier, 'rb') as f:
  84. objet = pickle.load(f)
  85. f.close()
  86. # except IOError:
  87. # objet = None
  88. return objet
  89. def sessionEnCours():
  90. """retourne la donnee demandee de la session idS"""
  91. arg = QApplication.argv()
  92. idS = arg[1]
  93. s = charger(os.path.join(os.path.join(ppal, "tmp\\"), "{}.tmp".format(idS)))
  94. return s
  95. class Session(object):
  96. def __init__(self):
  97. self._idS = uid("se")
  98. self._idU = "defaut" #id utilisateur
  99. self._idP = "defaut" #id partie
  100. self._idJ = "defaut" #id du jeu (materiels)
  101. self.enregistrer()
  102. def idS(self):
  103. return self._idS
  104. def fichier(self):
  105. return os.path.join(os.path.join(ppal, "tmp\\") , "{}.tmp".format(self._idS))
  106. def enregistrer(self):
  107. enregistrerSous(self, self.fichier())
  108. def util(self):
  109. return self._idU
  110. def majUtil(self, idU):
  111. self._idU = idU
  112. self.enregistrer()
  113. def partie(self):
  114. return self._idP
  115. def majPartie(self, idP):
  116. self._idP = idP
  117. self.enregistrer()
  118. def jeu(self):
  119. return self._idJ
  120. def majJeu(self, idJ):
  121. self._idJ = idJ
  122. self.enregistrer()
  123. def fin(self):
  124. try:
  125. os.remove(self.fichier())
  126. except:
  127. pass
  128. ### balancer ces trucs dans les règles de jeu?
  129. def lstLibEtats(typMat):
  130. """liste des noms des statuts qui peuvent affecter les combattants et decors"""
  131. if typMat == "cb":
  132. return {0: "Indemne", 1: "Légèrement blessé", 2: "Blessé", 3:"Grievement blessé", 4: "Hors-combat"}
  133. else:
  134. return {0: "Indemne", 1: "Abîmé", 2: "Endommagé", 3:"Très endommagé", 4: "Détruit"}
  135. def lstLibStatuts():
  136. """liste des noms des statuts qui peuvent affecter les combattants et decors"""
  137. return {0: "Etourdi", 1: "Gelé", 2: "Paralysé", 3:"Empoisonné", 4: "Brûlé", 5:"Verrouillé"}