Advertisement
Guest User

Untitled

a guest
Nov 4th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.50 KB | None | 0 0
  1. -------------------
  2. -- Money library.
  3. -- I guess you could call it a library?
  4. -- By LauScript
  5. -------------------
  6.  
  7. -- glon module
  8. require("glon");
  9.  
  10. -- The main table.
  11. currency = {};
  12.  
  13. -- A sub table for config vars.
  14. currency.config = {};
  15. currency.config["DebugMode"] = true;
  16. currency.config["DefaultEvents"] = true;
  17.  
  18. if ( SERVER ) then
  19. -- For storing shit.
  20. currency.cache = {};
  21. currency.cache.types = {};
  22. currency.cache.events = {};
  23. end
  24. -- for later use.
  25. local META_ENTITY = FindMetaTable("Entity");
  26. local META_PLAYER = FindMetaTable("Player");
  27.  
  28. -- Just a quick lil' func.
  29. local function DebugPrint( text )
  30.    
  31.     -- Make sure debug is enabled.
  32.     if ( currency.config["DebugMode"] != true ) then return end;
  33.    
  34.     -- Print the text.
  35.     print( "Currency Debug: " .. text );
  36. end
  37.  
  38.  
  39. if ( SERVER ) then
  40. -- A way to add new currency types.
  41. function currency:AddNew( strName, strSymbol, strPlural )
  42.    
  43.     -- nil checks.
  44.     if ( strName == nil ) then return end;
  45.     if ( strSymbol == nil ) then return end;
  46.     if ( strPlural == nil ) then
  47.         strPlural = strName;
  48.     end
  49.    
  50.     -- Length check.
  51.     if ( string.len( strName ) < 1 ) then
  52.         print("Could not add new currency, name was too short.");
  53.         return;
  54.     end
  55.    
  56.     -- Store it.
  57.     self.cache.types[ strName ] = { symbol = strSymbol, plural = strPlural };
  58.     DebugPrint( "Currency created with values:\nName: " .. strName .. "\nSymbol: " .. strSymbol .. "\nPlural: " .. strPlural );
  59. end
  60.  
  61. function currency:getTable( strCurrency )
  62.     if ( strCurrency == nil ) then return end;
  63.     if ( self.cache.types[strCurrency] == nil ) then return end;
  64.    
  65.     return self.cache.types[strCurrency];
  66. end
  67.  
  68. function currency:addEvent( strEvent, strRecord )
  69.     if ( strEvent == nil ) then return end;
  70.     if ( strRecord == nil ) then return end;
  71.    
  72.     if ( self.cache.events[ strEvent ] == nil ) then
  73.         self.cache.events[ strEvent ] = {};
  74.     end
  75.    
  76.     table.insert( self.cache.events[strEvent], strRecord );
  77. end
  78.  
  79. function currency:getAllEvents()
  80.     return self.cache.events;
  81. end
  82.  
  83. function currency:getEvent( strEvent )
  84.     if ( strEvent == nil ) then return end;
  85.     if ( self.cache.events[strEvent] == nil ) then return nil; end;
  86.    
  87.     return self.cache.events[strEvent];
  88. end
  89.  
  90. function currency.cache:saveEvents()
  91.     file.Write( "currency/events/store.txt", glon.encode( self.events ) );
  92. end
  93.  
  94. function currency.cache:loadEvents()
  95.     if ( !file.Exists("currency/events/store.txt") ) then return end;
  96.    
  97.     local read = file.Read("currency/events/store.txt");
  98.     local read_dec = glon.decode(read);
  99.    
  100.     if ( read_dec != nil ) then
  101.         self.events = read_dec;
  102.     end
  103. end
  104.  
  105. function currency:pnetworkUpdate( player, strCurrency, intValue)
  106.     if ( self.cache.types[strCurrency] ==  nil ) then return end;
  107.     umsg.Start( "pcurrencyNetworkUpdate", player )
  108.         umsg.String( strCurrency )
  109.         umsg.Short( intValue );
  110.     umsg.End();
  111. end
  112.  
  113. function META_ENTITY:giveCurrency( strCurrency, intAmount )
  114.     if ( strCurrency == nil ) then return end;
  115.     if ( intAmount == nil ) then return end;
  116.     if ( intAmount <= 0 ) then return end;
  117.    
  118.     if ( self.currency == nil) then
  119.         self.currency = {};
  120.     end
  121.    
  122.     local add = self.currency[strCurrency] or 0;
  123.     self.currency[strCurrency] = add + intAmount;
  124.    
  125.     -- save and shit.
  126.     if ( currency.config["DefaultEvents"] ) then
  127.         currency:addEvent( "EntityGiveCurrency", self:EntIndex() .. " has been given " .. intAmount .. " of " .. strCurrency .. ".")
  128.     end
  129.    
  130.     if ( self:IsPlayer() ) then
  131.         self:saveCurrency();
  132.         currency:pnetworkUpdate( self, strCurrency, self.currency[strCurrency] );
  133.     end
  134. end
  135.  
  136. function META_ENTITY:takeCurrency( strCurrency, intAmount )
  137.     if ( strCurrency == nil ) then return end;
  138.     if ( intAmount == nil ) then return end;
  139.     if ( self.currency == nil or self.currency[strCurrency] == nil ) then return end;
  140.    
  141.     if ( self.currency[strCurrency] < intAmount ) then return end;
  142.    
  143.     self.currency[strCurrency] = current_amount - intAmount;
  144.    
  145.     -- save and shit.
  146.     if ( currency.config["DefaultEvents"] ) then
  147.         currency:addEvent( "EntityTakeCurrency", self:EntIndex() .. " has had " .. intAmount .. " of " .. strCurrency .. " taken from it.")
  148.     end
  149.    
  150.     if ( self:IsPlayer() ) then
  151.         self:saveCurrency();
  152.         currency:pnetworkUpdate( self, strCurrency, self.currency[strCurrency] );
  153.     end
  154. end
  155.  
  156. function META_PLAYER:saveCurrency()
  157.     if ( self.currency == nil ) then return end;
  158.     self:SetPData("CurrencyTable", glon.encode(self.currency));
  159.     -- do an event.
  160.     if ( currency.config["DefaultEvents"] ) then
  161.         currency:addEvent( "PlayerSaveCurrency", self:Nick() .. "(" .. self:SteamID() .. ")'s currency has been saved.");
  162.     end
  163. end
  164.  
  165. function META_PLAYER:loadCurrency()
  166.     if ( self:GetPData("CurrencyTable") == nil ) then return end;
  167.     self.currency = glon.decode(self:GetPData("CurrencyTable"));
  168.    
  169.     if ( currency.config["DefaultEvents"] ) then
  170.         currency:addEvent( "PlayerLoadCurrency", self:Nick() .. "(" .. self:SteamID() .. ")'s currency has been loaded.");
  171.     end
  172. end
  173.  
  174. end -- This is the if server.
  175.  
  176. if ( CLIENT ) then
  177.  
  178. function pnetworkupdate( um )
  179.     local currency = um:ReadString();
  180.     local value = um:ReadShort();
  181.    
  182.     if ( clientcurrencytable == nil ) then
  183.         clientcurrencytable = {};
  184.     end
  185.    
  186.     clientcurrencytable[currency] = value;
  187. end
  188. usermessage.Hook( "pcurrencyNetworkUpdate", pnetworkupdate );
  189.  
  190. end
  191.  
  192. function META_PLAYER:getCurrency( strCurrency )
  193.     if ( SERVER ) then
  194.         if ( !self.currency ) then return end;
  195.         return self.currency[strCurrency] or 0;
  196.        
  197.     elseif( CLIENT ) then
  198.         if ( !clientcurrencytable ) then return end;
  199.         return clientcurrencytable[strCurrency] or 0;
  200.     end
  201. end
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement