2016-04-19 17:40:59 +00:00
""" Contains functions used to write specific server packets to byte streams """
2016-10-02 20:48:14 +00:00
from common . constants import privileges
from common . ripple import userUtils
2016-05-18 17:12:46 +00:00
from constants import dataTypes
from constants import packetIDs
2016-10-02 20:48:14 +00:00
from constants import userRanks
from helpers import packetHelper
from objects import glob
2016-04-19 17:40:59 +00:00
2016-09-02 10:41:19 +00:00
""" Login errors packets """
2016-04-19 17:40:59 +00:00
def loginFailed ( ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userID , [ [ - 1 , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
def forceUpdate ( ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userID , [ [ - 2 , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
def loginBanned ( ) :
2016-09-02 15:16:22 +00:00
packets = packetHelper . buildPacket ( packetIDs . server_userID , [ [ - 1 , dataTypes . SINT32 ] ] )
2016-10-06 15:41:40 +00:00
packets + = notification ( " You are banned. You can appeal after one month since your ban by sending an email to support@ripple.moe from the email address you ' ve used to sign up. " )
2016-09-13 09:39:39 +00:00
return packets
def loginLocked ( ) :
packets = packetHelper . buildPacket ( packetIDs . server_userID , [ [ - 1 , dataTypes . SINT32 ] ] )
packets + = notification ( " Your account is locked. You can ' t log in, but your profile and scores are still visible from the website. If you want to unlock your account, send an email to support@ripple.moe from the email address you ' ve used to sign up. " )
2016-04-24 09:46:33 +00:00
return packets
2016-04-19 17:40:59 +00:00
def loginError ( ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userID , [ [ - 5 , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
def needSupporter ( ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userID , [ [ - 6 , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
2016-06-11 16:43:27 +00:00
def needVerification ( ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userID , [ [ - 8 , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
2016-09-02 10:41:19 +00:00
2016-04-19 17:40:59 +00:00
""" Login packets """
def userID ( uid ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userID , [ [ uid , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
def silenceEndTime ( seconds ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_silenceEnd , [ [ seconds , dataTypes . UINT32 ] ] )
2016-04-19 17:40:59 +00:00
def protocolVersion ( version = 19 ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_protocolVersion , [ [ version , dataTypes . UINT32 ] ] )
2016-04-19 17:40:59 +00:00
def mainMenuIcon ( icon ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_mainMenuIcon , [ [ icon , dataTypes . STRING ] ] )
2016-04-19 17:40:59 +00:00
2016-10-06 15:41:13 +00:00
def userSupporterGMT ( supporter , GMT , tournamentStaff ) :
2016-04-19 17:40:59 +00:00
result = 1
2016-09-02 15:45:10 +00:00
if supporter :
2016-10-05 21:28:26 +00:00
result | = userRanks . SUPPORTER
2016-09-02 15:45:10 +00:00
if GMT :
2016-10-05 21:28:26 +00:00
result | = userRanks . BAT
2016-10-06 15:41:13 +00:00
if tournamentStaff :
2016-10-05 21:28:26 +00:00
result | = userRanks . TOURNAMENT_STAFF
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_supporterGMT , [ [ result , dataTypes . UINT32 ] ] )
2016-04-19 17:40:59 +00:00
def friendList ( userID ) :
2016-10-02 20:48:14 +00:00
friends = userUtils . getFriendList ( userID )
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_friendsList , [ [ friends , dataTypes . INT_LIST ] ] )
2016-04-19 17:40:59 +00:00
def onlineUsers ( ) :
2016-07-03 18:51:19 +00:00
userIDs = [ ]
2016-04-19 17:40:59 +00:00
users = glob . tokens . tokens
2016-07-03 18:51:19 +00:00
# Create list with all connected (and not restricted) users
for _ , value in users . items ( ) :
2016-09-02 15:45:10 +00:00
if not value . restricted :
2016-07-03 18:51:19 +00:00
userIDs . append ( value . userID )
2016-04-19 17:40:59 +00:00
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userPresenceBundle , [ [ userIDs , dataTypes . INT_LIST ] ] )
2016-04-19 17:40:59 +00:00
""" Users packets """
def userLogout ( userID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userLogout , [ [ userID , dataTypes . SINT32 ] , [ 0 , dataTypes . BYTE ] ] )
2016-04-19 17:40:59 +00:00
2016-07-04 16:58:10 +00:00
def userPanel ( userID , force = False ) :
2016-07-03 18:51:19 +00:00
# Connected and restricted check
2016-04-19 17:40:59 +00:00
userToken = glob . tokens . getTokenFromUserID ( userID )
2016-10-05 21:28:26 +00:00
if userToken is None or ( ( userToken . restricted ) and not force ) :
2016-07-03 18:51:19 +00:00
return bytes ( )
# Get user data
2016-06-16 11:38:17 +00:00
username = userToken . username
2016-09-02 10:41:19 +00:00
timezone = 24 + userToken . timeOffset
2016-06-16 11:38:17 +00:00
country = userToken . country
gameRank = userToken . gameRank
2016-04-19 17:40:59 +00:00
latitude = userToken . getLatitude ( )
longitude = userToken . getLongitude ( )
# Get username color according to rank
# Only admins and normal users are currently supported
2016-10-05 21:28:26 +00:00
userRank = 0
2016-04-19 17:40:59 +00:00
if username == " FokaBot " :
2016-10-05 21:28:26 +00:00
userRank | = userRanks . MOD
2016-10-02 20:48:14 +00:00
elif userUtils . isInPrivilegeGroup ( userID , " community manager " ) :
2016-10-05 21:28:26 +00:00
userRank | = userRanks . MOD
2016-10-02 20:48:14 +00:00
elif userUtils . isInPrivilegeGroup ( userID , " developer " ) :
2016-10-05 21:28:26 +00:00
userRank | = userRanks . ADMIN
2016-09-02 10:41:19 +00:00
elif ( userToken . privileges & privileges . USER_DONOR ) > 0 :
2016-10-05 21:28:26 +00:00
userRank | = userRanks . SUPPORTER
2016-04-19 17:40:59 +00:00
else :
2016-10-05 21:28:26 +00:00
userRank | = userRanks . NORMAL
2016-04-19 17:40:59 +00:00
return packetHelper . buildPacket ( packetIDs . server_userPanel ,
[
2016-09-02 15:16:22 +00:00
[ userID , dataTypes . SINT32 ] ,
[ username , dataTypes . STRING ] ,
[ timezone , dataTypes . BYTE ] ,
[ country , dataTypes . BYTE ] ,
[ userRank , dataTypes . BYTE ] ,
[ longitude , dataTypes . FFLOAT ] ,
[ latitude , dataTypes . FFLOAT ] ,
[ gameRank , dataTypes . UINT32 ]
2016-04-19 17:40:59 +00:00
] )
2016-07-04 16:58:10 +00:00
def userStats ( userID , force = False ) :
2016-04-19 17:40:59 +00:00
# Get userID's token from tokens list
userToken = glob . tokens . getTokenFromUserID ( userID )
2016-10-05 21:28:26 +00:00
if userToken is None or ( ( userToken . restricted or userToken . irc or userToken . tournament ) and not force ) :
2016-07-14 10:37:07 +00:00
return bytes ( )
2016-09-02 10:41:19 +00:00
2016-04-19 17:40:59 +00:00
return packetHelper . buildPacket ( packetIDs . server_userStats ,
[
2016-09-02 15:16:22 +00:00
[ userID , dataTypes . UINT32 ] ,
[ userToken . actionID , dataTypes . BYTE ] ,
[ userToken . actionText , dataTypes . STRING ] ,
[ userToken . actionMd5 , dataTypes . STRING ] ,
[ userToken . actionMods , dataTypes . SINT32 ] ,
[ userToken . gameMode , dataTypes . BYTE ] ,
2016-09-13 09:56:53 +00:00
[ userToken . beatmapID , dataTypes . SINT32 ] ,
2016-09-02 15:16:22 +00:00
[ userToken . rankedScore , dataTypes . UINT64 ] ,
[ userToken . accuracy , dataTypes . FFLOAT ] ,
[ userToken . playcount , dataTypes . UINT32 ] ,
[ userToken . totalScore , dataTypes . UINT64 ] ,
[ userToken . gameRank , dataTypes . UINT32 ] ,
2016-10-02 21:11:18 +00:00
[ userToken . pp if 65535 > = userToken . pp > 0 else 0 , dataTypes . UINT16 ]
2016-04-19 17:40:59 +00:00
] )
""" Chat packets """
def sendMessage ( fro , to , message ) :
2016-09-02 10:41:19 +00:00
return packetHelper . buildPacket ( packetIDs . server_sendMessage , [
2016-09-02 15:16:22 +00:00
[ fro , dataTypes . STRING ] ,
[ message , dataTypes . STRING ] ,
[ to , dataTypes . STRING ] ,
2016-10-02 20:48:14 +00:00
[ userUtils . getID ( fro ) , dataTypes . SINT32 ]
2016-09-02 10:41:19 +00:00
] )
2016-04-19 17:40:59 +00:00
def channelJoinSuccess ( userID , chan ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_channelJoinSuccess , [ [ chan , dataTypes . STRING ] ] )
2016-04-19 17:40:59 +00:00
def channelInfo ( chan ) :
channel = glob . channels . channels [ chan ]
2016-09-02 10:41:19 +00:00
return packetHelper . buildPacket ( packetIDs . server_channelInfo , [
2016-09-02 15:16:22 +00:00
[ chan , dataTypes . STRING ] ,
[ channel . description , dataTypes . STRING ] ,
[ channel . getConnectedUsersCount ( ) , dataTypes . UINT16 ]
2016-09-02 10:41:19 +00:00
] )
2016-04-19 17:40:59 +00:00
def channelInfoEnd ( ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_channelInfoEnd , [ [ 0 , dataTypes . UINT32 ] ] )
2016-04-19 17:40:59 +00:00
def channelKicked ( chan ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_channelKicked , [ [ chan , dataTypes . STRING ] ] )
2016-04-19 17:40:59 +00:00
2016-06-07 20:46:31 +00:00
def userSilenced ( userID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_userSilenced , [ [ userID , dataTypes . UINT32 ] ] )
2016-06-07 20:46:31 +00:00
2016-04-19 17:40:59 +00:00
""" Spectator packets """
def addSpectator ( userID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_spectatorJoined , [ [ userID , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
def removeSpectator ( userID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_spectatorLeft , [ [ userID , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
def spectatorFrames ( data ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_spectateFrames , [ [ data , dataTypes . BBYTES ] ] )
2016-04-19 17:40:59 +00:00
def noSongSpectator ( userID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_spectatorCantSpectate , [ [ userID , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
2016-06-08 15:26:31 +00:00
def fellowSpectatorJoined ( userID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_fellowSpectatorJoined , [ [ userID , dataTypes . SINT32 ] ] )
2016-06-08 15:26:31 +00:00
def fellowSpectatorLeft ( userID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_fellowSpectatorLeft , [ [ userID , dataTypes . SINT32 ] ] )
2016-06-08 15:26:31 +00:00
2016-04-19 17:40:59 +00:00
""" Multiplayer Packets """
def createMatch ( matchID ) :
# Make sure the match exists
if matchID not in glob . matches . matches :
2016-10-08 18:34:17 +00:00
return bytes ( )
2016-04-19 17:40:59 +00:00
# Get match binary data and build packet
match = glob . matches . matches [ matchID ]
return packetHelper . buildPacket ( packetIDs . server_newMatch , match . getMatchData ( ) )
2016-10-05 21:28:26 +00:00
# TODO: Add match object argument to save some CPU
2016-04-19 17:40:59 +00:00
def updateMatch ( matchID ) :
# Make sure the match exists
if matchID not in glob . matches . matches :
2016-10-08 18:34:17 +00:00
return bytes ( )
2016-04-19 17:40:59 +00:00
# Get match binary data and build packet
match = glob . matches . matches [ matchID ]
return packetHelper . buildPacket ( packetIDs . server_updateMatch , match . getMatchData ( ) )
def matchStart ( matchID ) :
# Make sure the match exists
if matchID not in glob . matches . matches :
2016-10-08 18:34:17 +00:00
return bytes ( )
2016-04-19 17:40:59 +00:00
# Get match binary data and build packet
match = glob . matches . matches [ matchID ]
return packetHelper . buildPacket ( packetIDs . server_matchStart , match . getMatchData ( ) )
def disposeMatch ( matchID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_disposeMatch , [ [ matchID , dataTypes . UINT32 ] ] )
2016-04-19 17:40:59 +00:00
def matchJoinSuccess ( matchID ) :
# Make sure the match exists
if matchID not in glob . matches . matches :
2016-10-08 18:34:17 +00:00
return bytes ( )
2016-04-19 17:40:59 +00:00
# Get match binary data and build packet
match = glob . matches . matches [ matchID ]
data = packetHelper . buildPacket ( packetIDs . server_matchJoinSuccess , match . getMatchData ( ) )
return data
def matchJoinFail ( ) :
return packetHelper . buildPacket ( packetIDs . server_matchJoinFail )
def changeMatchPassword ( newPassword ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_matchChangePassword , [ [ newPassword , dataTypes . STRING ] ] )
2016-04-19 17:40:59 +00:00
def allPlayersLoaded ( ) :
return packetHelper . buildPacket ( packetIDs . server_matchAllPlayersLoaded )
def playerSkipped ( userID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_matchPlayerSkipped , [ [ userID , dataTypes . SINT32 ] ] )
2016-04-19 17:40:59 +00:00
def allPlayersSkipped ( ) :
return packetHelper . buildPacket ( packetIDs . server_matchSkip )
def matchFrames ( slotID , data ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_matchScoreUpdate , [ [ data [ 7 : 11 ] , dataTypes . BBYTES ] , [ slotID , dataTypes . BYTE ] , [ data [ 12 : ] , dataTypes . BBYTES ] ] )
2016-04-19 17:40:59 +00:00
def matchComplete ( ) :
return packetHelper . buildPacket ( packetIDs . server_matchComplete )
def playerFailed ( slotID ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_matchPlayerFailed , [ [ slotID , dataTypes . UINT32 ] ] )
2016-04-19 17:40:59 +00:00
def matchTransferHost ( ) :
return packetHelper . buildPacket ( packetIDs . server_matchTransferHost )
2016-09-02 10:41:19 +00:00
2016-04-19 17:40:59 +00:00
""" Other packets """
def notification ( message ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_notification , [ [ message , dataTypes . STRING ] ] )
2016-04-19 17:40:59 +00:00
def banchoRestart ( msUntilReconnection ) :
2016-09-02 15:16:22 +00:00
return packetHelper . buildPacket ( packetIDs . server_restart , [ [ msUntilReconnection , dataTypes . UINT32 ] ] )