-
Notifications
You must be signed in to change notification settings - Fork 0
/
tamasimulation.py
269 lines (211 loc) · 9.05 KB
/
tamasimulation.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
import json
import item
import shop
import constants as con
class TamaSimulation(object):
def __init__(self, uid, password=None):
self.uid = uid
self.type = "basetama"
self.password = password
self.MAX_HUNGER = 100
self.hunger = self.MAX_HUNGER
self.mood = 50
self.sick = False
self.money = 100
self.inventory = []
self.knows = {}
def getDBValueLabels(self):
"""Returns a tuple of names to be used with getDBValues"""
return ("uid", "type", "password",
"hunger", "mood", "sick","money")
def getDBValues(self):
"""Returns a tuple of values to be inserted into a database"""
return (self.uid, self.type, self.password,
self.hunger, self.mood, self.sick, self.money)
def getInventoryDBValues(self):
"""Returns a list of tuples of items (uid,key,amount))
to be inserted into a database"""
keys = set(self.inventory) #get unique values
itemDBValues = []
for key in keys:
itemDBValues.append((self.uid, key, self.inventory.count(key)))
return itemDBValues
def readDBValues(self, dbValuesTuple):
"""Reads a tuple of values read from a database"""
self.uid, self.type, self.password, self.hunger,\
self.mood, self.sick, self.money = dbValuesTuple
def readInventoryDBValues(self, dbValuesTuple):
"""Reads a tuple of values read from a database"""
#self.uid, self.type, self.password, self.hunger,\
# self.mood, self.sick, self.money = dbValuesTuple
pass
def possessiveName(self):
if self.uid.endswith("s") or self.uid.endswith("z"):
return self.uid + "'"
else:
return self.uid + "'s"
def getImageFileName(self):
"""Returns the file path to the image of the tama depending on
it's mood and other factors
"""
directory = "images/tama/%s/" % self.type
if self.mood > con.LIKE_LIMIT:
return directory + "happy.png"
else:
return directory + "regular.png"
def getStatusJSON(self, formatForHTML=False):
valueDict = dict(zip(self.getDBValueLabels(), self.getDBValues()))
valueDict.update({"error": False})
s = json.dumps(valueDict, indent=4)
if formatForHTML:
s = s.replace("\n", "</br>\n").replace('"','').replace(":",": ")
s = s.replace("{","Status:").replace("}", "")
return s
#"<img src='%s'></img>" % ("/"+self.getImageFileName())
def addItem(self, itemStr):
self.inventory.append(itemStr)
return "%s now has a %s" % (self.uid, itemStr)
def addItemJSON(self, itemStr):
self.inventory.append(itemStr)
s = "%s now has a %s" % (self.uid, itemStr)
return json.dumps({"error": False, "message": s})
def eat(self, itemStr):
if itemStr not in self.inventory:
return "%s doesn't have a %s" % (self.uid, itemStr)
if not item.isEdible(itemStr):
return "%s can't eat a %s" % (self.uid, itemStr)
s = "%s ate a %s!" % (self.uid, itemStr)
if item.isPoisonous(itemStr):
if not self.sick: #only tell if we're not already sick
s += " It sickened %s!" % self.uid
self.sick = True
if self.sick and item.isHealing(itemStr):
self.sick = False
s += " It healed %s sickness." % (self.possessiveName())
self.inventory.remove(itemStr)
return s
def eatJSON(self, itemStr):
"""Eats an item.
Returns a dictionary to be JSON'ed later on
"""
if not itemStr:
return json.dumps(
{"error": True, "message": "Tried to eat nothing!"})
if itemStr not in self.inventory:
s = "%s doesn't have a %s" % (self.uid, itemStr)
return json.dumps(
{"error": True, "message": s})
if not item.isEdible(itemStr):
s = "%s can't eat a %s" % (self.uid, itemStr)
return json.dumps({"error": False, "message": s})
s = "%s ate a %s!" % (self.uid, itemStr)
if item.hasProperty(itemStr, item.POISONOUS):
if not self.sick: #only tell if we're not already sick
s += " It sickened %s!" % self.uid
self.sick = True
if self.sick and item.isHealing(itemStr):
self.sick = False
s += " It healed %s sickness." % (self.possessiveName())
self.inventory.remove(itemStr)
return json.dumps({"error": False, "message": s})
def petJSON(self, itemStr=None):
if itemStr is None:
self.changeMood(con.MOOD_INCREASE_IF_LIKE)
else:
if item.isPettable(itemStr):
self.changeMood(con.MOOD_INCREASE_IF_LOVE)
else:
self.changeMood(con.MOOD_INCREASE_IF_DISLIKE)
if itemStr is None: #Was petted without an item
s = "%s was petted!" % (self.uid)
return json.dumps({"error": False, "message": s,
"newmood": self.mood})
elif itemStr not in self.inventory: #Don't have that item!
s = "%s doesn't have a %s" % (self.uid, itemStr)
return json.dumps({"error": True, "message": s})
s = "%s was petted with a %s!" % (self.uid, itemStr)
if item.isPettable(itemStr):
s += "\nIt liked it"
else:
s += "\nIt didn't like it"
s += "\nNew mood: %s" % self.mood
if item.hasProperty(itemStr, item.POISONOUS):
if not self.sick: #only tell if we're not already sick
s += " It sickened %s!" % self.uid
self.sick = True
return json.dumps({"error": False, "message": s,
"newmood": self.mood})
def playWithItemJSON(self, itemStr):
if not itemStr:
s = "No item was given!"
return json.dumps({"error": True, "message": s})
if item.isPlayable(itemStr):
s = "%s played with %s, becoming happier in the process!" % \
(self.uid, itemStr)
self.changeMood(con.MOOD_INCREASE_IF_LIKE)
else:
s = "%s doesn't want to play with %s..." % (self.uid, itemStr)
return json.dumps({"error": False, "message": s})
def changeMood(self, amount):
self.mood += amount
if self.mood < 0:
self.mood = 0
elif self.mood > con.MAX_MOOD:
self.mood = con.MAX_MOOD
def addFriend(self, uid2):
self.knows[uid2] = 0
s = "%s now knows %s!" % (self.uid, uid2)
return json.dumps({"error":False, "message": s})
def playWithTamaJSON(self, otherTama):
"""Plays with another tama. This will change it's mood.
"""
s = ""
id2 = otherTama.uid
if id2 not in self.knows:
self.knows[id2] = con.START_KNOWLEDGE_LEVEL
s += "%s just learned about %s!</br>" % (self.uid, otherTama.uid)
if self.knows[id2] < con.LIKE_LIMIT: #dislikes, will dislike more
self.knows[id2] += con.CHANGE_ON_DISLIKE
s += "%s now likes %s less...</br>" % (self.uid, otherTama.uid)
else:
self.knows[id2] += con.CHANGE_ON_LIKE
s += "%s now likes %s more!</br>" % (self.uid, otherTama.uid)
if self.knows[id2] > con.LOVE_LIMIT:
s += "%s loves %s!</br>" % (self.uid, otherTama.uid)
self.changeMood(con.MOOD_INCREASE_IF_LOVE)
elif self.knows[id2] >= con.LIKE_LIMIT:
self.changeMood(con.MOOD_INCREASE_IF_LIKE)
elif self.knows[id2] <= con.HATE_LIMIT:
s += "%s hates %s!</br>" % (self.uid, otherTama.uid)
self.changeMood(con.MOOD_INCREASE_IF_HATE)
else:
self.changeMood(con.MOOD_INCREASE_IF_DISLIKE)
if self.knows[id2] < 0:
self.knows[id2] = 0
elif self.knows[id2] > con.MAX_KNOWLEDGE_LEVEL:
self.knows[id2] = con.MAX_KNOWLEDGE_LEVEL
return json.dumps({"error": False, "message": s})
def updateMood(self):
if self.hunger > 50:
pass
else:
pass
def buyItem(self, shopObject, itemStr):
if itemStr not in shopObject.itemAndCostDict:
return json.dumps({"error": True,
"message": "Item %s isn't in the shop!" % itemStr})
itemCost = shopObject.itemAndCostDict[itemStr]
if self.money >= itemCost:
self.money -= itemCost
self.inventory.append(itemStr)
del shopObject.itemAndCostDict[itemStr]
s = "%s bought a %s for $%s" % (self.uid, itemStr, itemCost)
return json.dumps({"error": False, "message": s})
else:
s = "%s only have $%s, but the %s costs $%s!" % \
(self.uid, self.money, itemStr, itemCost)
return json.dumps({"error": True, "message": s})
def updateSimulation(self, dt):
dtMinutes = dt / 60.0
self.hunger = min(self.hunger + dtMinutes, self.MAX_HUNGER)
self.updateMood()