Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function overwrite(sName,replacement)
- rawset(getfenv(),sName,replacement)
- rawset(_G,sName,replacement)
- local cur=0
- repeat
- cur=cur+1
- local ok,t=pcall(function() return getfenv(cur) end)
- if ok and type(t)=="table" and type(rawget(t,sName))=="table" then
- rawset(t,sName,replacement)
- end
- until not ok
- end
- local oldTerm=term
- local term=setmetatable({},{__index=oldTerm})
- term.old=oldTerm
- local tCharacterRecord={}
- local tTextColorRecord={}
- local tBackgroundColorRecord={}
- local tColorShortHand={w=colors.white,o=colors.orange,m=colors.magenta,B=colors.lightBlue,y=colors.yellow,l=colors.lime,P=colors.pink,g=colors.gray,G=colors.lightGray,c=colors.cyan,p=colors.purple,b=colors.blue,z=colors.green,r=colors.red,a=colors.black}
- local iTextColor=colors.white
- term.setTextColor=function(iColor)
- iTextColor=iColor
- return oldTerm.setTextColor(iColor)
- end
- term.setTextColour=function(iColor)
- iTextColor=iColor
- return oldTerm.setTextColour(iColor)
- end
- term.getTextColor=function()
- return iTextColor
- end
- local iBackgroundColor=colors.black
- term.setBackgroundColor=function(iColor)
- iBackgroundColor=iColor
- return oldTerm.setBackgroundColor(iColor)
- end
- term.setBackgroundColor=function(iColor)
- iBackgroundColor=iColor
- return oldTerm.setBackgroundColor(iColor)
- end
- term.getBackgroundColor=function()
- return iBackgroundColor
- end
- term.write=function(sText)
- local CursorPosition={term.getCursorPos()}
- local TextColorShorthand="w"
- local backgroundColorShorthand="a"
- for k,v in pairs(tColorShortHand) do
- if v==term.getTextColor() then TextColorShorthand=k end
- if v==term.getBackgroundColor() then backgroundColorShorthand=k end
- end
- local function replaceLine(tRecord,sAddText)
- tRecord[CursorPosition[2]+1]=string.sub(tRecord[CursorPosition[2]+1] or "",1,CursorPosition[1]-1)..(CursorPosition[1]>string.len(tRecord[CursorPosition[2]+1] or "") and string.rep(" ",CursorPosition[1]-string.len(tRecord[CursorPosition[2]+1] or "")-1) or "")..sAddText..string.sub(tRecord[CursorPosition[2]+1] or "",CursorPosition[1]+#sAddText)
- end
- replaceLine(tCharacterRecord,sText)
- replaceLine(tTextColorRecord,string.rep(TextColorShorthand,#sText))
- replaceLine(tBackgroundColorRecord,string.rep(backgroundColorShorthand,#sText))
- for k,v in ipairs(tCharacterRecord) do
- if #v>CursorPosition[1] then
- tCharacterRecord[k+1]=string.sub(v,CursorPosition[1]+1)..string.sub(tCharacterRecord[k-1],#v+1)
- end
- end
- return oldTerm.write(sText)
- end
- term.getTextAt=function(x,y,iLength)
- if tCharacterRecord[y+1] then
- return string.sub(tCharacterRecord[y+1],x,x+(iLength or 1)-1),tColorShortHand[string.sub(tTextColorRecord[y+1],x,x)],tColorShortHand[string.sub(tBackgroundColorRecord[y+1],x,x)]
- else
- return nil
- end
- end
- term.screenShot=function()
- local tNew={{},{},{}}
- for k,v in pairs(tCharacterRecord) do
- tNew[1][k]=v
- tNew[2][k]=tTextColorRecord[k]
- tNew[3][k]=tBackgroundColorRecord[k]
- end
- return unpack(tNew)
- end
- term.loadScreen=function(t,t2,t3)
- for k,v in pairs(t) do
- term.setCursorPos(1,k)
- for i=1,#v do
- if type(t2)=="table" then
- term.setTextColor(tColorShortHand[string.sub(t2[k],i,i)])
- elseif type(t2)=="number" and i==1 then
- term.setTextColor(t2)
- end
- if type(t3)=="table" then
- term.setBackgroundColor(tColorShortHand[string.sub(t3[k],i,i)])
- elseif type(t3)=="number" and i==1 then
- term.setTextColor(t3)
- end
- write(string.sub(v,i,i))
- end
- end
- end
- term.clear=function()
- tCharacterRecord={}
- tTextColorRecord={}
- tBackgroundColorRecord={}
- return oldTerm.clear
- end
- term.scroll=function(num)
- for k,v in pairs(tCharacterRecord) do
- if type(k)=="number" and type(num)=="number" and k-num>1 then
- tCharacterRecord[k-num]=v
- tTextColorRecord[k-num]=tTextColorRecord[k]
- tBackgroundColorRecord[k-num]=tBackgroundColorRecord[k]
- end
- end
- return oldTerm.scroll(num)
- end
- overwrite("term",term)
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- "Global" variables --
- -------------------------------------------------------------
- -------------------------------------------------------------
- local url = "http://localhost/CC/ccu/"
- local lectures = {}
- local currentUser = nil
- local S_WIDTH, S_HEIGHT = term.getSize()
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- Functions used by all classes --
- -------------------------------------------------------------
- -------------------------------------------------------------
- function string:split(sep)
- local sep, fields = sep or ":", {}
- local pattern = string.format("([^%s]+)", sep)
- self:gsub(pattern, function(c) fields[#fields+1] = c end)
- return fields
- end
- math.round = function(n)
- return math.floor(n + .5)
- end
- local function dostring(s)
- local f = io.open("__tmp", "w")
- f:write(s)
- f:close()
- local t = dofile("__tmp")
- fs.delete("__tmp")
- return t
- end
- local function inheritsFrom(baseClass)
- local new_class = {}
- local class_mt = {__index = new_class}
- new_class.new = function()
- local newinst = {}
- setmetatable(newinst, class_mt)
- return newinst
- end
- if baseClass then
- setmetatable(new_class, {__index = baseClass})
- end
- return new_class
- end
- local function getLines(data)
- local f = io.open("__tmp", "w")
- f:write(data)
- f:close()
- local f = io.open("__tmp", "r")
- local l = f:read("*l")
- local lines = {}
- while l ~= nil do
- table.insert(lines, l)
- l = f:read("*l")
- end
- f:close()
- fs.delete("__tmp")
- return lines
- end
- local function attemptRegister(usr, psw)
- local data = getLines(http.get(url .. "register.php?usr=" .. textutils.urlEncode(usr) .. "&psw=" .. textutils.urlEncode(psw)).readAll())
- if data[1]:find("success") then
- return true
- end
- return false, data[2]
- end
- local function attemptLogin(usr, psw)
- local data = getLines(http.get(url .. "login.php?usr=" .. textutils.urlEncode(usr) .. "&psw=" .. textutils.urlEncode(psw) .. "&dolgn=1").readAll())
- if data[1]:find("success") then
- local w = string.split(data[2], " ")
- local ids = {}
- local s = "student"
- if w[1] == "LECTURES" and w[2] ~= nil and w[2] ~= "" then
- ids = string.split(w[2], ",")
- for i = 1, #ids do
- ids[i] = tonumber(ids[i])
- end
- end
- if w[1] == "STATUS" and w[2] ~= nil and w[2] ~= "" then
- s = w[2]
- end
- return true, ids, s
- end
- return false, data[2]
- end
- local function attemptCreateLecture(lec, st, t)
- --[[
- local f = io.open("url.txt", "w")
- f:write(url .. "lectures.php?act=create&usr=" .. st.name .. "&psw=" .. st.psw .. "&l_name=" .. textutils.urlEncode(lec:getName()) .. "&l_prof=" .. textutils.urlEncode(lec:getProfessor()) .. "&l_time=" .. t .. "&l_info=" .. textutils.urlEncode(lec:getInfo()))
- f:close()
- --]]
- local data = getLines(http.get(url .. "lectures.php?act=create&usr=" .. textutils.urlEncode(st.name) .. "&psw=" .. textutils.urlEncode(st.psw) .. "&l_name=" .. textutils.urlEncode(lec:getName()) .. "&l_prof=" .. textutils.urlEncode(lec:getProfessor()) .. "&l_time=" .. textutils.urlEncode(t) .. "&l_info=" .. textutils.urlEncode(lec:getInfo())).readAll())
- -- local data = getLines(http.get(url .. "register.php?usr=" .. usr .. "&psw=" .. psw).readAll())
- if data[1]:find("success") then
- return true
- end
- return false, data[2]
- end
- local function attemptAddToWL(st, new_usr)
- local data = getLines(http.get(url .. "whitelist.php?usr=" .. textutils.urlEncode(st.name) .. "&psw=" .. textutils.urlEncode(st.psw) .. "&new_usr=" .. textutils.urlEncode(new_usr)).readAll())
- -- local data = getLines(http.get(url .. "register.php?usr=" .. usr .. "&psw=" .. psw).readAll())
- if data[1]:find("success") then
- return true
- end
- return false, data[2]
- end
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- GUI Server class --
- -------------------------------------------------------------
- -------------------------------------------------------------
- local BUTTON_LEFT = 1;
- local BUTTON_RIGHT = 2;
- local BUTTON_W_UP = 3;
- local BUTTON_W_DOWN = 4;
- local BUTTON_W = 5;
- local gui_event = {
- x = 0;
- y = 0;
- button = BUTTON_LEFT;
- key = 0;
- }
- gui_event.__index = gui_event
- gui_event.new = function()
- local t = {}
- setmetatable(t, gui_event)
- return t
- end
- local gui_server = {
- children = nil;
- width = 100; -- %
- height = 100; -- %
- x = 0;
- y = 0;
- parent = nil;
- parentIndex = 1;
- hasBgColor = true;
- bgColor = colors.lightBlue;
- isParent = false;
- }
- gui_server.__index = gui_server
- gui_server.new = function(x, y, w, h, c)
- local t = {
- x = x;
- y = y;
- width = w;
- height = h;
- isParent = true;
- }
- t.children = (c == nil or type(c) ~= "table") and {} or c;
- setmetatable(t, gui_server)
- return t
- end
- function gui_server:addChild(c)
- if self.isParent then
- c.parent = self
- c.parentIndex = #self.children + 1
- table.insert(self.children, c)
- end
- end
- function gui_server:removeChild(i)
- if self.isParent then
- table.remove(self.children, i)
- for l = i, #self.children do
- self.children[i]:setIndex(self.children[i].parentIndex - 1)
- end
- end
- end
- function gui_server:pxSize()
- if self.parent == nil then
- local w, h = term.getSize()
- return math.round(w / 100 * self.width), math.round(h / 100 * self.height)
- end
- local w, h = self.parent:pxSize()
- return math.round(w / 100 * self.width), math.round(h / 100 * self.height)
- end
- function gui_server:pxPosLocal()
- if self.parent == nil then
- local w, h = term.getSize()
- return math.round(w / 100 * self.x), math.round(h / 100 * self.y)
- end
- local w, h = self.parent:pxSize()
- return math.round(w / 100 * self.x), math.round(h / 100 * self.y)
- end
- function gui_server:pxPosGlobal()
- if self.parent == nil then
- local w, h = term.getSize()
- return math.round(w / 100 * self.x), math.round(h / 100 * self.y)
- end
- local w, h = self.parent:pxSize()
- local pX, pY = self.parent:pxPosGlobal()
- -- print("w, h = " .. w .. ", " .. h)
- return math.round(w / 100 * self.x) + pX, math.round(h / 100 * self.y) + pY
- end
- function gui_server:removeSelf()
- if self.parent ~= nil then
- self.parent:removeChild(self.parentIndex)
- end
- end
- function gui_server:onMouseEvent(ev)
- -- print("Pressed button " .. ev.button .. " at (" .. ev.x .. ", " .. ev.y .. ")")
- if self.isParent then
- for _, c in ipairs(self.children) do
- c:onMouseEvent(ev)
- end
- end
- end
- function gui_server:onKeyEvent(ev)
- -- print("Pressed key " .. ev.key)
- if self.isParent then
- for _, c in ipairs(self.children) do
- c:onKeyEvent(ev)
- end
- end
- end
- function gui_server:draw()
- if self.hasBgColor then
- term.setBackgroundColor(self.bgColor)
- local w, h = self:pxSize()
- local x, y = self:pxPosGlobal()
- for _x = 0, w do
- for _y = 0, h do
- term.setCursorPos(_x + x, _y + x)
- io.write(" ")
- end
- end
- end
- if self.isParent then
- for _, c in ipairs(self.children) do
- c:draw()
- end
- end
- end
- function gui_server:redraw()
- if self.parent == nil then
- -- Do the drawing
- self:draw()
- return
- end
- self.parent:draw()
- end
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- GUI Button class --
- -------------------------------------------------------------
- -------------------------------------------------------------
- local gui_button = inheritsFrom(gui_server)
- function gui_button:init(text, x, y, w, h)
- self.textColor = colors.black
- self.bgColor = colors.white
- self.text = text
- self.x = x
- self.y = y
- self.width = w
- self.height = h
- self.onClick = function(x, y) end
- end
- function gui_button:draw()
- term.setTextColor(self.textColor)
- if self.hasBgColor then
- term.setBackgroundColor(self.bgColor)
- local w, h = self:pxSize()
- local x, y = self:pxPosGlobal()
- -- print("x, y = " .. x .. ", " .. y)
- for _x = 0, w - 1 do
- for _y = 0, h - 1 do
- -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
- term.setCursorPos(_x + x, _y + y)
- io.write(" ")
- end
- end
- end
- local pX, pY = self:pxPosGlobal()
- -- print(pX .. ", " .. pY)
- local w, h = self:pxSize()
- local cX, cY = math.floor(pX + w / 2) - self.text:len() / 2, math.floor(pY + h / 2)
- term.setCursorPos(cX, cY)
- io.write(self.text:sub(1, (self.text:len() > w - 1 and w - 1 or self.text:len())))
- end
- function gui_button:onMouseEvent(ev)
- local _x, _y = self:pxPosGlobal()
- local w, h = self:pxSize()
- if ev.x >= _x and ev.x < _x + w and ev.y >= _y and ev.y < _y + h then
- self.onClick(ev.x, ev.y)
- end
- end
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- GUI Textbox class --
- -------------------------------------------------------------
- -------------------------------------------------------------
- local gui_textfield = inheritsFrom(gui_server)
- function gui_textfield:init(text, x, y, w, m_ch)
- self.text = text
- self.textColor = colors.black
- self.bgColor = colors.white
- self.replaceChar = nil
- self.chars = {}
- for i = 1, text:len() do
- table.insert(self.chars, text:sub(i, i))
- end
- local _w, h = term.getSize()
- self.x = x
- self.y = y
- self.width = w or 30
- self.height = 100 / h
- --[[
- local sW, sH = self:pxSize()
- while sH ~= 1 do
- self.height = self.height - 0.2
- sW, sH = self:pxSize()
- end
- ]]
- self.m_ch = m_ch or false
- self.__onClick = function(x, y)
- self:edit()
- end
- self.onClick = function(x, y) end
- end
- function gui_textfield:draw()
- term.setTextColor(self.textColor)
- if self.hasBgColor then
- term.setBackgroundColor(self.bgColor)
- local w, h = self:pxSize()
- local x, y = self:pxPosGlobal()
- -- print("x, y = " .. x .. ", " .. y)
- for _x = 0, w - 1 do
- for _y = 0, h - 1 do
- -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
- term.setCursorPos(_x + x, _y + y)
- io.write(" ")
- end
- end
- end
- local pX, pY = self:pxPosGlobal()
- -- print(pX .. ", " .. pY)
- local w, h = self:pxSize()
- local cX, cY = pX, math.floor(pY + h / 2)
- term.setCursorPos(cX, cY)
- local line = self.text
- local _line = line
- if self.replaceChar then
- line = ""
- for i = 1, _line:len() do
- line = line .. self.replaceChar:sub(1, 1)
- end
- end
- io.write(line:sub(1, (line:len() > w - 1 and w - 1 or line:len())))
- end
- function gui_textfield:edit()
- local pX, pY = self:pxPosGlobal()
- -- print(pX .. ", " .. pY)
- local w, h = self:pxSize()
- term.setCursorPos(pX, math.floor(pY + h / 2))
- term.setCursorBlink( true )
- local line = self.text
- local cursorX = line:len()
- local chars = {}
- for i = 1, self.text:len() do
- table.insert(chars, self.text:sub(i, i))
- end
- local function redraw(r_ch)
- term.setTextColor(self.textColor)
- term.setBackgroundColor(self.bgColor)
- term.setCursorPos(pX, math.floor(pY + h / 2))
- line = ""
- for _, c in ipairs(chars) do
- line = line .. c
- end
- local _line = line
- if self.replaceChar then
- line = ""
- for i = 1, _line:len() do
- line = line .. self.replaceChar:sub(1, 1)
- end
- end
- for i = 1, w do
- io.write(" ")
- end
- term.setCursorPos(pX, math.floor(pY + h / 2))
- local currentX = pX
- if line:len() >= w - 1 and cursorX >= w - 1 then
- io.write(line:sub(cursorX - (w - 2), cursorX))
- currentX = pX + cursorX - line:sub(1, cursorX - (w - 1)):len()
- elseif line:len() >= w - 1 and cursorX < w - 1 then
- io.write(line:sub(1, w - 1))
- currentX = pX + cursorX
- else
- io.write(line)
- currentX = pX + cursorX
- end
- line = _line
- term.setCursorPos(currentX, math.floor(pY + h / 2))
- end
- redraw(self.replaceChar)
- while true do
- local ev, key = os.pullEvent()
- if ev == "key" and (m_ch and #chars < m_ch or true) then
- if key == keys.left then
- if cursorX > 0 then
- cursorX = cursorX - 1
- end
- elseif key == keys.right then
- if cursorX < line:len() then
- cursorX = cursorX + 1
- end
- elseif key == keys.enter then
- break
- elseif key == keys.backspace then
- if cursorX > 0 then
- table.remove(chars, cursorX)
- cursorX = cursorX - 1
- end
- elseif key == keys.delete then
- if cursorX < line:len() then
- table.remove(chars, cursorX + 1)
- end
- end
- elseif ev == "char" then
- table.insert(chars, cursorX + 1, key)
- cursorX = cursorX + 1
- end
- redraw()
- end
- term.setCursorBlink(false)
- self.text = line
- end
- function gui_textfield:setText(t)
- self.text = t
- for i = 1, t:len() do
- self.chars[i] = t:sub(i, i)
- end
- end
- function gui_textfield:onMouseEvent(ev)
- local _x, _y = self:pxPosGlobal()
- local w, h = self:pxSize()
- if ev.x >= _x and ev.x < _x + w and ev.y >= _y and ev.y < _y + h then
- self.__onClick(ev.x, ev.y)
- self.onClick(ev.x, ev.y)
- end
- end
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- GUI Label class --
- -------------------------------------------------------------
- -------------------------------------------------------------
- local gui_label = inheritsFrom(gui_server)
- function gui_label:init(text, x, y)
- self.textColor = colors.black
- self.bgColor = colors.white
- self.text = text
- self.x = x
- self.y = y
- self.width = 100 / S_WIDTH
- local sW, sH = self:pxSize()
- while sW ~= text:len() do
- self.width = self.width + 1
- sW, sH = self:pxSize()
- end
- self.height = 100 / S_HEIGHT
- end
- function gui_label:draw()
- term.setTextColor(self.textColor)
- if self.hasBgColor then
- term.setBackgroundColor(self.bgColor)
- local w, h = self:pxSize()
- local x, y = self:pxPosGlobal()
- -- print("x, y = " .. x .. ", " .. y)
- for _x = 0, w - 1 do
- for _y = 0, h - 1 do
- -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
- term.setCursorPos(_x + x, _y + y)
- io.write(" ")
- end
- end
- end
- local pX, pY = self:pxPosGlobal()
- -- print(pX .. ", " .. pY)
- local w, h = self:pxSize()
- local cX, cY = math.floor(pX + w / 2) - self.text:len() / 2, math.floor(pY + h / 2)
- for i = 1, self.text:sub(1, (self.text:len() > w - 1 and w - 1 or self.text:len())) do
- local t, f, b = term.getTextAt(cX + i - 1, cY, 1)
- term.setBackgroundColor(b)
- term.setCursorPos(cX + i - 1, cY)
- io.write(self.text:sub(1, (self.text:len() > w - 1 and w - 1 or self.text:len())):sub(i, i))
- end
- end
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- Lecture class --
- -------------------------------------------------------------
- -------------------------------------------------------------
- local lecture = {
- name = "none";
- prof = "none";
- time = nil;
- timeString = "0:0 1/1 - 2013";
- id = "none";
- parent = nil;
- parentIndex = 1;
- }
- lecture.__index = lecture
- local function newLecture(id)
- local t = {
- id = id;
- }
- t.time = {
- m = 0;
- h = 0;
- day = 1;
- mon = 1;
- yr = 2013;
- }
- setmetatable(t, lecture)
- return t
- end
- function lecture:setIndex(i)
- self.parentIndex = i
- end
- function lecture:getIndex()
- return self.parentIndex
- end
- function lecture:setParent(p)
- self.parent = p
- end
- function lecture:getParent()
- return self.parent
- end
- function lecture:load()
- local data = getLines(http.get(url .. "lectures.php?act=load&id=" .. self.id).readAll())
- if data[1]:find("loading failure") ~= nil then
- return false
- end
- local inInfo = false
- local infoLines = {}
- for _, line in ipairs(data) do
- if line ~= nil then
- if inInfo then
- local words = string.split(line, " ")
- if words ~= nil then
- if words[1] ~= nil and words[2] ~= nil then
- if words[1] == "END" and words[2] == "INFO" then
- inInfo = false
- else
- table.insert(infoLines, line .. "\n")
- end
- end
- end
- else
- local words = string.split(line, " ")
- if words ~= nil then
- if words[1] ~= nil and words[2] ~= nil then
- if words[1] == "START" and words[2] == "INFO" then
- inInfo = true
- elseif words[1] == "NAME" then
- self.name = line:sub(words[1]:len() + 2)
- elseif words[1] == "PROFESSOR" then
- self.prof = line:sub(words[1]:len() + 2)
- elseif words[1] == "TIME" then
- self.timeString = line:sub(words[1]:len() + 2)
- local w = string.split(line:sub(words[1]:len() + 2), " ")
- self.time.h = tonumber(w[1])
- self.time.m = tonumber(w[2])
- self.time.day = tonumber(w[3])
- self.time.mon = tonumber(w[4])
- self.time.yr = tonumber(w[5])
- -- print("ADDED TIME")
- elseif words[1] == "ID" then
- self.id = tonumber(words[2])
- end
- end
- end
- end
- end
- end
- self.info = ""
- for _, l in ipairs(infoLines) do
- self.info = self.info .. l .. "\n"
- end
- end
- function lecture:create(st)
- local t = self.timeString
- t = t:gsub(":", " ")
- t = t:gsub("/", " ")
- t = t:gsub("-", "")
- t = t:gsub(" ", " ")
- local c, msg = attemptCreateLecture(self, st, t)
- if c then
- return true, msg
- end
- return false, msg
- end
- function lecture:getInfo()
- return self.info
- end
- function lecture:getName()
- return self.name
- end
- function lecture:getProfessor()
- return self.prof
- end
- function lecture:getTime(zone)
- local w = string.split(zone, " ")
- local z = w[1]
- local a = tonumber(w[2])
- local h = self.time.h + a
- local day = self.time.day
- local mon = self.time.mon
- local yr = self.time.yr
- if h >= 24 then
- h = h - 24
- day = day + 1
- if day > 30 then
- day = 1
- mon = mon + 1
- end
- if mon > 12 then
- mon = 1
- yr = yr + 1
- end
- end
- return h .. ":" .. self.time.m .. " " .. day .. "/" .. mon .. " - " .. yr
- end
- function lecture:getID()
- return self.id
- end
- function lecture:setTime(t)
- self.timeString = t
- t = t:gsub(":", " ")
- t = t:gsub("/", " ")
- t = t:gsub("-", "")
- t = t:gsub(" ", " ")
- local w = string.split(t, " ")
- self.time.h = w[1]
- self.time.m = w[2]
- self.time.day = w[3]
- self.time.mon = w[4]
- self.time.yr = w[5]
- end
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- Student class --
- -------------------------------------------------------------
- -------------------------------------------------------------
- local student = {
- name = "none";
- psw = "none";
- t_zone = {
- "GMT";
- 1;
- };
- lectures = {};
- lec_c = 0;
- logged_in = false;
- status = "student";
- }
- student.__index = student
- local function newStudent(usr, psw)
- local t = {
- name = usr;
- psw = psw;
- }
- setmetatable(t, student)
- return t
- end
- function student:login()
- self.logged_in, lecs, status = attemptLogin(self.name, self.psw)
- if self.logged_in then
- for _, l in ipairs(lecs) do
- for _, _l in ipairs(lectures) do
- if l == _l:getID() then
- self:addLecture(_l)
- end
- end
- end
- self.status = status
- end
- return self.logged_in, lecs
- end
- function student:register()
- return attemptRegister(self.name, self.psw)
- end
- function student:addLecture(lec)
- lec:setIndex(#self.lectures + 1)
- lec:setParent(self)
- table.insert(self.lectures, lec)
- end
- function student:removeLecture(i)
- table.remove(self.lectures, i)
- for l = i, #self.lectures do
- self.lectures[i]:setIndex(self.lectures[i]:getIndex() - 1)
- end
- end
- function student:getLectureList()
- return self.lectures
- end
- -------------------------------------------------------------
- -------------------------------------------------------------
- -- General functions --
- -------------------------------------------------------------
- -------------------------------------------------------------
- local function initLectureList()
- local dat = getLines(http.get(url .. "lectures.php?act=list").readAll())
- local data = dat[1]
- if data:find("listing failure") then
- return false
- end
- local l = string.split(data, ",")
- for _, id in ipairs(l) do
- local lec = newLecture(id)
- -- print("Adding new lecture with ID #" .. id)
- lec:load()
- table.insert(lectures, lec)
- end
- end
- local arg = {...}
- initLectureList()
- --[[
- if arg[1] == "login" then
- local s = newStudent(arg[2], arg[3])
- local lo, msg = s:login()
- if lo then
- print("Success!")
- print("The student needs to take the following lectures:")
- for _, l in ipairs(s:getLectureList()) do
- print(l:getName())
- end
- else
- print("Failure.\n" .. msg)
- end
- elseif arg[1] == "register" then
- local s = newStudent(arg[2], arg[3])
- local r, msg = s:register()
- if r then
- print("Success!")
- else
- print("Failure.\n" .. msg)
- end
- else
- local x = createGUIServer(0, 0, 50, 50)
- x:redraw()
- print("\n")
- end
- --]]
- local x = gui_server.new(0, 0, 100, 100)
- local userLabel = gui_label.new()
- userLabel:init("Username: ", 10, 50, 30, 100 / S_HEIGHT)
- userLabel.hasBgColor = false
- local userField = gui_textfield.new()
- userField:init("", 50, 50, 30, 100 / S_HEIGHT)
- local passLabel = gui_label.new()
- passLabel:init("Password: ", 10, 70, 30, 100 / S_HEIGHT)
- passLabel.hasBgColor = false
- local passField = gui_textfield.new()
- passField:init("", 50, 70, 30, 100 / S_HEIGHT)
- passField.replaceChar = "*"
- x:addChild(userLabel)
- x:addChild(userField)
- x:addChild(passLabel)
- x:addChild(passField)
- x:redraw()
- while true do
- local ev, p1, p2, p3, p4 = os.pullEvent()
- local gui_ev = gui_event.new()
- if ev == "mouse_click" then
- gui_ev.button = p1
- if p1 == 3 then
- gui_ev.button = BUTTON_W
- end
- gui_ev.x = p2
- gui_ev.y = p3
- x:onMouseEvent(gui_ev)
- elseif ev == "mouse_scroll" then
- gui_ev.button = p1 == 1 and BUTTON_W_DOWN or BUTTON_W_UP
- gui_ev.x = p2
- gui_ev.y = p3
- x:onMouseEvent(gui_ev)
- elseif ev == "key" then
- gui_ev.key = p1
- x:onKeyEvent(gui_ev)
- end
- x:redraw()
- end
Advertisement
Add Comment
Please, Sign In to add comment