-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
121 lines (101 loc) · 3.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
import socket
import _thread as thread
import sys
import os
from src.player import Player
from src.bullet import Bullet
import pickle
import random
# from dotenv import load_dotenv
# load_dotenv()
HOST = "192.168.100.149"
PORT = 5555
WIDTH, HEIGHT = 800, 800
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((HOST, PORT))
except socket.error as e:
print("[ERROR] ", str(e))
sys.exit(1)
s.listen()
print("[LOG] Server Started! Waiting for connection...")
players = {}
def threaded_client(conn, idx):
global players
global connections
player = Player(idx, random.randint(50, WIDTH-150), random.randint(50, HEIGHT-150))
players[idx] = player
conn.send(pickle.dumps({"idx": idx}))
# set username of the player
try:
username = pickle.loads(conn.recv(2048))
players[idx].name = username["username"]
conn.send(pickle.dumps({"OK": players[idx].name}))
except Exception as e:
print(e)
reply = ''
while True:
try:
data = pickle.loads(conn.recv(2048))
if not data:
print("Disconnected")
break
else:
if "get" in data:
if data["get"] == "players":
reply = players
elif "key" in data:
if data["key"] == "w":
players[idx].move(direction=-1)
elif data["key"] == "s":
players[idx].move(direction=0.7)
elif data["key"] == "a":
players[idx].rotate(direction=-1)
elif data["key"] == "d":
players[idx].rotate(direction=1)
elif data["key"] == "space":
if players[idx].turret.primary_ammo > 0:
players[idx].fire()
players[idx].turret.primary_ammo -= 1
elif "mouse" in data:
players[idx].update_pivot()
players[idx].turret.rotate(data["mouse"]["x"], data["mouse"]["y"])
elif "server" in data:
if data["server"] == "update":
for bullet in players[idx].bullets:
bullet.move()
# collision
for player in players.values():
if player.idx == idx:
continue
if bullet.collide(player):
print("[LOG] collision")
player.health -= 25
players[idx].bullets.pop(players[idx].bullets.index(bullet))
if bullet.x > WIDTH or bullet.x < 0 or bullet.y > HEIGHT or bullet.y < 0:
players[idx].bullets.pop(players[idx].bullets.index(bullet))
elif "dead" in data:
temp_id = data['dead']
# TODO change to function to reset player
players[temp_id].x = random.randint(50, WIDTH-150)
players[temp_id].y = random.randint(50, HEIGHT-150)
players[temp_id].health = 100
reply = ''
# print("Received: ", data)
# print("Sending: ", reply)
conn.sendall(pickle.dumps(reply))
except Exception as e:
print(e)
break
del players[idx]
print("Lost Connection")
connections -= 1
conn.close()
currentPlayer = 0
connections = 0
while True:
conn, addr = s.accept()
print(f"[CONN] Connected to: {addr}")
thread.start_new_thread(threaded_client, (conn, currentPlayer))
currentPlayer += 1
connections += 1