This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CreeperGoBoom's profile picture

repeat until not working

Started by CreeperGoBoom, 17 December 2019 - 10:59 AM
CreeperGoBoom #1
Posted 17 December 2019 - 11:59 AM
This keeps happening on and off. Works then doesn't. Then works then doesn't.

Very odd behavior.

Earlier ones that did work, for some weird reason are accepting any answer despite checks.

look for startupConfig

I tried reloading the world and reloading the game.

code:
Spoiler
--Tollway by CreeperGoBoom
--v1.3.1 STABLE
--Fixed large tolls not working
--
---------CREDITS---------
--Lupus590: Up and coming hungry toll idea and programming issues.
--
--
--User vars. Change these as you like--
local tollType  --answers: "freepass" "perm-toll", "debt-toll"
local chestside
local errornoteblockside="left" --change to an unused side if you don't want to use this.
local toll=64
local paytype="Iron Ingot" --Display name of item to use.
--just don't tell players it works off this or that it's even my code!
local freepass=false --controls default behavior for the tollway.
--set to false for paid tollway based on debt settings.
--set to true for free passage.
local pushdir="east" --what direction do items need to go from input chest?
local consize=27 --How many slots in the container used? normal chest=27
local errorsoundcount=3 --how many times do you want your noteblock to sound for insufficient funds?
local errorsounddelay=0.125 --how long in seconds between each ding of insufficient funds feedback?

--redstone control
local redOutputType="bundled"  --available answers: "redstone" or "bundled"
local redout="bottom" --for redstone output
local redpass="right" --for detecting when an admit has been fulfilled
local redin="front" --Where will the signal come from for pay query?
local bundledSide="bottom"	--only applicable if using bundled. what side to use bundled cables?
local bundledInColor=colors.white --what color to use for player query input?
local bundledOutColor=colors.red --what color to use for redstone control?
local bundledPassColor=colors.green --what color to use for player pass



--Program vars. Do not change these
local version = "1.3.1 STABLE"
local list={}
local results={}
local paid=0
local topay=toll
local count=0
local ispaid=false
local rs=redstone
local tollmax=consize*64
local config={}



--Not currently in use
local tolldebt=500 --This is to determine the overall cost of the toll before it becomes free
local deptpaid=0 --program controlled, this keeps track of the payment progress
local hasdebt=true --set to false to make it a permament tollway.
local afterDebtAction
local hungry=false



--Functions--
function OpenTollway(inputside) --opens tollway if closed. also returns if it was successful.
  os.pullEvent("redstone")
  if redOutputType=="redstone" and rs.getInput(inputside,true) then
	rs.setOutput(redout,true)
	local finished=true
	return finished
  elseif (redOutputType=="bundled") and colors.test(rs.getBundledInput(bundledSide),bundledInColor) then
	rs.setBundledOutput(bundledSide,bundledOutColor)
	local finished = true
	return finished
  end
end

function CloseTollway(inputside) --closes tollway if open. also returns if it was successful.
  os.pullEvent("redstone")
  if redOutputType=="redstone" and rs.getInput(inputside,true) then
	rs.setOutput(redout,false)
	local finished=true
	return finished
  elseif redOutputType=="bundled" and colors.test(rs.getBundledInput(bundledSide),bundledPassColor) then
	rs.setBundledOutput(bundledSide,0)
	local finished = true
	return finished
  end
end

function makefile(filename)
  local file =fs.open(filename,"a")
  file.close(file)
end

function fileGetNextEmptyLine(file) --also reads file into a returned table
  local lines={}
  local i = 1
  hFile = fs.open(file, "r")
  repeat
	lines[i] = hFile.readLine()
	local i = i + 1
  until lines[i] == "" or lines[i] == nil
  hFile.close()
  return i, lines
end

function fileGetData(file)
  local lines={}
  local i = 1
  hFile = fs.open(file, "r")
  repeat
	lines[i] = hFile.readLine()
	local i = i + 1
  until lines[i] == "" or lines[i] == nil
  hFile.close()
  return lines
end

function fileRemoveData(file,stringToRemove) --unfinished, possibly redundant
  local i,tab = fileGetNextEmptyLine(file)
  for key, val in pairs(tab) do
	if tab[val] == stringToRemove then
	  tab[val] = ""
	end
  end
end

function fileReplaceData(file,stringToRemove,stringToReplaceWith) --unfinished
  i,tab = fileGetNextEmptyLine(file)
  for key, val in pairs(tab) do
	if tab[val] == stringToRemove then
	  tab[val] = stringToReplaceWith
	end
  end
end

function fileSave(file,seperator,fDataTable) --saves data to file from a table
  local hFile = fs.open(file)
  for key,val in pairs(fDataTable) do
	fs.writeLine(key,seperator,val)
  end
  hFile.close()
end

function getUserInput(stringPrompt)  
  print(stringPrompt)
  local result = io.read()
  return result
end

function startupConfig()
  if not fs.exists("config") then
	term.clear()
	term.setCursorPos(1,1)
	print("Tollway by CreeperGoBoom")
	print("Version: ",version)
	print("Easy Setup Config Wizard")	--This is where all the prints for the easy setup wizard need to be
	print("Toll behaviour:")
	repeat
	  redOutputType=getUserInput("What control type are you using? redstone or bundled?")
	until redOutputType=="redstone" or "bundled"
	repeat
	  tollType=getUserInput("What type of toll do you want: freepass, perma-toll or debt-toll?")
	  if tollType=="freepass" then
		freepass=true
	  elseif tollType== "perma-toll" then
		freepass=false
		hasdebt=false
	  elseif tollType== "debt-toll" then
		freepass=false
		hasdebt=true
	  else
		print("Toll Type: Your answer does not match available answers. please try again.")
	  end
	until tollType=="freepass" or "perma-toll" or "debt-toll"
	if tollType=="debt-toll" then
	  repeat
		afterDebtAction=getUserInput("What do you want this tollway set to after debt paid? freepass or perma-toll?")
	  until afterDebtAction=="freepass" or "perma-toll"
	  tolldebt=getUserInput("How much do you want to charge in total before this tollway is set to "..afterDebtAction.."?")
	  --print("How much do you want to charge in total before this tollway is set to ",afterDebtAction,"?")
	  --tolldebt=io.read()
	end
	if tollType ~="freepass" then
	  print("Container info:")
	  repeat
		chestside=getUserInput("What side is your container to push items from?")
		if chestside~=("left" or "right" or "front" or "back" or "top" or "bottom") then
		  print("Answer not available. please use left, right, up or down.")
		end
	  until chestside=="left" or "right" or "front" or "back" or "top" or "bottom"
	  local chest=peripheral.wrap(chestside)
	  print("Payment Info:")
	  paytype=getUserInput("What is the Display Name of the item you would like to use?")
	  toll=getUserInput("How many "..paytype.." would you like to charge per admission?")
	  if (tonumber(toll)>tollmax) and (hungry==false) then
		repeat
		  print("Container size verification error. Toll amount more than container size!")
		  print("You selected (you/container): ",toll,"/",tollmax)
		  print("Note: This error does not occur while hungry toll mode is on")
		  toll=getUserInput("How many "..paytype.." would you like to charge per admission?")
		until toll < tollmax
	  end
	end
	repeat
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Tollway by CreeperGoBoom. version: ",version)
	  print("Settings Confirmation Page")
	  print("Toll Control type: ",redOutputType)
	  print("Toll Type: ",tollType)
	  --if tollType=="freepass" then
		--print("Chest: NOT NEEDED")
		
	  --elseif tollType=="debt-toll" then
	  
	  --elseif tollType=="perma-toll" then
	  
	  --end
	  local cDone=getUserInput("Please confirm your settings. Enter Y to save, Enter N to start over. Note: Non case sensitive.")
	  if cDone=="y" then
		makefile("config")
		term.clear()
		term.setCursorPos(1,5)
		print("Thankyou for using the Easy Setup Config Wizard. Have a nice day!")
		print("To reconfigure. Delete 'config' and restart.")
		print("Now starting...")
		sleep(5)
	  elseif cDone=="n" then
		startupConfig()
	  else
		print("I only understand Y or N (non case sensitive). Please try again.")
	  end
	  cDone=cDone:lower()
	until cDone=="y" or "n"
  else
	config=fileGetData("config")
  end
end

function loadSettings()

end

function ClearResults()
  for i,v in pairs(results) do
	results[i]=nil
  end
end

function RingBell(count,delay)
  for i=1,count do
	rs.setOutput(errornoteblockside,true)
	sleep(0.1)
	rs.setOutput(errornoteblockside,false)
	sleep(delay)
  end
end

function AutoUpdate()
  local handle = shell.run("pastebin get QEai09dx temp")
  if handle then
	shell.run("delete startup")
	shell.run("rename temp startup")
  end
end


--AutoUpdate()
startupConfig()

--Code--
term.clear()
term.setCursorPos(1,1)
print("Tollway by CreeperGoBoom")
print("Version: ",version)
print("Current Config Screen")
print("Toll Type: ",tollType)
print("Player admit query side: "..redin)
print("Redstone out control side: "..redout)
print("Admit fulfill detection side: ",redpass)
if not freepass then
  print("Insuffient funds noteblock (redstone) out: ",errornoteblockside)
  print("Payment selected: ",toll," ",paytype)
  print("Maximum payment selectable: ",tollmax," ",paytype)
end


while true do
  while (ispaid==false) and (freepass==false) do
	rs.setOutput(redout,false)
	rs.setBundledOutput(bundledSide,0)
	os.pullEvent("redstone")
	if rs.getInput(redin,true) or colors.test(rs.getBundledInput(bundledSide),bundledInColor) then
	  --sleep(0.25)
	  print("player query detected")
	  for slot= 1,consize do
		if chest.getStackInSlot(slot) then
		  for key,val in pairs(chest.getStackInSlot(slot)) do
			list[key]=val --Gets item info and saves it for slot check of item type
			if key=="display_name" and val==paytype then
			  results[slot]=list["qty"]
			end
		  end
		end
	  end
	  for key,val in pairs(results) do
		print(results[key])
		print(key," : ",val)
		count=count+val
	  end
	  if count >= toll then
		topay=toll
		print("Total payment detected: ",count)
		print("Amount to pay: ",topay)
		RingBell(1,0)
		repeat
		  print(i," : ",v)
		  sleep(0)
		  for key,val in pairs(results) do
			if (val >= toll) and topay > 0 then --This is where the tollway / item push code should go
			  print("Granting access: Code 1")
			  if redOutputType=="redstone" then
				rs.setOutput(redout,true)
			  elseif redOutputType=="bundled" then
				rs.setBundledOutput(bundledSide,bundledOutColor)
			  end
			  if chest.pushItem(pushdir,key,toll) then
				paid = toll
				topay=0
				ispaid=true
				break
			  else
				print("toll not payable from current slot, moving on")
			  end
			elseif val < toll and topay > 0 and val > 0 then
			  print("Counting pennies")
			  if  val < topay  then
				chest.pushItem(pushdir,key,val)
				topay=topay-val
				print("Amount left to pay: ",topay)
				if topay < 0 then
				  error("Oops! something went wrong and chest was overcharged!")
				end
			  elseif val >= topay then
				chest.pushItem(pushdir,key,topay)
				topay=0
				print("Payment success!")
			  end
			end
		  end
		until topay==0
		topay=toll
		ispaid=true
	  else
		print("Access denied! Insufficient funds!")
		RingBell(errorsoundcount,errorsounddelay)
	  end
	  if redOutputType=="redstone" then
		rs.setOutput(redout,true)
	  elseif redOutputType=="bundled" then
		rs.setBundledOutput(bundledSide,bundledOutColor)
	  end
	  --reset everything
	  results={}
	  count=0
	end
  end
  while ispaid and (freepass==false) do
	if CloseTollway(redpass,bundledPassColor) then
	  ispaid=false
	  topay=toll
	end
  end
  while freepass do
	if OpenTollway(redin,bundledInColor) then CloseTollway(redpass,bundledPassColor) end
  end
end

-- while ispaid do
  -- print("access granted")
  -- ispaid=false
-- end


What could possibly cause things to work and then not?

EDIT: LOL I think I know whats about to be said, you said it earlier in another post…

Meh. I'm done with this MC version anyway. too unreliable, must be a memory leak causing things to work and then not or behave oddly.
Edited on 17 December 2019 - 11:28 AM
Lupus590 #2
Posted 17 December 2019 - 01:37 PM
That's not how "or" works (or "and" for that matter)

"Or" and "and" combine booleans, your strings are not booleans, so you need the == variable again

Also, have you tried the discord or the new forums? People might respond quicker there.
CreeperGoBoom #3
Posted 17 December 2019 - 02:32 PM
Ahh of course, those string only become booleans once they have been checked:

if if string=="this" is now a boolean because if it is then it becomes true. if not false

Thanks Lupus
Edited on 17 December 2019 - 01:32 PM