-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
253 lines (162 loc) · 8 KB
/
server.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
import json
import datetime
import os
from flask import Flask, render_template, request, render_template_string, url_for, session
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key_here'
app.static_folder = 'static'
# Main route
@app.route("/")
def index():
if not os.path.exists('Uploads'):
os.mkdir('Uploads')
return render_template("index.html") #Redirect to the index page
@app.route("/auction", methods=['POST'])
def auction():
config_file = request.files["file"] # Save the file auction config name
if config_file: # The user wants to resume an auction
session['file_name'] = "Uploads/" + config_file.filename
config_file.save("Uploads/" + config_file.filename) # Save the file into the Uploads folder
configs, partecipants, events = get_data_from_file("Uploads/" + config_file.filename)
return render_template("auction.html", partecipants=partecipants, events=events)
else:
return render_template("auction-config.html")
@app.route("/manage-auction", methods=['POST'])
def manage_auction():
# Retrieve data from session
file_name = session.get('file_name')
if not file_name: # New auction
formatted_datetime = datetime.datetime.now().strftime("%H-%M-%d-%m-%Y") # Get formatted daytime
configs = {
"partecipants": int(request.form.get('partecipants')),
"credits": int(request.form.get('credits')),
"gk": int(request.form.get('gk')),
"def": int(request.form.get('def')),
"mid": int(request.form.get('mid')),
"att": int(request.form.get('att')),
"file": f"Uploads/{formatted_datetime}-auction.txt"
} # Save configs
session['file_name'] = f"Uploads/{formatted_datetime}-auction.txt"
partecipants = []
events = []
for i in range(0, configs["partecipants"]): # Initialize the partecipants dict
partecipants.append({
"name": f"Partecipant {i}",
"credits": configs["credits"],
"gk": [],
"def": [],
"mid": [],
"att": []
})
save_new_data(f"Uploads/{formatted_datetime}-auction.txt", configs, partecipants, events)
elif request.form.get("new-player"): # New player sold
configs, partecipants, events = get_data_from_file(file_name)
new_player_name = request.form.get("new-player").upper() # Get the player name
new_player_cost = int(request.form.get("cost")) # Get the player cost
partecipant = request.form.get("partecipant") # Get the partecipant
for p in partecipants: # Find the partecipant
if p["name"] == partecipant:
p["credits"] -= new_player_cost # Remove the credits
roles = ["gk", "def", "mid", "att"] # Roles list
for r in roles: # Choose the role
if len(p[r]) < configs[r]:
role = r
break
p[role].append({new_player_name: new_player_cost}) # Add the player to the partecipant's list
events.append({"player": new_player_name, "cost": new_player_cost, "partecipant": p["name"]}) # Add the event
print(f"{new_player_name} sold for {new_player_cost} to {p['name']}")
save_new_data(file_name, configs, partecipants, events)
break
return render_template("auction.html", partecipants=partecipants, events=events)
@app.route("/edit-event", methods=['POST'])
def edit_event():
file_name = session.get('file_name')
configs, partecipants, events = get_data_from_file(file_name)
new_player_name = request.form.get("new-player").upper() # Get the new player name
new_player_cost = int(request.form.get("new-cost")) # Get the new player cost
new_partecipant = request.form.get("new-partecipant") # Get the new partecipant
old_player_name = request.form.get("old-player").upper() # Get the old player name
old_player_cost = int(request.form.get("old-cost")) # Get the old player cost
old_partecipant = request.form.get("old-partecipant") # Get the old partecipant
# Remove the player to the old player
for p in partecipants: # Find the partecipant
if p["name"] == old_partecipant:
p["credits"] += old_player_cost # Add the credits
roles = ["gk", "def", "mid", "att"] # Roles list
for r in roles:
for player in p[r]:
if old_player_name in player:
p[r].remove(player)
break
for e in events: # Edit the event
if old_player_name in e.get("player"):
e["player"] = new_player_name
e["cost"] = new_player_cost
e["partecipant"] = new_partecipant
break
break
# Add the player to the new partecipant
for p in partecipants: # Find the partecipant
if p["name"] == new_partecipant:
p["credits"] -= new_player_cost # Remove the credits
roles = ["gk", "def", "mid", "att"] # Roles list
for r in roles: # Choose the role
if len(p[r]) < configs[r]:
role = r
break
p[role].append({new_player_name: new_player_cost}) # Add the player to the partecipant's list
break
save_new_data(file_name, configs, partecipants, events)
return render_template("auction.html", partecipants=partecipants, events=events)
@app.route("/edit-partecipant", methods=['POST'])
def edit_partecipant():
file_name = session.get('file_name')
configs, partecipants, events = get_data_from_file(file_name)
new_partecipant = request.form.get("new-partecipant") # Get the new partecipant
old_partecipant = request.form.get("old-partecipant") # Get the old partecipant
for p in partecipants: # Rename the old partecipant
if p["name"] == old_partecipant:
p["name"] = new_partecipant
break
for e in events: # Rename the partecipant in each event
if e["partecipant"] == old_partecipant:
e["partecipant"] = new_partecipant
save_new_data(file_name, configs, partecipants, events)
return render_template("auction.html", partecipants=partecipants, events=events)
@app.route("/delete-event", methods=['POST'])
def delete_event():
file_name = session.get('file_name')
configs, partecipants, events = get_data_from_file(file_name)
player = request.form.get("event") # Get the player name
for e in events: # Edit the event
if player in e.get("player"):
events.remove(e)
partecipant = e["partecipant"]
cost = e["cost"]
break
# Remove the player to the old player
for part in partecipants: # Find the partecipant
if part["name"] == partecipant:
part["credits"] += cost # Add the credits
roles = ["gk", "def", "mid", "att"] # Roles list
for r in roles:
for p in part[r]:
if player in p:
part[r].remove(p)
break
break
save_new_data(file_name, configs, partecipants, events)
return render_template("auction.html", partecipants=partecipants, events=events)
def get_data_from_file(file_name):
with open(file_name, 'r') as file: # Read configs and partecipants
data = json.load(file) # Retrieve data
configs = data["configs"]
partecipants = data["partecipants"]
events = data["events"]
return configs, partecipants, events
def save_new_data(file_name, configs, partecipants, events):
with open(file_name, 'w') as file: # Save configs and partecipants in the file
data = {"configs": configs, "partecipants": partecipants, "events": events}
json.dump(data, file)
if __name__ == "__main__": # Run the app
app.run()