forked from Igoorx/PyRoyale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
185 lines (137 loc) · 5.77 KB
/
player.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
import re
import emoji
from twisted.internet import reactor
from buffer import Buffer
class Player(object):
def __init__(self, client, name, team, match):
self.client = client
self.server = client.server
self.match = match
self.name = ' '.join(emoji.emojize(re.sub(r"[^\x00-\x7F]+", "", emoji.demojize(name)).strip())[:20].split()).upper()
self.team = team
if len(self.team) > 0 and self.server.checkCurse(self.name): # Don't filter players out of squads
self.name = str()
if len(self.name) == 0:
self.name = self.server.defaultName
self.pendingWorld = None
self.level = int()
self.zone = int()
self.posX = int()
self.posY = int()
self.dead = True
self.win = bool()
self.voted = bool()
self.loaded = bool()
self.lobbier = bool()
self.trustCount = int()
self.lastX = int()
self.lastXOk = True
self.id = match.addPlayer(self)
def sendJSON(self, j):
self.client.sendJSON(j)
def sendBin(self, code, b):
self.client.sendBin(code, b)
def getSimpleData(self):
return {"id": self.id, "name": self.name, "team": self.team}
def serializePlayerObject(self):
return Buffer().writeInt16(self.id).writeInt8(self.level).writeInt8(self.zone).writeShor2(self.posX, self.posY).toBytes()
def loadWorld(self, worldName):
self.dead = True
self.loaded = False
self.pendingWorld = worldName
self.sendJSON({"packets": [
{"game": worldName, "type": "g01"}
], "type": "s01"})
self.client.startDCTimer(15)
def setStartTimer(self, time):
self.sendJSON({"packets": [
{"time": time, "type": "g13"}
], "type": "s01"})
def onEnterIngame(self):
if not self.dead:
return
if self.match.world == "lobby":
self.lobbier = True
self.loadWorld(self.match.world)
def onLoadComplete(self):
if self.loaded or self.pendingWorld is None:
return
self.client.stopDCTimer()
self.level = 0
self.zone = 0
self.posX = 35
self.posY = 3
self.win = False
self.dead = False
self.loaded = True
self.pendingWorld = None
self.lastXOk = True
self.sendBin(0x02, Buffer().writeInt16(self.id)) # ASSIGN_PID
self.match.onPlayerReady(self)
def handlePkt(self, code, b, pktData):
if code == 0x10: # CREATE_PLAYER_OBJECT
level, zone, pos = b.readInt8(), b.readInt8(), b.readShor2()
self.level = level
self.zone = zone
wasDead = self.dead
self.dead = False
if wasDead:
self.match.broadPlayerList()
self.client.stopDCTimer()
self.match.broadBin(0x10, Buffer().writeInt16(self.id).write(pktData))
elif code == 0x11: # KILL_PLAYER_OBJECT
if self.dead:
return
self.dead = True
self.client.startDCTimer(60)
self.match.broadBin(0x11, Buffer().writeInt16(self.id))
elif code == 0x12: # UPDATE_PLAYER_OBJECT
if self.dead:
return
level, zone, pos, sprite, reverse = b.readInt8(), b.readInt8(), b.readVec2(), b.readInt8(), b.readBool()
self.level = level
self.zone = zone
self.posX = pos[0]
self.posY = pos[1]
if sprite > 5 and self.match.world == "lobby" and zone == 0:
self.client.block(0x1)
return
self.match.broadBin(0x12, Buffer().writeInt16(self.id).write(pktData))
elif code == 0x13: # PLAYER_OBJECT_EVENT
if self.dead:
return
type = b.readInt8()
if self.match.world == "lobby":
self.client.block(0x2)
return
self.match.broadBin(0x13, Buffer().writeInt16(self.id).write(pktData))
elif code == 0x17:
killer = b.readInt16()
if self.id == killer:
return
killer = self.match.getPlayer(killer)
if killer is None:
return
killer.sendBin(0x17, Buffer().writeInt16(self.id).write(pktData))
elif code == 0x18: # PLAYER_RESULT_REQUEST
if self.dead or self.win:
return
self.win = True
self.client.startDCTimer(120)
self.match.broadBin(0x18, Buffer().writeInt16(self.id).writeInt8(self.match.getWinners()).writeInt8(0))
elif code == 0x19:
self.trustCount += 1
if self.trustCount > 8:
self.client.block(0x3)
elif code == 0x20: # OBJECT_EVENT_TRIGGER
if self.dead:
return
level, zone, oid, type = b.readInt8(), b.readInt8(), b.readInt32(), b.readInt8()
if self.match.world == "lobby" and oid == 458761:
self.match.goldFlowerTaken = True
self.match.broadBin(0x20, Buffer().writeInt16(self.id).write(pktData))
elif code == 0x30: # TILE_EVENT_TRIGGER
if self.dead:
return
level, zone, pos, type = b.readInt8(), b.readInt8(), b.readShor2(), b.readInt8()
self.match.broadBin(0x30, Buffer().writeInt16(self.id).write(pktData))