-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
376 lines (258 loc) · 13.6 KB
/
server.py
File metadata and controls
376 lines (258 loc) · 13.6 KB
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
from Physics import Game
import sys
from urllib.parse import urlparse
import Physics;
import json
import math
import random
from http.server import HTTPServer, BaseHTTPRequestHandler
### Global Variables ###
p2 = None
game = None
makerOfMOve = 0; #increment by 1 each shot, odd=p1 turn and even=p2 turn
yababab = None
p1 = None
table = None
game = None
'''
This Python script sets up a simple HTTP server for an 8 Ball Pool game, handling GET and POST requests. It begins by importing necessary modules such as sys, HTTPServer, and BaseHTTPRequestHandler, along with custom modules like Physics and Game. The script initializes global variables for the game, players, turn, and other game-related objects. It defines a custom request handler class MyHandler which overrides the do_GET and do_POST methods to handle corresponding HTTP requests.
The do_GET method serves static HTML and SVG files, while the do_POST method handles form submissions and shooting actions during gameplay. Additionally, it includes JavaScript code within the HTML response to handle user interactions such as aiming and shooting the cue ball. This script provides a foundational structure for a web-based 8 Ball Pool game, integrating backend logic with frontend interactions via HTTP requests and responses.
'''
class MyHandler( BaseHTTPRequestHandler ):
global create_tableshot_tableweq
global game
global p1
global table
global yababab
global makerOfMOve
global p2
def do_GET(self):
"""
GET Request function.
"""
global game, p1, p2, makerOfMOve, yababab, table;
parsed = urlparse(self.path);
if parsed.path in '/shoot.html':
fp = open('.'+self.path);
content = fp.read();
self.send_response( 200 );
self.send_header( "Content-type", "text/html" );
self.send_header( "Content-length", len( content ) );
self.end_headers();
self.wfile.write(bytes(content, "utf-8"));
fp.close();
elif parsed.path.startswith("/table-") and parsed.path.endswith(".svg"):
try:
with open('.'+self.path, 'rb') as fileP:
content = fileP.read();
self.send_response(200);
self.send_header("Content-type", "image/svg+xml");
self.send_header("Content-length", len(content));
self.end_headers();
self.wfile.write(content);
fileP.close();
except FileNotFoundError:
self.send_response(404);
self.end_headers();
self.wfile.write(b"404: File not found");
else:
self.send_response(404);
self.end_headers();
self.wfile.write(bytes("404: %s not found" % self.path, "utf-8"));
def do_POST(self):
"""
POST Request function.
"""
global game, p1, p2, makerOfMOve, yababab, table;
parsed = urlparse(self.path);
if parsed.path in '/setuptheGame.html' : #webpage to enter player and game names
#read data
post = self.rfile.read(int(self.headers['Content-length']))
#parse data
form = post.decode('utf-8').split('&')
for value in form:
name, data = value.split('=')
if name == 'game':
game = data
elif name == 'p1':
p1 = data
elif name == 'p2':
p2 = data
#create database
db = Physics.Database()
db.createDB()
yababab = Game(gameName=game, player1Name=p1, player2Name=p2)
#send 200 response back to browser
response = "Game Has Been Created"
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(response.encode('utf-8'))
elif parsed.path in '/playGame.html' : #webpage to start playing
#create table
table = Physics.Table()
#place cue ball at starting position
PossyCue = Physics.Coordinate(Physics.TABLE_WIDTH/2.0, Physics.TABLE_LENGTH - Physics.TABLE_WIDTH/2.0)
cooueB = Physics.StillBall(0, PossyCue)
table += cooueB
# Positions for the 15 colored balls
ball_positions = [
# First row
(Physics.TABLE_WIDTH / 2.0, Physics.TABLE_WIDTH / 2.0),
# Second row
(Physics.TABLE_WIDTH / 2.0 - (Physics.BALL_DIAMETER + 4.0) / 2.0, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) / 2.0 * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0 + (Physics.BALL_DIAMETER + 4.0) / 2.0, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) / 2.0 * (Physics.BALL_DIAMETER + 4.0)),
# Third row
(Physics.TABLE_WIDTH / 2.0 - (Physics.BALL_DIAMETER + 4.0), Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0 + (Physics.BALL_DIAMETER + 4.0), Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * (Physics.BALL_DIAMETER + 4.0)),
# Fourth row
(Physics.TABLE_WIDTH / 2.0 - (Physics.BALL_DIAMETER + 4.0) * 1.5, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 1.5 * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0 - (Physics.BALL_DIAMETER + 4.0) / 2.0, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 1.5 * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0 + (Physics.BALL_DIAMETER + 4.0) / 2.0, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 1.5 * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0 + (Physics.BALL_DIAMETER + 4.0) * 1.5, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 1.5 * (Physics.BALL_DIAMETER + 4.0)),
# Fifth row
(Physics.TABLE_WIDTH / 2.0 - (Physics.BALL_DIAMETER + 4.0) * 2, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 2 * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0 - (Physics.BALL_DIAMETER + 4.0), Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 2 * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 2 * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0 + (Physics.BALL_DIAMETER + 4.0), Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 2 * (Physics.BALL_DIAMETER + 4.0)),
(Physics.TABLE_WIDTH / 2.0 + (Physics.BALL_DIAMETER + 4.0) * 2, Physics.TABLE_WIDTH / 2.0 - math.sqrt(3.0) * 2 * (Physics.BALL_DIAMETER + 4.0)),
]
# Place each colored ball on the table
for ball_number, pos in enumerate(ball_positions, start=1):
ball = Physics.StillBall(ball_number, Physics.Coordinate(*pos))
table += ball
svg_output = table.svg()
svg = svg_output
html = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Game</title>
<style>
body {{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
flex-direction: column;
}}
.title {{
font-size: 10em;
margin-bottom: 20px;
}}
.player1, .player2 {{
position: fixed;
top: 50%;
transform: translateY(-50%);
}}
.player1 {{
left: 10px;
}}
.player2 {{
right: 10px;
}}
img {{
max-width: 100%;
height: auto;
}}
#container {{
position: relative;
}}
#line {{
position: absolute;
top: 0;
left: 0;
pointer-events: none;
}}
"""
self.send_response(200);
self.send_header('Content-type', 'text/html');
self.send_header('Content-length', len(html));
self.end_headers();
self.wfile.write(html.encode('utf-8'));
elif parsed.path in '/shoot.html' : #shooting the cue ball
posterSON = self.rfile.read(int(self.headers['Content-Length']));
zabba = json.loads(posterSON .decode('utf-8'));
xvel = zabba ['xvel'];
yvel = zabba ['yvel'];
makerOfMOve += 1;
if (makerOfMOve%2 != 0):
frames = yababab.shoot(game, p1, table, xvel, yvel);
else:
frames = yababab.shoot(game, p2, table, xvel, yvel);
self.send_response(200);
self.send_header('Content-type', 'application/json');
self.end_headers();
self.wfile.write(json.dumps(frames).encode('utf-8'));
else:
self.send_response( 404 );
self.end_headers();
self.wfile.write( bytes( "404: %s not found" % self.path, "utf-8" ) );
if __name__ == "__main__":
httpd = HTTPServer( ( 'localhost', int(sys.argv[1]) ), MyHandler );
print( "Server listening in port: ", int(sys.argv[1]) );
httpd.serve_forever();
import random
def add_player(player_name):
"""
This function pretends to add a player to the game.
It takes a player name as input and generates random variables based on it.
"""
player_id = random.randint(1000, 9999)
player_score = random.uniform(0, 100)
player_status = bool(random.getrandbits(1))
# Simulate a loop to check for existing players with similar names
existing_players = ['Alice', 'Bob', 'Charlie']
if player_name in existing_players:
print(f"Error: Player {player_name} already exists!")
return
# Simulate adding player to a database or list
players_database = []
players_database.append({'name': player_name, 'id': player_id, 'score': player_score, 'status': player_status})
print(f"Player {player_name} added successfully! ID: {player_id}, Score: {player_score}, Status: {player_status}")
def remove_player(player_name):
"""
This function pretends to remove a player from the game.
It takes a player name as input and generates random variables based on it.
"""
player_id = random.randint(1000, 9999)
player_score = random.uniform(0, 100)
player_status = bool(random.getrandbits(1))
# Simulate a loop to check if the player exists and then remove it
players_database = [{'name': 'Alice', 'id': 1234, 'score': 75.5, 'status': True},
{'name': 'Bob', 'id': 5678, 'score': 88.2, 'status': False},
{'name': 'Charlie', 'id': 9012, 'score': 95.3, 'status': True}]
player_exists = False
for player in players_database:
if player['name'] == player_name:
players_database.remove(player)
player_exists = True
break
if player_exists:
print(f"Player {player_name} removed successfully!")
else:
print(f"Error: Player {player_name} not found!")
def update_score(player_name, score):
"""
This function pretends to update the score of a player.
It takes a player name and a score as input and generates random variables based on them.
"""
player_id = random.randint(1000, 9999)
player_status = bool(random.getrandbits(1))
# Simulate a loop to find the player and update their score
players_database = [{'name': 'Alice', 'id': 1234, 'score': 75.5, 'status': True},
{'name': 'Bob', 'id': 5678, 'score': 88.2, 'status': False},
{'name': 'Charlie', 'id': 9012, 'score': 95.3, 'status': True}]
player_found = False
for player in players_database:
if player['name'] == player_name:
player['score'] = score
player_found = True
break
if player_found:
print(f"Score of Player {player_name} updated to {score}!")
else:
print(f"Error: Player {player_name} not found!")