Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------
- -- Money library.
- -- I guess you could call it a library?
- -- By LauScript
- -------------------
- -- glon module
- require("glon");
- -- The main table.
- currency = {};
- -- A sub table for config vars.
- currency.config = {};
- currency.config["DebugMode"] = true;
- currency.config["DefaultEvents"] = true;
- if ( SERVER ) then
- -- For storing shit.
- currency.cache = {};
- currency.cache.types = {};
- currency.cache.events = {};
- end
- -- for later use.
- local META_ENTITY = FindMetaTable("Entity");
- local META_PLAYER = FindMetaTable("Player");
- -- Just a quick lil' func.
- local function DebugPrint( text )
- -- Make sure debug is enabled.
- if ( currency.config["DebugMode"] != true ) then return end;
- -- Print the text.
- print( "Currency Debug: " .. text );
- end
- if ( SERVER ) then
- -- A way to add new currency types.
- function currency:AddNew( strName, strSymbol, strPlural )
- -- nil checks.
- if ( strName == nil ) then return end;
- if ( strSymbol == nil ) then return end;
- if ( strPlural == nil ) then
- strPlural = strName;
- end
- -- Length check.
- if ( string.len( strName ) < 1 ) then
- print("Could not add new currency, name was too short.");
- return;
- end
- -- Store it.
- self.cache.types[ strName ] = { symbol = strSymbol, plural = strPlural };
- DebugPrint( "Currency created with values:\nName: " .. strName .. "\nSymbol: " .. strSymbol .. "\nPlural: " .. strPlural );
- end
- function currency:getTable( strCurrency )
- if ( strCurrency == nil ) then return end;
- if ( self.cache.types[strCurrency] == nil ) then return end;
- return self.cache.types[strCurrency];
- end
- function currency:addEvent( strEvent, strRecord )
- if ( strEvent == nil ) then return end;
- if ( strRecord == nil ) then return end;
- if ( self.cache.events[ strEvent ] == nil ) then
- self.cache.events[ strEvent ] = {};
- end
- table.insert( self.cache.events[strEvent], strRecord );
- end
- function currency:getAllEvents()
- return self.cache.events;
- end
- function currency:getEvent( strEvent )
- if ( strEvent == nil ) then return end;
- if ( self.cache.events[strEvent] == nil ) then return nil; end;
- return self.cache.events[strEvent];
- end
- function currency.cache:saveEvents()
- file.Write( "currency/events/store.txt", glon.encode( self.events ) );
- end
- function currency.cache:loadEvents()
- if ( !file.Exists("currency/events/store.txt") ) then return end;
- local read = file.Read("currency/events/store.txt");
- local read_dec = glon.decode(read);
- if ( read_dec != nil ) then
- self.events = read_dec;
- end
- end
- function currency:pnetworkUpdate( player, strCurrency, intValue)
- if ( self.cache.types[strCurrency] == nil ) then return end;
- umsg.Start( "pcurrencyNetworkUpdate", player )
- umsg.String( strCurrency )
- umsg.Short( intValue );
- umsg.End();
- end
- function META_ENTITY:giveCurrency( strCurrency, intAmount )
- if ( strCurrency == nil ) then return end;
- if ( intAmount == nil ) then return end;
- if ( intAmount <= 0 ) then return end;
- if ( self.currency == nil) then
- self.currency = {};
- end
- local add = self.currency[strCurrency] or 0;
- self.currency[strCurrency] = add + intAmount;
- -- save and shit.
- if ( currency.config["DefaultEvents"] ) then
- currency:addEvent( "EntityGiveCurrency", self:EntIndex() .. " has been given " .. intAmount .. " of " .. strCurrency .. ".")
- end
- if ( self:IsPlayer() ) then
- self:saveCurrency();
- currency:pnetworkUpdate( self, strCurrency, self.currency[strCurrency] );
- end
- end
- function META_ENTITY:takeCurrency( strCurrency, intAmount )
- if ( strCurrency == nil ) then return end;
- if ( intAmount == nil ) then return end;
- if ( self.currency == nil or self.currency[strCurrency] == nil ) then return end;
- if ( self.currency[strCurrency] < intAmount ) then return end;
- self.currency[strCurrency] = current_amount - intAmount;
- -- save and shit.
- if ( currency.config["DefaultEvents"] ) then
- currency:addEvent( "EntityTakeCurrency", self:EntIndex() .. " has had " .. intAmount .. " of " .. strCurrency .. " taken from it.")
- end
- if ( self:IsPlayer() ) then
- self:saveCurrency();
- currency:pnetworkUpdate( self, strCurrency, self.currency[strCurrency] );
- end
- end
- function META_PLAYER:saveCurrency()
- if ( self.currency == nil ) then return end;
- self:SetPData("CurrencyTable", glon.encode(self.currency));
- -- do an event.
- if ( currency.config["DefaultEvents"] ) then
- currency:addEvent( "PlayerSaveCurrency", self:Nick() .. "(" .. self:SteamID() .. ")'s currency has been saved.");
- end
- end
- function META_PLAYER:loadCurrency()
- if ( self:GetPData("CurrencyTable") == nil ) then return end;
- self.currency = glon.decode(self:GetPData("CurrencyTable"));
- if ( currency.config["DefaultEvents"] ) then
- currency:addEvent( "PlayerLoadCurrency", self:Nick() .. "(" .. self:SteamID() .. ")'s currency has been loaded.");
- end
- end
- end -- This is the if server.
- if ( CLIENT ) then
- function pnetworkupdate( um )
- local currency = um:ReadString();
- local value = um:ReadShort();
- if ( clientcurrencytable == nil ) then
- clientcurrencytable = {};
- end
- clientcurrencytable[currency] = value;
- end
- usermessage.Hook( "pcurrencyNetworkUpdate", pnetworkupdate );
- end
- function META_PLAYER:getCurrency( strCurrency )
- if ( SERVER ) then
- if ( !self.currency ) then return end;
- return self.currency[strCurrency] or 0;
- elseif( CLIENT ) then
- if ( !clientcurrencytable ) then return end;
- return clientcurrencytable[strCurrency] or 0;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement