Guest User

Untitled

a guest
Jun 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.57 KB | None | 0 0
  1. -[[
  2.     Player Shared Variables
  3. --]]
  4.  
  5. require( "glon" );
  6.  
  7. pipeworks.playerdata = {};
  8.  
  9. if ( CLIENT ) then
  10.  
  11.     pipeworks.playerdata.cache = {};
  12.    
  13.    
  14.     usermessage.Hook( "PlayerDataNetworkServerVar", function(bfr)
  15.    
  16.         local key = bfr:ReadString();
  17.         local description = bfr:ReadString();
  18.         local valuetype = bfr:ReadString();
  19.         local value;
  20.        
  21.         if ( valuetype == "string" ) then
  22.             value = bfr:ReadString();
  23.         elseif( valuetype == "number" ) then
  24.             value = bfr:ReadLong();
  25.         elseif( valuetype == "boolean" ) then
  26.             value = bfr:ReadBool();
  27.         elseif( valuetype == "Vector" ) then
  28.             value = bfr:ReadVector();
  29.         elseif( valuetype == "Angle" ) then
  30.             value = bfr:ReadAngle();
  31.         elseif( valuetype == "table" ) then
  32.             value = glon.decode(bfr:ReadString(table));
  33.         end;
  34.        
  35.         pipeworks.playerdata.cache[key] = {};
  36.         pipeworks.playerdata.cache[key].description = description;
  37.         pipeworks.playerdata.cache[key].value = value;
  38.  
  39.     end);
  40.    
  41.     usermessage.Hook( "PlayerDataNetworkServerVarUpdate", function(bfr)
  42.    
  43.         local key = bfr:ReadString();
  44.         local valuetype = bfr:ReadString();
  45.         local value;
  46.        
  47.         if ( valuetype == "string" ) then
  48.             value = bfr:ReadString();
  49.         elseif( valuetype == "number" ) then
  50.             value = bfr:ReadLong();
  51.         elseif( valuetype == "boolean" ) then
  52.             value = bfr:ReadBool();
  53.         elseif( valuetype == "Vector" ) then
  54.             value = bfr:ReadVector();
  55.         elseif( valuetype == "Angle" ) then
  56.             value = bfr:ReadAngle();
  57.         elseif( valuetype == "table" ) then
  58.             value = glon.decode(bfr:ReadString(table));
  59.         end;
  60.        
  61.         pipeworks.playerdata.cache[key].value = value;
  62.     end);
  63.    
  64.     usermessage.Hook( "PlayerDataSyncAll", function( bfr )
  65.         local tbldecoded = glon.decode( bfr:ReadString() );
  66.        
  67.         pipeworks.playerdata.cache = tbldecoded;
  68.     end);
  69. end;
  70.  
  71.  
  72. /*
  73.     Create a player data key.
  74. */
  75. function pipeworks.playerdata.Create( player, strkey, strdescription, value )
  76.  
  77.         local valuetype = type(value);
  78.     if ( SERVER ) then
  79.    
  80.         if ( !player:IsPlayer() ) then return; end;
  81.        
  82.         if ( !player.data ) then
  83.             player.data = {};
  84.         end;
  85.    
  86.         player.data[strkey] = { description = strdescription, value = value };
  87.        
  88.         umsg.Start( "PlayerDataNetworkServerVar", player );
  89.                 umsg.String( strkey );
  90.                 umsg.String( strdescription );
  91.             if ( valuetype == "boolean" ) then
  92.                 umsg.String( "boolean" );
  93.                 umsg.Bool( value );
  94.             elseif( valuetype == "number" ) then
  95.                 umsg.String( "number" );
  96.                 umsg.Long( value );
  97.             elseif( valuetype == "Vector" ) then
  98.                 umsg.String( "Vector" );
  99.                 umsg.Vector( value );
  100.             elseif( valuetype == "table" ) then
  101.                 umsg.String( "table" );
  102.                 local tblencoded = glon.encode(value);
  103.                 umsg.String( tblencoded );
  104.             elseif ( valuetype == "Angle" ) then
  105.                 umsg.String( "Angle" );
  106.                 umsg.Angle( value );
  107.             elseif( valuetype == "string" ) then
  108.                 umsg.String( "string" );
  109.                 umsg.String( value );
  110.             elseif( valuetype == "Entity" ) then
  111.                 umsg.String( "Entity" );
  112.                 umsg.Entity( value );
  113.             end;
  114.         umsg.End();
  115.        
  116.         pipeworks.playerdata.Save(player); -- saving
  117.         return;
  118.     end;
  119.    
  120.     if ( CLIENT ) then
  121.         pipeworks.playerdata.cache[strkey] = {};
  122.         pipeworks.playerdata.cache[strkey].description = strdescription;
  123.         pipeworks.playerdata.cache[strkey].value = value;
  124.     end;
  125. end;
  126.  
  127.  
  128. /*
  129.     Update a player data key.
  130. */
  131. function pipeworks.playerdata.Update( player, strkey, value )
  132.    
  133.     if ( SERVER ) then
  134.         if ( !player:IsPlayer() ) then return; end;
  135.         if ( !player.data[strkey] ) then return; end;
  136.        
  137.         player.data[strkey].value = value;
  138.        
  139.         local valuetype = type(value);
  140.        
  141.         pipeworks.playerdata.Save(player);
  142.        
  143.         umsg.Start( "PlayerDataNetworkServerVarUpdate", player );
  144.             print( "Usermessage sent for player data update!" );
  145.            
  146.                 umsg.String( strkey );
  147.                
  148.             if ( valuetype == "boolean" ) then
  149.                 umsg.String( "boolean" );
  150.                 umsg.Bool( value );
  151.             elseif( valuetype == "number" ) then
  152.                 umsg.String( "number" );
  153.                 umsg.Long( value );
  154.             elseif( valuetype == "Vector" ) then
  155.                 umsg.String( "Vector" );
  156.                 umsg.Vector( value );
  157.             elseif( valuetype == "table" ) then
  158.                 umsg.String( "table" );
  159.                 local tblencoded = glon.encode(value);
  160.                 umsg.String( tblencoded );
  161.             elseif ( valuetype == "Angle" ) then
  162.                 umsg.String( "Angle" );
  163.                 umsg.Angle( value );
  164.             elseif( valuetype == "string" ) then
  165.                 umsg.String( "string" );
  166.                 umsg.String( value );
  167.             elseif( valuetype == "Entity" ) then
  168.                 umsg.String( "Entity" );
  169.                 umsg.Entity( value );
  170.             end;
  171.         umsg.End();
  172.        
  173.         return;
  174.     end;
  175.    
  176.     if ( CLIENT ) then
  177.         pipeworks.playerdata.cache[strkey].value = value;
  178.     end;
  179. end;
  180.  
  181. if ( SERVER ) then
  182.  
  183.  
  184. /*
  185.     Save the players current playerdata stored on their player object.
  186. */
  187. function pipeworks.playerdata.Save(player)
  188.     if ( !player.data ) then return; end;
  189.    
  190.     player:SetPData( "PlayerDataStored", glon.encode(player.data) );
  191. end;
  192.  
  193.  
  194. /*
  195.     Load the saved version of the player data onto the player object.
  196. */
  197. function pipeworks.playerdata.Load(player)
  198.     if ( !player:GetPData("PlayerDataStored") ) then return; end;
  199.    
  200.     player.data = glon.decode(player:GetPData("PlayerDataStored"));
  201. end;
  202.  
  203.  
  204. /*
  205.     Makes a copy of the player data stored on the player object over onto the clients store table.
  206. */
  207. function pipeworks.playerdata.SyncAllData(player)
  208.     if ( !player.data ) then return; end;
  209.    
  210.     local dataencoded = glon.encode(player.data);
  211.    
  212.     umsg.Start("PlayerDataSyncAll", player );
  213.         umsg.String(dataencoded);
  214.     umsg.End();
  215. end;
  216.  
  217.  
  218. /*
  219.     Executed in pipework's engine PlayerSpawn hook.
  220. */
  221. function pipeworks.playerdata.OnPlayerJoin(player)
  222.  
  223.     pipeworks.playerdata.Load(player);
  224.     pipeworks.playerdata.SyncAllData(player);
  225. end;
  226.  
  227. end;
  228.  
  229.  
  230. /*
  231.     Get a specific value of a playerdata key.
  232. */
  233. function pipeworks.playerdata.GetValue( strkey, player)
  234.     if ( SERVER ) then
  235.         if ( !player.data ) then return; end;
  236.         return player.data[strkey].value;
  237.     end;
  238.    
  239.     if ( CLIENT ) then
  240.         return pipeworks.playerdata.cache[strkey].value;
  241.     end;
  242. end
  243.  
  244.  
  245. /*
  246.     Get a players current playerdata stored on their player object.
  247. */
  248. function pipeworks.playerdata.GetTable( player )
  249.     if ( SERVER ) then
  250.         if ( !player:IsPlayer() or !player.data ) then return; end;
  251.         return player.data;
  252.     end;
  253.    
  254.     if ( CLIENT ) then
  255.         return pipeworks.playerdata.cache;
  256.     end;
  257. end;
  258.  
  259.  
  260. --[[
  261.     engine functions.
  262. --]]
  263.  
  264.  
  265. pipeworks.engine = {};
  266.  
  267. if ( CLIENT ) then
  268.  
  269.  
  270.     pipeworks.engine.synced = false;
  271.     pipeworks.engine.period_count = 0;
  272.     pipeworks.engine.period_delay = 0.5;
  273.     pipeworks.engine.period_tick = 0;
  274.    
  275.    
  276.     usermessage.Hook( "EnginePlayerSetSynced", function(bfr)
  277.         local value = bfr:ReadBool();
  278.        
  279.         if ( value == false ) then
  280.             pipeworks.engine.period_count = 0;
  281.             pipeworks.engine.period_tick = CurTime();
  282.         end;
  283.        
  284.         pipeworks.engine.synced = value;
  285.        
  286.     end);
  287.    
  288.     usermessage.Hook( "EnginePlayerSetCanSpawn", function( bfr )
  289.         local value = bfr:ReadBool();
  290.        
  291.         pipeworks.engine.CanSpawn = value;
  292.     end);
  293. end;
  294.  
  295.  
  296. /*
  297.     Set a player to be synced or unsynced.
  298. */
  299. function pipeworks.engine.PlayerSetSynced( player, bSynced )
  300.  
  301.     if ( SERVER ) then
  302.         if ( !player:IsPlayer() ) then return; end;
  303.        
  304.         player.IsSynced = bSynced;
  305.        
  306.         umsg.Start( "EnginePlayerSetSynced", player );
  307.             umsg.Bool( bSynced );
  308.         umsg.End();
  309.        
  310.         if ( bSynced == false ) then
  311.             player:SetColor( 0,0,0,0 );
  312.             --player:Lock();
  313.         else
  314.             player:SetColor( 255,255,255,255 );
  315.             --player:UnLock();
  316.         end;
  317.    
  318.         return;
  319.     end;
  320.    
  321.     if ( CLIENT ) then
  322.         pipeworks.engine.synced = value;
  323.     end;
  324. end;
  325.  
  326.  
  327.  
  328. if ( SERVER ) then
  329.  
  330.  
  331. /*
  332.     A callback to make sure the player is synced when setting a player to be synced/unsynced.
  333. */
  334. function pipeworks.engine.PlayerCanSync(player)
  335.     if ( !player:IsPlayer() ) then return; end;
  336.    
  337.     return true;
  338. end
  339.  
  340.  
  341. /*
  342.     This is the controller for the start up process, first
  343.     it will set the player to unsynced, call the process, then when it's over and after the script
  344.     is done being delayed we will set it so they are synced again.
  345. */
  346. function pipeworks.engine:OnPlayerSpawn( player )
  347.    
  348.     self.SetPlayerCanSpawn(player,false);
  349.    
  350.     self.PlayerSetSynced(player,false);
  351.    
  352.     self.PlayerSyncProcess(player);
  353.    
  354.     timer.Simple(5, self.PlayerSetSynced, player, true);
  355.    
  356.     --self.PlayerSetSynced(player,true);
  357. end;
  358.  
  359.  
  360. /*
  361.     Put start-up shit in this.
  362. */
  363. function pipeworks.engine.PlayerSyncProcess(player)
  364.     pipeworks.playerdata.OnPlayerJoin(player);
  365. end;
  366.  
  367.  
  368. /*
  369.     Can a player spawn and be put in-game.
  370. */
  371. function pipeworks.engine.PlayerCanSpawn(player)
  372.     if ( player.IsSynced and player.CanSpawn ) then
  373.    
  374.         player:Spawn();
  375.         return true;
  376.     else
  377.         return false;
  378.     end;
  379. end;
  380.  
  381. end;
  382.  
  383.  
  384. /*
  385.     Set a player so they can spawn/not spawn.
  386. */
  387. function pipeworks.engine.SetPlayerCanSpawn( player, bSpawn )
  388.  
  389.    
  390.     if ( SERVER ) then
  391.         -- No point in setting what is already there.
  392.         if ( bSpawn == player.CanSpawn ) then return; end;
  393.    
  394.         player.CanSpawn = bSpawn;
  395.        
  396.         umsg.Start("EnginePlayerSetCanSpawn",player);
  397.             umsg.Bool(bSpawn);
  398.         umsg.End();
  399.        
  400.         return;
  401.     end
  402.    
  403.     if ( CLIENT ) then
  404.         -- No need to set what is set.
  405.         if ( pipeworks.engine.CanSpawn == bSpawn ) then return; end;
  406.        
  407.         pipeworks.engine.CanSpawn = bSpawn;
  408.     end;
  409. end;
  410.  
  411.  
  412. if ( SERVER ) then
  413.  
  414. function GM:PlayerLoadout(player)
  415. end;
  416.  
  417. function GM:PlayerInitialSpawn(player)
  418.         player:StripWeapons();
  419.         player:Lock();
  420.        
  421.         timer.Simple(0.3, player.KillSilent, player);
  422.         pipeworks.engine:OnPlayerSpawn(player);
  423. end;
  424.  
  425. function GM:PlayerDeathThink(player)
  426.     return pipeworks.engine.PlayerCanSpawn(player);
  427. end;
  428.  
  429.  
  430. function GM:PlayerDeath( victim, inflicter, killer )
  431.     print("Player Death");
  432.    
  433.     pipeworks.engine.SetPlayerCanSpawn(victim,false);
  434. end;
  435.  
  436. end;
  437.  
  438. if ( CLIENT ) then
  439.  
  440.  
  441. function GM:HUDShouldDraw( elementname )
  442.     if(elementname == "CHudHealth") or (elementname == "CHudBattery") or (elementname == "CHudAmmo") or (elementname == "CHudSecondaryAmmo") then
  443.         return false;
  444.     end;
  445.    
  446.     return true;
  447. end;
  448.  
  449.  
  450. function GM:HUDPaint()
  451.     if ( !pipeworks.engine.synced ) then
  452.         surface.SetDrawColor( 0,0,0,255);
  453.         surface.DrawRect(0,0, ScrW(), ScrH());
  454.        
  455.         if ( pipeworks.engine.period_count == 0 ) then
  456.        
  457.             draw.DrawText("PipeWorks is syncing data, please wait ", "MenuLarge", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
  458.            
  459.             if ( CurTime() - pipeworks.engine.period_tick > pipeworks.engine.period_delay ) then
  460.                 pipeworks.engine.period_count = 1;
  461.                 pipeworks.engine.period_tick = CurTime();
  462.             end;
  463.            
  464.         elseif ( pipeworks.engine.period_count == 1 ) then
  465.             draw.DrawText("PipeWorks is syncing data, please wait .", "MenuLarge", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
  466.            
  467.             if ( CurTime() - pipeworks.engine.period_tick > pipeworks.engine.period_delay ) then
  468.                 pipeworks.engine.period_count = 2;
  469.                 pipeworks.engine.period_tick = CurTime();
  470.             end;
  471.            
  472.         elseif( pipeworks.engine.period_count == 2 ) then
  473.             draw.DrawText("PipeWorks is syncing data, please wait . .", "MenuLarge", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
  474.            
  475.             if ( CurTime() - pipeworks.engine.period_tick > pipeworks.engine.period_delay ) then
  476.                 pipeworks.engine.period_count = 3;
  477.                 pipeworks.engine.period_tick = CurTime();
  478.             end;
  479.            
  480.         elseif( pipeworks.engine.period_count == 3 ) then
  481.             draw.DrawText("PipeWorks is syncing data, please wait . . .", "MenuLarge", ScrW() / 2, ScrH() / 2, Color(255, 255, 255, 255),TEXT_ALIGN_CENTER)
  482.             if ( CurTime() - pipeworks.engine.period_tick > pipeworks.engine.period_delay ) then
  483.                 pipeworks.engine.period_count = 0;
  484.                 pipeworks.engine.period_tick = CurTime();
  485.             end;
  486.         end;
  487.     end
  488. end;
  489.  
  490. end
Add Comment
Please, Sign In to add comment