-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
404 lines (325 loc) · 18.1 KB
/
app.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
import dotenv
import os
import json
from flask import Flask, render_template, request, jsonify
from pocketbase import PocketBase
from random import randint
import requests
dotenv.load_dotenv()
client = PocketBase('http://localhost:8090')
admin_data = client.admins.auth_with_password(os.getenv('POCKETBASE_EMAIL'), os.getenv('POCKETBASE_PWD'))
app = Flask(__name__)
@app.route('/ping')
def ping():
return 'pong'
@app.route('/init')
def init():
#TODO: Add init html page to create groups and dealers and the money of those with there passwords
return render_template('init.html')
@app.route('/getDealerPrices', methods=['GET','POST'])
def getDealerPrices():
# Get the dealer prices of the materials for each dealer
# We take the dealer id as an argument and will create a json with the prices of the materials for every dealer exept the one with the id that we passed
# Do it like this in the request: http://localhost:5000/getDealerPrices?id=1
id = request.args.get('id')
dealers = client.collection("dealers").get_full_list()
dealer_data = {}
for dealer in dealers:
if dealer.id != id:
prices_id = dealer.prices
prices = client.collection("prices").get_one(prices_id)
if hasattr(prices, 'gold') and hasattr(prices, 'wood') and hasattr(prices, 'wool') and hasattr(prices, 'diamond') and hasattr(prices, 'cole'):
dealer_data[dealer.name] = {"gold": prices.gold, "wood": prices.wood, "wool": prices.wool, "diamond": prices.diamond, "cole": prices.cole}
else:
print(f"Prices record {prices_id} does not have all the required attributes")
return jsonify(dealer_data)
@app.route('/getOwnPrices', methods=['GET','POST'])
def getOwnPrices():
# Get the dealer prices of the materials for the dealer with the id that we pass
# We take the dealer id as an argument and will create a json with the prices of the materials for the dealer with the id that we passed
# Do it like this in the request: http://localhost:5000/getOwnPrices?id=1
id = request.args.get('id')
dealer = client.collection("dealers").get_one(id)
prices_id = dealer.prices
prices = client.collection("prices").get_one(prices_id)
return jsonify({"gold": prices.gold, "wood": prices.wood, "wool": prices.wool, "cole": prices.cole, "diamond": prices.diamond})
@app.route('/updatePrices', methods=['GET','POST'])
def updatePrices():
# Update the prices of the materials for the dealer with the id that we pass
# We take the dealer id as an argument and the new prices for the materials
# Do it like this in the request: http://localhost:5000/updatePrices?id=1&gold=100&wood=100&wool=50&cole=75&diamond=200
id = request.args.get('id')
gold = request.args.get('gold')
wood = request.args.get('wood')
wool = request.args.get('wool')
cole = request.args.get('cole')
diamond = request.args.get('diamond')
dealer = client.collection("dealers").get_one(id)
prices_id = dealer.prices
client.collection("prices").update(prices_id, {"gold": gold, "wood": wood, "wool": wool, "cole": cole, "diamond": diamond})
return "Prices updated"
@app.route('/listGroups', methods=['GET','POST'])
def listGroups():
# Get all Groups in one list with their names and ids
groups = client.collection("groups").get_full_list()
group_data = [{"id": record.id, "name": record.name} for record in groups]
return jsonify(group_data)
@app.route('/listDealers', methods=['GET','POST'])
def listDealers():
# Get all Dealers in one list with their names and ids
dealers = client.collection("dealers").get_full_list()
dealer_data = [{"id": record.id, "name": record.name} for record in dealers]
return jsonify(dealer_data)
@app.route('/checkForPwd/groups', methods=['GET','POST'])
def checkForPwdGroups():
# The id of the group sql will be passed in the request with the key 'id'
# And the password that the group had entered
# Do it like this in the request http://localhost:5000/checkForPwd/groups?id=1&password=1234
id = request.args.get('id')
pwd = client.collection("groups").get_one(id).password
if pwd == int(request.args.get('password')):
return 'true'
else:
return 'false'
@app.route('/checkForPwd/dealers', methods=['GET','POST'])
def checkForPwdDealers():
# The id of the dealer sql will be passed in the request with the key 'id'
# And the password that the dealer had entered
# Do it like this in the request http://localhost:5000/checkForPwd/dealers?id=1&password=1234
id = request.args.get('id')
pwd = client.collection("dealers").get_one(id).password
if pwd == int(request.args.get('password')):
return 'true'
return 'false'
@app.route('/openTrade', methods=['GET','POST'])
def openTrade():
# Opens a trade that has to be accepted by the dealer
# We take the dealer id the materialSum, the blingSum, the transactionType(if its a sell or a buy from the group) and the material name(gold, wood, etc) as arguments
# Do it like this in the request: http://localhost:5000/openTrade?dealer_id=1&materialSum=100&blingSum=100&transactionType=buy&material=gold
dealer_id = request.args.get('dealer_id')
materialSum = request.args.get('materialSum')
blingSum = request.args.get('blingSum')
transactionType = request.args.get('transactionType')
material = request.args.get('material')
client.collection("openTrades").create({
"dealer": dealer_id,
"materialsum": materialSum,
"blingsum": blingSum,
"transactiontype": transactionType,
"material": material
})
return "Trade opened"
@app.route('/listDealersClosedTrades', methods=['GET','POST'])
def listDealersTrades():
# Get all the trades that the dealer has opened
# We take the dealer id as an argument
# Do it like this in the request: http://localhost:5000/listDealersClosedTrades?id=1
dealer_id = request.args.get('id')
trades = client.collection("closedTrades").get_full_list()
trades_data = [{"id": record.id, "created": record.created, "dealer": record.dealer, "materialSum": record.materialsum, "blingSum": record.blingsum, "transactionType": record.transactiontype, "material": record.material} for record in trades if record.dealer == dealer_id ]
return jsonify(trades_data)
@app.route('/listDealerOpenTrades', methods=['GET','POST'])
def listDealerOpenTrades():
# Get all the trades that the dealer has opened
# We take the dealer id as an argument
# Do it like this in the request: http://localhost:5000/listDealerOpenTrades?id=1
dealer_id = request.args.get('id')
trades = client.collection("openTrades").get_full_list()
trades_data = [{"id": record.id, "created": record.created, "dealer": record.dealer, "materialSum": record.materialsum, "blingSum": record.blingsum, "transactionType": record.transactiontype, "material": record.material} for record in trades if record.dealer == dealer_id ]
return jsonify(trades_data)
@app.route('/getPortfolio', methods=['GET','POST'])
def getPortfolio():
# Get the portfolio of a group or a dealer
# We take the id of the group or dealer as an argument
# And the type of the portfolio, if its a group or a dealer
# And the resource that we want to get(gold, wood, bling, wool, cole, diamond)
# Do it like this in the request: http://localhost:5000/getPortfolio?id=1&type=group&resource=gold
id = request.args.get('id')
type = request.args.get('type')
resource = request.args.get('resource')
if type == "dealer":
getportfolio = client.collection("dealers").get_one(id).portfolio
if resource == "gold":
getgold = client.collection("portfolio").get_one(getportfolio).gold
return str(getgold)
if resource == "wood":
getwood = client.collection("portfolio").get_one(getportfolio).wood
return str(getwood)
if resource == "bling":
getbling = client.collection("portfolio").get_one(getportfolio).bling
return str(getbling)
if resource == "wool":
getwool = client.collection("portfolio").get_one(getportfolio).wool
return str(getwool)
if resource == "cole":
getcole = client.collection("portfolio").get_one(getportfolio).cole
return str(getcole)
if resource == "diamond":
getdiamond = client.collection("portfolio").get_one(getportfolio).diamond
return str(getdiamond)
if type == "group":
getportfolio = client.collection("groups").get_one(id).portfolio
if resource == "gold":
getgold = client.collection("portfolio").get_one(getportfolio).gold
return str(getgold)
if resource == "wood":
getwood = client.collection("portfolio").get_one(getportfolio).wood
return str(getwood)
if resource == "bling":
getbling = client.collection("portfolio").get_one(getportfolio).bling
return str(getbling)
if resource == "wool":
getwool = client.collection("portfolio").get_one(getportfolio).wool
return str(getwool)
if resource == "cole":
getcole = client.collection("portfolio").get_one(getportfolio).cole
return str(getcole)
if resource == "diamond":
getdiamond = client.collection("portfolio").get_one(getportfolio).diamond
return str(getdiamond)
return "Error"
@app.route('/getPortfolioAll', methods=['GET','POST'])
def getPortfolioAll():
# Get the portfolio of a group or a dealer
# We take the id of the group or dealer as an argument
# And the type of the portfolio, if its a group or a dealer
# Do it like this in the request: http://localhost:5000/getPortfolioAll?id=1&type=group
id = request.args.get('id')
type = request.args.get('type')
if type == "dealer":
getportfolio = client.collection("dealers").get_one(id).portfolio
getgold = client.collection("portfolio").get_one(getportfolio).gold
getwood = client.collection("portfolio").get_one(getportfolio).wood
getbling = client.collection("portfolio").get_one(getportfolio).bling
return jsonify({"gold": getgold, "wood": getwood, "bling": getbling})
if type == "group":
getportfolio = client.collection("groups").get_one(id).portfolio
getgold = client.collection("portfolio").get_one(getportfolio).gold
getwood = client.collection("portfolio").get_one(getportfolio).wood
getbling = client.collection("portfolio").get_one(getportfolio).bling
return jsonify({"gold": getgold, "wood": getwood, "bling": getbling})
return "Error"
@app.route('/updatePortfolio', methods=['GET','POST'])
def updatePortfolio():
# Update the portfolio of a group or a dealer
# We take the id of the group or dealer as an argument
# And the type of the portfolio, if its a group or a dealer
# And the resource that we want to update(gold, wood, bling)
# And the value that we want to update it with
# Do it like this in the request: http://localhost:5000/updatePortfolio?id=1&type=group&resource=gold&value=100
id = request.args.get('id')
type = request.args.get('type')
resource = request.args.get('resource')
value = request.args.get('value')
if type == "dealer":
getportfolio = client.collection("dealers").get_one(id).portfolio
if resource == "gold":
client.collection("portfolio").update(getportfolio, {"gold": value})
if resource == "wood":
client.collection("portfolio").update(getportfolio, {"wood": value})
if resource == "bling":
client.collection("portfolio").update(getportfolio, {"bling": value})
if type == "group":
getportfolio = client.collection("groups").get_one(id).portfolio
if resource == "gold":
client.collection("portfolio").update(getportfolio, {"gold": value})
if resource == "wood":
client.collection("portfolio").update(getportfolio, {"wood": value})
if resource == "bling":
client.collection("portfolio").update(getportfolio, {"bling": value})
return "Portfolio updated"
@app.route('/getTradeInfo', methods=['GET','POST'])
def getTradeInfo():
# Get the trade information of a trade
# We take the id of the trade as an argument
# Do it like this in the request: http://localhost:5000/getTradeInfo?id=1
id = request.args.get('id')
get = client.collection("openTrades").get_one(id)
return jsonify({ "materialSum": get.materialsum, "blingSum": get.blingsum, "transactionType": get.transactiontype, "material": get.material})
@app.route('/closeTrade', methods=['GET','POST'])
def closeTrade():
# Closes a trade that has been created by the dealer
# We take the openTrade id and the id of the group that wants to close the trade
# Do it like this in the request: http://localhost:5000/closeTrade?id=1&groupId=1
print('in qu')
closeTradeId = request.args.get('id')
groupId = request.args.get('groupId')
# lets save the open trades informations in a variable
get = client.collection("OpenTrades").get_one(closeTradeId)
closeTradeMaterialSum = get.materialsum
closeTradeBlingSum = get.blingsum
closeTradeTransactionType = get.transactiontype
closeTradeMaterial = get.material
closedTradeDealerId = get.dealer
# lets transact the material and the bling
# before we do that we need to check if the group has enough material and bling to transact
if closeTradeTransactionType == "buy":
# check if the group has enough bling
urlGroup = "http://localhost:5000/getPortfolio?id=" + str(groupId) + "&type=group&resource=bling"
getbling = int(requests.get(urlGroup).text)
if getbling < closeTradeBlingSum:
return "Not enough bling"
# check if the dealer has enough material
urlDealer = "http://localhost:5000/getPortfolio?id=" + str(closedTradeDealerId) + "&type=dealer&resource=" + closeTradeMaterial
getmaterial = int(requests.get(urlDealer).text)
if getmaterial < closeTradeMaterialSum:
return "Not enough material"
# update the portfolio of the group
urlGroup = "http://localhost:5000/updatePortfolio?id=" + str(groupId) + "&type=group&resource=bling&value=" + str(getbling - closeTradeBlingSum)
requests.get(urlGroup)
# get the info about the material of the group
materialgroup = int(requests.get("http://localhost:5000/getPortfolio?id=" + str(groupId) + "&type=group&resource=" + closeTradeMaterial).text)
# add the material to the group
urlGroup = "http://localhost:5000/updatePortfolio?id=" + str(groupId) + "&type=group&resource=" + closeTradeMaterial + "&value=" + str(materialgroup + closeTradeMaterialSum)
requests.get(urlGroup)
# update the portfolio of the dealer
urlDealer = "http://localhost:5000/updatePortfolio?id=" + str(closedTradeDealerId) + "&type=dealer&resource=" + closeTradeMaterial + "&value=" + str(getmaterial - closeTradeMaterialSum)
requests.get(urlDealer)
# get the info about the bling of the dealer
blingdealer = int(requests.get("http://localhost:5000/getPortfolio?id=" + str(closedTradeDealerId) + "&type=dealer&resource=bling").text)
# add the bling to the dealer
urlDealer = "http://localhost:5000/updatePortfolio?id=" + str(closedTradeDealerId) + "&type=dealer&resource=bling&value=" + str(blingdealer + closeTradeBlingSum)
requests.get(urlDealer)
if closeTradeTransactionType == "sell":
# check if the group has enough material
urlGroup = "http://localhost:5000/getPortfolio?id=" + str(groupId) + "&type=group&resource=" + closeTradeMaterial
getmaterial = int(requests.get(urlGroup).text)
if getmaterial < closeTradeMaterialSum:
return "Not enough material"
# check if the dealer has enough bling
urlDealer = "http://localhost:5000/getPortfolio?id=" + str(closedTradeDealerId) + "&type=dealer&resource=bling"
getbling = int(requests.get(urlDealer).text)
if getbling < closeTradeBlingSum:
return "Not enough bling"
# update the portfolio of the group
urlGroup = "http://localhost:5000/updatePortfolio?id=" + str(groupId) + "&type=group&resource=" + closeTradeMaterial + "&value=" + str(getmaterial - closeTradeMaterialSum)
requests.get(urlGroup)
# get the info about the material of the group
materialgroup = int(requests.get("http://localhost:5000/getPortfolio?id=" + str(groupId) + "&type=group&resource=bling").text)
# add the material to the group
urlGroup = "http://localhost:5000/updatePortfolio?id=" + str(groupId) + "&type=group&resource=bling&value=" + str(materialgroup + closeTradeBlingSum)
requests.get(urlGroup)
# update the portfolio of the dealer
urlDealer = "http://localhost:5000/updatePortfolio?id=" + str(closedTradeDealerId) + "&type=dealer&resource=bling&value=" + str(getbling - closeTradeBlingSum)
requests.get(urlDealer)
# get the info about the resource of the dealer
blingdealer = int(requests.get("http://localhost:5000/getPortfolio?id=" + str(closedTradeDealerId) + "&type=dealer&resource=" + closeTradeMaterial).text)
# add the resource to the dealer
urlDealer = "http://localhost:5000/updatePortfolio?id=" + str(closedTradeDealerId) + "&type=dealer&resource=" + closeTradeMaterial + "&value=" + str(blingdealer + closeTradeMaterialSum)
requests.get(urlDealer)
# delete the trade
client.collection("OpenTrades").delete(closeTradeId)
# add trade to the closed trades
client.collection("closedTrades").create({
"dealer": closedTradeDealerId,
"group": groupId,
"materialsum": closeTradeMaterialSum,
"blingsum": closeTradeBlingSum,
"transactiontype": closeTradeTransactionType,
"material": closeTradeMaterial,
"typeofclosing": "completed"
})
print('que fast fertig')
return "Trade closed"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')