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

Function Stuck?

Started by Lewisk3, 03 July 2015 - 12:54 AM
Lewisk3 #1
Posted 03 July 2015 - 02:54 AM
so i was working on my email when, i found that when i click the inbox button the whole computer freezes!, please help

code:
Spoiler

local mailpath = "cozy/programs/email/"
local mailList = {}
local bgcolor = colors.black
local inEmail = false
local mType = "inbox"
local opMail = ""
local mailSender= ""
local id = os.getComputerID()
local username = ""
foundMail = false
local inInbox=false
os.loadAPI("ea")
os.loadAPI("cozy_string")
write("Enter Rednet Modem Side: ")
rednet.open(read())

function clearLineLength(x,y,l)
ea.scp(x,y)
ea.sbc(colors.white)
print("							    ")
end
function getUser()
ea.sbc(colors.blue)
term.clear()
ea.scp(20,5)
print("Sign Into User")
ea.scp(1,10)
print("Username: ")
print("Password: ")
ea.scp(10,10)
us = read()
ea.scp(10,11)
up = read("*")
rednet.broadcast("COZY_GETUSER")
local id, msg = rednet.receive(2)
if(msg == nil)then error("Connection to server failed!") end
rednet.send(id,us)
local id, valid = rednet.receive()
if(valid)then
  local id, cpass = rednet.receive()
  if(up == cpass)then corpass=true else corpass = false end
end
if(valid and corpass)then inEmail = true; username = us; clear(); CMail() end
if(not valid)then ea.sp("Username Invalid") getUser() end
if(not corpass)then ea.sp("Password Invalid") getUser() end
end

function createUser()
ea.sbc(colors.blue)
term.clear()
ea.scp(20,5)
print("Create User")
ea.scp(1,10)
print("Username: ")
print("Password: ")
ea.scp(10,10)
us = read()
ea.scp(10,11)
up = read()

rednet.broadcast("COZY_CREATEUSER")
local id, got = rednet.receive(2)
if(got ~= nil)then
  rednet.send(tonumber(id),us)
  local id, valid = rednet.receive()
   if(valid)then
    rednet.send(tonumber(id),up)
    ea.sp("User Created!")
   
    f = fs.open("cozy/programs/email/login/.user","w")
    f.writeLine("--]] Has Login [[--")
    f.writeLine("true")
    f.close()
   
    getUser()
   else
	 ea.sp("Username Unavailable!")
   end
else
  error("Server Connection Failed!")
end


end

function clear()
ea.sbc(bgcolor);
term.clear();
ea.scp(1,1)
ea.sbc(colors.blue)
term.clearLine()
ea.scp(1,1)
ea.stc(colors.white)
print("COZYMAIL")
ea.sbc(colors.gray)
for i = 3, 15 do
  ea.scp(1,i)
  print(string.rep(" ",12))
end
ea.sbc(colors.white)
for i = 3, 17 do
  ea.scp(15,i)
  print(string.rep(" ",36))
end
ea.scp(15,3)
ea.stc(colors.black)
print("["..string.rep("=",33).."]")

ea.scp(1,3)
ea.stc(colors.green)
ea.sbc(colors.gray)
print("|---------|")
print("| [INBOX] |")
print("| [SPAM ] |")
print("| [SEND ] |")
print("|---------|")
end

function getMail(ftype)
end
function openMail(mail)
i = 0
  clear()
  ea.scp(15,4)
  ea.scp(15,15)
  print("[REPLY]")
  ea.stc(colors.red)
  ea.scp(43,15)
  print("[DELETE]")
  ea.stc(colors.black)
  ea.sbc(colors.white)
  f = io.open(mail,"r")
  for line in f:lines() do
   ea.scp(15,i+4)
   print(line)
   i = i + 1
  end
end
function updateEmail()

m = {os.pullEvent()}
if(m[1] == "mouse_click" and m[2] == 1)then

  if(m[3] >= 3 and m[3] <= 8 and m[4] == 4 and not inInbox)then
  
   --mType = "inbox"
   clear()
   ea.scp(15,4)
   updateEmailFiles()
   if(readingMail)then readingMail = false end
   if(sending)then sending = false end
   inInbox=true
  end
  if(m[3] >= 3 and m[3] <= 8 and m[4] == 5)then
  
   --mType = "spam"
   clear()
   ea.scp(15,4)
   getMail("spam")
   printEmail("spam")
   if(readingMail)then readingMail = false end
   if(sending)then sending = false end
  end
  if(m[3] >= 3 and m[3] <= 8 and m[4] == 6)then
  
   clear()
   sendEmail()
   t = 0;
   sending=true
  end
  if(not readingMail and not sending)then
    for k,v in pairs(mailList) do
	  
	  if(m[3] >= 15 and m[3] <= #v.sender+15 and m[4] == v.offset+3)then
	   ea.sbc(colors.white)
	   readingMail = true
	   print(v.path.." "..v.offset)
		 openMail(v.path)
		 opMail = v.path
		 mailSender = v.sender
		 mType = v.type
	  end
    end
  else
   if(not sending)then
    if(m[3] >= 16 and m[3] <= 21 and m[4] == 15)then
	 rednet.broadcast(mailSender)
	
	 local id, ms = rednet.receive(1)
	 if(ms == nil)then ea.scp(15,16); ea.sp("Failed To Contact Sender!") end
    end
    if(m[3] >= 43 and m[3] <= 50 and m[4] == 15)then
	
	 fs.delete(opMail)
	 if(mType == "inbox")then
	  clear()
	  ea.scp(15,4)
	  updateEmailFiles()
	  readingMail = false
	 end
	 if(mType == "spam")then
	  clear()
	  ea.scp(15,4)
	  readingMail = false
	 end
    end
   end
   if(sending and not readingMail)then
	 if(m[3] >= 16 and m[4] == 4)then
	   
	    clearLineLength(19,4,15)
	    ea.scp(19,4)
	    rcr=read()
	 end
	 if(m[3] >= 16 and m[4] == 5)then
	   clearLineLength(24,5,15)
	   ea.scp(24,5)
	   rcsub=read()
	 end
	 if(m[3] >= 16 and m[4] == 8)then

	  ea.scp(15,8)
	  clearLineLength(15,8,15)
	  ea.scp(15,9)
	  clearLineLength(15,9,15)
	  ea.scp(15,10)
	  clearLineLength(15,10,15)
	  ea.scp(15,11)
	  clearLineLength(15,11,15)
	  ea.scp(15,12)
	  clearLineLength(15,12,15)
	  ea.scp(15,13)
	  clearLineLength(15,13,15)
	  ea.scp(15,14)
	  clearLineLength(15,14,15)
	  ea.scp(15,8)
	  bp1=read()
	  ea.scp(15,9)
	  bp2=read()
	  ea.scp(15,10)
	  bp3=read()
	  ea.scp(15,11)
	  bp4=read()
	  ea.scp(15,12)
	  bp5=read()
	  ea.scp(15,13)
	  bp6=read()
	  ea.scp(15,14)
	  bp7=read()
	 end
	 if(m[3] >= 43 and m[3] <= 49 and m[4] == 17)then
	  if(rcr ~= nil and rcsub ~= nil)then sendCozyMail(rcr,rcsub,bp1,bp2,bp3,bp4,bp5,bp6,bp7) end
	 end
   end
  end
end

end

function sendCozyMail(to,subject,bl1,bl2,bl3,bl4,bl5,bl6,bl7)

   f = fs.open(".tempcozymail","w")
   f.writeLine(to)
   f.writeLine(subject)
   f.writeLine(" ")
   f.writeLine(bl1)
   f.writeLine(bl2)
   f.writeLine(bl3)
   f.writeLine(bl4)
   f.writeLine(bl5)
   f.writeLine(bl6)
   f.writeLine(bl7)
   f.close()

   f = fs.open(".tempcozymail","r")
   sendm = f.readAll()
   f.close()
   fs.delete(".tempcozymail")
   rednet.broadcast("COZY_EMAIL")
   id, m1 = rednet.receive(1)
   if(m1 ~= nil)then
    rednet.send(id,to);
    local id, usr = rednet.receive()
   if(usr)then
    rednet.send(id,sendm)
    local id, isOK = rednet.receive(2)
    if(isOK ~= nil)then
	 ea.scp(15,17)
	 ea.stc(colors.green)
	 ea.sp("Mail Sent!")
    else
	 error("[SEVER] Server Crashed! Please Contact Server Admin RIGHT NOW!!")
    end
   else
    ea.scp(15,17)
    ea.sp("No such user!")
   end
  end
   if(m1 == nil)then ea.scp(15,17); ea.sp("Connection With Server Failed :(/>") end

end
function printEmail(type)
for k,v in pairs(mailList) do 
  if(v.type == type)then
   ea.sbc(colors.white)
   ea.scp(15,v.offset+3)
   if(v.sender == nil or v.disc == nil)then ea.stc(colors.red); print("CORRUPED EMAIL") else ea.stc(colors.black); print(v.sender.." - "..v.disc) end
  end
end
end
function sendEmail()
clear()
ea.stc(colors.black)
ea.sbc(colors.white)
ea.scp(15,4)
print("|To:							   |")
ea.scp(15,5)
print("|Subject:						  |")
ea.scp(15,6)
print("|==================================|")
ea.scp(15,7)
print("[Body]")
ea.scp(45,17)
ea.sbc(colors.green)
print("[SEND]")

end

function receiveEmailedFile()

   amm = math.random(1,9999999)
  rednet.send(senID,"FILE")
  id, dehfile = rednet.receive(1)
  f = fs.open("cozy/programs/email/inbox/cozymailID_"..amm,"w")
  f.write(dehfile)
  f.close()
end

function updateEmailFiles()
  rednet.broadcast("COZY_EMAIL_UPDATE")
local id, m = rednet.receive(2)
rednet.send(tonumber(id),username)
if(m == nil)then ea.scp(1,1) ea.sbc(colors.red) term.clearLine() ea.stc(colors.white); print("Connection With Server Failed!") end
if(m ~= nil)then
  local id, amount = rednet.receive()
  for i = 0, amount do
  
   id, file = rednet.receive()
   amm = math.random(1,9999999)
   f = fs.open("cozy/programs/email/inbox/cozymailID_"..amm,"w")
   f.write(file)
   f.close()
  end
 
   mailList = {}
  
   for k,v in pairs(fs.list(mailpath..ftype)) do
    if(mailList[v] == nil and not fs.isDir(mailpath..ftype..v))then
	 f = fs.open(mailpath..ftype.."/"..v,"r")
	 conl = f.readLine()
	 cDisc = f.readLine()
	 f.close()
	 mailList[v] = {type=ftype,sender=conl,disc = cDisc,offset = k,path=mailpath..ftype.."/"..v}
    end
   end
  printMail("inbox")
 
end
end

function para()
para()
end
function CMail()
getMail("spam")
getMail("inbox")
clear()
while inEmail do
  updateEmail()
   --para()
end
end

if(not fs.exists("cozy/programs/email/login/.user"))then
createUser()
else
getUser()
end
Edited on 03 July 2015 - 04:49 AM
Lewisk3 #2
Posted 03 July 2015 - 03:22 AM
Help anyone? im soo stuck here
Edited on 03 July 2015 - 04:49 AM
valithor #3
Posted 03 July 2015 - 08:24 AM
Does it actually print the mail or freeze on the screen with the buttons?
– If it prints the mail and then "freezes" it is because it is stuck in a loop attempting to redraw the mail over and over. When the inbox button is clicked it sets the other two variables to false, which mean only one of the if statements in the function can run (the one to draw the mail). Because no where else in the function has a place where the other two variables are changed it continously runs that one if statement over and over. I really have no idea what is going on if it freezes on the screen with the buttons.
Is the screen blank?
– There is no mail to print, so the program is stuck in a loop attempting to redraw the non existant mail over and over.

What is suppose to happen?

Seeing as this is almost 400 lines long I need more information before I am even going to try to help. Please give any information you can about what is suppose to happen what does happen if anything, and what is going on when it "Freezes".
Edited on 03 July 2015 - 06:35 AM
Lewisk3 #4
Posted 04 July 2015 - 04:36 AM
i fixed the issue, cant exactly remember what it was but heres the working code, its not finished yet, but look at its 500 lines makes me confused, i am going to work on it though
time to collapse those if statments!

http://pastebin.com/M0LTnX57