-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
407 lines (365 loc) · 11.1 KB
/
utils.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from pymongo import Connection
import otterapi
from random import randint
Connection=Connection('mongo2.stuycs.org')
db=Connection.admin
res=db.authenticate('ml7','ml7')
players=db.players
market=db.market
bank=db.bank
date=db.date
count = 0
#name:string
def add_user(name):
db=Connection['EmotionStock']
if not name in get_players():
player={"name":str(name),"money":10000,"soul":100,'stocks':[]}
players.insert(player)
else:
return False
#returns a list
def get_players():
db=Connection['EmotionStock']
names=[]
for line in players.find():
names.append(line['name'])
return names
#returns an int
def get_money(user):
if authenticate(user):
db=Connection['EmotionStock']
name=players.find_one({"name":user})
return name["money"]
else:
return False
'''
parameters:
name:string
returns:
boolean
'''
def authenticate(name):
db=Connection["EmotionStock"]
if not name in get_players():
return False
return True
def auth_stock(name):
db=Connection["EmotionStock"]
stock=market.find_one({"stock":name})
if stock == None:
return False
return True
def get_market_x():
db=Connection["EmotionStock"]
x={}
for name in get_stocks_names():
x[name]=(get_x(name))
return x
def get_market_y():
db=Connection["EmotionStock"]
y={}
for name in get_stocks_names():
y[name]=(get_y(name))
return y
def get_x(stock):
db=Connection["EmotionStock"]
name=market.find_one({"stock":stock})
x=[]
for data in name["data"]:
x.append(data["time"])
return x
def get_y(stock):
db=Connection["EmotionStock"]
name=market.find_one({"stock":stock})
y=[]
for data in name["data"]:
y.append(data["price"])
return y
'''
parameters:
name:string
stock:string
count:int
returns:
boolean
'''
def buy_stock(name,stock,count):
db=Connection["EmotionStock"]
if (authenticate(name) and auth_stock(stock)):
user=players.find_one({"name":name})
cost=get_stock_price(stock)*count
if (user["money"]<cost or count < 0):
return False
else:
user["money"]=user["money"]-cost
if stock in get_stocks_user(name):
mystocks=user["stocks"]
for i in mystocks:
if i["stock"]==stock:
i["shares"]=i["shares"]+count
else:
stocks={"stock":stock,"shares":count}
user["stocks"].append(stocks)
players.update({"name":name},user)
return True
return False
'''
parameters:
name:string
stock:string
count:int
returns:
boolean
'''
def sell_stock(name,stock,count):
db=Connection["EmotionStock"]
if (authenticate(name) and auth_stock(stock)):
stocks=get_stocks(name)[0]
for item in stocks:
if item["stock"]==stock:
if (item["shares"]<count):
return False
item["shares"]=item["shares"]-count
if item["shares"]==0:
stocks.remove(item)
user=players.find_one({"name":name})
user["stocks"]=stocks
gain=get_stock_price(stock)*count
user["money"]=user["money"]+gain
players.update({"name":name},user)
return True
return False
#return: list of stocks for a specific user
def get_stocks(user):
db=Connection["EmotionStock"]
if(authenticate(user)):
name=players.find_one({"name":user})
stock=[]
gain=0
for item in name["stocks"]:
tmp_name=item["stock"]
tmp_share=item["shares"]
tmp_price=get_stock_price(tmp_name)*tmp_share
gain=gain+tmp_price
x={"shares":tmp_share,"stock":tmp_name,"price":tmp_price}
stock.append(x)
return [stock,gain]
#return: names of stocks for a specific user
def get_stocks_user(user):
db=Connection["EmotionStock"]
if(authenticate(user)):
name=players.find_one({"name":user})
stock=[]
for item in name["stocks"]:
stock.append(item["stock"])
return stock
#get all the stocks' data
def get_market():
db=Connection["EmotionStock"]
stocks=[]
for item in market.find():
stocks.append(item)
return stocks
#get all stocks' name in the market
def get_stocks_names():
db=Connection["EmotionStock"]
stocks=get_market()
names=[]
for item in stocks:
names.append(str(item["stock"]))
return names
#get a specific stock's dictionary
def get_stock(name):
db=Connection["EmotionStock"]
return market.find_one({"stock":name})
#get a specific stock's price
def get_stock_price(name):
db=Connection["EmotionStock"]
stock=market.find_one({"stock":name})
return stock["data"][len(stock["data"])-1]["price"]
def get_icon(name):
db=Connection["EmotionStock"]
stock=market.find_one({"stock":name})
price = stock["data"][len(stock["data"])-1]["price"]
last_price = stock["data"][len(stock["data"])-2]["price"]
if price>last_price:
return ["Stock Index Up.png",abs(price-last_price)]
if price==last_price:
return ["zoom_out.png",abs(price-last_price)]
if price<last_price:
return ["Stock Index Down.png",abs(price-last_price)]
#soul:int
def sell_soul(name,soul):
db=Connection["EmotionStock"]
if get_soul(name)<=soul:
return False
else:
user=players.find_one({"name":name})
user["money"]=user["money"]+soul*100
user["soul"]=user["soul"]-soul
if in_bank(name):
buser=bank.find_one({"name":name})
buser["soul"]=buser["soul"]+soul
bank.update({"name":name},buser)
else:
nbalance={"name":name,"soul":soul}
bank.insert(nbalance)
players.update({"name":name},user)
return True
def buy_soul(name,soul):
db=Connection["EmotionStock"]
user=players.find_one({"name":name})
if get_bank_soul(name)>=soul and user["money"]>=soul*100:
user["money"]=user["money"]-(soul*100)
user["soul"]=user["soul"]+soul
buser=bank.find_one({"name":name})
buser["soul"]=buser["soul"]-soul
bank.update({"name":name},buser)
if buser["soul"]==0:
bank.remove(buser)
players.update({"name":str(name)},user)
return True
return False
#returns an int
def get_soul(name):
db=Connection["EmotionStock"]
user=players.find_one({"name":name})
return user["soul"]
def get_bank_soul(name):
db=Connection["EmotionStock"]
if in_bank(name):
user=bank.find_one({"name":name})
return user["soul"]
else:
return -1
def in_bank(name):
db=Connection["EmotionStock"]
names=[]
for line in bank.find():
names.append(line["name"])
if not name in names:
return False
return True
'''
called daily by JS timer
'''
def update_market():
db=Connection["EmotionStock"]
names=get_stocks_names()
for name in names:
test(name)
def update_price(name):
db=Connection["EmotionStock"]
stock=market.find_one({"stock":name})
if len(stock["data"])>6:
#pops the first one
market.update({"stock":name},{"$pop":{"data":-1}});
count=otterapi.setup(name)[1]
time=otterapi.setup(name)[0]
#print time
price = (float(count)-float(stock["last count"]))/float(stock["last count"])
price = float('%.1f' % (round(price,1)))+1
price = int(stock["data"][len(stock["data"])-1]["price"]*price)
price = randint(-5,5)+price
if price<1:
price=price*(randint(1,3))
if price == 0:
price = 1
#print [price, count, stock["last count"]]
#print [count, stock["last count"]]
data=({"time":time,"price":price})
stock["last count"]=count
#print [price, count]
market.update({"stock":name},{"$push":{"data":data}})
market.update({"stock":name},{"$set":{"last count":count}})
def fix(name):
db=Connection["EmotionStock"]
stock=market.find_one({"stock":name})
data=stock["data"]
data[len(data)-2]["time"]="01/20"
market.update({"stock":name},{"$set":{"data":data}})
market.update({"stock":name},{"$pop":{"data":-1}})
def test(name):
db=Connection["EmotionStock"]
stock=market.find_one({"stock":name})
if len(stock["data"])>6:
market.update({"stock":name},{"$pop":{"data":-1}});
times=otterapi.create_times()
count=otterapi.pre_set(name,times[5],times[6])[1]
time=otterapi.pre_set(name,times[5],times[6])[0]
price=stock["data"][len(stock["data"])-1]["price"]*(1+((count-stock["last count"])/(stock["last count"]*100)))
price = (float(count)-float(stock["last count"]))/float(stock["last count"])
price = float('%.1f' % (round(price,1)))+1
price = int(stock["data"][len(stock["data"])-1]["price"]*price)
price = randint(-5,5)+price
if price<1:
price=1
print [price, count, stock["last count"]]
#print [count, stock["last count"]]
data=({"time":time,"price":price})
stock["last count"]=count
market.update({"stock":name},{"$push":{"data":data}})
market.update({"stock":name},{"$set":{"last count":count}})
def market_setup():
db=Connection["EmotionStock"]
names=["happy","love","sad","tired","bored","mad","sick"]
for name in names:
# times=otterapi.create_times()
# count=otterapi.pre_set(name,times[0],times[1])[1]
# time=otterapi.pre_set(name,times[0],times[1])[0]
stock={"stock":name,"last count":10000,"data":[{"time":"01/14","price":40}]}
market.insert(stock)
def date_setup():
db=Connection["EmotionStock"]
last_date={"last date":otterapi.get_date()}
date.insert(last_date)
def update_date(stuff):
db=Connection["EmotionStock"]
date.update({"last date":get_date()},{"$set":{"last date":stuff}})
def get_date():
db=Connection["EmotionStock"]
tmp=date.find_one()
return tmp["last date"]
'''
testing purpose only
'''
def delete_players():
db=Connection["EmotionStock"]
players.drop()
def delete_market():
db=Connection["EmotionStock"]
market.drop()
def delete_stuff(name):
db=Connection["EmotionStock"]
stock=market.find_one({"stock":name})
#print stock
market.update({"stock":name},{"$pop":{"data":len(stock["data"])-1}});
if __name__=="__main__":
#market_setup()
name="mengdi"
stock="happy"
names=["happy","love","sad","tired","bored","mad","sick"]
print get_stock("mad")
#delete_market()
#market_setup()
#update_market()
#print otterapi.real_time(get_date())
#update_price(names[0])
#print otterapi.real_time(get_date())
#print otterapi.real_time(get_date())
#print get_stocks_names();
#print get_stock(names[0])
# print get_market()
#update_market()
#add_user(name)
#buy_stock(name,stock,count)
#print auth_stock("happy")
#sell_stock(name,stock,10)
#print get_stocks(name)
#sell_soul(name,10)
#buy_soul(name,10)
#print bank.find_one({"name":"mengdi"})
#print players.find_one({"name":"mengdi"})
#add_user(name)
#buy_stock(name,stock,count)
#print get_stock(name)