-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
106 lines (77 loc) · 2.28 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
'''
Purpose: Server responsible for routing
Author: Md. Tanvir Islam
Command to execute: python app.py
'''
from flask import Flask
from flask import render_template
from flask import json
from flask import request
import random
import sys
app = Flask(__name__)
print("Server is live...", file = sys.stderr)
users = []
@app.route("/")
def index():
return render_template("index.html"), 200
@app.route("/generate", methods = ["POST"])
def generate():
this_user = {} # init user
send_data = {} # data to be sent
post_obj = request.json
rc = dimension(post_obj)
send_data["x"] = rc["x"]
send_data["y"] = rc["y"]
send_data["speed"] = 20
this_user["name"] = post_obj["name"] # sets the user's name
this_user["speed"] = send_data["speed"] # sets the user's speed
this_user["size"] = 0
users.append(this_user) # append it to the list of users
return json.dumps(send_data), 200
# sends the x and y coordinates to the client
@app.route("/regenerate", methods = ["POST"])
def regenerate():
send_data = {}
post_obj = request.json
rc = dimension(post_obj)
send_data["x"] = rc["x"]
send_data["y"] = rc["y"]
return json.dumps(send_data), 200
# sends the size of the snake to the server
@app.route("/size", methods = ["POST"])
def size():
temp = {}
obj_obj = request.json
for i in range(len(users)):
if obj_obj["name"] == users[i]["name"]:
temp = users[i]
users[users.index(temp)]["size"] += 1
send_data = {}
send_data["size"] = users[users.index(temp)]["size"]
return json.dumps(send_data), 200
'''
Function: dimensions
Purpose: generates a random x and y coordinate within a limit to send it the client
in: obj
'''
def dimension(obj):
rc = {}
width_min = int(obj["width_min"])
width_max = int(obj["width_max"])
height_min = int(obj["height_min"])
height_max = int(obj["height_max"])
x = random_number(width_min, width_max)
y = random_number(height_min, height_max)
rc["x"] = x
rc["y"] = y
return rc
'''
Function: random_number
Purpose: generates a random number between a particular range
in: min, max
'''
def random_number(min, max):
return random.randint(min, max)
if __name__ == "__main__":
app.run(host = "localhost", port = 2406, debug = True)