This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 324
/
player.py
213 lines (161 loc) · 7.09 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
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
import re
import emoji
from twisted.internet import reactor
from buffer import Buffer
try:
from discord_webhook import DiscordEmbed
except Exception as e:
pass
class Player(object):
def __init__(self, client, name, team, match, skin):
self.client = client
self.server = client.server
self.match = match
self.skin = skin
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):
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.lastUpdatePkt = None
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).writeInt16(self.skin).toBytes()
def loadWorld(self, worldName, levelData):
self.dead = True
self.loaded = False
self.pendingWorld = worldName
msg = {"game": worldName, "type": "g01"}
if worldName == "custom":
msg["levelData"] = levelData
self.sendJSON({"packets": [msg], "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.match.onPlayerEnter(self)
self.loadWorld(self.match.world, self.match.customLevelData)
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).writeInt16(self.skin)) # 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
self.dead = False
self.client.stopDCTimer()
self.match.broadBin(0x10, Buffer().writeInt16(self.id).write(pktData).writeInt16(self.skin))
elif code == 0x11: # KILL_PLAYER_OBJECT
if self.dead or self.win:
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 or self.lastUpdatePkt == pktData:
return
level, zone, pos, sprite, reverse = b.readInt8(), b.readInt8(), b.readVec2(), b.readInt8(), b.readBool()
if self.level != level or self.zone != zone:
self.match.onPlayerWarp(self, level, zone)
self.level = level
self.zone = zone
self.posX = pos[0]
self.posY = pos[1]
self.lastUpdatePkt = pktData
if sprite > 5 and self.match.world == "lobby" and zone == 0:
self.client.block(0x1)
return
self.match.broadPlayerUpdate(self, pktData)
elif code == 0x13: # PLAYER_OBJECT_EVENT
if self.dead or self.win:
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)
pos = self.match.getWinners()
try:
# Maybe this should be assynchronous?
if self.server.discordWebhook is not None and pos == 1 and not self.match.private:
name = self.name
# We already filter players that have a squad so...
if len(self.team) == 0 and self.server.checkCurse(self.name):
name = "[ censored ]"
embed = DiscordEmbed(description='**%s** has achieved **#1** victory royale!' % name, color=0xffff00)
self.server.discordWebhook.add_embed(embed)
self.server.discordWebhook.execute()
self.server.discordWebhook.remove_embed(0)
except:
pass
# Make sure that everyone knows that the player is at the axe
self.match.broadPlayerUpdate(self, self.lastUpdatePkt)
self.match.broadBin(0x18, Buffer().writeInt16(self.id).writeInt8(pos).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))