-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameServer.py
executable file
·279 lines (222 loc) · 8.38 KB
/
gameServer.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
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
#!/usr/bin/python
# -*- coding: utf8 -*-
import sys
import time
#from time import sleep
#from weakref import WeakKeyDictionary
try:
import psyco
psyco.full()
except:
pass
import pygame
import random
from PodSixNet.Server import Server
from dbHandler import DbHandler, isValidName, isValidPassword
#from gameEngine import *
from gameEngine import Player, Mob, GameMap, getDist
from clientChannel import ClientChannel
#-----------------------------------------------------------------------
# Logger
#-----------------------------------------------------------------------
class Logger(object):
def __init__(self, filename = "log.txt"):
self.filename = filename
def write(self, msg):
self.logfile = open(self.filename, 'a')
today = time.strftime("%Y%m%d%H%M")
print "(log) %s %s" % (today, msg)
msg = today + " : " + msg + "\n"
self.logfile.write(msg)
self.logfile.close()
def quit(self):
self.logfile.close()
#-----------------------------------------------------------------------
# GameServer
#-----------------------------------------------------------------------
class GameServer(Server):
channelClass = ClientChannel
def __init__(self, *args, **kwargs):
Server.__init__(self, *args, **kwargs)
self.playerChannels = {}#WeakKeyDictionary() # playerChannel -> True
self.players = {} # playerName -> playerChannel
self.playerMaps = {} # playerName -> mapName
self.db = DbHandler("save/essai.db")
self.logger = Logger()
self.maps = {}
self.addMap("data/start.txt")
self.addMap("data/second.txt")
self.log('Server launched')
for m in self.maps:
for i in range(15):
self.maps[m].addMob(1,50.0,50.0)
self.prevTime = 0.0
pygame.init()
def log(self, msg):
self.logger.write(msg)
#-------------------------------------------------------------------
# generic purpose server to client
#-------------------------------------------------------------------
def SendTo(self, playerName, data):
self.players[playerName].Send(data)
def SendToAll(self, data):
[p.Send(data) for p in self.playerChannels]
def SendToMap(self, mapName, data):
for playerName in self.maps[mapName].players:
self.SendTo(playerName, data)
def Launch(self):
while True:
t = pygame.time.get_ticks()
dt = t - self.prevTime
self.prevTime = t
if dt:
#print "Server Main Loop : t = %s, dt = %s" % (t, dt)
for m in self.maps:
self.maps[m].update(dt)
self.Pump()
time.sleep(0.0001)
def Connected(self, channel, addr):
self.AddPlayerChannel(channel)
def AddPlayerChannel(self, playerChannel):
msg = "New player connected from " + str(playerChannel.addr) + ", waiting to log in..."
self.log(msg)
self.playerChannels[playerChannel] = True
def DelPlayerChannel(self, playerChannel):
msg = "Deleting Player connection for " + str(playerChannel.addr)
self.log(msg)
del self.playerChannels[playerChannel]
#self.SendPlayers()
#-------------------------------------------------------------------
# server messages to client
#-------------------------------------------------------------------
def SendLoginError(self, playerChannel, msg):
if playerChannel not in self.playerChannels:
self.log("Login Error : player channel unknown")
return
data = {"action":"login_error", "msg" : msg}
playerChannel.Send(data)
def SendLoginAccepted(self, playerChannel):
if playerChannel not in self.playerChannels:
self.log("Login Error : player channel unknown")
return
msg = "Sending login accepted"
self.log(msg)
data = {"action":"login_accepted", "mapFileName" : "maps/testmap.txt", "x": 50, "y":50}
playerChannel.Send(data)
def SendRegisterError(self, playerChannel, msg):
if playerChannel not in self.playerChannels:
self.log("Register Error : player channel unknown")
return
data = {"action":"register_error", "msg" : msg}
playerChannel.Send(data)
def SendRegisterAccepted(self, playerChannel, msg):
if playerChannel not in self.playerChannels:
self.log("Register Error : player channel unknown")
return
data = {"action":"register_accepted", "msg" : msg}
playerChannel.Send(data)
def SendPlayers(self):
for mapName in self.maps:
self.SendMapPlayers(mapName)
def SendMapPlayers(self, mapName):
mapPlayers = self.maps[mapName].players.keys()
for playerName in mapPlayers:
player = self.maps[mapName].players[playerName]
self.SendPlayerUpdateMove(mapName, playerName, player.x, player.y, player.dx, player.dy)
def SendWarpInfo(self, playerName):
mapName = self.playerMaps[playerName]
for warp in self.maps[mapName].warps:
name = warp.name
x = warp.x
y = warp.y
w = warp.w
h = warp.h
self.SendTo(playerName, {'action':'warp_info', 'name':name, 'x':x, 'y':y, 'w':w, 'h':h})
def SendPlayerUpdateMove(self, mapName, playerName, x, y, dx, dy):
self.SendToMap(mapName, {"action": "player_update_move", "id": playerName, "x" : x, "y" : y, "dx" : dx, "dy" : dy})
def SendMobUpdateMove(self, mapName, mobId, x, y, dx, dy):
self.SendToMap(mapName, {"action": "mob_update_move", "id": mobId, "x" : x, "y" : y, "dx" : dx, "dy" : dy})
def SendPlayerEnterMap(self, mapName, playerName):
player = self.maps[mapName].players[playerName]
x = player.x
y = player.y
dx = player.dx
dy = player.dy
self.SendToMap(mapName, {"action": "player_enter_map", "id": playerName, "x":x, "y":y, "dx":dx, "dy":dy})
def SendPlayerLeaveMap(self, mapName, playerName):
self.SendToMap(mapName, {"action": "player_leave_map", "id": playerName})
def SendPlayerWarp(self, mapName, playerName, x, y):
mapFileName = self.maps[mapName].mapFilename
self.SendTo(playerName, {"action": "warp", "mapFileName" : mapFileName, "x":x, "y":y})
def SendMobLeaveMap(self, mapName, mobId):
self.SendToMap(mapName, {"action": "mob_leave_map", "id": mobId})
def SendMobTookDamage(self, mapName, mobId, dmg):
self.SendToMap(mapName, {"action": "mob_took_damage", "id": mobId, "dmg":dmg})
def addMap(self, filePath):
m = GameMap(self, filePath)
self.maps[m.name] = m
#print "Map '%s' added to server" % (m.name)
def addPlayer(self, mapName, playerName, x, y):
self.playerMaps[playerName] = mapName
self.maps[mapName].addPlayer(playerName, x, y)
self.SendPlayerEnterMap(mapName, playerName)
def delPlayer(self, playerName):
if playerName in self.playerMaps:
mapName = self.playerMaps[playerName]
self.maps[mapName].delPlayer(playerName)
self.SendPlayerLeaveMap(mapName, playerName)
del self.players[playerName]
del self.playerMaps[playerName]
def warpPlayer(self, playerName, newMapName, x, y):
mapName = self.playerMaps[playerName]
if mapName == newMapName:
player = self.maps[mapName].players[playerName]
player.setPos(x, y)
#self.SendPlayerUpdateMove(mapName, playerName, x, y, 0, 0)
self.SendPlayerWarp(newMapName, playerName, x, y)
return
self.maps[mapName].delPlayer(playerName)
self.SendPlayerLeaveMap(mapName, playerName)
self.maps[newMapName].addPlayer(playerName, x, y)
self.SendPlayerWarp(newMapName, playerName, x, y)
self.SendPlayerEnterMap(newMapName, playerName)
self.playerMaps[playerName] = newMapName
#print("warped player %s from map %s to map %s : %s / %s" % (playerName, mapName, newMapName, x, y))
def addMob(self, mapName, mobId, x=60,y=60):
self.maps[mapName].addMob(mobId, x, y)
def onPlayerAttackMob(self, playerId, mobId):
if playerId not in self.playerMaps:
return
mapName = self.playerMaps[playerId]
if mobId not in self.maps[mapName].mobs:
return
mob = self.maps[mapName].mobs[mobId]
dist = getDist(mob.mapRect, self.maps[mapName].players[playerId].mapRect)
if dist > 40.0:
return
dmg = random.randint(1,6)
self.SendMobTookDamage(mapName, mobId, dmg)
mob.takeDamage(dmg)
mob.setMovement(0,0)
self.SendMobUpdateMove(mapName, mobId, mob.x, mob.y, 0,0)
if mob.hp <= 0:
self.onMobDie(mapName, mobId)
def onMobDie(self, mapName, mobId):
if mobId not in self.maps[mapName].mobs:
return
self.maps[mapName].delMob(mobId)
self.SendMobLeaveMap(mapName, mobId)
self.maps[mapName].addMob(1, 80,80)
'''
# get command line argument of server, port
if len(sys.argv) != 2:
print "Usage:", sys.argv[0], "host:port"
print "e.g.", sys.argv[0], "localhost:31425"
else:
host, port = sys.argv[1].split(":")
s = GameServer(localaddr=(host, int(port)))
s.Launch()
'''
if __name__=="__main__":
s = GameServer(localaddr=("88.173.217.230", 18647), listeners=5000)
s.Launch()