-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
203 lines (162 loc) · 5.82 KB
/
client.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
#!/usr/bin/env python
import time
import sys
import socket
import random
import json
import urllib
class Timer(object):
def __init__(self):
self.min_ping = 0
self.max_ping = 999999
self.attempted_count = 0
self.count = 0
self.avg_time = 0
def attempt(self):
self.attempted_count += 1
def log(self, elapsed):
if elapsed < self.min_ping:
self.min_ping = elapsed
if elapsed > self.max_ping:
self.max_ping = elapsed
self.count += 1
self.avg_time += elapsed
def update_server(self, client_id, print_out=False):
stop = False
if print_out:
print('--- client response time statistics ---')
print('rtt min/avg/max/n = {0:.3f}/{1:.3f}/{2:.3f}/{3} ms'.format(
self.min_ping, self.avg_time / self.count, self.max_ping, self.count
))
if self.client_id is not None:
data = {
'client_id': self.client_id,
'ping_min': self.min_ping,
'ping_avg': self.avg_time / self.count,
'ping_max': self.max_ping,
'ping_count': self.count,
'ping_misses': self.attemped_count - self.count,
}
req = urllib.request('https://www.rileyevans.co.uk/gp/add-result/')
res = urllib.urlopen(req, json.dumps(data))
res_data = json.loads(res.read())
if not res_data['error']:
stop = res_data['stop']
return stop
def time_it(f):
def wrapped_f(timer, *args):
start = time.time()
timer.attempt()
x = f(*args)
end = time.time()
elapsed = (end - start) * 1000
timer.log(elapsed)
return x
return wrapped_f
class Game(object):
def __init__(self, server_ip, interval):
self.server_ip = server_ip
self.port = 25565
self.interval = interval
self.player_id = None
self.me = {'x': 0, 'y': 0}
self.other_players = {}
self.timer = Timer()
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.clientSocket.settimeout(2)
self.timeout_count = 0
def start(self):
self.handshake()
self.stop = False
timer_updates = 20.0 / self.interval
count = 0
while not self.stop:
try:
self.move()
self.send_location()
time.sleep(self.interval)
except KeyboardInterrupt:
break
finally:
if count > timer_updates:
self.stop = self.timer.update_server(self.player_id)
self.timer.update_server(self.player_id)
def handshake(self):
agreed = False
player_id = None
while not agreed:
try:
player_id = random.randint(1, 255)
message = bytearray([1, player_id])
self.clientSocket.sendto(message, (self.server_ip, self.port))
data, server = receive(self.timer, self.clientSocket)
if data[0] == 2 and data[1] == player_id:
agreed = True
else:
if data[0] != 2:
print('unexpected handshake ? {}'.format(data))
else:
print('client id already taken, trying again')
except socket.timeout:
pass
self.player_id = player_id
def move(self):
self.me['x'] += random.randint(0, 2)
if self.me['x'] > 255:
self.me['x'] -= 255
self.me['y'] += random.randint(0, 2)
if self.me['y'] > 255:
self.me['y'] -= 255
def send_location(self):
message = bytearray([3, self.player_id, self.me['x'], self.me['y']])
try:
self.clientSocket.sendto(message, (self.server_ip, self.port))
data, server = receive(self.timer, self.clientSocket)
if data[0] == 4:
self.update_others(data[1:])
else:
print('unexpected packet ? {}'.format(data))
self.timeout_count = 0
except socket.timeout:
self.timeout_count += 1
if self.timeout_count > 10:
self.stop = True
print('client give up: 10 timeouts in a row')
def update_others(self, data):
n = data[0]
other_players = data[1:]
for i in range(n):
player_data = other_players[i * 3: i * 3 + 3]
if player_data[0] in self.other_players:
self.other_players[player_data[0]]['x'] = player_data[1]
self.other_players[player_data[0]]['y'] = player_data[2]
else:
self.other_players[player_data[0]] = {
'x': player_data[1],
'y': player_data[2],
}
@time_it
def receive(sock):
return sock.recvfrom(2048)
interval = 0.05 # 50 milliseconds
if __name__ == '__main__':
host = sys.argv[1] # set to server ip or hostname
port = 25565
game = Game(host, interval)
game.start()
# clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# clientSocket.settimeout(timeout)
# # message = bytearray([1] * message_bytes)
# timer = Timer()
# for seq in range(number_of_pings):
# try:
# message = bytearray('hey-{}'.format(seq), encoding='utf-8')
# clientSocket.sendto(message, (host, port))
# data, server = receive(timer, clientSocket)
# # data, server = clientSocket.recvfrom(2048)
# except socket.timeout:
# print('udp_seq=%d REQUEST TIMED OUT' % (seq))
# except KeyboardInterrupt:
# timer.summary()
# sys.exit()
# timer.summary()