/
quickpong_test_client.py
executable file
·143 lines (110 loc) · 4.91 KB
/
quickpong_test_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
# Developed by Roman Arkharov, arkharov@gmail.com. 04.2013
"""
This file is a test client for Quickpong server. Main client is written on ActionScript 3.0 for
humans gamers.
The goal of this client is a performance test of Quickpong server. This client emulates actions of
main AS client.
Don't forget to set correct value for max_clients variable.
If set 1 and no other users online, then you can play game against the simple AI :))
"""
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.internet.task import LoopingCall
import simplejson as json
host = 'kece.ru'
port = 10080
max_clients = 500
#max_clients = 1
class QuickpongTestClient():
def __init__(self):
print 'QuickpongTestClient init'
def init_client(self, client, addr):
# client is an instance of class QuickpongTestProtocol()
client.port = addr.port
client.host = addr.host
client.type = addr.type
client.addr = addr
client.data = []
client.status = 'waiting'
client.position = '' # left or right board
client.frame = 0
self.client = client
print 'added client %s' % (addr)
def work_with_server(self):
ball_y_id = 2
if self.client.position == 'left':
board_y_id = 7
else:
board_y_id = 8
if self.data[board_y_id] + 50 > self.data[ball_y_id]:
delta_y = -20
else:
delta_y = 20
data_for_send = [self.client.position, delta_y, self.client.frame]
#print data_for_send
self.client.writeData(data_for_send)
self.client.frame += 1
def on_data_received(self, raw_data):
local_data = raw_data.replace('\x00', '')
if 'Your game is stopped' in local_data:
local_data = 'Your game is stopped'
elif '][' in local_data:
# Take last item if received several items in one data packet
local_data = local_data.replace('][', ']+++[')
local_data = local_data.split('+++')
local_data = local_data[len(local_data) - 1]
if self.client.status == 'waiting' and local_data != 'prestart':
self.client.client_id = local_data
print 'client_id %s' % (local_data,)
elif self.client.status == 'waiting' and local_data == 'prestart':
self.client.status = 'ready'
self.client.writeData(['ready'])
elif self.client.status == 'ready':
self.client.status = 'working'
self.data = json.loads(local_data)
if self.data[0] == 'start-1':
self.client.position = 'left'
else:
self.client.position = 'right'
self.lc = LoopingCall(self.work_with_server)
self.lc.start(0.2)
elif self.client.status == 'working' and local_data != 'Your game is stopped':
#print local_data
self.data = json.loads(local_data)
elif self.client.status == 'working' and local_data == 'Your game is stopped':
print 'client %s disabled' % (self.client.client_id)
# recreate tester
del self
reactor.connectTCP(host, port, QuickpongTestClientFactory())
else:
# here client receives from server data array with ball and boards coordinates
pass
#print 'unhandled data %s, current status %s' % (data, self.client.status)
class QuickpongTestProtocol(Protocol):
"""
Object of this class is an item of dictionary clients in QuickpongTestClient()
"""
def dataReceived(self, data):
self.test_client.on_data_received(data)
def writeData(self, data):
self.transport.write(json.dumps(data))
class QuickpongTestClientFactory(ClientFactory):
protocol = QuickpongTestProtocol
def buildProtocol(self, addr):
print 'Test client %s connected to server.' % (addr,)
p = self.protocol()
p.test_client = QuickpongTestClient()
p.test_client.init_client(p, addr)
return p
def clientConnectionLost(self, connector, reason):
print 'Lost connection. Reason:', reason
def clientConnectionFailed(self, connector, reason):
print 'Connection failed. Reason:', reason
# Run QuickpongTestClientFactory() N times. This factory creates N instances of class QuickpongTestProtocol()
# with name "protocol" and attaches instance of QuickpongTestClient() to protocol object
#
# Every instance of QuickpongTestClient() emulates user actions: receives data from server, process it and send
# responses to server. Data sends to server evey 0,2 seconds through the LoopingCall.
for i in range(0, max_clients):
reactor.connectTCP(host, port, QuickpongTestClientFactory())
reactor.run()