# 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: constants/exceptions.py
# class periodicLoopException(Exception):
# 	pass
# 
# Path: events/logoutEvent.py
# def handle(userToken, _=None, deleteToken=True):
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# DATADOG_PREFIX = "peppy"
# 
# Path: objects/osuToken.py
# class token:
# 	def __init__(self, userID, token_ = None, ip ="", irc = False, timeOffset = 0, tournament = False):
# 	def enqueue(self, bytes_):
# 	def resetQueue(self):
# 	def joinChannel(self, channelObject):
# 	def partChannel(self, channelObject):
# 	def setLocation(self, latitude, longitude):
# 	def getLatitude(self):
# 	def getLongitude(self):
# 	def startSpectating(self, host):
# 	def stopSpectating(self):
# 	def updatePingTime(self):
# 	def joinMatch(self, matchID):
# 	def leaveMatch(self):
# 	def kick(self, message="You have been kicked from the server. Please login again.", reason="kick"):
# 	def silence(self, seconds = None, reason = "", author = 999):
# 	def spamProtection(self, increaseSpamRate = True):
# 	def isSilenced(self):
# 	def getSilenceSecondsLeft(self):
# 	def updateCachedStats(self):
# 	def checkRestricted(self):
# 	def checkBanned(self):
# 	def setRestricted(self):
# 	def resetRestricted(self):
# 	def joinStream(self, name):
# 	def leaveStream(self, name):
# 	def leaveAllStreams(self):
# 	def awayCheck(self, userID):
# 	def addMessageInBuffer(self, chan, message):
# 	def getMessagesBufferString(self):


import threading
import time
import redis
from common.ripple import userUtils
from common.log import logUtils as log
from common.sentry import sentry
from constants import serverPackets
from constants.exceptions import periodicLoopException
from events import logoutEvent
from objects import glob
from objects import osuToken




class tokenList:
	def __init__(self):
		self.tokens = {}
		self._lock = threading.Lock()

	def __enter__(self):
		self._lock.acquire()

	def __exit__(self, exc_type, exc_val, exc_tb):
		self._lock.release()

	def addToken(self, userID, ip = "", irc = False, timeOffset=0, tournament=False):
		"""
		Add a token object to tokens list

		:param userID: user id associated to that token
		:param ip: ip address of the client
		:param irc: if True, set this token as IRC client
		:param timeOffset: the time offset from UTC for this user. Default: 0.
		:param tournament: if True, flag this client as a tournement client. Default: True.
		:return: token object
		"""
		newToken = osuToken.token(userID, ip=ip, irc=irc, timeOffset=timeOffset, tournament=tournament)
		self.tokens[newToken.token] = newToken

# 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: constants/exceptions.py
# class periodicLoopException(Exception):
# 	pass
# 
# Path: events/logoutEvent.py
# def handle(userToken, _=None, deleteToken=True):
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# DATADOG_PREFIX = "peppy"
# 
# Path: objects/osuToken.py
# class token:
# 	def __init__(self, userID, token_ = None, ip ="", irc = False, timeOffset = 0, tournament = False):
# 	def enqueue(self, bytes_):
# 	def resetQueue(self):
# 	def joinChannel(self, channelObject):
# 	def partChannel(self, channelObject):
# 	def setLocation(self, latitude, longitude):
# 	def getLatitude(self):
# 	def getLongitude(self):
# 	def startSpectating(self, host):
# 	def stopSpectating(self):
# 	def updatePingTime(self):
# 	def joinMatch(self, matchID):
# 	def leaveMatch(self):
# 	def kick(self, message="You have been kicked from the server. Please login again.", reason="kick"):
# 	def silence(self, seconds = None, reason = "", author = 999):
# 	def spamProtection(self, increaseSpamRate = True):
# 	def isSilenced(self):
# 	def getSilenceSecondsLeft(self):
# 	def updateCachedStats(self):
# 	def checkRestricted(self):
# 	def checkBanned(self):
# 	def setRestricted(self):
# 	def resetRestricted(self):
# 	def joinStream(self, name):
# 	def leaveStream(self, name):
# 	def leaveAllStreams(self):
# 	def awayCheck(self, userID):
# 	def addMessageInBuffer(self, chan, message):
# 	def getMessagesBufferString(self):


# Path: objects/tokenList.py
import threading
import time
import redis
from common.ripple import userUtils
from common.log import logUtils as log
from common.sentry import sentry
from constants import serverPackets
from constants.exceptions import periodicLoopException
from events import logoutEvent
from objects import glob
from objects import osuToken





class tokenList:
	def __init__(self):
		self.tokens = {}
		self._lock = threading.Lock()

	def __enter__(self):
		self._lock.acquire()

	def __exit__(self, exc_type, exc_val, exc_tb):
		self._lock.release()

	def addToken(self, userID, ip = "", irc = False, timeOffset=0, tournament=False):
		"""
		Add a token object to tokens list

		:param userID: user id associated to that token
		:param ip: ip address of the client
		:param irc: if True, set this token as IRC client
		:param timeOffset: the time offset from UTC for this user. Default: 0.
		:param tournament: if True, flag this client as a tournement client. Default: True.
		:return: token object
		"""
		newToken = osuToken.token(userID, ip=ip, irc=irc, timeOffset=timeOffset, tournament=tournament)
		self.tokens[newToken.token] = newToken

		glob.redis.incr("ripple:online_users")