# Path: constants/serverPackets.py
# def loginFailed():
# def forceUpdate():
# def loginBanned():
# def loginLocked():
# def loginError():
# def needSupporter():
# def needVerification():
# def userID(uid):
# def silenceEndTime(seconds):
# def protocolVersion(version = 19):
# def mainMenuIcon(icon):
# def userSupporterGMT(supporter, GMT, tournamentStaff):
# def friendList(userID):
# def onlineUsers():
# def userLogout(userID):
# def userPanel(userID, force = False):
# def userStats(userID, force = False):
# def sendMessage(fro, to, message):
# def channelJoinSuccess(userID, chan):
# def channelInfo(chan):
# def channelInfoEnd():
# def channelKicked(chan):
# def userSilenced(userID):
# def addSpectator(userID):
# def removeSpectator(userID):
# def spectatorFrames(data):
# def noSongSpectator(userID):
# def fellowSpectatorJoined(userID):
# def fellowSpectatorLeft(userID):
# def createMatch(matchID):
# def updateMatch(matchID, censored = False):
# def matchStart(matchID):
# def disposeMatch(matchID):
# def matchJoinSuccess(matchID):
# def matchJoinFail():
# def changeMatchPassword(newPassword):
# def allPlayersLoaded():
# def playerSkipped(userID):
# def allPlayersSkipped():
# def matchFrames(slotID, data):
# def matchComplete():
# def playerFailed(slotID):
# def matchTransferHost():
# def matchAbort():
# def switchServer(address):
# def notification(message):
# def banchoRestart(msUntilReconnection):
# def rtx(message):
# 
# Path: helpers/chatHelper.py
# def joinChannel(userID = 0, channel = "", token = None, toIRC = True, force=False):
# def partChannel(userID = 0, channel = "", token = None, toIRC = True, kick = False, force=False):
# def sendMessage(fro = "", to = "", message = "", token = None, toIRC = True):
# def fixUsernameForBancho(username):
# def fixUsernameForIRC(username):
# def IRCConnect(username):
# def IRCDisconnect(username):
# def IRCJoinChannel(username, channel):
# def IRCPartChannel(username, channel):
# def IRCAway(username, message):
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# DATADOG_PREFIX = "peppy"


import time
import json
from common.log import logUtils as log
from constants import serverPackets
from helpers import chatHelper as chat
from objects import glob



def handle(userToken, _=None, deleteToken=True):
	# get usertoken data
	userID = userToken.userID
	username = userToken.username
	requestToken = userToken.token

	# Big client meme here. If someone logs out and logs in right after,
	# the old logout packet will still be in the queue and will be sent to
	# the server, so we accept logout packets sent at least 5 seconds after login
	# if the user logs out before 5 seconds, he will be disconnected later with timeout check
	if int(time.time() - userToken.loginTime) >= 5 or userToken.irc:
		# Stop spectating
		userToken.stopSpectating()

		# Part matches
		userToken.leaveMatch()
		
		# Part all joined channels
		for i in userToken.joinedChannels:

# Path: constants/serverPackets.py
# def loginFailed():
# def forceUpdate():
# def loginBanned():
# def loginLocked():
# def loginError():
# def needSupporter():
# def needVerification():
# def userID(uid):
# def silenceEndTime(seconds):
# def protocolVersion(version = 19):
# def mainMenuIcon(icon):
# def userSupporterGMT(supporter, GMT, tournamentStaff):
# def friendList(userID):
# def onlineUsers():
# def userLogout(userID):
# def userPanel(userID, force = False):
# def userStats(userID, force = False):
# def sendMessage(fro, to, message):
# def channelJoinSuccess(userID, chan):
# def channelInfo(chan):
# def channelInfoEnd():
# def channelKicked(chan):
# def userSilenced(userID):
# def addSpectator(userID):
# def removeSpectator(userID):
# def spectatorFrames(data):
# def noSongSpectator(userID):
# def fellowSpectatorJoined(userID):
# def fellowSpectatorLeft(userID):
# def createMatch(matchID):
# def updateMatch(matchID, censored = False):
# def matchStart(matchID):
# def disposeMatch(matchID):
# def matchJoinSuccess(matchID):
# def matchJoinFail():
# def changeMatchPassword(newPassword):
# def allPlayersLoaded():
# def playerSkipped(userID):
# def allPlayersSkipped():
# def matchFrames(slotID, data):
# def matchComplete():
# def playerFailed(slotID):
# def matchTransferHost():
# def matchAbort():
# def switchServer(address):
# def notification(message):
# def banchoRestart(msUntilReconnection):
# def rtx(message):
# 
# Path: helpers/chatHelper.py
# def joinChannel(userID = 0, channel = "", token = None, toIRC = True, force=False):
# def partChannel(userID = 0, channel = "", token = None, toIRC = True, kick = False, force=False):
# def sendMessage(fro = "", to = "", message = "", token = None, toIRC = True):
# def fixUsernameForBancho(username):
# def fixUsernameForIRC(username):
# def IRCConnect(username):
# def IRCDisconnect(username):
# def IRCJoinChannel(username, channel):
# def IRCPartChannel(username, channel):
# def IRCAway(username, message):
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# DATADOG_PREFIX = "peppy"


# Path: events/logoutEvent.py
import time
import json
from common.log import logUtils as log
from constants import serverPackets
from helpers import chatHelper as chat
from objects import glob




def handle(userToken, _=None, deleteToken=True):
	# get usertoken data
	userID = userToken.userID
	username = userToken.username
	requestToken = userToken.token

	# Big client meme here. If someone logs out and logs in right after,
	# the old logout packet will still be in the queue and will be sent to
	# the server, so we accept logout packets sent at least 5 seconds after login
	# if the user logs out before 5 seconds, he will be disconnected later with timeout check
	if int(time.time() - userToken.loginTime) >= 5 or userToken.irc:
		# Stop spectating
		userToken.stopSpectating()

		# Part matches
		userToken.leaveMatch()
		
		# Part all joined channels
		for i in userToken.joinedChannels:

			chat.partChannel(token=userToken, channel=i)