-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameServer.py
More file actions
181 lines (109 loc) · 3.3 KB
/
gameServer.py
File metadata and controls
181 lines (109 loc) · 3.3 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
# Battleship server
import socket
import selectors
import types
import argparse
from time import sleep
import models
MINIMUM_PLAYERS=2
sel = selectors.DefaultSelector()
gameManager = models.GameManager()
def main():
host, port = check_arguments()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.setblocking(False)
server.listen()
print("Listening on {0}".format((host, port)))
sel.register(server, selectors.EVENT_READ, data=None)
try:
# Game loop
while True:
events = sel.select(timeout=None)
for key, mask in events:
if key.data is None:
accept_connection(key.fileobj)
elif mask & selectors.EVENT_READ:
process_read_request(gameManager, key)
except KeyboardInterrupt:
print("caught keyboard interrupt, exiting")
finally:
sel.close()
server.close()
def check_arguments():
parser = argparse.ArgumentParser(description="<host> <port>")
parser.add_argument(help="<host>", nargs=1, type=str, dest='host')
parser.add_argument(help="<port>",nargs=1, type=int, dest='port')
args = parser.parse_args()
return (args.host[0], args.port[0])
def process_read_request(manager, conn):
# parse conn
sock = conn.fileobj
player = conn.data
# read message type
msgType = read_socket(sock, 4)
# Process incoming client message
# Change player's name
if msgType == "NAME":
# Change player's name
name = read_socket(sock, 20).lstrip(" ")
player.set_name(name)
# Add player to a game
elif msgType == "JOIN":
desiredPlayers = read_socket(sock, 1)
manager.join_open_game(int(desiredPlayers), player)
# Set a player's boat
elif msgType == "SETB":
# parse incoming message
gameId = int(read_socket(sock, 3))
boatId = int(read_socket(sock, 1))
# get game and boatLength
game = manager.get_game(gameId)
boatLength = game.get_boat_length(boatId)
# Game could not be found --> there was an error
if game == None:
pass
# Package coordinates
coords = [0] * (boatLength * 2)
for loc in range(len(coords)):
coords[loc] = int(read_socket(sock, game.coordLen))
# set boat position
player.place_boat(boatId, coords)
# Process player's shot
elif msgType == "SHOT":
# parse incoming message
gameId = int(read_socket(sock, 3))
target = read_socket(sock, 5)
game = manager.get_game(gameId)
# Game could not be found --> there was an error
if game == None:
pass
loc = [0] * 2
for i in range(len(loc)):
loc[i] = int(read_socket(sock, game.coordLen))
game.process_shot(player.get_port(), int(target), loc)
# game.advance_turn()
# Remove player from a game
elif msgType == "QUIT":
# parse incoming message
gameId = read_socket(sock, 3)
game = manager.get_game(gameId)
# Game could not be found --> there was an error
if game==None:
pass
game.player_quit(player.get_port())
player.quit_game()
def read_socket(connection, bytes):
return connection.recv(bytes).decode()
def write_socket(connection, msg):
connection.sendall(msg.encode())
def accept_connection(sock):
conn, addr = sock.accept()
host, port = addr
conn.setblocking(False)
print("accepting connection from {0}".format(addr))
data = models.Player(conn, port)
events = selectors.EVENT_READ | selectors.EVENT_WRITE
sel.register(conn, events, data=data)
if __name__ == '__main__':
main()