-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.py
173 lines (129 loc) · 3.92 KB
/
character.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf8 -*-
"""
Module to handle the characters in the game
"""
from Model import Model
import item
class character:
"""
Class to interact with the characters in the game.
"""
inventory = None
@staticmethod
def searchByNameAndIdArea(name, idArea):
"""
character.character.searchByNameAndIdArea(name, idArea) -> character.character
Search a character from its name in a given area
@param name string name of the character to search
@param idArea integer id of the area where the character is searched
@return character.character if found, else None
"""
m = model.loadBy({'name': name, 'id_area': idArea})
if len(m) > 0:
return character.loadFromModel(m[0])
return None
@staticmethod
def searchByIdArea(idArea):
"""
character.character.searchByIdArea(idArea) -> list
Search characters being in an area which id is given in argument.
@param idArea integer id of the area where the characters are searched
@return list list of characters founds
"""
models = model.loadBy({'id_area': idArea})
chars = list()
for m in models:
chars.append(character.loadFromModel(m))
return chars
@staticmethod
def loadFromModel(m):
"""
character.character.loadFromModel(m) -> character.character
Create a character from a given model.
@param m dict model of the character to create
@return character.character if the model is not empty, else None
"""
if len(m) == 0:
return None
else:
c = character()
c._model = m
return c
def getId(self):
"""
c.getId() -> integer
Returns the character's id
@return integer character's id
"""
return self._model['id_character']
def goTo(self, idArea):
"""
character.character.goTo(idArea)
Move a character to a given area
@param idArea id of the area where the character must go.
"""
self._model['id_area'] = idArea
model.savePosition(self._model['id_character'], self._model['id_area'])
def getInventory(self):
"""
character.character.getInventory() -> dict()
Returns the character's items.
The inventory has the following format:
{
idItem: {'quantity': number}
}
@return dict the character's inventory
"""
if self.inventory is None:
self.inventory = item.inventory.fromStr(str(self._model['inventory']))
return self.inventory
def addItemsToInventory(self, itemsId):
"""
character.character.addItemsToInventory(itemsIds)
Add a list of items in the character's inventory.
@param itemsId ids of the items to add
"""
self.inventory = item.inventory.addItems(self.getInventory(), itemsId)
model.saveInventory(self._model['id_character'], self.inventory)
def removeItemsFromInventory(self, itemsId):
"""
character.character.removeItemsFromInventory(itemsIds)
Remove a list of items from the character's inventory.
@param itemsId ids of the items to remove
"""
self.inventory = item.inventory.removeItems(self.getInventory(), itemsId)
model.saveInventory(self._model['id_character'], self.inventory)
class model(Model):
"""
Class to interact with the values in the database.
"""
fields = ['id_character', 'name', 'id_species', 'id_gender', 'id_area', 'inventory']
@staticmethod
def savePosition(idCharacter, idArea):
"""
character.model.savePosition(idCharacter, idArea)
Change a character's position
@param idCharacter integer id of the character to move
@param idArea id of the area where the character must go.
"""
model.update(
{'id_area': idArea},
('id_character = ?', [idCharacter])
)
@staticmethod
def saveInventory(idCharacter, inventory):
"""
character.model.saveInventory(idCharacter, inventory)
Save the character's inventory in the database.
@param idCharacter character's id.
@param inventory list items to add.
"""
model.update(
{'inventory': item.inventory.toStr(inventory)},
('id_character = ?', [idCharacter])
)
class exception(BaseException):
"""
Class for the exceptions concerning characters.
"""
pass