Advertisement
1lann

counter

Jan 26th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.30 KB | None | 0 0
  1. -- Display Help Function
  2. local function displayHelp()
  3.   if term.isColor() then term.setTextColor(colors.lightBlue) end
  4.   print(" -- Help/Usage -- ")
  5.   print("Counter v1.0 by 1lann")
  6.   print(" ")
  7.   print("help: Displays help")
  8.   print("?: Displays help")
  9.   print("exit: Exits the program")
  10.   print("<object>: Lists the number of the object")
  11.   print("<num> <object>: Adds a number of object to")
  12.   print("                  the database. Ex: 24 copper")
  13.   print("purge <object>: Removes the object completely")
  14.   print("                  Out of the database")
  15.   print("list: Lists the number of objects you have")
  16.   term.setTextColor(colors.white)
  17. end
  18.  
  19. local function calcNumber(str)
  20.   counterCaptureVariableThing = nil
  21.   loadstring("counterCaptureVariableThing = " .. tostring(str))()
  22.   return tonumber(counterCaptureVariableThing)
  23. end
  24.  
  25. local tableData = {}
  26.  
  27. local function isObject(data)
  28.   for k,v in pairs(tableData) do
  29.     if k == data then
  30.       return v
  31.     end
  32.   end
  33.   return false
  34. end
  35.  
  36. -- Load Data
  37. if fs.exists("/oreData") then
  38.   local f = io.open("/oreData", "r")
  39.   tableData = textutils.unserialize(f:read("*l"))
  40.   if not tableData then
  41.     tableData = {}
  42.   end
  43. end
  44.  
  45. term.setCursorPos(1,1)
  46. term.setBackgroundColor(colors.black)
  47. term.setTextColour(colors.white)
  48. term.clear()
  49.  
  50. -- Main Input Loop
  51. while true do
  52.   if term.isColor() then
  53.     term.setTextColor(colors.yellow)
  54.   end
  55.   write("Input> ")
  56.   term.setTextColor(colors.white)
  57.   local objects = read():lower()
  58.   objects = objects:gsub("^%s*(.-)%s*$", "%1") -- Removes trailing whitespace
  59.   if objects == "help" or objects == "?" then
  60.     displayHelp()
  61.   elseif objects == "exit" then
  62.     term.clear()
  63.     term.setCursorPos(1,1)
  64.     break
  65.   elseif objects == "list" then
  66.     if term.isColor() then term.setTextColor(colors.lime) end
  67.     print("There are:")
  68.     local _, y = term.getSize()
  69.     local loopNum = 0
  70.     for k,v in pairs(tableData) do
  71.       print(tostring(v) .. " " .. k)
  72.       loopNum = loopNum+1
  73.       if loopNum >= y-1 then
  74.         local _, pY = term.getCursorPos()
  75.         write("Press any key to continue...")
  76.         os.pullEvent("key")
  77.         term.clearLine()
  78.         term.setCursorPos(1, pY)
  79.       end
  80.     end
  81.   elseif isObject(objects) then
  82.     if term.isColor() then term.setTextColor(colors.lime) end
  83.     print("There are: " .. tableData[objects] .. " " .. objects)
  84.   elseif #{objects:find(" ")} == 2 then
  85.     local firstArg = objects:sub(1, objects:find(" ")-1)
  86.     local secondArg = objects:sub(objects:find(" ")+1, -1)
  87.     if firstArg == "purge" then
  88.       if tableData[secondArg] then
  89.         if term.isColor() then term.setTextColor(colors.lime) end
  90.         print("Deleted Entry: " .. secondArg)
  91.         print("Which had: " .. tostring(tableData[secondArg]) .. " items")
  92.         tableData[secondArg] = nil
  93.         local f = io.open("/oreData", "w")
  94.         f:write(textutils.serialize(tableData))
  95.         f:close()
  96.       else
  97.         if term.isColor() then term.setTextColor(colors.red) end
  98.         print("Invalid Use!")
  99.         displayHelp()
  100.       end
  101.     else
  102.       num = calcNumber(firstArg)
  103.       if num then
  104.         if term.isColor() then term.setTextColor(colors.lime) end
  105.         if tableData[secondArg] then
  106.           print("Before there were " .. tableData[secondArg] .. " " .. secondArg)
  107.           if math.floor(num) ~= num then
  108.             print("Number is not an integer, rounding to " .. math.ceil(num-0.5))
  109.             num = math.ceil(num-0.5)
  110.           end
  111.           if tostring(num):sub(1,1) == "-" then
  112.             print("Subtracting " .. tostring(num*-1))
  113.           else
  114.             print("Adding " .. tostring(num))
  115.           end
  116.           if tableData[secondArg] then
  117.            tableData[secondArg] = tableData[secondArg] + num
  118.           end
  119.         else
  120.           tableData[secondArg] = num
  121.         end
  122.         print("Now there are " .. tableData[secondArg] .. " " .. secondArg)
  123.         local f = io.open("/oreData", "w")
  124.         f:write(textutils.serialize(tableData))
  125.         f:close()
  126.       else
  127.         if term.isColor() then term.setTextColor(colors.red) end
  128.         print("Invalid Use!")
  129.         displayHelp()
  130.       end
  131.     end
  132.   else
  133.     if term.isColor() then term.setTextColor(colors.red) end
  134.     print("Unknown Command")
  135.     displayHelp()
  136.   end
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement