pep.py/events/joinMatchEvent.py

68 lines
2.2 KiB
Python
Raw Normal View History

2016-10-02 20:48:14 +00:00
from common import generalUtils
from common.log import logUtils as log
2016-05-18 17:12:46 +00:00
from constants import clientPackets
from constants import exceptions
2016-10-02 20:48:14 +00:00
from constants import serverPackets
from helpers import chatHelper as chat
2016-10-02 20:48:14 +00:00
from objects import glob
2016-04-19 17:40:59 +00:00
def handle(userToken, packetData):
# read packet data
packetData = clientPackets.joinMatch(packetData)
# Get match from ID
joinMatch(userToken, packetData["matchID"], packetData["password"])
def joinMatch(userToken, matchID, password, isPasswordHashed = False):
2016-04-19 17:40:59 +00:00
try:
# Stop spectating
userToken.stopSpectating()
2016-04-19 17:40:59 +00:00
# Leave other matches
if userToken.matchID > -1 and userToken.matchID != matchID:
userToken.partMatch()
# Get usertoken data
2016-04-19 17:40:59 +00:00
userID = userToken.userID
# Make sure the match exists
if matchID not in glob.matches.matches:
raise exceptions.matchNotFoundException
# Match exists, get object
match = glob.matches.matches[matchID]
# Hash password if needed
if isPasswordHashed == False and password != "":
2016-10-02 20:48:14 +00:00
password = generalUtils.stringMd5(password)
2016-04-19 17:40:59 +00:00
# Check password
# TODO: Admins can enter every match
if match.matchPassword != "":
if match.matchPassword != password:
raise exceptions.matchWrongPasswordException
# Password is correct, join match
result = match.userJoin(userID)
# Check if we've joined the match successfully
2016-09-02 15:45:10 +00:00
if not result:
2016-04-19 17:40:59 +00:00
raise exceptions.matchJoinErrorException
# Match joined, set matchID for usertoken
userToken.joinMatch(matchID)
# Send packets
userToken.enqueue(serverPackets.matchJoinSuccess(matchID))
chat.joinChannel(token=userToken, channel="#multi_{}".format(matchID))
2016-04-19 17:40:59 +00:00
except exceptions.matchNotFoundException:
userToken.enqueue(serverPackets.matchJoinFail())
log.warning("{} has tried to join a mp room, but it doesn't exist".format(userToken.username))
2016-04-19 17:40:59 +00:00
except exceptions.matchWrongPasswordException:
userToken.enqueue(serverPackets.matchJoinFail())
log.warning("{} has tried to join a mp room, but he typed the wrong password".format(userToken.username))
2016-04-19 17:40:59 +00:00
except exceptions.matchJoinErrorException:
userToken.enqueue(serverPackets.matchJoinFail())
log.warning("{} has tried to join a mp room, but an error has occured".format(userToken.username))