-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
297 lines (234 loc) · 8.98 KB
/
stats.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
"""stats.py class required for stats used by the user.
Information contained in this module is related with cells that can be placed
on the board. Every cells is a stat value that the used should be account for.
New cells, it means new stats can be added along the design of the game.
:author: Jose Carlos Recuero
:version: 0.1
:since: 10/01/2014
"""
__docformat__ = 'restructuredtext en'
###############################################################################
## _ _
## (_)_ __ ___ _ __ ___ _ __| |_ ___
## | | '_ ` _ \| '_ \ / _ \| '__| __/ __|
## | | | | | | | |_) | (_) | | | |_\__ \
## |_|_| |_| |_| .__/ \___/|_| \__|___/
## |_|
###############################################################################
#
# import std python modules
#
import yaml
#
# import user python modules
#
import loggerator
###############################################################################
##
## ___ ___ _ __ ___| |_ __ _ _ __ | |_ ___
## / __/ _ \| '_ \/ __| __/ _` | '_ \| __/ __|
## | (_| (_) | | | \__ \ || (_| | | | | |_\__ \
## \___\___/|_| |_|___/\__\__,_|_| |_|\__|___/
##
###############################################################################
#
EXP_NEXT_LEVEL = 100
"""
:type: int
Experience required to reach the next level.
"""
###############################################################################
## _ _ _
## ___ _ _| |__ _ __ ___ _ _| |_(_)_ __ ___ ___
## / __| | | | '_ \| '__/ _ \| | | | __| | '_ \ / _ \/ __|
## \__ \ |_| | |_) | | | (_) | |_| | |_| | | | | __/\__ \
## |___/\__,_|_.__/|_| \___/ \__,_|\__|_|_| |_|\___||___/
##
###############################################################################
#
#------------------------------------------------------------------------------
def addStats(theOneStat, theOtherStat):
""" Add two stats values (as a pair)
:type theOneStat: list
:param theOneStat: first stat to add
:type theOtherStat: list
:param theOtherStat: second stat to add
:rtype: list
:param: list with both stats added
"""
value = theOneStat[0] + theOtherStat[0]
count = theOneStat[1] + theOtherStat[1]
runs = theOneStat[1] + theOtherStat[1]
return (value, count, runs)
###############################################################################
## _ _ __ _ _ _ _
## ___| | __ _ ___ ___ __| | ___ / _(_)_ __ (_) |_(_) ___ _ __ ___
## / __| |/ _` / __/ __| / _` |/ _ \ |_| | '_ \| | __| |/ _ \| '_ \/ __|
## | (__| | (_| \__ \__ \ | (_| | __/ _| | | | | | |_| | (_) | | | \__ \
## \___|_|\__,_|___/___/ \__,_|\___|_| |_|_| |_|_|\__|_|\___/|_| |_|___/
##
###############################################################################
#
#
#------------------------------------------------------------------------------
class Stats (object):
"""
"""
#--------------------------------------------------------------------------
def __init__(self):
""" Initialize Stats instance
>>> s = Stats()
>>> s.level, s.exp
(0, 0)
>>> s.coin.value
{'count': 0, 'value': 0}
>>> s.axe
{'count': 0, 'value': 0}
"""
self.level = 0
self.exp = 0
statsStream = open('config/stats.yaml', 'r')
self.stdict = yaml.load(statsStream)
for key, val in self.stdict.iteritems():
self.stdict[key] = self._initStat(val)
self.logger = loggerator.getLoggerator('stat')
#--------------------------------------------------------------------------
def _initStat(self, theInitValue):
""" Initialize value for a stat attribute.
:type theInitValue: int
:param theInitValue: initial value for a stat
:rtype: dict
:return: dict with initial values
"""
return {'count': 0, 'value': theInitValue, 'runs': 0}
#--------------------------------------------------------------------------
def _incStatField(self, theStat, theField, theIncVal=1):
""" Increase a field for a stat.
:type theStat: dict
:param theStat: dictinary with the stat to update
:type theField: str
:param theField: name of the field
:type theIncVal: int
:param theIncVal: magnitude to increase the value
"""
theStat[theField] += theIncVal
return theStat[theField]
#--------------------------------------------------------------------------
def addExp(self, theExp):
""" Add some experience
>>> s = Stats()
>>> s.addExp(10)
>>> s.exp, s.level
(10, 0)
>>> s.addExp(125)
>>> s.exp, s.level
(35, 1)
:type theExp: int
:param theExp: experience to be added
"""
self.exp += theExp
while self.exp >= EXP_NEXT_LEVEL:
self.exp -= EXP_NEXT_LEVEL
self.levelUp()
#--------------------------------------------------------------------------
def levelUp(self):
""" Level up instance
>>> s = Stats()
>>> s.levelUp()
>>> s.level
1
"""
self.level += 1
for key in self.stdict.keys():
self.addStatValue(key)
#--------------------------------------------------------------------------
def getStatsData(self):
""" Return dictionary with stats
:rtype: dict
:return: dictionary with stats
"""
return self.stdict
#--------------------------------------------------------------------------
def _getStatField(self, theKlass, theField):
""" Return the given stat instance.
:type theKlass: str
:param theKlass: name of the stat
:type theField: str
:param theField: name of the field to retrieve information
:rtype: int
:return: field content
"""
stat = self.stdict.get(theKlass, None)
return stat[theField] if stat else None
#--------------------------------------------------------------------------
def getStatValue(self, theKlass):
""" Return value for the given stat
>>> s = Stats()
>>> s.coin = 100
:type theKlass: object
:param theKlass: instance with the cell stat
"""
return self._getStatField(theKlass, 'value')
#--------------------------------------------------------------------------
def getStatCount(self, theKlass):
""" Return count for the given stat
:type theKlass: object
:param theKlass: instance with the cell stat
"""
return self._getStatField(theKlass, 'count')
#--------------------------------------------------------------------------
def getStatRuns(self, theKlass):
""" Return runs for the given stat
:type theKlass: object
:param theKlass: instance with the cell stat
"""
return self._getStatField(theKlass, 'runs')
#--------------------------------------------------------------------------
def addStatValue(self, theKlass, theValue=1):
""" Add a value to the stat value field.
:type theValue: int
:param theValue: value to add to the counter
:type theKlass: object
:param theKlass: instance with the cell stat
:rtype: int/bool
:return: new stat count value, None if stat not found
"""
stat = self.stdict.get(theKlass, None)
return self._incStatField(stat, 'value', theValue) if stat else None
#--------------------------------------------------------------------------
def addStatCount(self, theKlass, theValue=1):
""" Add a value to the stat counter field.
:type theValue: int
:param theValue: value to add to the counter
:type theKlass: object
:param theKlass: instance with the cell stat
:rtype: int/bool
:return: new stat count value, None if stat not found
"""
stat = self.stdict.get(theKlass, None)
return self._incStatField(stat, 'count', theValue) if stat else None
#--------------------------------------------------------------------------
def addStatRuns(self, theKlass, theValue=1):
""" Add a value to the stat runs field.
:type theValue: int
:param theValue: value to add to the counter
:type theKlass: object
:param theKlass: instance with the cell stat
:rtype: int/bool
:return: new stat count value, None if stat not found
"""
stat = self.stdict.get(theKlass, None)
return self._incStatField(stat, 'runs', theValue) if stat else None
###############################################################################
## _
## _ __ ___ __ _(_)_ __
## | '_ ` _ \ / _` | | '_ \
## | | | | | | (_| | | | | |
## |_| |_| |_|\__,_|_|_| |_|
##
###############################################################################
#
if __name__ == '__main__':
import doctest
doctest.testmod()