-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·200 lines (166 loc) · 5.32 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
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
#!/usr/bin/env python3.4
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys
import time
import threading
import socket
from clientstate import ClientState, MOUSE_UP, MOUSE_DOWN
from annimations import CountdownAnimation, DisplayWinners
from winner import WINNER_LEFT, WINNER_NOBODY, WINNER_RIGHT
from pingpong import PingPong
from clickthedisks import ClickTheDisks
from followthecircle import FollowTheCircle
SOCKET_SERVER_HOST = "localhost"
SOCKET_SERVER_PORT = 10000
class ClientDragData:
def __init__(self, game, client):
self.game = game
self.client = client
self.prevMouseState = MOUSE_UP
self.obj = None
def startDragging(self, x, y):
self.obj = self.game.getObjectAt(x, y)
if self.obj is not None:
self.obj.draggable = False
def endDragging(self):
if self.obj is not None:
self.obj.draggable = True
self.obj = None
def update(self):
x, y, s = self.client.state.getState()
if self.obj is None and self.prevMouseState == MOUSE_UP and s == MOUSE_DOWN:
self.startDragging(x, y)
elif s == MOUSE_UP:
self.endDragging()
self.prevMouseState = s
if self.obj is not None:
self.obj.moveTo(x, y)
class Client(threading.Thread):
def __init__(self, shape, socketServer, state):
super(Client, self).__init__()
print("Waiting for client...")
conn, addr = socketServer.accept()
print("Client connected: {}".format(addr))
self.conn = conn
self.state = state
self.shape = shape
self.clientDragData = None
def setClientDragData(self, clientDragData):
self.clientDragData = clientDragData
def run(self):
self.state.recvTeamName(self.conn)
while True:
self.state.recv(self.conn)
def _drawCircle(self):
CIRCLE_RADIUS = 10
gluDisk(gluNewQuadric(), 0, CIRCLE_RADIUS, 100, 1)
def _drawSquare(self):
HALF_SQUARE_SIZE = 10
glBegin(GL_POLYGON)
glVertex3f(-HALF_SQUARE_SIZE, -HALF_SQUARE_SIZE, 0)
glVertex3f(+HALF_SQUARE_SIZE, -HALF_SQUARE_SIZE, 0)
glVertex3f(+HALF_SQUARE_SIZE, +HALF_SQUARE_SIZE, 0)
glVertex3f(-HALF_SQUARE_SIZE, +HALF_SQUARE_SIZE, 0)
glEnd()
def draw(self):
x, y, s = self.state.getState()
if s == MOUSE_UP:
glColor3f(1, 1, 1)
else:
glColor3f(1, 0, 0)
glPushMatrix()
glTranslate(x, 480 - y, 0)
if self.shape == 'circle':
self._drawCircle()
else:
self._drawSquare()
glPopMatrix()
def update(self):
if self.clientDragData is not None:
self.clientDragData.update()
class GameServer:
def __init__(self):
self.game = None
self.annimation = None
def initServer(self):
self.socketServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socketServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socketServer.bind((SOCKET_SERVER_HOST, SOCKET_SERVER_PORT))
self.socketServer.listen(1)
def waitForClients(self):
self.client1 = Client('square', self.socketServer, ClientState(0, 0, MOUSE_UP))
self.client1.start()
self.client2 = None
def startGUI(self):
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(640, 480)
glutCreateWindow(b"Game Server")
glutReshapeFunc(self.reshape)
glutMainLoop()
def reshape(self, w, h):
glutDisplayFunc(lambda: self.display(w, h))
glutPostRedisplay();
def display(self, w, h):
time.sleep(0.04)
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, 640, 0, 480, -1, 1)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
if self.annimation is not None:
if self.annimation.done:
self.annimation = None
else:
self.annimation.draw()
else:
if self.game is not None:
self.game.update()
self.game.step()
self.game.draw()
glutSwapBuffers()
glutPostRedisplay();
def play(self, gameClass):
self.annimation = None
self.game = gameClass(self.client1, self.client2)
self.client1.setClientDragData(ClientDragData(self.game, self.client1))
def playAnnimation(self, annimationObj):
self.game = None
self.annimation = annimationObj
class GameSequencer(threading.Thread):
def __init__(self, gameServer):
super(GameSequencer, self).__init__()
self.gameServer = gameServer
def waitForAnnimationToComplete(self):
while self.gameServer.annimation is not None:
time.sleep(0.1)
def waitForGameOver(self):
while not self.gameServer.game.gameOver:
time.sleep(0.5)
def run(self):
sequence = [
PingPong, ClickTheDisks, FollowTheCircle,
PingPong, ClickTheDisks, FollowTheCircle,
PingPong, ClickTheDisks, FollowTheCircle,
]
for gameClass in sequence:
self.gameServer.playAnnimation(CountdownAnimation())
self.waitForAnnimationToComplete()
self.gameServer.play(gameClass)
self.waitForGameOver()
assert self.gameServer.game.winner is not None
self.gameServer.playAnnimation(DisplayWinners(self.gameServer.game.winner))
self.waitForAnnimationToComplete()
def main():
gameServer = GameServer()
gameServer.initServer()
gameServer.waitForClients()
gameSequencer = GameSequencer(gameServer)
gameSequencer.start()
gameServer.startGUI()
if __name__ == '__main__':
main()