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

[OS][Clock issue?]Cursor moving from desired point

Started by Kryptanyte, 11 February 2013 - 09:05 PM
Kryptanyte #1
Posted 11 February 2013 - 10:05 PM
Okay, so I have been developing a basic control system for a facility with a lock etc. I have been having a sort of bug with the login part however, when you are on the login screen, the cursor will move from the area it is supposed to be, to a point on the right margin a few lines above the text, it seems to just be a display bug as when you continue typing it will resume its current position. I think is has something to do with the 'timeDisplay()' function that is displaying the time. Now the code is a bit messy and it might be a little random, I know I have random ass things where not needed but its basically a BETA version. Anyway, any help would be appreciated, thanks!

PS. Theres two login users for debugging; login:pass and test:testing

Spoiler

debugMode = "true"
rednet.open("right")
accessGrantedMessage = "Access Granted You May Proceed"
accessLevelDeniedMessage = "Access Level Not High Enough"
accessDeniedMessage = "Access Denied To This Terminal"
accessRequired = 3
authenticationErrorMessage = "Error, something went wrong"

function configOptions()



end

function mathThings()

	xMax, yMax = term.getSize()
	xHalfMax = math.floor(xMax/2)
	yHalfMax = math.floor(yMax/2)
	
end

function errorMessage(errorType, messageType)

	mathThings()

	if errorType == "login" then
	
		if messageType == "username" then
	
			xUserError = math.floor(xHalfMax - string.len(usernameErrorMessage)/2)
			
			term.clear()
			term.setCursorPos(xUserError, yHalfMax)
			term.write(usernameErrorMessage)
			term.setCursorPos(1, yHalfMax+1)
			
		elseif messageType == "password" then
		
			xPassError = math.floor(xHalfMax - string.len(passwordErrorMessage)/2)
			
			term.clear()
			term.setCursorPos(xPassError, yHalfMax)
			term.write(passwordErrorMessage)
			term.setCursorPos(1, yHalfMax+1)
			
		elseif messageType == "auth" then
		
			xAuthError = math.floor(xHalfMax - string.len(authenticationErrorMessage)/2)
			
			term.clear()
			term.setCursorPos(xAuthError, yHalfMax)
			term.write(authenticationErrorMessage)
			term.setCursorPos(1, yHalfMax+1)
			
			
		end
		
	end
		
end

function checkLogin(name, pass)

	if debugMode == "false" then
	
		rednet.send(server, name)
		sleep(1)
		rednet.send(server, pass)
	
		event, id, EncryptedAccess = os.pullEvent("redstone_message")

		authenticationEncrypted = EncryptedAccess
	
		event, id, EncryptedLevel = os.pullEvent("redstone_message")
	
		levelEncrypted = EncryptedLevel
	
		event, id, serverid = os.pullEvent("redstone_message")
	
		decryptID = serverid


	
		if Encrypted == "true" then
	
			decryptData = {authenticationEncrypted, levelEncrypted}

			decryptSend = textutils.serialize(decryptData)
		
			rednet.send(decryptID, decryptSend)
		
			decryptID = nil
		
			event, id, decryptedData = os.pullEvent("redstone_message")
		
			data = textutils.unserialize(decryptedData)
		
			return data[1], data[2]
		
		elseif Encrypted == "false" then

			return authenticationEncrypted, LevelEncrypted
		
		end
		
	--elseif debugMode == "true" then
	else
	
		debugUser1 = "login"
		debugUser2 = "test"
		debugPass1 = "pass"
		debugPass2 = "testing"
		
		if name == debugUser1 and pass == debugPass1 then
		
			return "accessGranted", 3
			
		elseif name == debugUser2 and pass == debugPass2 then
		
			return "accessGranted", 1
			
		else
		
			return "accessDenied"
		
		end
		
	end			
	
end

function drawGUI()
	term.clear()
	xMax, yMax = term.getSize()
	term.setCursorPos(1,1)
	xPos = 2
	for i = 1, xMax - 2 do
		
		term.setCursorPos(xPos,1)
		term.write("-")
		term.setCursorPos(xPos,3)
		term.write("-")
		term.setCursorPos(xPos,yMax)
		term.write("-")		
		xPos = xPos + 1
		
	end
	yPos = 1
	for i = 1, yMax do
		
		term.setCursorPos(1,yPos)
		term.write("|")
		term.setCursorPos(xMax, yPos)
		term.write("|")
		yPos = yPos + 1
	
	end
	
	term.setCursorPos(2,2)
	
end

function timeDisplay()
	while true do
	if running == false then
	xMax, yMax = term.getSize()
	local getTime = os.time()
	printTime = textutils.formatTime(getTime, false)
	
	term.setCursorPos(xMax - 1 - #printTime, 2)
	term.write(printTime)
	sleep(3)
	term.setCursorPos(2,4)
	else
	
	end
end
end



function writeLogin()

	xMax, yMax = term.getSize()
	xUser = math.floor((xMax/2)- string.len("Username: "))
	xPass = math.floor((xMax/2)- string.len("Password: "))
	yUser = math.floor((yMax/2)- 1)
	yPass = math.floor((yMax/2)+ 1)
	xText = math.floor(xMax/2)
	
	term.clear()
	
	term.setCursorPos(xUser, yUser)
	term.write("Username: ")
	term.setCursorPos(xPass, yPass)
	term.write("Password: ")
	
	return xText, yUser, yPass
	
end

function Login()

	xPos, yUser, yPass = writeLogin()
	
	term.setCursorPos(xPos, yUser)
	user = read()
	term.setCursorPos(xPos, yPass)
	pass = read("*")
	
	return user, pass
	
end

function lock()

	user, pass = Login()
	authentication, level = checkLogin(user, pass)
	
	if level ~= nil then
	
		if authentication == "accessGranted" then
		
			return authentication, level
		
		elseif authentication == "false" then
		
			return authentication
			
		else
		
			
		
		end
		
	end
	
end

function checkEnter()

	p1, p2, p3, p4 ,p5 = os.pullEvent()
	if p1 == "key" then
	
		if p2 == 28 then
		
		return true
		
		else
		
		return false
		
		end
		
	else
	
	return false
	
	end

end
running = false
function main()

drawGUI()

while true do
	running = false

	running = checkEnter()

	if running == true then
		
		auth, level = lock()
		mathThings()
		

		if auth == "accessGranted" then

			if level >= accessRequired then

				xGranted = math.floor(xMax/2 - math.floor(string.len(accessGrantedMessage)/2))
	
				term.clear()
				term.setCursorPos(xGranted, yHalfMax)
				term.write(accessGrantedMessage)
				sleep(3)
				term.clear()
				term.setCursorPos(1,1)
				break
		
			else
	
				xLevelError = math.floor(xMax/2 - math.floor(string.len(accessLevelDeniedMessage)/2))
		
				term.clear()
				term.setCursorPos(xLevelError, yHalfMax)
				term.write(accessLevelDeniedMessage)
				sleep(3)
				term.clear()
				term.setCursorPos(1,1)
				break
			
			end
	
		else
	
			xDenied = math.floor(xMax/2 - math.floor(string.len(accessDeniedMessage)/2))
	
			term.clear()
			term.setCursorPos(xDenied, yHalfMax)
			term.write(accessDeniedMessage)
			sleep(3)
			term.clear()
			term.setCursorPos(1,1)
			break
		
		end	
		
	else
	
	end
end
end

parallel.waitForAny(main, timeDisplay)
remiX #2
Posted 11 February 2013 - 10:30 PM
Try adding x,y = term.getCursorPos() before printing then set it to that after printin

function timeDisplay()
while true do
if running == false then
xMax, yMax = term.getSize()
local getTime = os.time()
printTime = textutils.formatTime(getTime, false)
Local x, y = term.getCursorPos()
term.setCursorPos(xMax - 1 - #printTime, 2)
term.write(printTime)
term.setCursorPos(x,y)
sleep(3)
else

end
end
end
Kryptanyte #3
Posted 11 February 2013 - 10:59 PM
Ahh, thank you. I didn't spot that error. Must have put that in when I was messing with it before I made the ugly GUI xD

Okay quick edit, for anyone who is using the time function on this code, after the 'else' statement, make sure you put a sleep(number) there, otherwise the while loop doesn't know what to and will run to long without yeilding.