Loader

First Lua Script

Started by mrd, 29-01-2009

0 Members and 2 Guests are viewing this topic.

if file.Exists( "interest.txt" ) then
   interestRate = tonumber( file.Read( "interest.txt" ) )
else
   interestRate = 0
end


function timeToDay( myTime )
   newTime = math.floor( myTime / 86400 )
   return newTime
end

function addInterest()
   fileNames = {}
   fileNames = file.Find("debts/*")
   num1 = 1

   while( fileNames[num1] != nil ) do
      newValue = tonumber(file.Read("debts/"..fileNames[num1]) ) * (1 + interestRate/100)
      file.Write("debts/"..fileNames[num1], newValue )
      num1 = num1 + 1
   end

end


if file.Exists( "interestDate.txt" ) then
   if( tonumber(file.Read("interestDate.txt")) < timeToDay( os.time() ) ) then
      num2 = timeToDay( os.time() ) - tonumber(file.Read("interestDate.txt"))
      while( num2 > 0 ) do
         addInterest()
         num2 = num2 - 1
      end
      file.Write( "interestDate.txt", timeToDay( os.time() ) )
   end
else
   file.Write( "interestDate.txt", timeToDay( os.time() ) )
end


function removeDebt( debtorName )
   if( file.Exists( "debts/"..debtorName..".txt" ) ) then
      file.Delete( "debts/"..debtorName..".txt")
      ErrorNoHalt( "Success\n" )
   else
      file.Write( "debts/"..debtorName..".txt", debtorAmount )
      ErrorNoHalt( "Record does not exist\n" )
   end
end


function addToDebt( debtorName, debtorAmount )

   ErrorNoHalt( "Success\n" )
   if( file.Exists( "debts/"..debtorName..".txt" ) ) then
      newAmnt = tostring( tonumber( file.Read( "debts/"..debtorName..".txt" ) ) + debtorAmount )
      file.Write( "debts/"..debtorName..".txt", newAmnt )
   else
      file.Write( "debts/"..debtorName..".txt", debtorAmount )
   end

end


function subToDebt( debtorName, debtorAmount )

   ErrorNoHalt( "Success\n" )
   if( file.Exists( "debts/"..debtorName..".txt" ) ) then
      newAmnt = tostring( tonumber( file.Read( "debts/"..debtorName..".txt" ) ) - debtorAmount )
      file.Write( "debts/"..debtorName..".txt", newAmnt )
   else
      file.Write( "debts/"..debtorName..".txt", debtorAmount * -1 )
   end

end


function setToDebt( debtorName, debtorAmount )

   ErrorNoHalt( "Success\n" )
   file.Write( "debts/"..debtorName..".txt", debtorAmount )

end


function checkDebt( debtorName )

   if( file.Exists( "debts/"..debtorName..".txt" ) ) then
      ErrorNoHalt( debtorName.." owes $"..file.Read( "debts/"..debtorName..".txt" ).."\n" )
   else
      ErrorNoHalt( "No record for that name has been found\n" )
   end

end

function listDebts()
   fileNames = {}
   fileNames = file.Find("debts/*")
   num1 = 1

   while( fileNames[num1] != nil ) do
      Msg( string.sub(fileNames[num1],1,string.len(fileNames[num1])-4).."\t\t\t"..file.Read("debts/"..fileNames[num1]).."\n")
      
      newValue = tonumber(file.Read("debts/"..fileNames[num1]) ) * (1 + interestRate/100)
      file.Write("debts/"..fileNames[num1], newValue )
      num1 = num1 + 1
   end
end


function DuckChatTextHook( playerindex, playername, text, messagetype )
   if( player.GetByID(playerindex) == LocalPlayer() ) then
      
      //Add an amount
      if( string.sub( text, 1, 4 ) == "-add" ) then
         foundSpace = string.find( text, " ", 6 )
         if( foundSpace == nil ) then
            ErrorNoHalt( "No name or amount entered\n" )
         else
            while( string.find( text, " ", foundSpace + 1 ) != nil ) do
               foundSpace = string.find( text, " ", foundSpace + 1 )
            end
            debtorName = string.sub( text, 6, foundSpace - 1 )
            debtorAmount = tonumber( string.sub(text,foundSpace) )
            if( debtorAmount != nil ) then
               addToDebt( debtorName, debtorAmount )
            else
               ErrorNoHalt( "Non-number entered as an amount\n" )
            end
         end
      end

      //Subtract an amount
      if( string.sub( text, 1, 4 ) == "-sub" ) then
         foundSpace = string.find( text, " ", 6 )
         if( foundSpace == nil ) then
            ErrorNoHalt( "No name or amount entered\n" )
         else
            while( string.find( text, " ", foundSpace + 1 ) != nil ) do
               foundSpace = string.find( text, " ", foundSpace + 1 )
            end
            debtorName = string.sub( text, 6, foundSpace - 1 )
            debtorAmount = tonumber( string.sub(text,foundSpace) )
            if( debtorAmount != nil ) then
               subToDebt( debtorName, debtorAmount )
            else
               ErrorNoHalt( "Non-number entered as an amount\n" )
            end
         end
      end

      //Set to an amount
      if( string.sub( text, 1, 5 ) == "-set " ) then
         foundSpace = string.find( text, " ", 6 )
         if( foundSpace == nil ) then
            ErrorNoHalt( "No name or amount entered\n" )
         else
            while( string.find( text, " ", foundSpace + 1 ) != nil ) do
               foundSpace = string.find( text, " ", foundSpace + 1 )
            end
            debtorName = string.sub( text, 6, foundSpace - 1 )
            debtorAmount = tonumber( string.sub(text,foundSpace) )
            if( debtorAmount != nil ) then
               setToDebt( debtorName, debtorAmount )
            else
               ErrorNoHalt( "Non-number entered as an amount\n" )
            end
         end
      end

      //Check an amount
      if( string.sub( text, 1, 6 ) == "-check" ) then
         debtorName = string.sub( text, 8 )
         checkDebt( debtorName )
      end

      //Check interest rate
      if( string.sub( text, 1, 5 ) == "-rate" ) then
         ErrorNoHalt( "Interest Rate: "..interestRate.."\n" )
      end

      //Set interest rate
      if( string.sub( text, 1, 8 ) == "-setrate" ) then
         interestRate = tonumber( string.sub( text, 10 ) )
         if( interestRate != nil ) then
            file.Write( "interest.txt", interestRate)
            ErrorNoHalt( "Success\n" )
         else
            ErrorNoHalt( "Invalid amount entered\n" )
         end
      end

      if( string.sub( text, 1, 7 ) == "-remove" ) then
         debtorName = string.sub( text, 9 )
         removeDebt( debtorName )
      end

      if( string.sub( text, 1, 5 ) == "-list" ) then
         listDebts()
      end


   end
end

hook.Add( "ChatText", "DuckChatTextHook", DuckChatTextHook )

Whatcha think?

What does it do?

Theres no way ducky wrote this, it's a bank script i think.

Quotebreslau: if i cant cheat i dont wanna play
breslau: period

Print("lol")

end

My friend Fry walked me through most of it :P So I didn't do it completely by myself. Yes, it is a bank script that keeps track of players debt. If I typed -add Silver 500 then it would add him to a list. If I typed -check Silver then it would print in console how much money Silver owes me. Interest is added as well.


I think my brain just exploded.