forked from nixeagle/pokemon-online-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
310 lines (236 loc) · 11.3 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
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
# server.py
# Pokemon Online Client / HTTP Server
# Acts as a middleman between webclient and PO servers
#
# (c) Toni Fadjukoff 2011
# Licensed under BSD style license
# See LICENSE for details
from twisted.internet.protocol import Protocol, Factory
from twisted.web import resource
from twisted.web.server import Site
from twisted.web.static import File
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from txsockjs.factory import SockJSFactory
from struct import pack, unpack
from json import loads, dumps
from hashlib import md5
from poprotocol.interfaces import TwistedClientProtocol, TwistedRegistryProtocol
from poprotocol import ChallengeInfo
from teamloader import loadTeam
class Registry(TwistedRegistryProtocol):
def __init__(self):
TwistedRegistryProtocol.__init__(self)
self.servers = []
self.announcement = Deferred()
self.serverlist = Deferred()
def onAnnouncement(self, announcement):
self.announcement.callback(announcement)
def onPlayersList(self, name, desc, numplayers, ip, maxplayers, port, protected):
self.servers.append([name, desc, numplayers, ip, maxplayers, port, protected])
def onServerListEnd(self):
self.serverlist.callback(self.servers)
class Receiver(TwistedClientProtocol):
def __init__(self):
TwistedClientProtocol.__init__(self)
self.client = None
# Helpers
def serializePlayer(self, playerInfo):
player = { 'id': playerInfo.id, 'name': playerInfo.name, 'info': playerInfo.info,
'auth': playerInfo.auth, 'away': playerInfo.away,
'hasLadder': playerInfo.hasLadder,
'teams': playerInfo.teams, 'avatar': playerInfo.avatar,
'color': playerInfo.color.__dict__ }
return player
def serializeTeamBattle(self, teamBattle):
team = {'m_pokemons': []}
for poke in teamBattle.m_pokemons:
pd = poke.__dict__
pd['num'] = pd['num'].__dict__ # unique id
for i in range(4):
pd['move'][i] = pd['move'][i].__dict__ # battlemove
team['m_pokemons'].append(pd)
return team
# PO Events
def onVersionControl(self, version, name):
self.client.sendObject({'type': 'VersionControl', 'version': version, 'name': name})
def onRegister(self):
self.client.sendObject({'type': 'Register'})
def onAskForPass(self, salt):
self.client.sendObject({'type': 'AskForPass', 'salt': salt})
def onAnnouncement(self, announcement):
self.client.sendObject({'type': 'Announcement', 'announcement': announcement})
def onLogin(self, playerInfo):
player = self.serializePlayer(playerInfo)
self.client.sendObject({'type': 'Login', 'player': player})
def onLogout(self, id):
self.client.sendObject({'type': 'Logout', 'playerId': id})
def onTierSelection(self, tiers):
self.client.sendObject({'type': 'TierSelection', 'tiers': tiers})
def onChannelsList(self, channels):
self.client.sendObject({'type': 'ChannelsList', 'channels': channels})
def onPlayersList(self, playerInfos):
players = []
for player in playerInfos:
players.append(self.serializePlayer(player))
self.client.sendObject({'type': 'PlayersList', 'players': (players)})
def onPlayerBan(self, playerId, srcId):
self.client.sendObject({'type': 'PlayerBan', 'playerId': playerId, 'srcId': srcId})
def onPlayerKick(self, playerId, srcId):
self.client.sendObject({'type': 'PlayerKick', 'playerId': playerId, 'srcId': srcId})
def onSendTeam(self, playerInfo):
player = self.serializePlayer(playerInfo)
self.client.sendObject({'type': 'SendTeam', 'player': player})
def onChallengeStuff(self, challengeInfo):
info = { 'dsc': challengeInfo.dsc, 'opp': challengeInfo.opp,
'clauses': challengeInfo.clauses, 'mode': challengeInfo.mode}
self.client.sendObject({'type': 'ChallengeStuff', 'challengeInfo': info})
def onEngageBattle(self, battleid, player1, player2, battleConf, teamBattle):
if player1 == 0:
self.client.sendObject({'type': 'EngageBattle', 'battleId': battleid,
'playerId1': player1, 'playerId2': player2,
'battleConf': battleConf.__dict__, 'teamBattle': self.serializeTeamBattle(teamBattle)})
else:
self.client.sendObject({'type': 'EngageBattle', 'battleId': battleid,
'playerId1': player1, 'playerId2': player2})
def onBattleFinished(self, battleid, outcome, winner, loser):
self.client.sendObject({'type': 'BattleFinished', 'battleId': battleid,
'outcome': outcome, 'winner': winner, 'loser': loser})
def onChannelPlayers(self, chanid, playerlist):
self.client.sendObject({'type': 'ChannelPlayers', 'chanId': chanid, 'playerList': playerlist})
def onJoinChannel(self, chanid, playerid):
self.client.sendObject({'type': 'JoinChannel', 'chanId': chanid, 'playerId': playerid})
def onLeaveChannel(self, chanid, playerid):
self.client.sendObject({'type': 'LeaveChannel', 'chanId': chanid, 'playerId': playerid})
def onChannelBattle(self, chanid, battleid, player1, player2):
self.client.sendObject({'type': 'ChannelBattle', 'chanId': chanid, 'battleId': battleid,
'player1': player1, 'player2': player2})
def onChannelMessage(self, chanid, user, message):
self.client.sendObject({'type': 'ChannelMessage', 'chanId': chanid, 'user': user, 'message': message})
def onRemoveChannel(self, chanid):
self.client.sendObject({'type': 'RemoveChannel', 'chanId': chanid})
def onAddChannel(self, chanid, channame):
self.client.sendObject({'type': 'AddChannel', 'chanId': chanid, 'chanName': channame})
def onSendPM(self, playerid, message):
self.client.sendObject({'type': 'SendPM', 'playerId': playerid, 'message': message})
def onAway(self, playerid, isAway):
self.client.sendObject({'type': 'Away', 'playerId': playerid, 'isAway': isAway})
def onSendMessage(self, message, hasId=False, hasChannel=False, isHtml=False, id=0, channel=0, user=None):
self.client.sendObject({'type': 'SendMessage', 'id': id, 'message': message, 'channel': channel, 'hasChannel': hasChannel, 'hasId': hasId, 'isHtml': isHtml})
def onHtmlMessage(self, message):
self.client.sendObject({'type': 'HtmlMessage', 'message': message})
def onHtmlChannel(self, chanid, message):
self.client.sendObject({'type': 'HtmlChannel', 'message': message, 'chanId':chanid})
# Other Events
def connectionLost(self, reason):
if self.client:
self.client.sendObject({'type': 'Disconnected', 'reason': str(reason)})
print "DISCONNECTED " + str(reason)
self.client = None
class POFactory(Factory):
def buildProtocol(self, addr):
return POHandler()
class POHandler(Protocol):
def __init__(self):
self.proxy = None
self.pending = []
def __del__(self):
print 'Deleting handler'
def connectionMade(self):
print 'Connected to client.'
factory = Factory()
factory.protocol = Registry
point = TCP4ClientEndpoint(reactor, "pokemon-online.dynalias.net", 5090)
d = point.connect(factory)
def gotRegistry(registry):
def gotAnnouncement(announcement):
self.sendObject({'type': 'RegistryAnnouncement', 'announcement': announcement})
def gotServerList(servers):
self.sendObject({'type': 'ServerList', 'servers': servers})
registry.transport.loseConnection()
registry.announcement.addCallback(gotAnnouncement)
registry.serverlist.addCallback(gotServerList)
d.addCallback(gotRegistry)
d.addErrback(self.cantConnect)
def dataReceived(self, data):
json = loads(data)
if json["type"] == "Connect":
ip = json.get("ip", None)
port = json.get("port", None)
if ip and port:
self.createProxyConnection(ip, port)
return
if not self.proxy:
self.pending.append(data)
return
#try:
method = "on{0}".format(json["type"])
if hasattr(self, method):
getattr(self, method)(json)
else:
print "Unknown event:", method
#except Exception as e:
# print "Error handling in JSON event: {0}".format(json.get("type"), "UnknownEvent")
# print "{0}: {1}".format(e.__class__.__name__, e)
def sendObject(self, o):
self.transport.write(dumps(o))
def createProxyConnection(self, ip, port):
factory = Factory()
factory.protocol = Receiver
point = TCP4ClientEndpoint(reactor, ip, port)
d = point.connect(factory)
d.addCallback(self.gotProxy)
d.addErrback(self.cantConnect)
def connectionLost(self, reason):
print 'Lost connection.'
if self.proxy:
self.proxy.transport.loseConnection()
self.proxy = None
def gotProxy(self, proxy):
print "Connected!"
self.proxy = proxy
proxy.client = self
self.proxy.setProxyIP(self.proxy.transport.getHost().host)
for p in self.pending:
self.dataReceived(p)
def cantConnect(self, msg):
print 'Can\'t connect: {0}'.format(msg)
# Handing JSON Events from Websocket client
def onLogin(self, json):
self.proxy.login(json['name'], clientType=u'webclient', defaultChannel=u'WebClient')
def onChannelMessage(self, json):
self.proxy.sendChannelMessage(json['chanId'], json['message'])
def onJoinChannel(self, json):
self.proxy.joinChannel(json['chanName'])
def onLeaveChannel(self, json):
self.proxy.partChannel(json['chanId'])
def onRegister(self, json):
self.proxy.register()
def onAskForPass(self, json):
pw = json["password"]
salt = json["salt"]
s = md5(md5(pw.decode("utf-8").encode("iso-8859-1", "ignore")).hexdigest() + salt.encode("iso-8859-1", "ignore")).hexdigest()
u = s.decode("iso-8859-1")
self.proxy.askForPass(u)
def onSendPM(self, json):
self.proxy.sendPM(json['playerId'], json['message'])
def onChallengeStuff(self, json):
self.proxy.challengeStuff(ChallengeInfo(json['info']['type'], json['info']['id'], json['info'].get('clauses', 0), json['info'].get('mode', 0)));
def onBattleCommand(self, json):
bc = BattleChoice()
bc.type = BattleChoice.CancelType;
bc.spot = json['spot']
self.proxy.battleCommand(json['battleid'], bc)
def onBattleFinished(self, json):
self.proxy.battleFinished(json['battleid'], json['result'])
if __name__ == "__main__":
# run our websocket server
# serve index.html from the local directory
root = File('static')
factory = Site(root)
reactor.listenTCP(8080, factory)
factory = POFactory()
factory = SockJSFactory(factory)
reactor.listenTCP(8081, factory)
reactor.run()