.BANCHO. Switched from tornado to bottle + gevent, code cleaning

This commit is contained in:
Nyo
2016-08-17 16:41:05 +02:00
parent 0e74e5c1ce
commit 7910291b77
32 changed files with 463 additions and 787 deletions

View File

@@ -8,14 +8,14 @@ class banchoConfig:
config = {"banchoMaintenance": False, "freeDirect": True, "menuIcon": "", "loginNotification": ""}
def __init__(self, __loadFromDB = True):
def __init__(self, loadFromDB = True):
"""
Initialize a banchoConfig object (and load bancho_settings from db)
[__loadFromDB -- if True, load values from db. If False, don't load values. Default: True]
[loadFromDB -- if True, load values from db. If False, don't load values. Default: True]
"""
if __loadFromDB:
if loadFromDB:
try:
self.loadSettings()
except:
@@ -31,12 +31,12 @@ class banchoConfig:
self.config["menuIcon"] = glob.db.fetch("SELECT value_string FROM bancho_settings WHERE name = 'menu_icon'")["value_string"]
self.config["loginNotification"] = glob.db.fetch("SELECT value_string FROM bancho_settings WHERE name = 'login_notification'")["value_string"]
def setMaintenance(self, __maintenance):
def setMaintenance(self, maintenance):
"""
Turn on/off bancho maintenance mode. Write new value to db too
__maintenance -- if True, turn on maintenance mode. If false, turn it off
maintenance -- if True, turn on maintenance mode. If false, turn it off
"""
self.config["banchoMaintenance"] = __maintenance
glob.db.execute("UPDATE bancho_settings SET value_int = %s WHERE name = 'bancho_maintenance'", [int(__maintenance)])
self.config["banchoMaintenance"] = maintenance
glob.db.execute("UPDATE bancho_settings SET value_int = %s WHERE name = 'bancho_maintenance'", [int(maintenance)])

View File

@@ -5,22 +5,22 @@ class channel:
A chat channel
"""
def __init__(self, __name, __description, __publicRead, __publicWrite, temp, hidden):
def __init__(self, name, description, publicRead, publicWrite, temp, hidden):
"""
Create a new chat channel object
__name -- channel name
__description -- channel description
__publicRead -- bool, if true channel can be read by everyone, if false it can be read only by mods/admins
__publicWrite -- bool, same as public read but relative to write permissions
name -- channel name
description -- channel description
publicRead -- bool, if true channel can be read by everyone, if false it can be read only by mods/admins
publicWrite -- bool, same as public read but relative to write permissions
temp -- if True, channel will be deleted when there's no one in the channel
hidden -- if True, channel won't be shown in channels list
"""
self.name = __name
self.description = __description
self.publicRead = __publicRead
self.publicWrite = __publicWrite
self.name = name
self.description = description
self.publicRead = publicRead
self.publicWrite = publicWrite
self.moderated = False
self.temp = temp
self.connectedUsers = [999] # Fokabot is always connected to every channels (otherwise it doesn't show up in IRC users list)
@@ -34,26 +34,26 @@ class channel:
self.clientName = "#multiplayer"
def userJoin(self, __userID):
def userJoin(self, userID):
"""
Add a user to connected users
__userID -- user ID that joined the channel
userID -- user ID that joined the channel
"""
if __userID not in self.connectedUsers:
self.connectedUsers.append(__userID)
if userID not in self.connectedUsers:
self.connectedUsers.append(userID)
def userPart(self, __userID):
def userPart(self, userID):
"""
Remove a user from connected users
__userID -- user ID that left the channel
userID -- user ID that left the channel
"""
if __userID in self.connectedUsers:
self.connectedUsers.remove(__userID)
if userID in self.connectedUsers:
self.connectedUsers.remove(userID)
# Remove temp channels if empty or there's only fokabot connected
l = len(self.connectedUsers)

View File

@@ -56,6 +56,7 @@ class channelList:
self.channels[name] = channel.channel(name, "Chat", True, True, True, True)
log.info("Created temp channel {}".format(name))
def removeChannel(self, name):
"""
Removes a channel from channels list

View File

@@ -12,23 +12,17 @@ npRegex = re.compile("^https?:\\/\\/osu\\.ppy\\.sh\\/b\\/(\\d*)")
def connect():
"""Add FokaBot to connected users and send userpanel/stats packet to everyone"""
token = glob.tokens.addToken(999)
token.actionID = actions.idle
glob.tokens.enqueueAll(serverPackets.userPanel(999))
####glob.tokens.enqueueAll(serverPackets.userStats(999))
glob.tokens.enqueueAll(serverPackets.userStats(999))
# NOTE: Debug thing to set all users as connected
#users = glob.db.fetchAll("SELECT id FROM users")
#for i in users:
# t = glob.tokens.addToken(i["id"])
# t.actionID = actions.idle
def disconnect():
"""Remove FokaBot from connected users"""
glob.tokens.deleteToken(glob.tokens.getTokenFromUserID(999))
def fokabotResponse(fro, chan, message):
"""
Check if a message has triggered fokabot (and return its response)
@@ -39,7 +33,6 @@ def fokabotResponse(fro, chan, message):
return -- fokabot's response string or False
"""
for i in fokabotCommands.commands:
# Loop though all commands
#if i["trigger"] in message:

View File

@@ -20,7 +20,6 @@ tokens = tokenList.tokenList()
channels = channelList.channelList()
matches = matchList.matchList()
restarting = False
pool = None
fLocks = fileLocks.fileLocks()
verifiedCache = {}
cloudflare = False

View File

@@ -1,24 +0,0 @@
'''
import threading
class task:
def __init__(self, function, args = (), kwargs = {}):
self.function = function
self.args = args
self.kwargs = kwargs
class logThread:
def __init__(self):
self.thread = threading.Thread()
self.queue = []
def enqueue(self, function, args = (), kwargs = {}):
self.queue.append(task(function, args, kwargs))
def run(self):
for i in self.queue:
self.thread = threading.Thread(i.function, i.args, i.kwargs)
self.thread.run()
self.thread.join()
self.queue = []
'''

View File

@@ -29,29 +29,29 @@ class match:
matchModMode = matchModModes.normal
seed = 0
def __init__(self, __matchID, __matchName, __matchPassword, __beatmapID, __beatmapName, __beatmapMD5, __gameMode, __hostUserID):
def __init__(self, matchID, matchName, matchPassword, beatmapID, beatmapName, beatmapMD5, gameMode, hostUserID):
"""
Create a new match object
__matchID -- match progressive identifier
__matchName -- match name, string
__matchPassword -- match md5 password. Leave empty for no password
__beatmapID -- beatmap ID
__beatmapName -- beatmap name, string
__beatmapMD5 -- beatmap md5 hash, string
__gameMode -- game mode ID. See gameModes.py
__hostUserID -- user id of the host
matchID -- match progressive identifier
matchName -- match name, string
matchPassword -- match md5 password. Leave empty for no password
beatmapID -- beatmap ID
beatmapName -- beatmap name, string
beatmapMD5 -- beatmap md5 hash, string
gameMode -- game mode ID. See gameModes.py
hostUserID -- user id of the host
"""
self.matchID = __matchID
self.matchID = matchID
self.inProgress = False
self.mods = 0
self.matchName = __matchName
self.matchPassword = __matchPassword
self.beatmapID = __beatmapID
self.beatmapName = __beatmapName
self.beatmapMD5 = __beatmapMD5
self.hostUserID = __hostUserID
self.gameMode = __gameMode
self.matchName = matchName
self.matchPassword = matchPassword
self.beatmapID = beatmapID
self.beatmapName = beatmapName
self.beatmapMD5 = beatmapMD5
self.hostUserID = hostUserID
self.gameMode = gameMode
self.matchScoringTypes = matchScoringTypes.score # default values
self.matchTeamType = matchTeamTypes.headToHead # default value
self.matchModMode = matchModModes.normal # default value
@@ -618,8 +618,6 @@ class match:
self.setSlot(slotID, None, newTeam)
self.sendUpdate()
def sendUpdate(self):
# Send to users in room
for i in range(0,16):

View File

@@ -13,50 +13,50 @@ class matchList:
self.usersInLobby = []
self.lastID = 1
def createMatch(self, __matchName, __matchPassword, __beatmapID, __beatmapName, __beatmapMD5, __gameMode, __hostUserID):
def createMatch(self, matchName, matchPassword, beatmapID, beatmapName, beatmapMD5, gameMode, hostUserID):
"""
Add a new match to matches list
__matchName -- match name, string
__matchPassword -- match md5 password. Leave empty for no password
__beatmapID -- beatmap ID
__beatmapName -- beatmap name, string
__beatmapMD5 -- beatmap md5 hash, string
__gameMode -- game mode ID. See gameModes.py
__hostUserID -- user id of who created the match
matchName -- match name, string
matchPassword -- match md5 password. Leave empty for no password
beatmapID -- beatmap ID
beatmapName -- beatmap name, string
beatmapMD5 -- beatmap md5 hash, string
gameMode -- game mode ID. See gameModes.py
hostUserID -- user id of who created the match
return -- match ID
"""
# Add a new match to matches list
matchID = self.lastID
self.lastID+=1
self.matches[matchID] = match.match(matchID, __matchName, __matchPassword, __beatmapID, __beatmapName, __beatmapMD5, __gameMode, __hostUserID)
self.matches[matchID] = match.match(matchID, matchName, matchPassword, beatmapID, beatmapName, beatmapMD5, gameMode, hostUserID)
return matchID
def lobbyUserJoin(self, __userID):
def lobbyUserJoin(self, userID):
"""
Add userID to users in lobby
__userID -- user who joined mp lobby
userID -- user who joined mp lobby
"""
# Make sure the user is not already in mp lobby
if __userID not in self.usersInLobby:
if userID not in self.usersInLobby:
# We don't need to join #lobby, client will automatically send a packet for it
self.usersInLobby.append(__userID)
self.usersInLobby.append(userID)
def lobbyUserPart(self, __userID):
def lobbyUserPart(self, userID):
"""
Remove userID from users in lobby
__userID -- user who left mp lobby
userID -- user who left mp lobby
"""
# Make sure the user is in mp lobby
if __userID in self.usersInLobby:
if userID in self.usersInLobby:
# Part lobby and #lobby channel
self.usersInLobby.remove(__userID)
self.usersInLobby.remove(userID)
def disposeMatch(self, __matchID):

View File

@@ -35,11 +35,11 @@ class token:
"""
def __init__(self, __userID, token = None, ip = "", irc = False, timeOffset = 0):
def __init__(self, userID, token = None, ip = "", irc = False, timeOffset = 0):
"""
Create a token object and set userID and token
__userID -- user associated to this token
userID -- user associated to this token
token -- if passed, set token to that value
if not passed, token will be generated
ip -- client ip. optional.
@@ -47,7 +47,7 @@ class token:
"""
# Set stuff
self.userID = __userID
self.userID = userID
self.username = userHelper.getUsername(self.userID)
self.privileges = userHelper.getPrivileges(self.userID)
self.admin = userHelper.isInPrivilegeGroup(self.userID, "developer") or userHelper.isInPrivilegeGroup(self.userID, "community manager")
@@ -102,12 +102,6 @@ class token:
if ip != "":
userHelper.saveBanchoSession(self.userID, self.ip)
# If we are restricted, send message from FokaBot to user
# NOTE: Sent later
#if self.restricted == True:
# self.setRestricted()
def enqueue(self, __bytes):
"""
Add bytes (packets) to queue
@@ -164,11 +158,11 @@ class token:
return self.location[1]
def startSpectating(self, __userID):
"""Set the spectating user to __userID
def startSpectating(self, userID):
"""Set the spectating user to userID
__userID -- target userID"""
self.spectating = __userID
userID -- target userID"""
self.spectating = userID
def stopSpectating(self):
@@ -176,24 +170,24 @@ class token:
self.spectating = 0
def addSpectator(self, __userID):
"""Add __userID to our spectators
def addSpectator(self, userID):
"""Add userID to our spectators
userID -- new spectator userID"""
# Add userID to spectators if not already in
if __userID not in self.spectators:
self.spectators.append(__userID)
if userID not in self.spectators:
self.spectators.append(userID)
def removeSpectator(self, __userID):
"""Remove __userID from our spectators
def removeSpectator(self, userID):
"""Remove userID from our spectators
userID -- old spectator userID"""
# Remove spectator
if __userID in self.spectators:
self.spectators.remove(__userID)
if userID in self.spectators:
self.spectators.remove(userID)
def setCountry(self, __countryID):