forked from Igoorx/PyRoyale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.py
193 lines (159 loc) · 6.64 KB
/
match.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
from twisted.internet import reactor
from buffer import Buffer
import os
import json
import random
import jsonschema
levelJsonSchema = json.loads(open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "levelSchema.json"), "r").read())
class Match(object):
def __init__(self, server, roomName, private):
self.server = server
self.forceLevel = ""
self.customLevelData = ""
self.world = "lobby"
self.roomName = roomName
self.closed = False
self.private = private
self.playing = False
self.autoStartTimer = None
self.startingTimer = None
self.startTimer = int()
self.votes = int()
self.winners = int()
self.lastId = -1
self.players = list()
self.goldFlowerTaken = bool()
def getNextPlayerId(self):
self.lastId += 1
return self.lastId
def addPlayer(self, player):
self.players.append(player)
return self.getNextPlayerId()
def removePlayer(self, player):
if player not in self.players:
return
self.players.remove(player)
if len(self.players) == 0:
try:
self.startingTimer.cancel()
except:
pass
try:
self.autoStartTimer.cancel()
except:
pass
self.server.removeMatch(self)
return
if not player.dead and not player.win: # Don't kill podium players
self.broadBin(0x11, Buffer().writeInt16(player.id)) # KILL_PLAYER_OBJECT
self.broadPlayerList()
if player.voted:
self.votes -= 1
elif self.server.enableVoteStart and not self.playing and self.votes >= len(self.players) * self.server.voteRateToStart:
self.start()
def getPlayer(self, pid):
for player in self.players:
if player.id == pid:
return player
return None
def getWinners(self):
self.winners += 1
return self.winners
def broadJSON(self, j):
for player in self.players:
if not player.loaded:
continue
player.sendJSON(j)
def broadBin(self, code, buff, ignore = None):
buff = buff.toBytes() if isinstance(buff, Buffer) else buff
for player in self.players:
if not player.loaded or (ignore is not None and player.id == ignore):
continue
player.sendBin(code, buff)
def broadLoadWorld(self):
for player in self.players:
player.loadWorld(self.world, self.customLevelData)
def broadStartTimer(self, time):
self.startTimer = time * 30
for player in self.players:
if not player.loaded:
continue
player.setStartTimer(self.startTimer)
if time > 0:
reactor.callLater(1, self.broadStartTimer, time - 1)
else:
self.closed = True
def broadPlayerList(self):
data = self.getPlayersData()
for player in self.players:
if not player.loaded:
continue
player.sendJSON({"packets": [
{"players": (data + ([player.getSimpleData()] if player.dead else [])),
"type": "g12"}
], "type": "s01"})
def getPlayersData(self):
playersData = []
for player in self.players:
if not player.loaded or player.dead:
continue
playersData.append(player.getSimpleData())
return playersData
def onPlayerReady(self, player):
if not self.private and not self.playing: # Ensure that the game starts even with fewer players
try:
self.autoStartTimer.cancel()
except:
pass
self.autoStartTimer = reactor.callLater(30, self.start, True)
if self.world == "lobby" and self.goldFlowerTaken:
self.broadBin(0x20, Buffer().writeInt16(-1).writeInt8(0).writeInt8(0).writeInt32(458761).writeInt8(0))
if self.world == "lobby" or not player.lobbier or self.closed:
for p in self.players:
if not p.loaded or p == player:
continue
player.sendBin(0x10, p.serializePlayerObject())
if self.startTimer != 0 or self.closed:
player.setStartTimer(self.startTimer)
self.broadPlayerList()
if not self.playing:
if self.startingTimer is None and len(self.players) >= self.server.playerCap:
self.startingTimer = reactor.callLater(3, self.start, True)
# This is needed because if the votes where sufficient to start but there wasn't sufficient players,
# when someone enters the game, it can make it possible to start the game.
elif self.server.enableVoteStart and self.votes >= len(self.players) * self.server.voteRateToStart:
self.startingTimer = reactor.callLater(3, self.start, True)
def voteStart(self):
self.votes += 1
if self.server.enableVoteStart and not self.playing and self.votes >= len(self.players) * self.server.voteRateToStart:
self.start()
def start(self, forced = False):
if self.playing or (not forced and len(self.players) < (1 if self.private else self.server.playerMin)): # We need at-least 10 players to start
return
self.playing = True
try:
self.startingTimer.cancel()
except:
pass
try:
self.autoStartTimer.cancel()
except:
pass
self.world = self.forceLevel if self.forceLevel != "" else random.choice(self.server.worlds)
self.broadLoadWorld()
reactor.callLater(1, self.broadStartTimer, self.server.startTimer)
def validateCustomLevel(self, level):
lk = json.loads(level)
jsonschema.validate(instance=lk, schema=levelJsonSchema)
def selectLevel(self, level):
if level == "" or level in self.server.worlds:
self.forceLevel = level
self.broadLevelSelect()
def broadLevelSelect(self):
for player in self.players:
player.sendJSON({"type":"gsl", "name":self.forceLevel, "status":"update", "message":""})
def selectCustomLevel(self, level):
self.validateCustomLevel(level)
self.forceLevel = "custom"
self.customLevelData = level
self.broadLevelSelect()