Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local curid = 0
- local bits = 0
- local nettable = {}
- -- FIXME: Put into separate PR
- function math.BitCount( num )
- if ( num == 0 ) then
- return 0
- end
- return math.floor( math.log( num, 2 ) ) + 1
- end
- if ( SERVER ) then
- achievements = {}
- util.AddNetworkString( "GMod_Achievements" )
- util.AddNetworkString( "GMod_Achievements_Sync" )
- util.AddNetworkString( "GMod_Achievements_New" )
- -- Shared functions --
- function achievements.Register( name, func --[[= nil]], update --[[= true]] )
- -- FIXME: Typecheck func
- if ( nettable[ name ] == nil ) then
- nettable[ name ] = curid
- if ( func != nil ) then
- nettable[ curid ] = func
- end
- bits = math.BitCount( curid )
- curid = curid + 1
- if ( update == nil || update ) then
- net.Start( "GMod_Achievements_New" )
- net.WriteString( name )
- net.Broadcast()
- end
- else
- nettable[ nettable[ name ] ] = func
- end
- end
- function achievements.Exists( name )
- return nettable[ name ] != nil
- end
- function achievements.GetFunction( name )
- local id = nettable[ name ]
- if ( id == nil ) then
- return nil
- end
- return nettable[ id ]
- end
- -- Server functions --
- function achievements.Call( name, plys --[[= player.GetAll()]], ... )
- -- FIXME: Typecheck plys
- local id = nettable[ name ]
- if ( id == nil ) then
- ErrorNoHalt( string.format( "Attempted to call unregistered achievement '%s'", name ) )
- return false
- end
- net.Start( "GMod_Achievements" )
- net.WriteUInt( id, bits )
- local func = nettable[ id ]
- if ( func != nil ) then
- func( ... )
- end
- if ( plys == nil ) then
- net.Broadcast()
- else
- net.Send( plys )
- end
- return true
- end
- function achievements.Sync( plys --[[= player.GetAll()]] )
- -- FIXME: Typecheck plys
- net.Start( "GMod_Achievements_Sync" )
- net.WriteUInt( curid, 32 )
- for id = 0, curid - 1 do
- net.WriteString( nettable[ id ] )
- end
- if ( plys == nil ) then
- net.Broadcast()
- else
- net.Send( plys )
- end
- end
- -- FIXME: https://github.com/Facepunch/garrysmod-requests/issues/718
- hook.Add( "PlayerInitialSpawn", "GMod_Achievements", function( ply )
- local id = "GMod_Achievements_" .. ply:SteamID64()
- hook.Add( "SetupMove", id, function( pply, _, cmd )
- if ( !ply:IsValid() ) then
- hook.Remove( "SetupMove", id )
- elseif ( pply == ply && cmd:IsForced() ) then
- achievements.Sync( ply )
- hook.Remove( "SetupMove", id )
- end
- end)
- end )
- else
- local funcs = {}
- -- Shared functions --
- function achievements.Register( name, func --[[= nil]] )
- -- FIXME: Typecheck func
- funcs[ name ] = func
- local id = nettable[ name ]
- if ( id ~= nil ) then
- nettable[ id ] = func
- end
- end
- function achievements.Exists( name )
- return nettable[ name ] != nil || funcs[ name ] != nil
- end
- function achievements.GetFunction( name )
- return funcs[ name ]
- end
- -- Client functions --
- function achievements.Pooled( name )
- return nettable[ name ] != nil
- end
- net.Receive( "GMod_Achievements", function()
- local func = nettable[ net.ReadUInt( bits ) ]
- if ( func != nil ) then
- func()
- end
- end )
- net.Receive( "GMod_Achievements_Sync", function()
- curid = net.ReadUInt( 32 )
- for id = 0, curid - 1 do
- local name = net.ReadString()
- nettable[ name ] = id
- nettable[ id ] = funcs[ name ]
- end
- end )
- net.Receive( "GMod_Achievements_New", function()
- bits = math.BitCount( curid )
- local name = net.ReadString()
- nettable[ name ] = curid
- nettable[ curid ] = funcs[ name ]
- curid = curid + 1
- end )
- end
- achievements.Register( "BalloonPopped", Either( CLIENT, achievements.BalloonPopped, nil ) )
- achievements.Register( "EatBall", Either( CLIENT, achievements.EatBall, nil ) )
- achievements.Register( "SpawnedNPC", Either( CLIENT, achievements.SpawnedNPC, nil ) )
- achievements.Register( "SpawnedProp", Either( CLIENT, achievements.SpawnedProp, nil ) )
- achievements.Register( "SpawnedRagdoll", Either( CLIENT, achievements.SpawnedRagdoll, nil ) )
- achievements.Register( "Remover", Either( CLIENT, function()
- for i = 0, net.ReadUInt( 32 ) do
- achievements.Remover()
- end
- end, function( count --[[= 1]] )
- if ( isnumber( count ) ) then
- net.WriteUInt( count, 32 )
- else
- net.WriteUInt( 1, 32 )
- end
- end ) )
Add Comment
Please, Sign In to add comment