-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
351 lines (339 loc) · 15.4 KB
/
client.py
File metadata and controls
351 lines (339 loc) · 15.4 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import sys
import socket
import threading
import random
import gameState
import gameModes
import gameLogic
import uiElements
import cDefines
import rendererUpdates
SERVER = -1
class Commands:
@staticmethod
def seedRNG(seed):
random.seed(seed)
@staticmethod
def setMap(mapName):
# gameState.setMapName(mapName)
if(hasattr(gameState.getGameMode(),"setMap")):
gameState.getGameMode().setMap(mapName)
else:
gameState.setMapName(mapName)
@staticmethod
def setOwnUserName(userName):
if(gameState.getOwnUserName() == None):#"Player X" from server, prefer real username, which would already be set
gameState.setOwnUserName(userName)
#
if(hasattr(gameState.getGameMode(),"redrawTeams")):
gameState.getGameMode().redrawTeams()
@staticmethod
def changeUserName(args):
tokens = args.split(":")
oldUserName = gameState.getPlayers()[int(tokens[0])].userName
gameState.changeUserName(int(tokens[0]),tokens[1])
if(hasattr(gameState.getGameMode(),"redrawTeams")):
gameState.getGameMode().redrawTeams()
# for index in range(0,8):
# if(oldUserName == "Player " + str(index+1)):
# server.playerUserNames[index] = False
@staticmethod
def changeUserNameUndo(args):
return
@staticmethod
def changeUserNameRedo(args):
return
@staticmethod
def setTeamSize(teamSize):
gameState.setTeamSize(int(teamSize))
if(hasattr(gameState.getGameMode(),"redrawTeams")):
gameState.getGameMode().redrawTeams()
@staticmethod
def setPlayerNumber(playerNumber):
gameState.setPlayerNumber(int(playerNumber))
# if(gameState.getOwnUserName() == None):
# gameState.setOwnUserName("Player " + playerNumber)
@staticmethod
def removePlayer(playerNumber):
gameState.removePlayer(int(playerNumber))
if(hasattr(gameState.getGameMode(),"redrawTeams")):
gameState.getGameMode().redrawTeams()
@staticmethod
def disconnectedPlayer(playerNumber):
if(hasattr(gameState.getGameMode(),"playerMissing")):
gameState.getGameMode().playerMissing = True
gameState.getGameMode().missingPlayers.append(int(playerNumber))
uiElements.playerDisconnectedModal()
@staticmethod
def addPlayer(args):
tokens = args.split(":")
playerNumber = int(tokens[0])
userName = tokens[1]
gameState.addPlayer(playerNumber=playerNumber,userName=userName,requestHandler=None)
for player in gameState.getPlayers():
if(player != None):
if(gameState.getPlayerNumber() == player.playerNumber):
player.isOwnPlayer = True
gameState.setPlayerNumber(player.playerNumber)
else:
player.isOwnPlayer = False
if(hasattr(gameState.getGameMode(),"redrawTeams")):
gameState.getGameMode().redrawTeams()
@staticmethod
def changePlayerNumber(args):
tokens = args.split(":")
oldNumber = int(tokens[0])
newNumber = int(tokens[1])
gameState.movePlayer(oldNumber,newNumber)
for player in gameState.getPlayers():
if(player != None):
if(gameState.getPlayerNumber() == player.playerNumber):
player.isOwnPlayer = True
gameState.setPlayerNumber(player.playerNumber)
else:
player.isOwnPlayer = False
if(hasattr(gameState.getGameMode(),"redrawTeams")):
gameState.getGameMode().redrawTeams()
@staticmethod
def startGame():
gameState.setGameMode(gameModes.playMode)
gameState.getGameMode().startGame()
gameState.rendererUpdateQueue.put(rendererUpdates.playSound(cDefines.defines["DARBUKA_HIT_INDEX"]))
for player in gameState.getPlayers():
if player != None and player.isAI:
player.analyzeMap()
@staticmethod
def chooseNextUnit():
gameState.getGameMode().chooseNextUnit()
@staticmethod
def moveTo(args):
tokens = args.split(" ")
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
gameState.getGameMode().nextUnit.moveTo(node)
@staticmethod
def moveToUndo(args):
tokens = args.split(" ")
@staticmethod
def moveToRedo(args):
tokens = args.split(" ")
@staticmethod
def skip():
gameState.getGameMode().nextUnit.skip()
@staticmethod
def skipUndo():
pass
# tokens = args.split(" ")
@staticmethod
def skipRedo():
pass
# tokens = args.split(" ")
@staticmethod
def attackTo(args):
tokens = args.split(" ")
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
gameState.getGameMode().nextUnit.attackTo(node)
@staticmethod
def attackToUndo(args):
tokens = args.split(" ")
@staticmethod
def attackToRedo(args):
tokens = args.split(" ")
@staticmethod
def healTo(args):
tokens = args.split(" ")
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
gameState.getGameMode().nextUnit.healTo(node)
@staticmethod
def healToUndo(args):
tokens = args.split(" ")
@staticmethod
def healToRedo(args):
tokens = args.split(" ")
@staticmethod
def startMeditating(args):
tokens = args.split(" ")
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
node.unit.isMeditating = True
if(node.city != None and node.unit.unitType.name == "gatherer"):
gameState.reevalAvailableUnitTypes()
# if(node == gameState.getGameMode().selectedNode and node.unit.isOwnUnit()):
# if(uiElements.viewer.theViewer != None):
# uiElements.viewer.theViewer.destroy()
# if(gameState.getGameMode().selectedNode.city != None):
# uiElements.viewer.theViewer = uiElements.summonerViewer(node)
# else:
# uiElements.viewer.theViewer = uiElements.unitViewer(node)
@staticmethod
def startMeditatingUndo(args):
tokens = args.split(" ")
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
node.unit.isMeditating = False
@staticmethod
def startMeditatingRedo(args):
tokens = args.split(" ")
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
node.unit.isMeditating = True
@staticmethod
def startSummoning(args):
tokens = args.split(" ",2)
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
unitType = gameState.theUnitTypes[tokens[2]]
gameState.getGameMode().players[node.unit.player].redWood = gameState.getGameMode().players[node.unit.player].redWood - (gameState.researchProgress[node.unit.player][unitType][0]*unitType.costRed)
gameState.getGameMode().players[node.unit.player].blueWood = gameState.getGameMode().players[node.unit.player].blueWood - (gameState.researchProgress[node.unit.player][unitType][0]*unitType.costBlue)
node.unit.queueUnit(gameLogic.unit(unitType,node.unit.player,node))
node.unit.isMeditating = True
if(gameState.getGameMode().selectedNode == node and hasattr(uiElements.viewer.theViewer,"isSummonerViewer")):
uiElements.viewer.theViewer.destroy()
uiElements.viewer.theViewer = uiElements.summonerViewer(node)
@staticmethod
def startSummoningUndo(args):
tokens = args.split(" ",2)
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
unitType = gameState.theUnitTypes[tokens[2]]
if(node.unit != None and node.unit.unitType.name == "summoner"):
node.unit.unqueueUnit()
@staticmethod
def startSummoningRedo(args):
tokens = args.split(" ",2)
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
unitType = gameState.theUnitTypes[tokens[2]]
if(node.unit != None and node.unit.unitType.name == "summoner"):
node.unit.queueUnit(gameLogic.unit(unitType,node.unit.player,node))
@staticmethod
def cancelQueuedThing(args):
tokens = args.split(" ")
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
index = int(tokens[2])
if(len(node.unit.buildQueue) > 0):
cancelledQueuedThing = node.unit.buildQueue.pop(index-1)
node.unit.cancelledUnits.append((cancelledQueuedThing,index,))
if(hasattr(cancelledQueuedThing,"unitType")):#unit
gameState.getGameMode().players[node.unit.player].redWood = gameState.getGameMode().players[node.unit.player].redWood + cancelledQueuedThing.unitType.costRed
gameState.getGameMode().players[node.unit.player].blueWood = gameState.getGameMode().players[node.unit.player].blueWood + cancelledQueuedThing.unitType.costBlue
else:#unittype
gameState.getGameMode().players[node.unit.player].redWood = gameState.getGameMode().players[node.unit.player].redWood + cancelledQueuedThing.researchCostRed
gameState.getGameMode().players[node.unit.player].blueWood = gameState.getGameMode().players[node.unit.player].blueWood + cancelledQueuedThing.researchCostBlue
if(gameState.getGameMode().selectedNode == node and hasattr(uiElements.viewer.theViewer,"isSummonerViewer")):
uiElements.viewer.theViewer.destroy()
uiElements.viewer.theViewer = uiElements.summonerViewer(node)
@staticmethod
def cancelQueuedThingUndo(args):
tokens = args.split(" ")
index = int(tokens[2])
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
cancelledThing,index = node.unit.cancelledUnits.pop()
node.unit.buildQueue.insert(index-1,cancelledThing)
@staticmethod
def cancelQueuedThingRedo(args):
tokens = args.split(" ")
index = int(tokens[2])
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
if(len(node.unit.buildQueue) > 0):
node.unit.buildQueue.pop(index-1)
@staticmethod
def startResearch(args):
tokens = args.split(" ",2)
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
unitType = gameState.theUnitTypes[tokens[2]]
node.unit.queueResearch(unitType)
gameState.getGameMode().players[node.unit.player].redWood = gameState.getGameMode().players[node.unit.player].redWood - unitType.researchCostRed
gameState.getGameMode().players[node.unit.player].blueWood = gameState.getGameMode().players[node.unit.player].blueWood - unitType.researchCostBlue
node.unit.isMeditating = True
if(gameState.getGameMode().selectedNode == node and hasattr(uiElements.viewer.theViewer,"isSummonerViewer")):
uiElements.viewer.theViewer.destroy()
uiElements.viewer.theViewer = uiElements.summonerViewer(node)
@staticmethod
def startResearchUndo(args):
tokens = args.split(" ",2)
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
unitType = gameState.theUnitTypes[tokens[2]]
node.unit.unqueueResearch()
@staticmethod
def startResearchRedo(args):
tokens = args.split(" ",2)
node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
unitType = gameState.theUnitTypes[tokens[2]]
node.unit.queueResearch(unitType)
@staticmethod
def chat(args):
if(hasattr(gameState.getGameMode(),"chatDisplay")):
gameState.getGameMode().chatDisplay.addText(args)
commandLock = threading.Lock()
def doCommand(commandName,args=None):
# print commandName + ":" + (args if args!=None else "")
commandFunc = getattr(Commands,commandName)
if(commandFunc != None):
if(args != None and args != ''):
commandFunc(args)
else:
commandFunc()
else:
print "ERROR: COMMAND " + commandName + " does not exist"
class Client:
def __init__(self,hostIP,port=-1):
if(port < 0):
port = int(gameState.getConfig()["serverPort"])
self.hostIP = hostIP
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((hostIP,port))
self.socket.setblocking(0)
self.commandLog = []
def checkSocket(self):
try:#this gives errors all the time...
receivedData = self.socket.recv(1024)
except:
receivedData = ''
for command in receivedData.split("|"):
if(len(command) > 0):
# print command
tokens = command.split(" ",2)
if(tokens[0] == "chooseNextUnit"):
with commandLock:
if(len(self.commandLog) > 0):
for command in self.commandLog:
doCommand(command[0]+"Undo",command[1])
doCommand("chooseNextUnit")
if(len(self.commandLog) > 0):
for command in self.commandLog:
doCommand(command[0]+"Redo",command[1])
self.commandLog = []
else:
if(gameState.getPlayerNumber() == SERVER or int(tokens[1]) != gameState.getPlayerNumber() or tokens[0] == "changePlayerNumber"):#skip our own commands, they were executed immediately
if(len(tokens) > 2):
with commandLock:
doCommand(tokens[0],args=tokens[2])
else:
with commandLock:
doCommand(tokens[0])
else:
self.commandLog = self.commandLog[:-1:]
#print "commandLog: " + str(self.commandLog)
if(gameState.getOwnUserName() != None and gameState.getPlayers()[gameState.getPlayerNumber()] != None and gameState.getPlayers()[gameState.getPlayerNumber()].userName != gameState.getOwnUserName()):
gameState.getClient().sendCommand("changeUserName",str(gameState.getPlayerNumber()) + ":" + gameState.getOwnUserName())
def sendCommand(self,command,argsString=""):
if(command != "chooseNextUnit" and command != "changePlayerNumber"):
with commandLock:
self.commandLog.append((command,argsString))
if(argsString != ""):
doCommand(command,argsString)
#TODO: TEST THE ENTIRE GAME IN SINGLE PLAYER WITH THESE COMMANDS UNCOMMENTED
# doCommand(command+"Undo",argsString)
# doCommand(command+"Redo",argsString)
else:
doCommand(command)
self.socket.send(command + " " + str(gameState.getPlayerNumber()) + " " + argsString + "|")
def startClient(hostIP,hostPort=-1):
gameState.setClient(Client(hostIP,hostPort))
def stopClient():
try:
gameState.getClient().socket.shutdown(socket.SHUT_RD)
except socket.error as e:
print e.errno
gameState.getClient().socket.close()
gameState.setClient(None)
gameState.resetPlayers()
gameState.resetAIs()
gameState.setOwnUserName(None)
# clientThread = ClientThread(hostIP)
# clientThread.daemon = True
# clientThread.start()