Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -[[
- Player Shared Variables
- --]]
- require( "glon" );
- pipeworks.playerdata = {};
- if ( CLIENT ) then
- pipeworks.playerdata.cache = {};
- usermessage.Hook( "PlayerDataNetworkServerVar", function(bfr)
- local key = bfr:ReadString();
- local description = bfr:ReadString();
- local valuetype = bfr:ReadString();
- local value;
- if ( valuetype == "string" ) then
- value = bfr:ReadString();
- elseif( valuetype == "number" ) then
- value = bfr:ReadLong();
- elseif( valuetype == "boolean" ) then
- value = bfr:ReadBool();
- elseif( valuetype == "Vector" ) then
- value = bfr:ReadVector();
- elseif( valuetype == "Angle" ) then
- value = bfr:ReadAngle();
- elseif( valuetype == "table" ) then
- value = glon.decode(bfr:ReadString(table));
- end;
- pipeworks.playerdata.cache[key] = {};
- pipeworks.playerdata.cache[key].description = description;
- pipeworks.playerdata.cache[key].value = value;
- end);
- usermessage.Hook( "PlayerDataNetworkServerVarUpdate", function(bfr)
- local key = bfr:ReadString();
- local valuetype = bfr:ReadString();
- local value;
- if ( valuetype == "string" ) then
- value = bfr:ReadString();
- elseif( valuetype == "number" ) then
- value = bfr:ReadLong();
- elseif( valuetype == "boolean" ) then
- value = bfr:ReadBool();
- elseif( valuetype == "Vector" ) then
- value = bfr:ReadVector();
- elseif( valuetype == "Angle" ) then
- value = bfr:ReadAngle();
- elseif( valuetype == "table" ) then
- value = glon.decode(bfr:ReadString(table));
- end;
- pipeworks.playerdata.cache[key].value = value;
- end);
- usermessage.Hook( "PlayerDataSyncAll", function( bfr )
- local tbldecoded = glon.decode( bfr:ReadString() );
- pipeworks.playerdata.cache = tbldecoded;
- end);
- end;
- /*
- Create a player data key.
- */
- function pipeworks.playerdata.Create( player, strkey, strdescription, value )
- local valuetype = type(value);
- if ( SERVER ) then
- if ( !player:IsPlayer() ) then return; end;
- if ( !player.data ) then
- player.data = {};
- end;
- player.data[strkey] = { description = strdescription, value = value };
- umsg.Start( "PlayerDataNetworkServerVar", player );
- umsg.String( strkey );
- umsg.String( strdescription );
- if ( valuetype == "boolean" ) then
- umsg.String( "boolean" );
- umsg.Bool( value );
- elseif( valuetype == "number" ) then
- umsg.String( "number" );
- umsg.Long( value );
- elseif( valuetype == "Vector" ) then
- umsg.String( "Vector" );
- umsg.Vector( value );
- elseif( valuetype == "table" ) then
- umsg.String( "table" );
- local tblencoded = glon.encode(value);
- umsg.String( tblencoded );
- elseif ( valuetype == "Angle" ) then
- umsg.String( "Angle" );
- umsg.Angle( value );
- elseif( valuetype == "string" ) then
- umsg.String( "string" );
- umsg.String( value );
- elseif( valuetype == "Entity" ) then
- umsg.String( "Entity" );
- umsg.Entity( value );
- end;
- umsg.End();
- pipeworks.playerdata.Save(player); -- saving
- return;
- end;
- if ( CLIENT ) then
- pipeworks.playerdata.cache[strkey] = {};
- pipeworks.playerdata.cache[strkey].description = strdescription;
- pipeworks.playerdata.cache[strkey].value = value;
- end;
- end;
- /*
- Update a player data key.
- */
- function pipeworks.playerdata.Update( player, strkey, value )
- if ( SERVER ) then
- if ( !player:IsPlayer() ) then return; end;
- if ( !player.data[strkey] ) then return; end;
- player.data[strkey].value = value;
- local valuetype = type(value);
- pipeworks.playerdata.Save(player);
- umsg.Start( "PlayerDataNetworkServerVarUpdate", player );
- print( "Usermessage sent for player data update!" );
- umsg.String( strkey );
- if ( valuetype == "boolean" ) then
- umsg.String( "boolean" );
- umsg.Bool( value );
- elseif( valuetype == "number" ) then
- umsg.String( "number" );
- umsg.Long( value );
- elseif( valuetype == "Vector" ) then
- umsg.String( "Vector" );
- umsg.Vector( value );
- elseif( valuetype == "table" ) then
- umsg.String( "table" );
- local tblencoded = glon.encode(value);
- umsg.String( tblencoded );
- elseif ( valuetype == "Angle" ) then
- umsg.String( "Angle" );
- umsg.Angle( value );
- elseif( valuetype == "string" ) then
- umsg.String( "string" );
- umsg.String( value );
- elseif( valuetype == "Entity" ) then
- umsg.String( "Entity" );
- umsg.Entity( value );
- end;
- umsg.End();
- return;
- end;
- if ( CLIENT ) then
- pipeworks.playerdata.cache[strkey].value = value;
- end;
- end;
- if ( SERVER ) then
- /*
- Save the players current playerdata stored on their player object.
- */
- function pipeworks.playerdata.Save(player)
- if ( !player.data ) then return; end;
- player:SetPData( "PlayerDataStored", glon.encode(player.data) );
- end;
- /*
- Load the saved version of the player data onto the player object.
- */
- function pipeworks.playerdata.Load(player)
- if ( !player:GetPData("PlayerDataStored") ) then return; end;
- player.data = glon.decode(player:GetPData("PlayerDataStored"));
- end;
- /*
- Makes a copy of the player data stored on the player object over onto the clients store table.
- */
- function pipeworks.playerdata.SyncAllData(player)
- if ( !player.data ) then return; end;
- local dataencoded = glon.encode(player.data);
- umsg.Start("PlayerDataSyncAll", player );
- umsg.String(dataencoded);
- umsg.End();
- end;
- /*
- Executed in pipework's engine PlayerSpawn hook.
- */
- function pipeworks.playerdata.OnPlayerJoin(player)
- pipeworks.playerdata.Load(player);
- pipeworks.playerdata.SyncAllData(player);
- end;
- end;
- /*
- Get a specific value of a playerdata key.
- */
- function pipeworks.playerdata.GetValue( strkey, player)
- if ( SERVER ) then
- if ( !player.data ) then return; end;
- return player.data[strkey].value;
- end;
- if ( CLIENT ) then
- return pipeworks.playerdata.cache[strkey].value;
- end;
- end
- /*
- Get a players current playerdata stored on their player object.
- */
- function pipeworks.playerdata.GetTable( player )
- if ( SERVER ) then
- if ( !player:IsPlayer() or !player.data ) then return; end;
- return player.data;
- end;
- if ( CLIENT ) then
- return pipeworks.playerdata.cache;
- end;
- end;
- --[[
- engine functions.
- --]]
- pipeworks.engine = {};
- if ( CLIENT ) then
- pipeworks.engine.synced = false;
- pipeworks.engine.period_count = 0;
- pipeworks.engine.period_delay = 0.5;
- pipeworks.engine.period_tick = 0;
- usermessage.Hook( "EnginePlayerSetSynced", function(bfr)
- local value = bfr:ReadBool();
- if ( value == false ) then
- pipeworks.engine.period_count = 0;
- pipeworks.engine.period_tick = CurTime();
- end;
- pipeworks.engine.synced = value;
- end);
- usermessage.Hook( "EnginePlayerSetCanSpawn", function( bfr )
- local value = bfr:ReadBool();
- pipeworks.engine.CanSpawn = value;
- end);
- end;
- /*
- Set a player to be synced or unsynced.
- */
- function pipeworks.engine.PlayerSetSynced( player, bSynced )
- if ( SERVER ) then
- if ( !player:IsPlayer() ) then return; end;
- player.IsSynced = bSynced;
- umsg.Start( "EnginePlayerSetSynced", player );
- umsg.Bool( bSynced );
- umsg.End();
- if ( bSynced == false ) then
- player:SetColor( 0,0,0,0 );
- --player:Lock();
- else
- player:SetColor( 255,255,255,255 );
- --player:UnLock();
- end;
- return;
- end;
- if ( CLIENT ) then
- pipeworks.engine.synced = value;
- end;
- end;
- if ( SERVER ) then
- /*
- A callback to make sure the player is synced when setting a player to be synced/unsynced.
- */
- function pipeworks.engine.PlayerCanSync(player)
- if ( !player:IsPlayer() ) then return; end;
- return true;
- end
- /*
- This is the controller for the start up process, first
- it will set the player to unsynced, call the process, then when it's over and after the script
- is done being delayed we will set it so they are synced again.
- */
- function pipeworks.engine:OnPlayerSpawn( player )
- self.SetPlayerCanSpawn(player,false);
- self.PlayerSetSynced(player,false);
- self.PlayerSyncProcess(player);
- timer.Simple(5, self.PlayerSetSynced, player, true);
- --self.PlayerSetSynced(player,true);
- end;
- /*
- Put start-up shit in this.
- */
- function pipeworks.engine.PlayerSyncProcess(player)
- pipeworks.playerdata.OnPlayerJoin(player);
- end;
- /*
- Can a player spawn and be put in-game.
- */
- function pipeworks.engine.PlayerCanSpawn(player)
- if ( player.IsSynced and player.CanSpawn ) then
- player:Spawn();
- return true;
- else
- return false;
- end;
- end;
- end;
- /*
- Set a player so they can spawn/not spawn.
- */
- function pipeworks.engine.SetPlayerCanSpawn( player, bSpawn )
- if ( SERVER ) then
- -- No point in setting what is already there.
- if ( bSpawn == player.CanSpawn ) then return; end;
- player.CanSpawn = bSpawn;
- umsg.Start("EnginePlayerSetCanSpawn",player);
- umsg.Bool(bSpawn);
- umsg.End();
- return;
- end
- if ( CLIENT ) then
- -- No need to set what is set.
- if ( pipeworks.engine.CanSpawn == bSpawn ) then return; end;
- pipeworks.engine.CanSpawn = bSpawn;
- end;
- end;
- if ( SERVER ) then
- function GM:PlayerLoadout(player)
- end;
- function GM:PlayerInitialSpawn(player)
- player:StripWeapons();
- player:Lock();
- timer.Simple(0.3, player.KillSilent, player);
- pipeworks.engine:OnPlayerSpawn(player);
- end;
- function GM:PlayerDeathThink(player)
- return pipeworks.engine.PlayerCanSpawn(player);
- end;
- function GM:PlayerDeath( victim, inflicter, killer )
- print("Player Death");
- pipeworks.engine.SetPlayerCanSpawn(victim,false);
- end;
- end;
- if ( CLIENT ) then
- function GM:HUDShouldDraw( elementname )
- if(elementname == "CHudHealth") or (elementname == "CHudBattery") or (elementname == "CHudAmmo") or (elementname == "CHudSecondaryAmmo") then
- return false;
- end;
- return true;
- end;
- function GM:HUDPaint()
- if ( !pipeworks.engine.synced ) then
- surface.SetDrawColor( 0,0,0,255);
- surface.DrawRect(0,0, ScrW(), ScrH());
- if ( pipeworks.engine.period_count == 0 ) then
- draw.DrawText("PipeWorks is syncing data, please wait ", "MenuLarge", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
- if ( CurTime() - pipeworks.engine.period_tick > pipeworks.engine.period_delay ) then
- pipeworks.engine.period_count = 1;
- pipeworks.engine.period_tick = CurTime();
- end;
- elseif ( pipeworks.engine.period_count == 1 ) then
- draw.DrawText("PipeWorks is syncing data, please wait .", "MenuLarge", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
- if ( CurTime() - pipeworks.engine.period_tick > pipeworks.engine.period_delay ) then
- pipeworks.engine.period_count = 2;
- pipeworks.engine.period_tick = CurTime();
- end;
- elseif( pipeworks.engine.period_count == 2 ) then
- draw.DrawText("PipeWorks is syncing data, please wait . .", "MenuLarge", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
- if ( CurTime() - pipeworks.engine.period_tick > pipeworks.engine.period_delay ) then
- pipeworks.engine.period_count = 3;
- pipeworks.engine.period_tick = CurTime();
- end;
- elseif( pipeworks.engine.period_count == 3 ) then
- draw.DrawText("PipeWorks is syncing data, please wait . . .", "MenuLarge", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
- if ( CurTime() - pipeworks.engine.period_tick > pipeworks.engine.period_delay ) then
- pipeworks.engine.period_count = 0;
- pipeworks.engine.period_tick = CurTime();
- end;
- end;
- end
- end;
- end
Add Comment
Please, Sign In to add comment