|
| 1 | +#-*- coding: utf-8 -*-# |
| 2 | +from orm.simpleDB import orm |
| 3 | +import json |
| 4 | +import ast |
| 5 | +import hashlib, random |
| 6 | +from mytools.utils import * |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +class utilisateur(object): |
| 12 | + """ |
| 13 | + La classe utilisateur définit les informations et les méthodes propres au joueur. |
| 14 | + Seule la clé primaire est obligatoire, les autres éléments peuvent être ignorés et non définis. |
| 15 | + """ |
| 16 | + |
| 17 | + __tablename__ = "user" |
| 18 | + __const_prices__ = {'pokeball':10, 'Baies': 10} |
| 19 | + |
| 20 | + def __init__(self, username, passwd, coin=100, zone=1, xp=0, pokeball=5, berry=2, pokCollectionStr='[]',pokedex='[]'): |
| 21 | + |
| 22 | + """ |
| 23 | + Attributs |
| 24 | +
|
| 25 | + :param username: Pseudonyme du joueur |
| 26 | + :type username: str |
| 27 | + :param passwd : Mot de passe du joueur |
| 28 | + :type passwd: str |
| 29 | + :param coin : argent du joueur |
| 30 | + :type coin : int |
| 31 | + :param zone : zone dans laquelle se situe le joueur |
| 32 | + :type zone : int |
| 33 | + :param xp : expérience du joueur |
| 34 | + :type xp : int |
| 35 | + :param pokeball : nombre de pokeball du joueur |
| 36 | + :type pokeball : int |
| 37 | + :param berry : nombre de baies du joueur |
| 38 | + :type berry : int |
| 39 | + :param pokCollectionStr : liste des pokemons capturés par le joueur |
| 40 | + :type pokCollectionStr : list |
| 41 | + """ |
| 42 | + self.username = [username, "VARCHAR(255)", {"PRIMARY KEY": True, "AUTOINCREMENT": False, "NOT NULL": True}] |
| 43 | + self.hashed_pass_protected = [hashlib.sha1(passwd.encode('utf8')).hexdigest(), "VARCHAR(40)", {"PRIMARY KEY": False, "AUTOINCREMENT": False, "NOT NULL": True}] |
| 44 | + self.coin = [coin, "INTEGER", {"PRIMARY KEY": False, "NOT NULL": True,"CHECK(coin >= 0)": True}] |
| 45 | + self.zone = [zone, "INTEGER", {"PRIMARY KEY": False,"NOT NULL": True, "CHECK(zone BETWEEN 1 AND 11)": True}] |
| 46 | + self.xp = [xp, "INTEGER", {"PRIMARY KEY": False,"NOT NULL": True, "CHECK(xp BETWEEN 1 AND 2000)": True}] |
| 47 | + self.pokeball = [pokeball, "INTEGER", {"PRIMARY KEY": False,"NOT NULL": True, "CHECK(pokeball BETWEEN 0 AND 20)": True}] |
| 48 | + self.berry = [berry, "INTEGER", {"PRIMARY KEY": False,"NOT NULL": True, "CHECK(berry BETWEEN 0 AND 20)": True}] |
| 49 | + self.pokCollection = [pokCollectionStr, "TEXT", {"PRIMARY KEY": False, "AUTOINCREMENT": False, "NOT NULL": False}] |
| 50 | + self.pokedex=[pokedex, "TEXT", {"PRIMARY KEY": False, "AUTOINCREMENT": False, "NOT NULL": False}] |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + def setPokemon(self, pokemon_uuid): |
| 55 | + """ Méthode permettant de définir un identifiant unique universel (UUID) à chaque Pokemon. |
| 56 | +
|
| 57 | + :param pokemon_uuid: UUID du Pokemon |
| 58 | + :type pokemon_uuid : str |
| 59 | + """ |
| 60 | + deSerial_pokArray = ast.literal_eval(self.pokCollection[0]) |
| 61 | + if pokemon_uuid in deSerial_pokArray: |
| 62 | + self._setPokemon = pokemon_uuid |
| 63 | + |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def retirer(pokObj): |
| 67 | + """ Méthode permettant de retirer de la base un Pokemon. |
| 68 | +
|
| 69 | + :param pokObj: objet Pokemon |
| 70 | + :type pokObj: str |
| 71 | + :return: table Pokemon filtrée du Pokemon retiré |
| 72 | + """ |
| 73 | + dbSession = orm() |
| 74 | + return dbSession.filterBy.delete(tablename="pokemon", id=pokObj.id[0]) |
| 75 | + |
| 76 | + |
| 77 | + @updateAfterEvent |
| 78 | + def shop(self, num_pokebal=0, num_berry=0): |
| 79 | + """ Méthode permettant d'acheter des Pokeball et des baies |
| 80 | +
|
| 81 | + :param num_pokebal: nombre de pokeball |
| 82 | + :type num_pokebal: int |
| 83 | + :param num_berry: nombre de baies |
| 84 | + :type num_berry: int |
| 85 | + :return: informations du joueur |
| 86 | + """ |
| 87 | + cost_ = num_pokebal*self.__const_prices__['pokeball'] + num_berry*self.__const_prices__['Baies'] |
| 88 | + if cost_ <= self.coin[0]: |
| 89 | + self.coin[0] -= cost_ |
| 90 | + self.pokeball[0] += num_pokebal |
| 91 | + self.berry[0] += num_berry |
| 92 | + print('Achat effectué') |
| 93 | + else: |
| 94 | + print('Solde insuffisant') |
| 95 | + return self |
| 96 | + |
| 97 | + |
| 98 | + @updateAfterEvent |
| 99 | + def Capture(self, pokeObj): |
| 100 | + """ Méthode permettant de Capture de nouveaux Pokemon |
| 101 | +
|
| 102 | + :param pokeObj: objet Pokemon |
| 103 | + :type pokeObj: str |
| 104 | + :return: informations du joueur |
| 105 | + """ |
| 106 | + xp_diff = int((pokeObj.xp[0]-self.xp[0])/100) |
| 107 | + xp_diff = int(min((20-xp_diff)/20, 1) * 10) |
| 108 | + ifeelLucky = random.choice([False]*xp_diff+[True]*(10-xp_diff)) |
| 109 | + ifeelLucky = True |
| 110 | + if self.pokeball[0]<=0: |
| 111 | + print("Vous n'avez plus de pokeballs") |
| 112 | + elif ifeelLucky: |
| 113 | + deSerial_pokArray = ast.literal_eval(self.pokCollection[0]) |
| 114 | + deSerial_pokArray.append(pokeObj.id[0]) |
| 115 | + self.pokCollection[0] = json.dumps(deSerial_pokArray) |
| 116 | + pokeObj.state[0] = 'Not Wild' |
| 117 | + self.pokeball[0] -= 1 |
| 118 | + updateObj(pokeObj) |
| 119 | + print(pokeObj.name[0]+" est capturé") |
| 120 | + else: |
| 121 | + print(pokeObj.name[0]+"n'a pas été capturé") |
| 122 | + |
| 123 | + return self |
| 124 | + |
| 125 | + @updateAfterEvent |
| 126 | + def move(self, targetZone): |
| 127 | + """ Méthode permettant de naviguer dans les différentes zones du jeu |
| 128 | +
|
| 129 | + :param targetZone: zone dans laquelle le joueur souhaite aller |
| 130 | + :type targetZone: int |
| 131 | + :return: informations du joueur |
| 132 | + """ |
| 133 | + self.zone[0] = targetZone |
| 134 | + return self |
| 135 | + |
| 136 | + |
| 137 | + @updateAfterEvent |
| 138 | + def heal(self,pokeObj): |
| 139 | + """ Méthode permettant de soigner des Pokemons |
| 140 | +
|
| 141 | + :param pokeObj: objet Pokemon |
| 142 | + :type pokeObj: str |
| 143 | + :return: informations du joueur |
| 144 | + """ |
| 145 | + if self.berry[0]<=0: |
| 146 | + print("Vous n'avez plus de baies") |
| 147 | + else : |
| 148 | + pokeObj.hp[0] = 100 |
| 149 | + self.berry[0] = max(1, self.berry[0]-1) |
| 150 | + print(pokeObj.name[0]+' est soigné') |
| 151 | + updateObj(pokeObj) |
| 152 | + return self |
0 commit comments