UNOBTANIUM

TurtleTradingStationComputer

May 13th, 2013
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.76 KB | None | 0 0
  1. local modemSide = "top"
  2. local version, versionnumber = "Turtle Trading Station Beta 0.3.0", "0.3.0"
  3. local pw = {6,7,8}
  4. local offerItemName, offerAmount, demandItemName, demandAmount = {},{},{},{}
  5. local offer, demand = {},{}
  6. local welcomeText = ""
  7. local maxChestSlots = 27
  8. local tradesAvailable = 0
  9. local computerID = os.getComputerID()
  10. local turtleID = 1
  11. w,h = term.getSize()
  12.  
  13. rednet.open(modemSide)
  14.  
  15. --chest[left|right][horizontal][vertical][name|amount]
  16.  
  17. --
  18. function printCentered(str, ypos)
  19.  term.setCursorPos(w/2 - #str/2, ypos)
  20.  term.write(str)
  21. end
  22.  
  23. function printCopyright()
  24.  local str = "by UNOBTANIUM"
  25.  term.setCursorPos(w-#str, h)
  26.  term.write(str)
  27. end
  28.  
  29. function printHeader(title, line)
  30.  printCentered(title, line)
  31.  printCentered(string.rep("-", w), line+1)
  32. end
  33.  
  34. function printReplace(str,line)
  35.  printCentered(string.rep(" ", w), line)
  36.  printCentered(str, line)
  37. end
  38.  
  39. function clearScreen()
  40.  term.clear()
  41.  term.setCursorPos(1,1)
  42.  term.clear()
  43. end
  44.  
  45. function transfer(i)
  46.  local keynames = {"<",1,2,3,4,5,6,7,8,9,
  47. 0,"-","+","backspace","tabulator","q","w","e","r","t",
  48. "y","u","i","o","p"," "," ","enter","left ctrg","a",
  49. "s","d","f","g","h","j","k","l","ü","ä",
  50. "ö","left shift","<","z","x","c","v","b","n","m",
  51. ",",".","","right shift","*","alt","spacebar","caps lock","left alt","spacebar",
  52. "right alt","","right ctrg","left","down","right","num lock","","","",
  53. 7,8,9,"-",4,5,6,"+",1,2,
  54. 3,"","","","","","","","","",
  55. "","","","","","","","","","",
  56. "","","","","","","","","","",
  57. "","","","","","","","","","",
  58. "","","","","","","","","","",
  59. "","","","","","","","","","",
  60. "","","","","","","","","","",
  61. "","","","","","","","","","",
  62. "","","","","","","","","","",
  63. "","","","","","","","","","",
  64. "/","","","","","","","","","",
  65. "","","","","","","","","","up",
  66. "","","left","","right","","","down","","",
  67. "right shift","","","","","","","","windows",""
  68. }
  69.  return keynames[i]
  70. end
  71.  
  72. function countArray(array)
  73.  local x = 0
  74.  for k,v in pairs(array) do
  75.   x = x + 1
  76.  end
  77.  return x
  78. end
  79.  
  80.  
  81. --MESSAGE
  82.  
  83. function sendMessage(sendingmessage)
  84.  sendingmessage = tostring(sendingmessage)
  85.  while true do
  86.   repeat
  87.    rednet.send(turtleID,sendingmessage)
  88.    id, message, distance = rednet.receive(1)
  89.   until type(message) == "string"
  90.   if id == turtleID then
  91.    sleep(0.1)
  92.    return
  93.   end
  94.  end
  95. end
  96.  
  97. function receiveMessage()
  98.  while true do
  99.   local senderChannel, receivedmessage = rednet.receive()
  100.   if senderChannel == turtleID then
  101.    sleep(0.1)
  102.    rednet.send(turtleID,"successful")
  103.    return receivedmessage
  104.   end
  105.  end
  106. end
  107.  
  108. -- PASSWORT
  109.  
  110. function checkPasswort(input)
  111.  local nameOne = ""
  112.  local nameTwo = ""
  113.  if countArray(input) >= countArray(pw) then
  114.   for i=1,countArray(pw) do
  115.    nameOne = nameOne .. pw[i]
  116.    nameTwo = nameTwo .. input[(countArray(input)-countArray(pw))+i]
  117.   end
  118.   if nameOne == nameTwo then
  119.    return true
  120.   end
  121.  end
  122.  return false
  123. end
  124.  
  125. -- SAVE & LOAD
  126.  
  127. function loadVariables()
  128.  if not fs.exists("ttsVariables") then
  129.   return false
  130.  end
  131.  local amount = 0
  132.  local file = fs.open("ttsVariables","r")
  133.  turtleID = math.floor(tonumber(file.readLine()))
  134.  maxChestSlots = tonumber(file.readLine())
  135.  tradesAvailable = tonumber(file.readLine())
  136.  welcomeText = file.readLine()
  137.  amount = math.floor(tonumber(file.readLine()))
  138.  if amount >= 1 then
  139.   for i=1,amount do
  140.    pw[i] = math.floor(tonumber(file.readLine()))
  141.   end
  142.  end
  143.  amount = math.floor(tonumber(file.readLine()))
  144.  if amount >= 1 then
  145.   for i=1,amount do
  146.    demand[i] = file.readLine()
  147.   end
  148.  end
  149.  amount = math.floor(tonumber(file.readLine()))
  150.  if amount >= 1 then
  151.   for i=1,amount do
  152.    offer[i] = file.readLine()
  153.   end
  154.  end
  155.  file.close()
  156.  return true
  157. end
  158.  
  159.  
  160. function saveVariables()
  161.  local file = fs.open("ttsVariables", "w")
  162.  file.writeLine(turtleID)
  163.  file.writeLine(maxChestSlots)
  164.  file.writeLine(tradesAvailable)
  165.  file.writeLine(welcomeText)
  166.  if countArray(pw) > 0 then
  167.   file.writeLine(countArray(pw))
  168.   for i=1, countArray(pw) do
  169.    file.writeLine(pw[i])
  170.   end
  171.  else
  172.   file.writeLine("0")
  173.  end
  174.  if countArray(demand) > 0 then
  175.   file.writeLine(countArray(demand))
  176.   for i=1, countArray(demand) do
  177.    file.writeLine(demand[i])
  178.   end
  179.  else
  180.   file.writeLine("0")
  181.  end
  182.  if countArray(offer) > 0 then
  183.   file.writeLine(countArray(offer))
  184.   for i=1, countArray(offer) do
  185.    file.writeLine(offer[i])
  186.   end
  187.  else
  188.   file.writeLine("0")
  189.  end
  190.  file.close()
  191. end
  192.  
  193.  
  194. function loadTrades()
  195.  if not fs.exists("ttsTrades") then
  196.   return false
  197.  end
  198.  loadVariables()
  199.  if tradesAvailable == 0 then return false end
  200.  local file = fs.open("ttsTrades","r")
  201.  for i=1,tradesAvailable do
  202.   offerItemName[i] = file.readLine()
  203.   offerAmount[i] = tonumber(file.readLine())
  204.   demandItemName[i] = file.readLine()
  205.   demandAmount[i] = tonumber(file.readLine())
  206.  end
  207.  file.close()
  208.  return true
  209. end
  210.  
  211. function saveTrades()
  212.  local file = fs.open("ttsTrades","w")
  213.  for i=1,tradesAvailable do
  214.   file.writeLine(offerItemName[i])
  215.   file.writeLine(offerAmount[i])
  216.   file.writeLine(demandItemName[i])
  217.   file.writeLine(demandAmount[i])
  218.  end
  219.  file.close()
  220. end
  221.  
  222. -- SINGULAR OR PLURAL
  223.  
  224. function SorP(amount)
  225.  if amount == 1 then
  226.   return ""
  227.  end
  228.  return "s"
  229. end
  230.  
  231. -- RUN
  232.  
  233. function run()
  234.  local running = true
  235.  loadTrades()
  236.  if not loadVariables() then
  237.   Options()
  238.   running = adminMenu()
  239.  end
  240.  while running do
  241.   if countArray(pw) > 0 then
  242.    userMenu()
  243.   else
  244.    Options()
  245.   end
  246.   running = adminMenu()
  247.  end
  248. end
  249.  
  250. -- SUB ADMIN MENUS
  251.  
  252.  
  253.  
  254. function changeSlots()
  255.  clearScreen()
  256.  printHeader("Change Slot Amount",1)
  257.  printCentered("How many slots does each chest have? (Number)",4)
  258.  local input = tonumber(read())
  259.  if input > 1 then
  260.   maxChestSlots = input
  261.   sendMessage("change slot amount")
  262.   sendMessage(maxChestSlots)
  263.   printReplace("Change the slot amount to: " .. maxChestSlots, 5)
  264.  end
  265.  saveVariables()
  266. end
  267.  
  268. function Options()
  269.  clearScreen()
  270.  printHeader("Passwort",1)
  271.  printCentered("Tipe a passwort (doesnt get shown)",4)
  272.  printCentered("Save with Enter",5)
  273.  local keypress = {}
  274.  local id, key = 0, 0
  275.  local passwort = ""
  276.  sleep(0.5)
  277.  while true do
  278.   id, key = os.pullEvent("key")
  279.   if key == 28 then
  280.    break
  281.   else
  282.    table.insert(keypress, key)
  283.   end
  284.  end
  285.  if countArray(keypress) > 0 then
  286.   if not next(pw) == nil then
  287.    for k,v in pairs(pw) do
  288.     pw[k] = nil
  289.    end  
  290.   end
  291.   for k,v in pairs(keypress) do
  292.    pw[k] = keypress[k]
  293.    passwort = passwort .. transfer(keypress[k])
  294.   end
  295.  end
  296.  printCentered(passwort,6)
  297.  sleep(0.6)
  298.  clearScreen()
  299.  printHeader("Turtle ID", 1)
  300.  printCentered("Type in the turtle's ID",4)
  301.  printCentered("Otherwise they dont communicate with each other.",6)
  302.  printCentered("Current Turtle ID: " .. turtleID,7)
  303.  printCentered("Computer ID: " .. computerID, 8)
  304.  printCentered("",10)
  305.  sleep(0.5)
  306.  local input = tonumber(read())
  307.  if input then
  308.   input = tonumber(input)
  309.   if input >= 1 and input <= 65555 then
  310.    turtleID = input
  311.   end
  312.  end
  313.  printReplace("Current ID: " .. turtleID,7)
  314.  sleep(0.5)
  315.  clearScreen()
  316.  printHeader("Welcome Text",1)
  317.  printCentered("Write down a text for the customers",3)
  318.  printCentered("Leave it empty if you dont want to change it",4)
  319.  print("")
  320.  local input = read()
  321.  if input == "" then else
  322.  welcomeText = input
  323.  end
  324.  saveVariables()
  325. end
  326.  
  327. function selectFromAll()
  328.  local name = {}
  329.  if demand[1] == nil then
  330.   return 0
  331.  end
  332.  for i=1,countArray(demand) do
  333.   table.insert(name, 1, demand[i])
  334.  end
  335.  for i=1,countArray(offer) do
  336.   local exists = false
  337.   for j=1,countArray(demand) do
  338.    if offer[i] == name[j] then
  339.     exists = true
  340.    end
  341.   end
  342.   if not exists then
  343.    table.insert(name,1, offer[i])
  344.   end
  345.  end
  346.  
  347.  local names = countArray(name)
  348.  local more = 0
  349.  local selectedItemName = ""
  350.  local choosing = true
  351.  
  352.  while choosing do
  353.   clearScreen()
  354.   printHeader("Item Names " .. more + 1 .. " to " .. more + 8 ,1)
  355.   for i=1,8 do
  356.    if i+more <= names then
  357.     printCentered(i .. " - " .. name[i+more], i+2)
  358.    end
  359.   end
  360.   if names >= 8+more then
  361.    printCentered("9 - Other items ", 11)
  362.   elseif names <= 8+more and names >= 8 then  
  363.    printCentered("9 - Back to first items", 11)
  364.   end
  365.   printCentered("0 - Back to the menu", 12)
  366.  
  367.   local id, key = os.pullEvent("key")
  368.   local input = transfer(key)
  369.  
  370.   if input >= 1 and input <= 8 then
  371.    if input+more <= names then
  372.     return name[input+more]
  373.    end
  374.   elseif input == 9 then
  375.    if names >= 8+more then
  376.     more = more + 8
  377.    elseif names <= 8+more and names >= 8 then  
  378.     more = 0
  379.    end
  380.   elseif input == 0 then
  381.    return 0
  382.   end
  383.  end
  384. end
  385.  
  386. function getItems()
  387.  local itemname = selectFromAll()
  388.  if type(itemname) == "string" then
  389.   clearScreen()
  390.   printHeader("Getting " .. itemname,1)
  391.   printCentered("Amount items in the chest:", 5)
  392.   printCentered("Receiving information",6)
  393.   sendMessage("get amount item")
  394.   sendMessage(itemname)
  395.   local amount = tonumber(receiveMessage())
  396.   if amount == 0 then
  397.    printReplace("No items left!",6)
  398.    sleep(2)
  399.    return
  400.   end
  401.   printReplace(tostring(amount), 6)
  402.   sleep(0.5)
  403.   printCentered("How many items do you want? (Number)",7)
  404.   printCentered("",8)
  405.   input = tonumber(read())
  406.   printReplace("", 8)
  407.   if input == 0 or input > amount then
  408.    printReplace("Amount is zero or too high!!!",7)
  409.    sleep(2)
  410.    return
  411.   end
  412.   printReplace("Getting your " .. input .. " " .. itemname .. SorP(amount) .. "...",7)
  413.   sendMessage("get items")
  414.   sendMessage(itemname)
  415.   sendMessage(input)
  416.   receiveMessage()
  417.   printReplace("Take the items out of the chest",7)
  418.   sleep(3)
  419.  end
  420. end
  421.  
  422. function refillChests()
  423.  local itemname = selectFromAll()
  424.  if type(itemname) == "string" then
  425.   clearScreen()
  426.   printHeader("Refill Chest",1)
  427.   printCentered("Place your " .. itemname .. "s into the chest!",4)
  428.   printCentered("Press Enter to continue...",5)
  429.   local id, key = os.pullEvent("key")
  430.   local input = transfer(key)
  431.   if not input == "enter" then
  432.    replaceCentered("Canceled",5)
  433.    sleep(1)
  434.    return
  435.   end
  436.   printReplace("Checking...",5)
  437.   sendMessage("refill")
  438.   sendMessage(itemname)
  439.   local m = receiveMessage()
  440.   printReplace(m,5)
  441.   sleep(3)
  442.  end
  443. end
  444.  
  445.  
  446. -- MENU (ADMIN)
  447.  
  448. function adminMenu()
  449.  local message = {}
  450.  table.insert(message, 1, "")
  451.  table.insert(message, 2, "Welcome!")
  452.  table.insert(message, 3, "")
  453.  while true do
  454.   clearScreen()
  455.   printHeader("Turtle Trading Station ADMIN",1)
  456.   printHeader("",3)
  457.   printHeader("",5)
  458.   printHeader("",7)
  459.   printHeader("",9)
  460.   printHeader("",11)
  461.   printHeader("",15)
  462.   if maxChestSlots <= 99 then
  463.    printCentered("     1 - Start      |    6 - Slots: " .. maxChestSlots .. "   ",3)
  464.   else
  465.    printCentered("     1 - Start      |    6 - Slots: " .. maxChestSlots .. "  ",3)  
  466.   end
  467.   printCentered("     2 - Add        |    7 - Options     ",5)
  468.   printCentered("     3 - Get        |    8 - Get Info    ",7)
  469.   printCentered("     4 - Refill     |    9 - Help        ",9)
  470.   printReplace("     5 - Refuel     |    0 - Quit        ",11)
  471.   printCentered(message[1],13)
  472.   printCentered(message[2],14)
  473.   printReplace(message[3],15)
  474.   term.setCursorPos(1,18)
  475.   print(computerID .. "              TTS ".. versionnumber .. "            by UNOBTANIUM")
  476.  
  477.   if message[2] == "Receiving information..." then
  478.    table.remove(message, 1)
  479.    table.remove(message, 1)
  480.    table.remove(message, 1)
  481.    sendMessage("information")
  482.    table.insert(message, 1, receiveMessage())
  483.    table.insert(message, 2, receiveMessage())
  484.    table.insert(message, 3, receiveMessage())
  485.   else
  486.    local id, key = os.pullEvent("key")
  487.    local input = transfer(key)
  488.    sleep(0.2)
  489.    if type(input) == "number" then
  490.     if input == 0 then
  491.      return false
  492.     elseif input == 1 then
  493.      return true
  494.     elseif input == 2 then
  495.      newTrade()
  496.      saveVariables()
  497.      saveTrades()
  498.     elseif input == 3 then
  499.      getItems()
  500.      saveTrades()
  501.     elseif input == 4 then
  502.      refillChests()
  503.      saveTrades()
  504.     elseif input == 5 then
  505.      sendMessage("refuel")
  506.     elseif input == 6 then
  507.      changeSlots()
  508.     elseif input == 7 then
  509.      Options()
  510.     elseif input == 8 then
  511.      table.remove(message, 1)
  512.      table.remove(message, 1)
  513.      table.remove(message, 1)
  514.      table.insert(message, 1, "Connecting to turtle")
  515.      table.insert(message, 2, "Receiving information...")
  516.      table.insert(message, 3, "Please wait!")
  517.     elseif input == 9 then
  518.      -- help()
  519.      clearScreen()
  520.      print("If you are having a question, send me a pn or message post in the ComputerCraft forums.")
  521.      print("Google: ComputerCraft Turtle Trading Station")
  522.      print("Press Enter to leave...")
  523.      read()
  524.     end
  525.    end
  526.   end
  527.  
  528.  end
  529. end
  530.  
  531. -- MENU (USER)
  532.  
  533. function userMenu()
  534.  local keypress = {}
  535.  while true do
  536.   clearScreen()
  537.   printCopyright()
  538.   printHeader("Turtle Trading Station", 1)
  539.   printCentered("Press a number key to navigate", 2)
  540.   printCentered("1 - Demand", 6)
  541.   printCentered("2 - Supply", 7)
  542.   term.setCursorPos(1,10)
  543.   print(welcomeText)
  544.  
  545.   local id, key = os.pullEvent("key")
  546.   table.insert(keypress, key)
  547.   if checkPasswort(keypress) then
  548.    return
  549.   end
  550.   local input = transfer(key)
  551.  
  552.   if type(input) == "number" then
  553.    if input == 1 then
  554.     local tradenumber = selectDemand()
  555.     if tradenumber > 0 then
  556.      tradeMenu(tradenumber)
  557.     end
  558.    elseif input == 2 then
  559.     local tradenumber = selectOffer()
  560.     if tradenumber > 0 then
  561.      tradeMenu(tradenumber)
  562.     end
  563.    elseif input == 3 then
  564.     newTrade()
  565.    end
  566.   end
  567.  end
  568. end
  569.  
  570. -- MENU (TRADE)
  571.  
  572. function tradeMenu(trade)
  573.  sendMessage("trade")
  574.  clearScreen()
  575.  printHeader("Purchase Overview", 1)
  576.  printCentered("You give",4)
  577.  printCentered(demandAmount[trade] .. " " .. demandItemName[trade] .. SorP(demandAmount[trade]),5)
  578.  printCentered("You get",7)
  579.  printCentered(offerAmount[trade] .. " " .. offerItemName[trade] ..SorP(offerAmount[trade]),8)
  580.  printCentered("Place all your " .. demandItemName[trade] .. "s in the chest.",10)
  581.  printCentered("ENTER - Accept the purchase",11)
  582.  printCentered("BACKSPACE - Cancel the purchase", 12)
  583.  local id, key = 0, ""
  584.  local input = ""
  585.  while true do
  586.   id, key = os.pullEvent("key")
  587.   input = transfer(key)
  588.   if type(input) == "string" and input == "enter" then
  589.    sendMessage("trade goes on")
  590.    break
  591.   elseif type(input) == "string" and input == "backspace" then
  592.    sendMessage("trade canceled")
  593.    return
  594.   end
  595.  end
  596.  
  597.  clearScreen()
  598.  printHeader("Purchase in Final Progress", 1)
  599.  printCentered("Sending purchase information to turtle...", 3)
  600.  sendMessage(offerItemName[trade]) -- Oak Wood
  601.  sendMessage(offerAmount[trade])   -- 8
  602.  sendMessage(demandItemName[trade])-- Iron Ingot
  603.  sendMessage(demandAmount[trade])  -- 1
  604.  printReplace("Sending trade information completed!",3)
  605.  printCentered("Counting all your " .. demandItemName[trade] .. "s...",4)
  606.  local tradeTimes =  tonumber(receiveMessage()) -- trade times
  607.  printReplace("Counting your " .. demandItemName[trade] .. "s completed!",4)
  608.  term.setCursorPos(1,5)
  609.  if tradeTimes == 0 then
  610.   print("No items found in the chest...")
  611.   sleep(3)
  612.   return
  613.  elseif tradeTimes == -1 then
  614.   print("Place the right kind of item in the chest!!!")
  615.   sleep(3)
  616.   return
  617.  elseif tradeTimes == -2 then
  618.   print("Sorry, there are no or not enough " .. offerItemName[trade] .. "s left.")
  619.   sleep(3)
  620.   return
  621.  elseif tradeTimes == -3 then
  622.   print("You didnt placed enough " .. demandItemName[trade] .. " in the chest!")
  623.   sleep(3)
  624.   return
  625.  elseif tradeTimes == -4 then
  626.   print("ERROR: Something messed up! No item found!")
  627.   sleep(3)
  628.   return
  629.  elseif tradeTimes == -5 then
  630.   print("Sorry, this trades has been made too many times. There can be no more " .. demandItemName[trade] .. "s stored.")
  631.   sleep(3)
  632.   return
  633.  elseif tradeTimes == -6 then
  634.   tradeTimes = receiveMessage()
  635.   printCentered("- Purchase goes allmost fine -",5)
  636.  else
  637.   printCentered("- Purchase goes completely fine -",5)
  638.  end
  639.  
  640.  printCentered("You get",6)
  641.  printCentered(tradeTimes*offerAmount[trade] .. " " .. offerItemName[trade] .. SorP(tradeTimes*offerAmount[trade]) .. " for" ,7)
  642.  printCentered(tradeTimes*demandAmount[trade] .. " of your " .. demandItemName[trade] .. SorP(tradeTimes*demandAmount[trade]), 8)
  643.  term.setCursorPos(1,10)
  644.  print("ENTER - Accept the purchase and get the " .. offerItemName[trade] .. SorP(tradeTimes*offerAmount[trade]))
  645.  printCentered("BACKSPACE - Cancel the purchase", 12)
  646.  local id, key = 0, ""
  647.  local input = ""
  648.  while true do
  649.   id, key = os.pullEvent("key")
  650.   input = transfer(key)
  651.   if type(input) == "string" and input == "enter" then
  652.    break
  653.   elseif type(input) == "string" and input == "backspace" then
  654.    sendMessage("trade canceled")
  655.    return
  656.   end
  657.  end
  658.  printReplace("Getting your " .. offerItemName[trade] .. SorP(tradeTimes*offerAmount[trade]),12)
  659.  printReplace("",10)
  660.  printReplace("",11)
  661.  printReplace("",12)
  662.  sendMessage("trade accepted")
  663.  receiveMessage()
  664.  printReplace("That's it! Thank you for your purchase",14)
  665.  sleep(5)
  666.  return
  667. end
  668.  
  669. -- SELECT OFFER
  670.  
  671. function selectOffer()
  672.  local offers = countArray(offer)
  673.  local more = 0
  674.  local selectedOfferName = ""
  675.  local choosing = true
  676.  
  677.  while choosing do
  678.   clearScreen()
  679.   printHeader("Supplies " .. more+1 .. " to " .. more+8,1)
  680.   for i=1,8 do
  681.    if i+more <= offers then
  682.     printCentered(i .. " - " .. offer[i+more], i+2)
  683.    end
  684.   end
  685.   if offers >= 8+more then
  686.    printCentered("9 - Other supplies ", 11)
  687.   elseif offers <= 8+more and offers >= 8 then  
  688.    printCentered("9 - Back to first supplies", 11)
  689.   end
  690.   printCentered("0 - Back to the menu", 12)
  691.  
  692.   local id, key = os.pullEvent("key")
  693.   local input = transfer(key)
  694.  
  695.   if type(input) == "number" then
  696.    if input >= 1 and input <= 8 then
  697.     if input+more <= offers then
  698.      selectedOfferName = offer[input+more]
  699.      choosing = false
  700.     end
  701.    elseif input == 9 then
  702.     if offers >= 8+more then
  703.      more = more + 8
  704.     elseif offers <= 8+more and offers >= 8 then  
  705.      more = 0
  706.     end
  707.    elseif input == 0 then
  708.     return 0
  709.    end
  710.   end
  711.  end
  712.  
  713.  clearScreen()
  714.  local fittingTrades = {}
  715.  for i=1,tradesAvailable do
  716.   if offerItemName[i] == selectedOfferName then
  717.    table.insert(fittingTrades, 1, i)
  718.   end
  719.  end
  720.  
  721.  choosing = true
  722.  more = 0
  723.  local amountFittingTrades = countArray(fittingTrades)
  724.  while choosing do
  725.   clearScreen()
  726.   printHeader("Demands for " .. selectedOfferName .. " " .. more+1 .. " to " .. more+8,1)
  727.   for i=1,8 do
  728.    if i+more <= amountFittingTrades then
  729.     printCentered(i .. " - Get " .. offerAmount[fittingTrades[i+more]] .. " " .. offerItemName[fittingTrades[i+more]] .. " for " .. demandAmount[fittingTrades[i+more]] .. " " .. demandItemName[fittingTrades[i+more]] , i+2)
  730.    end
  731.   end
  732.   if amountFittingTrades >= 8+more then
  733.    printCentered("9 - Other fitting trades ", 11)
  734.   elseif amountFittingTrades <= 8+more and amountFittingTrades >= 8 then  
  735.    printCentered("9 - Back to first trades", 11)
  736.   end
  737.   printCentered("0 - Back to the menu", 12)
  738.  
  739.   local id, key = os.pullEvent("key")
  740.   local input = transfer(key)
  741.  
  742.   if type(input) == "number" then
  743.    if input >= 1 and input <= 8 then
  744.     if input+more <= amountFittingTrades then
  745.      return fittingTrades[input+more]
  746.     end
  747.    elseif input == 9 then
  748.     if amountFittingTrades >= 8+more then
  749.      more = more + 8
  750.     elseif amountFittingTrades <= 8+more and amountFittingTrades >= 8 then  
  751.      more = 0
  752.     end
  753.    elseif input == 0 then
  754.     return 0
  755.    end
  756.   end
  757.  end
  758. end
  759.  
  760.  
  761.  
  762. -- SELECT DEMAND
  763.  
  764. function selectDemand()
  765.  local demands = countArray(demand)
  766.  local more = 0
  767.  local selectedDemandName = ""
  768.  local choosing = true
  769.  
  770.  while choosing do
  771.   clearScreen()
  772.   printHeader("Demands " .. more+1 .. " to " .. more+8,1)
  773.   for i=1,8 do
  774.    if i+more <= demands then
  775.     printCentered(i .. " - " .. demand[i+more], i+2)
  776.    end
  777.   end
  778.   if demands >= 8+more then
  779.    printCentered("9 - Other demands ", 11)
  780.   elseif demands <= 8+more and demands >= 8 then  
  781.    printCentered("9 - Back to first demands", 11)
  782.   end
  783.   printCentered("0 - Back to the menu", 12)
  784.  
  785.   local id, key = os.pullEvent("key")
  786.   local input = transfer(key)
  787.  
  788.   if type(input) == "number" then  
  789.    if input >= 1 and input <= 8 then
  790.     if input+more <= demands then
  791.      selectedDemandName = demand[input+more]
  792.      choosing = false
  793.     end
  794.    elseif input == 9 then
  795.     if demands >= 8+more then
  796.      more = more + 8
  797.     elseif demands <= 8+more and demands >= 8 then  
  798.      more = 0
  799.     end
  800.    elseif input == 0 then
  801.     return 0
  802.    end
  803.   end
  804.  end
  805.  
  806.  clearScreen()
  807.  local fittingTrades = {}
  808.  for i=1,tradesAvailable do
  809.   if demandItemName[i] == selectedDemandName then
  810.    table.insert(fittingTrades, i)
  811.   end
  812.  end
  813.  
  814.  choosing = true
  815.  more = 0
  816.  local amountFittingTrades = countArray(fittingTrades)
  817.  while choosing do
  818.   clearScreen()
  819.   printHeader("Supplies for " .. selectedDemandName .. " " .. more+1 .. " to " .. more+8,1)
  820.   for i=1,8 do
  821.    if i+more <= amountFittingTrades then
  822.     printCentered(i .. " - Get " .. offerAmount[fittingTrades[i+more]] .. " " .. offerItemName[fittingTrades[i+more]] .. " for " .. demandAmount[fittingTrades[i+more]] .. " " .. demandItemName[fittingTrades[i+more]] , i+2)
  823.    end
  824.   end
  825.   if amountFittingTrades >= 8+more then
  826.    printCentered("9 - Other fitting trades ", 11)
  827.   elseif amountFittingTrades <= 8+more and amountFittingTrades >= 8 then  
  828.    printCentered("9 - Back to first trades", 11)
  829.   end
  830.   printCentered("0 - Back to the menu", 12)
  831.  
  832.   local id, key = os.pullEvent("key")
  833.   local input = transfer(key)
  834.  
  835.   if type(input) == "number" then  
  836.    if input >= 1 and input <= 8 then
  837.     if input+more <= amountFittingTrades then
  838.      return fittingTrades[input+more]
  839.     end
  840.    elseif input == 9 then
  841.     if amountFittingTrades >= 8+more then
  842.      more = more + 8
  843.     elseif amountFittingTrades <= 8+more and amountFittingTrades >= 8 then  
  844.      more = 0
  845.     end
  846.    elseif input == 0 then
  847.     return 0
  848.    end
  849.   end
  850.  end
  851. end
  852.  
  853.  
  854.  
  855. -- CREATE NEW TRADE
  856.  
  857. function newTrade()
  858.  clearScreen()
  859.  local demands = countArray(demand)
  860.  local more = 0
  861.  local selectedDemandName = ""
  862.  local selectedOfferName = ""
  863.  local selectedAmountDemand = 0
  864.  local selectedAmountOffer = 0
  865.  local choosing = true
  866.  
  867.  while choosing do
  868.   clearScreen()
  869.   printHeader("Create new purchase: Select demand", 1)
  870.   for i=1,6 do
  871.    if i+more <= demands then
  872.     printCentered(i .. " - " .. demand[i],i+2)
  873.    end
  874.   end
  875.   printCentered("8 - New demand", 10)
  876.   if demands >= 6+more then
  877.    printCentered("9 - Other demands ", 11)
  878.   elseif demands <= 6+more and demands >= 6 then  
  879.    printCentered("9 - Back to first demands", 11)
  880.   end
  881.  
  882.   printCentered("0 - Back to the menu", 12)
  883.  
  884.   local id, key = os.pullEvent("key")
  885.   local input = transfer(key)
  886.  
  887.   if type(input) == "number" then  
  888.    if input >= 1 and input <= 6 then
  889.     if input+more <= demands then
  890.      selectedDemandName = demand[input+more]
  891.      choosing = false
  892.     end
  893.    elseif input == 8 then
  894.     clearScreen()
  895.     print("Which item do you want from the customers? (Name/Singular) (Demand)")
  896.     print("")
  897.     sleep(0.2)
  898.     selectedDemandName = read()
  899.     choosing = false
  900.    elseif input == 9 then
  901.     if demands >= 6+more then
  902.      more = more + 6
  903.     elseif demands <= 6+more and demands >= 6 then  
  904.      more = 0
  905.     end
  906.    elseif input == 0 then
  907.     return 0
  908.    end
  909.   end
  910.  end
  911.  
  912.  clearScreen()
  913.  print("Which item do you give the customers for " .. selectedDemandName .. "(s)? (Name/Singular) (Supply)")
  914.  print("")
  915.  sleep(0.2)
  916.  selectedOfferName = read()
  917.  
  918.  clearScreen()
  919.  print("How many " .. selectedDemandName .."(s) do you want? (Number) (Demand)")
  920.  print("")
  921.  selectedAmountDemand = tonumber(read())
  922.  
  923.  clearScreen()
  924.  print("How many " .. selectedOfferName .. "(s) do you give for the " .. selectedAmountDemand .. " " .. selectedDemandName .. "? (Number/Singular) (Supply)")
  925.  print("")
  926.  selectedAmountOffer = tonumber(read())
  927.  
  928.  clearScreen()
  929.  printHeader("Add new purchase",1)
  930.  printCentered("You get",5)
  931.  printCentered(selectedAmountDemand .. " " .. selectedDemandName .. SorP(selectedAmountDemand) .. " for", 6)
  932.  printCentered(selectedAmountOffer .. " " .. selectedOfferName .. SorP(selectedAmountOffer), 7)
  933.  printCentered("ENTER - Add the purchase",11)
  934.  printCentered("BACKSPACE - Back to the menu", 12)
  935.  local id, key = 0, ""
  936.  local input = ""
  937.  while true do
  938.   id, key = os.pullEvent("key")
  939.   input = transfer(key)
  940.   if type(input) == "string" and input == "enter" then
  941.    break
  942.   elseif type(input) == "string" and input == "backspace" then
  943.    return
  944.   end
  945.  end
  946.  
  947.  local exists = false
  948.  for i=1,countArray(demand) do
  949.   if demand[i] == selectedDemandName then
  950.    exists = true
  951.   end
  952.  end
  953.  if not exists then
  954.   table.insert(demand,selectedDemandName)
  955.   for i=1,countArray(offer) do
  956.    if offer[i] == selectedDemandName then
  957.     exists = true
  958.    end
  959.   end
  960.   if not exists then
  961.    local competentUser = false
  962.    clearScreen()
  963.    while not competentUser do
  964.     print("Place one item of ".. selectedDemandName .." into the chest and press Enter... (Indicator Item) (Demand)")
  965.     local id, key = os.pullEvent("key")
  966.     local input = transfer(key)
  967.     if input == "enter" then
  968.      sendMessage("indicator item")
  969.      local message = receiveMessage()
  970.      if message == "successful" then
  971.       sendMessage(selectedDemandName)
  972.       competentUser = true
  973.      else
  974.       clearScreen()
  975.       print(message)
  976.      end
  977.     end
  978.    end
  979.   end
  980.  end
  981.  
  982.  exists = false
  983.  for i=1,countArray(offer) do
  984.   if offer[i] == selectedOfferName then
  985.    exists = true
  986.   end
  987.  end
  988.  if not exists then
  989.   table.insert(offer,selectedOfferName)
  990.   for i=1,countArray(demand) do
  991.    if demand[i] == selectedOfferName then
  992.     exists = true
  993.    end
  994.   end
  995.   if not exists then
  996.    local competentUser = false
  997.    clearScreen()
  998.    while not competentUser do
  999.     print("Place one item of ".. selectedOfferName .." into the chest and press Enter... (Indicator Item) (Supply)")
  1000.     local id, key = os.pullEvent("key")
  1001.     local input = transfer(key)
  1002.     if input == "enter" then
  1003.      sendMessage("indicator item")
  1004.      local message = receiveMessage()
  1005.      if message == "successful" then
  1006.       sendMessage(selectedOfferName)
  1007.       competentUser = true
  1008.      else
  1009.       clearScreen()
  1010.       print(message)
  1011.       if message == "All chests are full!" then
  1012.        sleep(2)
  1013.        return
  1014.       end
  1015.      end
  1016.     end
  1017.    end
  1018.   end
  1019.  end
  1020.  
  1021.  clearScreen()
  1022.  printCentered("Adding new purchase...", 6)
  1023.  table.insert(offerItemName, 1,selectedOfferName)
  1024.  table.insert(offerAmount, 1,selectedAmountOffer)
  1025.  table.insert(demandItemName, 1,selectedDemandName)
  1026.  table.insert(demandAmount, 1,selectedAmountDemand)
  1027.  tradesAvailable = tradesAvailable + 1
  1028.  saveVariables()
  1029.  saveTrades()
  1030.  printCentered("Finished!", 7)
  1031.  sleep(0.5)
  1032.  clearScreen()
  1033. end
  1034.  
  1035. -- TTS
  1036.  
  1037. run()
Advertisement
Add Comment
Please, Sign In to add comment