Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Nano banking OS
- local bankServerID=3
- modem=rednet.open("back")
- currentUser=nil
- local desktopBackgroundColor=8
- local taskBarColor=256
- local menuButtonColor=2
- local menuColor=128
- local loginBackgroundColor=8
- local loginPanelColor=256
- local loginBarColor=128
- local loginButtonColor=128
- local screenX,screenY=term.getSize()
- if math.ceil(screenY/2)~=math.floor(screenY/2) then
- screenY=screenY-1
- end
- function relogin()
- rednet.send(bankServerID,"disconnect "..currentUser)
- repeat
- id,returnedMessage=rednet.receive(1)
- until id==bankServerID or id==nil
- if id==nil then
- loginErrorMessage(" Request timed out! ",2)
- return true
- end
- currentUser=nil
- drawLogin()
- loginEvents()
- end
- function kick()
- rednet.send(bankServerID,"disconnect "..currentUser)
- repeat
- id,returnedMessage=rednet.receive(1)
- until id==bankServerID or id==nil
- if id==nil then
- loginErrorMessage(" Request timed out! ",2)
- return true
- end
- currentUser=nil
- loginEvents()
- end
- mainMenuItems={
- ["Shut down"]={['c']=16384,['f']=os.shutdown},
- ["Reboot"]={['c']=32,['f']=os.reboot},
- ["Log off"]={['c']=2,['f']=relogin},
- }
- function os.pullEvent()
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- if event == "terminate" and termDisabled then
- end
- return event, p1, p2, p3, p4, p5
- end
- defaultPrograms={
- ["updateOS"]="7BEqH0QS"
- }
- for id,data in pairs(defaultPrograms) do
- if fs.exists(id)==false then
- shell.run("pastebin","get",data,id)
- end
- end
- function drawLogin()
- termDisabled=true
- userString=""
- passString=""
- term.setTextColor(2)
- loginDrawn=true
- term.setBackgroundColor(loginBackgroundColor)
- term.clear()
- term.setCursorPos(screenX/2-10,screenY/2-5)
- term.setBackgroundColor(loginBarColor)
- term.write(" Login ")
- term.setBackgroundColor(loginPanelColor)
- for i=1,7 do
- term.setCursorPos(screenX/2-10,(screenY/2-5)+i)
- term.write(" ")
- end
- term.setBackgroundColor(1)
- term.setCursorPos(screenX/2-2,(screenY/2-2))
- term.write(" ")
- term.setCursorPos(screenX/2-2,(screenY/2+0))
- term.write(" ")
- term.setBackgroundColor(loginPanelColor)
- term.setCursorPos(screenX/2-7,(screenY/2-2))
- term.write("User")
- term.setCursorPos(screenX/2-7,(screenY/2+0))
- term.write("Pass")
- term.setCursorPos(screenX/2+4,screenY/2+2)
- term.setBackgroundColor(loginButtonColor)
- term.write("Login")
- term.setBackgroundColor(1)
- term.setTextColor(32768)
- term.setCursorPos(screenX/2-2,(screenY/2-2))
- term.setCursorBlink(true)
- end
- function loginEvents()
- repeat
- event,v1,v2,v3,v4,v5=os.pullEvent()
- if event=="char" then
- loginType(v1)
- elseif event=="mouse_click" then
- button=v1
- x=v2
- y=v3
- if button==1 then
- if x>=screenX/2-3 and x< screenX/2+6 and y==math.floor(screenY/2-2) then
- term.setCursorPos(screenX/2-2+string.len(userString),(screenY/2-2))
- elseif x>=screenX/2-3 and x< screenX/2+6 and y==math.floor(screenY/2+0) then
- term.setCursorPos(screenX/2-2+string.len(passString),(screenY/2+0))
- elseif x>=screenX/2+4 and x<screenX/2+9 and y==math.floor(screenY/2+2) then
- serialiseLogin()
- end
- end
- elseif event=="key" then
- key=v1
- if key==14 then
- loginBackspace()
- elseif key==15 then
- loginTab()
- elseif key==28 then
- serialiseLogin()
- end
- elseif event=="timer" then
- if v1==kickTimer then
- drawLogin()
- loginErrorMessage(" Your login was timed out! ",2)
- kick()
- end
- end
- until currentUser~=nil
- end
- function serialiseLogin()
- loggedIn=attemptLogin(userString,passString)
- if not loggedIn then
- loginErrorMessage(" Inorrect Password! ",1)
- end
- end
- function attemptLogin(user,pass)
- rednet.send(bankServerID,"testAccount "..user)
- repeat
- id,returnedMessage=rednet.receive(1)
- until id==bankServerID or id==nil
- if returnedMessage=="Account does not exist" then
- rednet.send(bankServerID,"createAccount "..user..","..pass)
- repeat
- id,returnedMessage=rednet.receive(1)
- until id==bankServerID or id==nil
- if returnedMessage=="true" then
- passLength=string.len(pass)
- if passLength<8 then
- for i=1,8-passLength do
- pass=pass.." "
- end
- end
- login(user)
- elseif id==nil then
- loginErrorMessage(" Request timed out! ",2)
- else
- loginErrorMessage(" An error occured ",1)
- loginErrorMessage(" Error #"..returnedMessage.." ",2)
- end
- return true
- elseif returnedMessage=="true" then
- rednet.send(bankServerID,"requestPassword "..user..","..pass)
- repeat
- id,returnedMessage=rednet.receive(1)
- until id==bankServerID
- if returnedMessage=="true" then
- login(user)
- return true
- else
- return false
- end
- elseif returnedMessage~="Incorrect password" then
- loginErrorMessage(" An error occured ",1)
- if returnedMessage~=nil then
- loginErrorMessage(" Error #"..returnedMessage.." ",2)
- end
- return true
- elseif id==nil then
- loginErrorMessage(" Request timed out! ",2)
- return true
- end
- end
- function loginErrorMessage(message,timeOut)
- if message~=nil then
- oldX,oldY=term.getCursorPos()
- term.setCursorBlink(false)
- term.setCursorPos(screenX/2-string.len(message)/2,screenY/2+5)
- term.setTextColor(16384)
- term.setBackgroundColor(32768)
- term.write(message)
- sleep(timeOut)
- term.setBackgroundColor(8)
- term.setCursorPos(screenX/2-string.len(message)/2,screenY/2+5)
- for i=1,string.len(message) do
- term.write(" ")
- end
- term.setCursorPos(oldX,oldY)
- term.setTextColor(32768)
- term.setCursorBlink(true)
- end
- end
- function login(user)
- rednet.send(bankServerID,"connect "..user)
- repeat
- id,returnedMessage=rednet.receive(1)
- until id==bankServerID or id==nil
- if id==nil then
- loginErrorMessage(" Request timed out! ",2)
- return true
- elseif returnedMessage~="true" then
- loginErrorMessage(returnedMessage,2)
- end
- kickTimer=os.startTimer(25)
- currentUser=user
- term.setCursorBlink(false)
- loginDrawn=false
- currentDirectory=""
- drawDesktop()
- desktopEvents()
- end
- function loginType(char)
- x,y=term.getCursorPos()
- if y==math.floor(screenY/2+0) and string.len(passString)<8 then
- writeChar="*"
- passString=passString..char
- elseif string.len(userString)<8 then
- writeChar=char
- userString=userString..char
- end
- term.setBackgroundColor(1)
- term.write(writeChar)
- end
- function loginTab()
- x,y=term.getCursorPos()
- if y==screenY/2 then
- term.setCursorPos(screenX/2-2+string.len(userString),(screenY/2-2))
- else
- term.setCursorPos(screenX/2-2+string.len(passString),(screenY/2+0))
- end
- end
- function loginBackspace()
- x,y=term.getCursorPos()
- x,y=term.getCursorPos()
- if x>=screenX/2-2 then
- if y==screenY/2 then
- passString=string.sub(passString,0,string.len(passString)-1)
- elseif y==screenY/2-2 then
- userString=string.sub(userString,0,string.len(userString)-1)
- end
- term.setBackgroundColor(1)
- x,y=term.getCursorPos()
- term.setCursorPos(x-1,y)
- term.write(" ")
- term.setCursorPos(x-1,y)
- end
- end
- function drawDesktop()
- if currentUser==nil then
- loginEvents()
- else
- term.setBackgroundColor(desktopBackgroundColor) --Taskbar on top of the screen
- touchScreenTable={}
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(taskBarColor)
- for i=1,screenX do
- term.setCursorPos(i,1)
- term.write(" ")
- --term.setCursorPos(i,2)
- --term.write(" ")
- end
- term.setCursorPos(1,1)
- term.setBackgroundColor(menuButtonColor)
- term.setTextColor(32768)
- term.write(" Menu ")
- touchScreenTable["menu"]={1,1,10,0,false,mainMenuFunction}
- term.setBackgroundColor(taskBarColor)
- term.setCursorPos(screenX-10,1)
- term.setTextColor(32768)
- term.write(currentUser)
- term.setBackgroundColor(8)
- term.setTextColor(1)
- term.setCursorPos(1,2)
- end
- end
- function backFile()
- touchScreenTable={}
- drawDesktop()
- end
- function mainMenuFunction()
- if mainmenuShown==nil then
- mainmenuShown=false
- end
- if mainmenuShown==true then
- touchScreenTable={}
- drawDesktop()
- else
- term.setTextColor(1)
- term.setCursorPos(1,1)
- term.setBackgroundColor(menuColor)
- term.write(" Menu ")
- term.setBackgroundColor(menuColor)
- for i=1,3 do
- term.setCursorPos(1,i+1)
- term.write(" ")
- end
- term.setBackgroundColor(menuColor)
- i=2
- for name,data in pairs(mainMenuItems) do
- term.setCursorPos(1,i)
- term.setTextColor(data['c'])
- nameLength=string.len(name)
- if nameLength<9 then
- for g=1,9-nameLength do
- name=name.." "
- end
- end
- term.write(name)
- if data['f']==nil then
- touchScreenTable[name]={1,i,9,0,true}
- else
- touchScreenTable[name]={1,i,9,0,false,data['f']}
- end
- i=i+1
- end
- end
- mainmenuShown=not mainmenuShown
- end
- function desktopEvents()
- while true do
- event,v1,v2,v3,v4,v5=os.pullEvent()
- if event=="mouse_click" then
- button=v1
- x=v2
- y=v3
- for id,data in pairs(touchScreenTable) do
- if button==1 and x>=math.floor(data[1]) and x<=math.floor(data[3]) and y>=math.floor(data[2]) and y<=math.floor(data[2]+data[4]) then
- if data[5] then
- termDisabled=false
- shell.run(currentDirectory.."/"..id)
- drawDesktop()
- termDisabled=true
- elseif fs.isDir(currentDirectory.."/"..id) then
- currentDirectory="users/"..currentUser.."/"..id
- drawDesktop()
- termDisabled=true
- else
- termDisabled=false
- data[6](id)
- termDisabled=true
- end
- end
- end
- elseif event=="char" then
- elseif event=="key" then
- elseif event=="timer" then
- if v1==kickTimer then
- drawLogin()
- loginErrorMessage(" Your login was timed out! ",2)
- kick()
- end
- end
- end
- end
- function startOS()
- drawLogin()
- loginEvents()
- end
- startOS()
Add Comment
Please, Sign In to add comment