Advertisement
Guest User

Untitled

a guest
Dec 18th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.94 KB | None | 0 0
  1. -- the table from the main thing, this infotables is only one library of BRP, a old library of a ton of roleplay useful features.
  2. BRPLb = {};
  3.  
  4. BRPLib.infotables = {};
  5.  
  6. -- storage
  7. BRPLib.infotables.tables = {};
  8. BRPLib.infotables.operations = {};
  9. BRPLib.infotables.operations.commands = {};
  10. BRPLib.infotables.players = {};
  11.  
  12. -- add a new infotable.
  13. function BRPLib.infotables:new( id, defaultinfo )
  14.     if ( self.tables[id] != nil ) then return end;
  15.    
  16.     self.tables[id] = {
  17.         default = defaultinfo,
  18.         operations = {}
  19.         };
  20. end
  21.  
  22. -- get a specific info table.
  23. function BRPLib.infotables:get( id )
  24.     if ( self.tables[id] == nil ) then return nil; end;
  25.     return self.tables[id];
  26. end
  27.  
  28. -- add a new infotable operation
  29. function BRPLib.infotables.operations:add( infotableid, operationid, operationFunc )
  30.     if ( BRPLib.infotables.tables[infotableid] == nil ) then return end;
  31.     if ( BRPLib.infotables.tables[infotableid].operations == nil ) then return end;
  32.     if ( BRPLib.infotables.tables[infotableid].operations[operationid] != nil ) then return end;
  33.    
  34.     BRPLib.infotables.tables[infotableid].operations[operationid] = operationFunc;
  35. end
  36.  
  37. -- call a infotable operation
  38. function BRPLib.infotables.operations:call( infotable, operation, ...)
  39.     if ( BRPLib.infotables.tables[infotable] == nil ) then return end;
  40.     if ( BRPLib.infotables.tables[infotable].operations[operation] == nil ) then return end;
  41.    
  42.     BRPLib.infotables.tables[infotable].operations[operation]( infotable, operation, ... );
  43. end
  44.  
  45. function BRPLib.infotables.operations:addcommand( infotable, operation, public, filter)
  46.     if ( public == false ) then
  47.    
  48.         concommand.Add( infotable .. "_" .. operation, function( p, c, a )
  49.             if ( filter(p) != true ) then return end;
  50.            
  51.             -- Convert all steam id's to players.
  52.             local na = a;
  53.             for k,v in pairs(na) do
  54.                 for _,p in pairs( player.GetAll() ) do
  55.                     if ( p:SteamID() == v ) then
  56.                         na[k] = p;
  57.                     end
  58.                 end
  59.             end
  60.            
  61.             BRPLib.infotables.operations:call( infotable, operation, unpack(na) );
  62.         end);
  63.        
  64.     else
  65.         concommand.Add( infotable .. "_" .. operation, function( p, c, a )
  66.        
  67.             local na = a;
  68.             for k,v in pairs(na) do
  69.                 for _,p in pairs( player.GetAll() ) do
  70.                     if ( p:SteamID() == v ) then
  71.                         na[k] = p;
  72.                     end
  73.                 end
  74.             end
  75.             BRPLib.infotables.operations:call( infotable, operation, unpack(na) );
  76.         end);
  77.     end
  78. end
  79.  
  80. -- save all infotables.
  81. function BRPLib.infotables:saveall(player)
  82.     if ( player.infotables == nil ) then return end;
  83.    
  84.     for id,info in pairs( player.infotables ) do
  85.         player:SetPData( id, glon.encode(info) );
  86.     end
  87. end
  88.  
  89. -- save a specific infotable
  90. function BRPLib.infotables:save(player,id)
  91.     if ( player.infotables == nil ) then return end;
  92.     if ( player.infotables[id] == nil ) then return end;
  93.    
  94.     player:SetPData( id, glon.encode(player.infotables[id]) );
  95. end
  96.  
  97. -- Reload the entire player's infotable.
  98. function BRPLib.infotables:refresh(player)
  99.     if ( player.infotables == nil ) then
  100.         player.infotables = {};
  101.     end
  102.    
  103.     for id,data in pairs( self.tables ) do
  104.         if ( self:getsaved(player,id) != nil ) then
  105.             player.infotables[id] = self:getsaved(player,id);
  106.         else
  107.             print("Could not refresh infotable '" .. id .. "' on player '" .. player:Nick() .. "->" .. player:SteamID() .. "'.");
  108.         end
  109.     end
  110. end
  111.  
  112. -- This gets a saved info table by id.
  113. function BRPLib.infotables:getsaved(player,infotable)
  114.     if ( self.tables[infotable] == nil ) then return end;
  115.    
  116.     return glon.decode( player:GetPData(infotable) ) or nil;
  117. end
  118.  
  119. -- This should be called when the player connects.
  120. function BRPLib.infotables:connect(player)
  121.  
  122.     player.infotables = {};
  123.    
  124.     for id,data in pairs( self.tables ) do
  125.    
  126.         if ( self:getsaved(player,id) != nil ) then
  127.             player.infotables[id] = self:getsaved(player,id);
  128.         else
  129.         player.infotables[id] = data.default;
  130.         end
  131.     end
  132.    
  133. end
  134.  
  135. function BRPLib.infotables:network(player,infotable)
  136.     if ( player.infotables[infotable] == nil ) then return end;
  137.    
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement