-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.py
339 lines (289 loc) · 10.2 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
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
print (" [+] Loading basics...")
import os
import json
import urllib
import requests
import io
if os.name == 'nt':
os.system("color")
os.system("title Social Wars Server")
else:
import sys
sys.stdout.write("\x1b]2;Social Wars Server\x07")
print (" [+] Loading game config...")
from get_game_config import get_game_config
print (" [+] Loading players...")
from get_player_info import get_player_info, get_neighbor_info
from sessions import load_saves, load_static_villages, load_quests, all_saves_userid, all_saves_info, save_info, new_village, fb_friends_str
load_saves()
print (" [+] Loading static villages...")
load_static_villages()
print (" [+] Loading quests...")
load_quests()
# print (" [+] Loading auction house data...")
# from auctions import AuctionHouse
# auction_house = AuctionHouse()
print (" [+] Loading server...")
from flask import Flask, render_template, send_from_directory, request, redirect, session, send_file
from flask.debughelpers import attach_enctype_error_multidict
from command import command
from engine import timestamp_now
from version import version_name
from bundle import ASSETS_DIR, STUB_DIR, TEMPLATES_DIR, BASE_DIR
from constants import Quests
host = '127.0.0.1'
port = 5055
app = Flask(__name__, template_folder=TEMPLATES_DIR)
print (" [+] Configuring server routes...")
##########
# ROUTES #
##########
__STATIC_ROOT = "/static/socialwars"
__DYNAMIC_ROOT = "/dynamic/menvswomen/srvsexwars"
## PAGES AND RESOURCES
@app.route("/", methods=['GET', 'POST'])
def login():
# Log out previous session
session.pop('USERID', default=None)
session.pop('GAMEVERSION', default=None)
# Reload saves (not static villages nor quests). Allows saves modification without server reset
load_saves()
# If logging in, set session USERID, and go to play
if request.method == 'POST':
session['USERID'] = request.form['USERID']
session['GAMEVERSION'] = request.form['GAMEVERSION']
print("[LOGIN] USERID:", request.form['USERID'])
print("[LOGIN] GAMEVERSION:", request.form['GAMEVERSION'])
return redirect("/play.html")
# Login page
if request.method == 'GET':
saves_info = all_saves_info()
return render_template("login.html", saves_info=saves_info, version=version_name)
@app.route("/play.html")
def play():
if 'USERID' not in session:
return redirect("/")
if 'GAMEVERSION' not in session:
return redirect("/")
if session['USERID'] not in all_saves_userid():
return redirect("/")
USERID = session['USERID']
GAMEVERSION = session['GAMEVERSION']
print("[PLAY] USERID:", USERID)
print("[PLAY] GAMEVERSION:", GAMEVERSION)
return render_template("play.html", save_info=save_info(USERID), serverTime=timestamp_now(), friendsInfo=fb_friends_str(USERID), version=version_name, GAMEVERSION=GAMEVERSION, SERVERIP=host, SERVERPORT=port)
@app.route("/new.html")
def new():
session['USERID'] = new_village()
session['GAMEVERSION'] = "Basesec_1.5.4.swf"
return redirect("play.html")
@app.route("/crossdomain.xml")
def crossdomain():
return send_from_directory(STUB_DIR, "crossdomain.xml")
@app.route("/img/<path:path>")
def images(path):
return send_from_directory(TEMPLATES_DIR + "/img", path)
@app.route("/avatars/<path:path>")
def avatars(path):
return send_from_directory(TEMPLATES_DIR + "/avatars", path)
@app.route("/css/<path:path>")
def css(path):
return send_from_directory(TEMPLATES_DIR + "/css", path)
## GAME STATIC
@app.route(__STATIC_ROOT + "/<path:path>")
def static_assets_loader(path):
# LITE-WEIGHT BUILD: ASSETS FROM GITHUB
if False:
cdn = "https://raw.githubusercontent.com/AcidCaos/socialwarriors/main/assets/"
try:
r = requests.get(cdn + path) # TODO timeout, retry
except requests.exceptions:
return ""
m = io.BytesIO(r.content)
m.seek(0)
return send_file(m, download_name=path.split("/")[-1:][0])
# OFFLINE BUILD
return send_from_directory(ASSETS_DIR, path)
## GAME DYNAMIC
@app.route(__DYNAMIC_ROOT + "/track_game_status.php", methods=['POST'])
def track_game_status_response():
status = request.values['status']
installId = request.values['installId']
user_id = request.values['user_id']
# print(f"track_game_status: status={status}, installId={installId}, user_id={user_id}. --", request.values)
print(f"[STATUS] USERID {user_id}: {status}")
return ("", 200)
@app.route(__DYNAMIC_ROOT + "/get_game_config.php")
def get_game_config_response():
USERID = request.values['USERID']
user_key = request.values['user_key']
language = request.values['language']
# print(f"get_game_config: USERID: {USERID}. --", request.values)
print(f"[CONFIG] USERID {USERID}.")
return get_game_config()
@app.route(__DYNAMIC_ROOT + "/get_player_info.php", methods=['POST'])
def get_player_info_response():
USERID = request.values['USERID']
user_key = request.values['user_key']
language = request.values['language']
user = request.values['user'] if 'user' in request.values else None
client_id = int(request.values['client_id']) if 'client_id' in request.values else None
map = int(request.values['map']) if 'map' in request.values else None
# print(f"get_player_info: USERID: {USERID}. --", request.values)
# Current Player
if user is None:
print(f"[PLAYER INFO] USERID {USERID}.")
return (get_player_info(USERID), 200)
# General Mike
elif user in ["100000030","100000031"]:
print(f"[VISIT] USERID {USERID} visiting General Mike ({user}).")
return (get_neighbor_info("100000030", map), 200)
# Quest Maps
elif user.startswith("100000"):
print(f"[QUEST] USERID {USERID} loading", Quests.QUEST[user] if user in Quests.QUEST else "?", f"({user}).")
return (get_neighbor_info(user, map), 200)
# Static Neighbours
else:
print(f"[VISIT] USERID {USERID} visiting user: {user}.")
return (get_neighbor_info(user, map), 200)
## AUCTION HOUSE
# @app.route(__DYNAMIC_ROOT + "/bets/get_bets_list.php", methods=['POST'])
# def get_bets_list():
# USERID = request.values['USERID']
# user_key = request.values['user_key']
# language = request.values['language']
# data = request.values['data']
#
# if not data.startswith("{"):
# data = data[65:]
#
# data = json.loads(data)
# user_id = data["user_id"]
# level = data["level"]
#
# bets = auction_house.get_auctions(user_id, level)
# for bet in bets:
# bet["isPrivate"] = 0
# bet["isWinning"] = 0
# bet["won"] = 0
# bet["finished"] = 0
#
# r = {}
# r["result"] = "success"
# r["data"] = {"bets": bets}
#
# response = json.dumps(r)
# # print("RESPONSE:")
# # print(response)
#
# return (response, 200)
#
# @app.route(__DYNAMIC_ROOT + "/bets/get_bet_detail.php", methods=['POST'])
# def get_bet_detail():
# USERID = request.values['USERID']
# user_key = request.values['user_key']
# language = request.values['language']
# data = request.values['data']
#
# if not data.startswith("{"):
# data = data[65:]
#
# data = json.loads(data)
# uuid = data["uuid"]
# checkFinish = 0
# if "checkFinish" in data:
# checkFinish = data["checkFinish"]
#
# bet = auction_house.get_auction_detail(USERID, uuid, checkFinish)
#
# print(f"Get bet details for BET UUID {uuid}")
# print(json.dumps(data, indent="\t"))
#
# r = {}
# r["result"] = "success"
# r["data"] = bet
#
# response = json.dumps(r, indent="\t")
# print("RESPONSE:")
# print(response)
#
# return (response, 200)
#
# @app.route(__DYNAMIC_ROOT + "/bets/set_bet.php", methods=['POST'])
# def set_bet():
# USERID = request.values['USERID']
# user_key = request.values['user_key']
# language = request.values['language']
# data = request.values['data']
#
# if not data.startswith("{"):
# data = data[65:]
#
# data = json.loads(data)
# uuid = data["uuid"]
# bet_amount = data["bet"]
# bet_round = data["round"]
#
# print(json.dumps(data, indent="\t"))
#
# auction_house.set_bet(USERID, uuid, bet_amount, bet_round)
#
# r = {}
# r["result"] = "success"
# r["data"] = {
# "betResult": "OK"
# }
#
# response = json.dumps(r)
# # print("RESPONSE:")
# # print(response)
#
# return (response, 200)
@app.route(__DYNAMIC_ROOT + "/sync_error_track.php", methods=['POST'])
def sync_error_track_response():
USERID = request.values['USERID']
user_key = request.values['user_key']
language = request.values['language']
# print(f"sync_error_track: USERID: {USERID}. --", request.values)
return ("", 200)
@app.route("/null")
def flash_sync_error_response():
sp_ref_cat = request.values['sp_ref_cat']
if sp_ref_cat == "flash_sync_error":
reason = "reload On Sync Error"
elif sp_ref_cat == "flash_reload_quest":
reason = "reload On End Quest"
elif sp_ref_cat == "flash_reload_attack":
reason = "reload On End Attack"
print("flash_sync_error", reason, ". --", request.values)
return redirect("/play.html")
@app.route(__DYNAMIC_ROOT + "/command.php", methods=['POST'])
def command_response():
USERID = request.values['USERID']
user_key = request.values['user_key']
language = request.values['language']
# print(f"command: USERID: {USERID}. --", request.values)
data_str = request.values['data']
data_hash = data_str[:64]
assert data_str[64] == ';'
data_payload = data_str[65:]
data = json.loads(data_payload)
command(USERID, data)
return ({"result": "success"}, 200)
# Used by Player's World and Alliance buttons
# I added this so the error message stops appearing
@app.route(__DYNAMIC_ROOT + "/alliance/", methods=['POST'])
def alliance():
USERID = request.values['USERID']
user_key = request.values['user_key']
language = request.values['language']
method = request.values['method']
response = {}
return(response, 200)
########
# MAIN #
########
print (" [+] Running server...")
if __name__ == '__main__':
app.secret_key = 'SECRET_KEY'
app.run(host=host, port=port, debug=False)