mad1231999

Untitled

Jan 26th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.24 KB | None | 0 0
  1. local function overwrite(sName,replacement)
  2.         rawset(getfenv(),sName,replacement)
  3.         rawset(_G,sName,replacement)
  4.         local cur=0
  5.         repeat
  6.                 cur=cur+1
  7.                 local ok,t=pcall(function() return getfenv(cur) end)
  8.                 if ok and type(t)=="table" and type(rawget(t,sName))=="table" then
  9.                         rawset(t,sName,replacement)
  10.                 end
  11.         until not ok
  12. end
  13.  
  14. local oldTerm=term
  15. local term=setmetatable({},{__index=oldTerm})
  16. term.old=oldTerm
  17. local tCharacterRecord={}
  18. local tTextColorRecord={}
  19. local tBackgroundColorRecord={}
  20.  
  21. 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}
  22.  
  23. local iTextColor=colors.white
  24. term.setTextColor=function(iColor)
  25.         iTextColor=iColor
  26.         return oldTerm.setTextColor(iColor)
  27. end
  28. term.setTextColour=function(iColor)
  29.         iTextColor=iColor
  30.         return oldTerm.setTextColour(iColor)
  31. end
  32.  
  33. term.getTextColor=function()
  34.         return iTextColor
  35. end
  36.  
  37. local iBackgroundColor=colors.black
  38. term.setBackgroundColor=function(iColor)
  39.         iBackgroundColor=iColor
  40.         return oldTerm.setBackgroundColor(iColor)
  41. end
  42. term.setBackgroundColor=function(iColor)
  43.         iBackgroundColor=iColor
  44.         return oldTerm.setBackgroundColor(iColor)
  45. end
  46.  
  47. term.getBackgroundColor=function()
  48.         return iBackgroundColor
  49. end
  50.  
  51. term.write=function(sText)
  52.         local CursorPosition={term.getCursorPos()}
  53.         local TextColorShorthand="w"
  54.         local backgroundColorShorthand="a"
  55.         for k,v in pairs(tColorShortHand) do
  56.                 if v==term.getTextColor() then TextColorShorthand=k end
  57.                 if v==term.getBackgroundColor() then backgroundColorShorthand=k end
  58.         end
  59.         local function replaceLine(tRecord,sAddText)
  60.                 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)
  61.         end
  62.  
  63.         replaceLine(tCharacterRecord,sText)
  64.         replaceLine(tTextColorRecord,string.rep(TextColorShorthand,#sText))
  65.         replaceLine(tBackgroundColorRecord,string.rep(backgroundColorShorthand,#sText))
  66.        
  67.         for k,v in ipairs(tCharacterRecord) do
  68.                 if #v>CursorPosition[1] then
  69.                         tCharacterRecord[k+1]=string.sub(v,CursorPosition[1]+1)..string.sub(tCharacterRecord[k-1],#v+1)
  70.                 end
  71.         end
  72.         return oldTerm.write(sText)
  73. end
  74.  
  75. term.getTextAt=function(x,y,iLength)
  76.         if tCharacterRecord[y+1] then
  77.                 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)]
  78.         else
  79.                 return nil
  80.         end
  81. end
  82.  
  83. term.screenShot=function()
  84.         local tNew={{},{},{}}
  85.         for k,v in pairs(tCharacterRecord) do
  86.                 tNew[1][k]=v
  87.                 tNew[2][k]=tTextColorRecord[k]
  88.                 tNew[3][k]=tBackgroundColorRecord[k]
  89.         end
  90.         return unpack(tNew)
  91. end
  92.  
  93. term.loadScreen=function(t,t2,t3)
  94.         for k,v in pairs(t) do
  95.                 term.setCursorPos(1,k)
  96.                 for i=1,#v do
  97.                         if type(t2)=="table" then
  98.                                 term.setTextColor(tColorShortHand[string.sub(t2[k],i,i)])
  99.                         elseif type(t2)=="number" and i==1 then
  100.                                 term.setTextColor(t2)
  101.                         end
  102.                         if type(t3)=="table" then
  103.                                 term.setBackgroundColor(tColorShortHand[string.sub(t3[k],i,i)])
  104.                         elseif type(t3)=="number" and i==1 then
  105.                                 term.setTextColor(t3)
  106.                         end
  107.                         write(string.sub(v,i,i))
  108.                 end
  109.         end    
  110. end
  111.  
  112. term.clear=function()
  113.         tCharacterRecord={}
  114.         tTextColorRecord={}
  115.         tBackgroundColorRecord={}
  116.         return oldTerm.clear
  117. end
  118.  
  119. term.scroll=function(num)
  120.         for k,v in pairs(tCharacterRecord) do
  121.                 if type(k)=="number" and type(num)=="number" and k-num>1 then
  122.                         tCharacterRecord[k-num]=v
  123.                         tTextColorRecord[k-num]=tTextColorRecord[k]
  124.                         tBackgroundColorRecord[k-num]=tBackgroundColorRecord[k]
  125.                 end
  126.         end
  127.         return oldTerm.scroll(num)
  128. end
  129.  
  130. overwrite("term",term)
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. -------------------------------------------------------------
  142. -------------------------------------------------------------
  143. --                   "Global" variables                    --
  144. -------------------------------------------------------------
  145. -------------------------------------------------------------
  146. local url = "http://localhost/CC/ccu/"
  147. local lectures = {}
  148. local currentUser = nil
  149. local S_WIDTH, S_HEIGHT = term.getSize()
  150.  
  151.  
  152.  
  153. -------------------------------------------------------------
  154. -------------------------------------------------------------
  155. --               Functions used by all classes             --
  156. -------------------------------------------------------------
  157. -------------------------------------------------------------
  158. function string:split(sep)
  159.     local sep, fields = sep or ":", {}
  160.     local pattern = string.format("([^%s]+)", sep)
  161.     self:gsub(pattern, function(c) fields[#fields+1] = c end)
  162.     return fields
  163. end
  164.  
  165. math.round = function(n)
  166.     return math.floor(n + .5)
  167. end
  168.  
  169. local function dostring(s)
  170.     local f = io.open("__tmp", "w")
  171.     f:write(s)
  172.     f:close()
  173.  
  174.     local t = dofile("__tmp")
  175.     fs.delete("__tmp")
  176.  
  177.     return t
  178. end
  179.  
  180. local function inheritsFrom(baseClass)
  181.     local new_class = {}
  182.     local class_mt = {__index = new_class}
  183.  
  184.     new_class.new = function()
  185.         local newinst = {}
  186.         setmetatable(newinst, class_mt)
  187.         return newinst
  188.     end
  189.  
  190.     if baseClass then
  191.         setmetatable(new_class, {__index = baseClass})
  192.     end
  193.  
  194.     return new_class
  195. end
  196.  
  197. local function getLines(data)
  198.     local f = io.open("__tmp", "w")
  199.     f:write(data)
  200.     f:close()
  201.  
  202.     local f = io.open("__tmp", "r")
  203.     local l = f:read("*l")
  204.     local lines = {}
  205.  
  206.     while l ~= nil do
  207.         table.insert(lines, l)
  208.  
  209.         l = f:read("*l")
  210.     end
  211.  
  212.     f:close()
  213.  
  214.     fs.delete("__tmp")
  215.  
  216.     return lines
  217. end
  218.  
  219. local function attemptRegister(usr, psw)
  220.     local data = getLines(http.get(url .. "register.php?usr=" .. textutils.urlEncode(usr) .. "&psw=" .. textutils.urlEncode(psw)).readAll())
  221.  
  222.     if data[1]:find("success") then
  223.         return true
  224.     end
  225.  
  226.     return false, data[2]
  227. end
  228.  
  229. local function attemptLogin(usr, psw)
  230.     local data = getLines(http.get(url .. "login.php?usr=" .. textutils.urlEncode(usr) .. "&psw=" .. textutils.urlEncode(psw) .. "&dolgn=1").readAll())
  231.  
  232.     if data[1]:find("success") then
  233.         local w = string.split(data[2], " ")
  234.         local ids = {}
  235.         local s = "student"
  236.  
  237.         if w[1] == "LECTURES" and w[2] ~= nil and w[2] ~= "" then
  238.             ids = string.split(w[2], ",")
  239.  
  240.             for i = 1, #ids do
  241.                 ids[i] = tonumber(ids[i])
  242.             end
  243.         end
  244.  
  245.         if w[1] == "STATUS" and w[2] ~= nil and w[2] ~= "" then
  246.             s = w[2]
  247.         end
  248.  
  249.         return true, ids, s
  250.     end
  251.  
  252.     return false, data[2]
  253. end
  254.  
  255. local function attemptCreateLecture(lec, st, t)
  256.     --[[
  257.     local f = io.open("url.txt", "w")
  258.     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()))
  259.     f:close()
  260.     --]]
  261.  
  262.     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())
  263.  
  264.     -- local data = getLines(http.get(url .. "register.php?usr=" .. usr .. "&psw=" .. psw).readAll())
  265.  
  266.     if data[1]:find("success") then
  267.         return true
  268.     end
  269.  
  270.     return false, data[2]
  271. end
  272.  
  273. local function attemptAddToWL(st, new_usr)
  274.     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())
  275.  
  276.     -- local data = getLines(http.get(url .. "register.php?usr=" .. usr .. "&psw=" .. psw).readAll())
  277.  
  278.     if data[1]:find("success") then
  279.         return true
  280.     end
  281.  
  282.     return false, data[2]
  283. end
  284.  
  285.  
  286.  
  287. -------------------------------------------------------------
  288. -------------------------------------------------------------
  289. --                    GUI Server class                     --
  290. -------------------------------------------------------------
  291. -------------------------------------------------------------
  292. local BUTTON_LEFT = 1;
  293. local BUTTON_RIGHT = 2;
  294. local BUTTON_W_UP = 3;
  295. local BUTTON_W_DOWN = 4;
  296. local BUTTON_W = 5;
  297.  
  298. local gui_event = {
  299.     x = 0;
  300.     y = 0;
  301.     button = BUTTON_LEFT;
  302.     key = 0;
  303. }
  304. gui_event.__index = gui_event
  305.  
  306. gui_event.new = function()
  307.     local t = {}
  308.     setmetatable(t, gui_event)
  309.  
  310.     return t
  311. end
  312.  
  313. local gui_server = {
  314.     children = nil;
  315.     width = 100; -- %
  316.     height = 100; -- %
  317.     x = 0;
  318.     y = 0;
  319.     parent = nil;
  320.     parentIndex = 1;
  321.     hasBgColor = true;
  322.     bgColor = colors.lightBlue;
  323.     isParent = false;
  324. }
  325. gui_server.__index = gui_server
  326.  
  327. gui_server.new = function(x, y, w, h, c)
  328.     local t = {
  329.         x = x;
  330.         y = y;
  331.         width = w;
  332.         height = h;
  333.         isParent = true;
  334.     }
  335.     t.children = (c == nil or type(c) ~= "table") and {} or c;
  336.     setmetatable(t, gui_server)
  337.  
  338.     return t
  339. end
  340.  
  341. function gui_server:addChild(c)
  342.     if self.isParent then
  343.         c.parent = self
  344.         c.parentIndex = #self.children + 1
  345.         table.insert(self.children, c)
  346.     end
  347. end
  348.  
  349. function gui_server:removeChild(i)
  350.     if self.isParent then
  351.         table.remove(self.children, i)
  352.  
  353.         for l = i, #self.children do
  354.             self.children[i]:setIndex(self.children[i].parentIndex - 1)
  355.         end
  356.     end
  357. end
  358.  
  359. function gui_server:pxSize()
  360.     if self.parent == nil then
  361.         local w, h = term.getSize()
  362.  
  363.         return math.round(w / 100 * self.width), math.round(h / 100 * self.height)
  364.     end
  365.  
  366.     local w, h = self.parent:pxSize()
  367.  
  368.     return math.round(w / 100 * self.width), math.round(h / 100 * self.height)
  369. end
  370.  
  371. function gui_server:pxPosLocal()
  372.     if self.parent == nil then
  373.         local w, h = term.getSize()
  374.         return math.round(w / 100 * self.x), math.round(h / 100 * self.y)
  375.     end
  376.  
  377.     local w, h = self.parent:pxSize()
  378.  
  379.     return math.round(w / 100 * self.x), math.round(h / 100 * self.y)
  380. end
  381.  
  382. function gui_server:pxPosGlobal()
  383.     if self.parent == nil then
  384.         local w, h = term.getSize()
  385.         return math.round(w / 100 * self.x), math.round(h / 100 * self.y)
  386.     end
  387.  
  388.     local w, h = self.parent:pxSize()
  389.     local pX, pY = self.parent:pxPosGlobal()
  390.  
  391.     -- print("w, h = " .. w .. ", " .. h)
  392.  
  393.     return math.round(w / 100 * self.x) + pX, math.round(h / 100 * self.y) + pY
  394. end
  395.  
  396. function gui_server:removeSelf()
  397.     if self.parent ~= nil then
  398.         self.parent:removeChild(self.parentIndex)
  399.     end
  400. end
  401.  
  402. function gui_server:onMouseEvent(ev)
  403.     -- print("Pressed button " .. ev.button .. " at (" .. ev.x .. ", " .. ev.y .. ")")
  404.  
  405.     if self.isParent then
  406.         for _, c in ipairs(self.children) do
  407.             c:onMouseEvent(ev)
  408.         end
  409.     end
  410. end
  411.  
  412. function gui_server:onKeyEvent(ev)
  413.     -- print("Pressed key " .. ev.key)
  414.  
  415.     if self.isParent then
  416.         for _, c in ipairs(self.children) do
  417.             c:onKeyEvent(ev)
  418.         end
  419.     end
  420. end
  421.  
  422. function gui_server:draw()
  423.     if self.hasBgColor then
  424.         term.setBackgroundColor(self.bgColor)
  425.         local w, h = self:pxSize()
  426.         local x, y = self:pxPosGlobal()
  427.  
  428.         for _x = 0, w do
  429.             for _y = 0, h do
  430.                 term.setCursorPos(_x + x, _y + x)
  431.                 io.write(" ")
  432.             end
  433.         end
  434.     end
  435.  
  436.     if self.isParent then
  437.         for _, c in ipairs(self.children) do
  438.             c:draw()
  439.         end
  440.     end
  441. end
  442.  
  443. function gui_server:redraw()
  444.     if self.parent == nil then
  445.         -- Do the drawing
  446.         self:draw()
  447.         return
  448.     end
  449.  
  450.     self.parent:draw()
  451. end
  452.  
  453.  
  454.  
  455. -------------------------------------------------------------
  456. -------------------------------------------------------------
  457. --                    GUI Button class                     --
  458. -------------------------------------------------------------
  459. -------------------------------------------------------------
  460. local gui_button = inheritsFrom(gui_server)
  461.  
  462. function gui_button:init(text, x, y, w, h)
  463.     self.textColor = colors.black
  464.     self.bgColor = colors.white
  465.     self.text = text
  466.     self.x = x
  467.     self.y = y
  468.     self.width = w
  469.     self.height = h
  470.     self.onClick = function(x, y) end
  471. end
  472.  
  473. function gui_button:draw()
  474.     term.setTextColor(self.textColor)
  475.  
  476.     if self.hasBgColor then
  477.         term.setBackgroundColor(self.bgColor)
  478.         local w, h = self:pxSize()
  479.         local x, y = self:pxPosGlobal()
  480.  
  481.         -- print("x, y = " .. x .. ", " .. y)
  482.  
  483.         for _x = 0, w - 1 do
  484.             for _y = 0, h - 1 do
  485.                 -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
  486.                 term.setCursorPos(_x + x, _y + y)
  487.                 io.write(" ")
  488.             end
  489.         end
  490.     end
  491.  
  492.     local pX, pY = self:pxPosGlobal()
  493.     -- print(pX .. ", " .. pY)
  494.     local w, h = self:pxSize()
  495.     local cX, cY = math.floor(pX + w / 2) - self.text:len() / 2, math.floor(pY + h / 2)
  496.     term.setCursorPos(cX, cY)
  497.     io.write(self.text:sub(1, (self.text:len() > w - 1 and w - 1 or self.text:len())))
  498. end
  499.  
  500. function gui_button:onMouseEvent(ev)
  501.     local _x, _y = self:pxPosGlobal()
  502.     local w, h = self:pxSize()
  503.  
  504.     if ev.x >= _x and ev.x < _x + w and ev.y >= _y and ev.y < _y + h then
  505.         self.onClick(ev.x, ev.y)
  506.     end
  507. end
  508.  
  509.  
  510.  
  511. -------------------------------------------------------------
  512. -------------------------------------------------------------
  513. --                    GUI Textbox class                    --
  514. -------------------------------------------------------------
  515. -------------------------------------------------------------
  516. local gui_textfield = inheritsFrom(gui_server)
  517.  
  518. function gui_textfield:init(text, x, y, w, m_ch)
  519.     self.text = text
  520.     self.textColor = colors.black
  521.     self.bgColor = colors.white
  522.     self.replaceChar = nil
  523.     self.chars = {}
  524.  
  525.     for i = 1, text:len() do
  526.         table.insert(self.chars, text:sub(i, i))
  527.     end
  528.  
  529.     local _w, h = term.getSize()
  530.  
  531.     self.x = x
  532.     self.y = y
  533.     self.width = w or 30
  534.     self.height = 100 / h
  535.  
  536.     --[[
  537.     local sW, sH = self:pxSize()
  538.    
  539.     while sH ~= 1 do
  540.         self.height = self.height - 0.2
  541.         sW, sH = self:pxSize()
  542.     end
  543.     ]]
  544.  
  545.     self.m_ch = m_ch or false
  546.     self.__onClick = function(x, y)
  547.         self:edit()
  548.     end
  549.     self.onClick = function(x, y) end
  550. end
  551.  
  552. function gui_textfield:draw()
  553.     term.setTextColor(self.textColor)
  554.  
  555.     if self.hasBgColor then
  556.         term.setBackgroundColor(self.bgColor)
  557.         local w, h = self:pxSize()
  558.         local x, y = self:pxPosGlobal()
  559.  
  560.         -- print("x, y = " .. x .. ", " .. y)
  561.  
  562.         for _x = 0, w - 1 do
  563.             for _y = 0, h - 1 do
  564.                 -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
  565.                 term.setCursorPos(_x + x, _y + y)
  566.                 io.write(" ")
  567.             end
  568.         end
  569.     end
  570.  
  571.     local pX, pY = self:pxPosGlobal()
  572.     -- print(pX .. ", " .. pY)
  573.     local w, h = self:pxSize()
  574.     local cX, cY = pX, math.floor(pY + h / 2)
  575.     term.setCursorPos(cX, cY)
  576.    
  577.     local line = self.text
  578.     local _line = line
  579.  
  580.     if self.replaceChar then
  581.         line = ""
  582.  
  583.         for i = 1, _line:len() do
  584.             line = line .. self.replaceChar:sub(1, 1)
  585.         end
  586.     end
  587.  
  588.     io.write(line:sub(1, (line:len() > w - 1 and w - 1 or line:len())))
  589. end
  590.  
  591. function gui_textfield:edit()
  592.     local pX, pY = self:pxPosGlobal()
  593.     -- print(pX .. ", " .. pY)
  594.     local w, h = self:pxSize()
  595.     term.setCursorPos(pX, math.floor(pY + h / 2))
  596.     term.setCursorBlink( true )
  597.  
  598.     local line = self.text
  599.     local cursorX = line:len()
  600.     local chars = {}
  601.  
  602.     for i = 1, self.text:len() do
  603.         table.insert(chars, self.text:sub(i, i))
  604.     end
  605.  
  606.     local function redraw(r_ch)
  607.         term.setTextColor(self.textColor)
  608.         term.setBackgroundColor(self.bgColor)
  609.         term.setCursorPos(pX, math.floor(pY + h / 2))
  610.  
  611.         line = ""
  612.  
  613.         for _, c in ipairs(chars) do
  614.             line = line .. c
  615.         end
  616.  
  617.         local _line = line
  618.  
  619.         if self.replaceChar then
  620.             line = ""
  621.  
  622.             for i = 1, _line:len() do
  623.                 line = line .. self.replaceChar:sub(1, 1)
  624.             end
  625.         end
  626.  
  627.         for i = 1, w do
  628.             io.write(" ")
  629.         end
  630.  
  631.         term.setCursorPos(pX, math.floor(pY + h / 2))
  632.  
  633.         local currentX = pX
  634.  
  635.         if line:len() >= w - 1 and cursorX >= w - 1 then
  636.             io.write(line:sub(cursorX - (w - 2), cursorX))
  637.             currentX = pX + cursorX - line:sub(1, cursorX - (w - 1)):len()
  638.         elseif line:len() >= w - 1 and cursorX < w - 1 then
  639.             io.write(line:sub(1, w - 1))
  640.             currentX = pX + cursorX
  641.         else
  642.             io.write(line)
  643.             currentX = pX + cursorX
  644.         end
  645.  
  646.         line = _line
  647.  
  648.         term.setCursorPos(currentX, math.floor(pY + h / 2))
  649.     end
  650.  
  651.     redraw(self.replaceChar)
  652.  
  653.     while true do
  654.         local ev, key = os.pullEvent()
  655.  
  656.         if ev == "key" and (m_ch and #chars < m_ch or true) then
  657.             if key == keys.left then
  658.                 if cursorX > 0 then
  659.                     cursorX = cursorX - 1
  660.                 end
  661.             elseif key == keys.right then
  662.                 if cursorX < line:len() then
  663.                     cursorX = cursorX + 1
  664.                 end
  665.             elseif key == keys.enter then
  666.                 break
  667.             elseif key == keys.backspace then
  668.                 if cursorX > 0 then
  669.                     table.remove(chars, cursorX)
  670.                     cursorX = cursorX - 1
  671.                 end
  672.             elseif key == keys.delete then
  673.                 if cursorX < line:len() then
  674.                     table.remove(chars, cursorX + 1)
  675.                 end
  676.             end
  677.         elseif ev == "char" then
  678.             table.insert(chars, cursorX + 1, key)
  679.             cursorX = cursorX + 1
  680.         end
  681.  
  682.         redraw()
  683.     end
  684.  
  685.     term.setCursorBlink(false)
  686.     self.text = line
  687. end
  688.  
  689. function gui_textfield:setText(t)
  690.     self.text = t
  691.  
  692.     for i = 1, t:len() do
  693.         self.chars[i] = t:sub(i, i)
  694.     end
  695. end
  696.  
  697. function gui_textfield:onMouseEvent(ev)
  698.     local _x, _y = self:pxPosGlobal()
  699.     local w, h = self:pxSize()
  700.  
  701.     if ev.x >= _x and ev.x < _x + w and ev.y >= _y and ev.y < _y + h then
  702.         self.__onClick(ev.x, ev.y)
  703.         self.onClick(ev.x, ev.y)
  704.     end
  705. end
  706.  
  707.  
  708.  
  709. -------------------------------------------------------------
  710. -------------------------------------------------------------
  711. --                    GUI Label class                      --
  712. -------------------------------------------------------------
  713. -------------------------------------------------------------
  714. local gui_label = inheritsFrom(gui_server)
  715.  
  716. function gui_label:init(text, x, y)
  717.     self.textColor = colors.black
  718.     self.bgColor = colors.white
  719.     self.text = text
  720.     self.x = x
  721.     self.y = y
  722.  
  723.     self.width = 100 / S_WIDTH
  724.  
  725.     local sW, sH = self:pxSize()
  726.    
  727.     while sW ~= text:len() do
  728.         self.width = self.width + 1
  729.         sW, sH = self:pxSize()
  730.     end
  731.  
  732.     self.height = 100 / S_HEIGHT
  733. end
  734.  
  735. function gui_label:draw()
  736.     term.setTextColor(self.textColor)
  737.  
  738.     if self.hasBgColor then
  739.         term.setBackgroundColor(self.bgColor)
  740.         local w, h = self:pxSize()
  741.         local x, y = self:pxPosGlobal()
  742.  
  743.         -- print("x, y = " .. x .. ", " .. y)
  744.  
  745.         for _x = 0, w - 1 do
  746.             for _y = 0, h - 1 do
  747.                 -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
  748.                 term.setCursorPos(_x + x, _y + y)
  749.                 io.write(" ")
  750.             end
  751.         end
  752.     end
  753.  
  754.     local pX, pY = self:pxPosGlobal()
  755.     -- print(pX .. ", " .. pY)
  756.     local w, h = self:pxSize()
  757.     local cX, cY = math.floor(pX + w / 2) - self.text:len() / 2, math.floor(pY + h / 2)
  758.  
  759.     for i = 1, self.text:sub(1, (self.text:len() > w - 1 and w - 1 or self.text:len())) do
  760.         local t, f, b = term.getTextAt(cX + i - 1, cY, 1)
  761.         term.setBackgroundColor(b)
  762.         term.setCursorPos(cX + i - 1, cY)
  763.         io.write(self.text:sub(1, (self.text:len() > w - 1 and w - 1 or self.text:len())):sub(i, i))
  764.     end
  765. end
  766.  
  767.  
  768.  
  769. -------------------------------------------------------------
  770. -------------------------------------------------------------
  771. --                     Lecture class                       --
  772. -------------------------------------------------------------
  773. -------------------------------------------------------------
  774. local lecture = {
  775.     name = "none";
  776.     prof = "none";
  777.     time = nil;
  778.     timeString = "0:0 1/1 - 2013";
  779.     id = "none";
  780.     parent = nil;
  781.     parentIndex = 1;
  782. }
  783. lecture.__index = lecture
  784.  
  785. local function newLecture(id)
  786.     local t = {
  787.         id = id;
  788.     }
  789.     t.time = {
  790.         m = 0;
  791.         h = 0;
  792.         day = 1;
  793.         mon = 1;
  794.         yr = 2013;
  795.     }
  796.     setmetatable(t, lecture)
  797.  
  798.     return t
  799. end
  800.  
  801. function lecture:setIndex(i)
  802.     self.parentIndex = i
  803. end
  804.  
  805. function lecture:getIndex()
  806.     return self.parentIndex
  807. end
  808.  
  809. function lecture:setParent(p)
  810.     self.parent = p
  811. end
  812.  
  813. function lecture:getParent()
  814.     return self.parent
  815. end
  816.  
  817. function lecture:load()
  818.     local data = getLines(http.get(url .. "lectures.php?act=load&id=" .. self.id).readAll())
  819.    
  820.     if data[1]:find("loading failure") ~= nil then
  821.         return false
  822.     end
  823.  
  824.     local inInfo = false
  825.     local infoLines = {}
  826.  
  827.     for _, line in ipairs(data) do
  828.         if line ~= nil then
  829.             if inInfo then
  830.                 local words = string.split(line, " ")
  831.  
  832.                 if words ~= nil then
  833.                     if words[1] ~= nil and words[2] ~= nil then
  834.                         if words[1] == "END" and words[2] == "INFO" then
  835.                             inInfo = false
  836.                         else
  837.                             table.insert(infoLines, line .. "\n")
  838.                         end
  839.                     end
  840.                 end
  841.             else
  842.                 local words = string.split(line, " ")
  843.  
  844.                 if words ~= nil then
  845.                     if words[1] ~= nil and words[2] ~= nil then
  846.                         if words[1] == "START" and words[2] == "INFO" then
  847.                             inInfo = true
  848.                         elseif words[1] == "NAME" then
  849.                             self.name = line:sub(words[1]:len() + 2)
  850.                         elseif words[1] == "PROFESSOR" then
  851.                             self.prof = line:sub(words[1]:len() + 2)
  852.                         elseif words[1] == "TIME" then
  853.                             self.timeString = line:sub(words[1]:len() + 2)
  854.                             local w = string.split(line:sub(words[1]:len() + 2), " ")
  855.  
  856.                             self.time.h = tonumber(w[1])
  857.                             self.time.m = tonumber(w[2])
  858.                             self.time.day = tonumber(w[3])
  859.                             self.time.mon = tonumber(w[4])
  860.                             self.time.yr = tonumber(w[5])
  861.  
  862.                             -- print("ADDED TIME")
  863.                         elseif words[1] == "ID" then
  864.                             self.id = tonumber(words[2])
  865.                         end
  866.                     end
  867.                 end
  868.             end
  869.         end
  870.     end
  871.  
  872.     self.info = ""
  873.  
  874.     for _, l in ipairs(infoLines) do
  875.         self.info = self.info .. l .. "\n"
  876.     end
  877. end
  878.  
  879. function lecture:create(st)
  880.     local t = self.timeString
  881.     t = t:gsub(":", " ")
  882.     t = t:gsub("/", " ")
  883.     t = t:gsub("-", "")
  884.     t = t:gsub("  ", " ")
  885.  
  886.     local c, msg = attemptCreateLecture(self, st, t)
  887.  
  888.     if c then
  889.         return true, msg
  890.     end
  891.  
  892.     return false, msg
  893. end
  894.  
  895. function lecture:getInfo()
  896.     return self.info
  897. end
  898.  
  899. function lecture:getName()
  900.     return self.name
  901. end
  902.  
  903. function lecture:getProfessor()
  904.     return self.prof
  905. end
  906.  
  907. function lecture:getTime(zone)
  908.     local w = string.split(zone, " ")
  909.     local z = w[1]
  910.     local a = tonumber(w[2])
  911.  
  912.     local h = self.time.h + a
  913.     local day = self.time.day
  914.  
  915.     local mon = self.time.mon
  916.     local yr = self.time.yr
  917.  
  918.     if h >= 24 then
  919.         h = h - 24
  920.         day = day + 1
  921.  
  922.         if day > 30 then
  923.             day = 1
  924.             mon = mon + 1
  925.         end
  926.  
  927.         if mon > 12 then
  928.             mon = 1
  929.             yr = yr + 1
  930.         end
  931.     end
  932.  
  933.     return h .. ":" .. self.time.m .. " " .. day .. "/" .. mon .. " - " .. yr
  934. end
  935.  
  936. function lecture:getID()
  937.     return self.id
  938. end
  939.  
  940. function lecture:setTime(t)
  941.     self.timeString = t
  942.     t = t:gsub(":", " ")
  943.     t = t:gsub("/", " ")
  944.     t = t:gsub("-", "")
  945.     t = t:gsub("  ", " ")
  946.  
  947.     local w = string.split(t, " ")
  948.  
  949.     self.time.h = w[1]
  950.     self.time.m = w[2]
  951.     self.time.day = w[3]
  952.     self.time.mon = w[4]
  953.     self.time.yr = w[5]
  954. end
  955.  
  956. -------------------------------------------------------------
  957. -------------------------------------------------------------
  958. --                     Student class                       --
  959. -------------------------------------------------------------
  960. -------------------------------------------------------------
  961. local student = {
  962.     name = "none";
  963.     psw = "none";
  964.     t_zone = {
  965.         "GMT";
  966.         1;
  967.     };
  968.     lectures = {};
  969.     lec_c = 0;
  970.     logged_in = false;
  971.     status = "student";
  972. }
  973. student.__index = student
  974.  
  975. local function newStudent(usr, psw)
  976.     local t = {
  977.         name = usr;
  978.         psw = psw;
  979.     }
  980.     setmetatable(t, student)
  981.  
  982.     return t
  983. end
  984.  
  985. function student:login()
  986.     self.logged_in, lecs, status = attemptLogin(self.name, self.psw)
  987.  
  988.     if self.logged_in then
  989.         for _, l in ipairs(lecs) do
  990.             for _, _l in ipairs(lectures) do
  991.                 if l == _l:getID() then
  992.                     self:addLecture(_l)
  993.                 end
  994.             end
  995.         end
  996.  
  997.         self.status = status
  998.     end
  999.  
  1000.     return self.logged_in, lecs
  1001. end
  1002.  
  1003. function student:register()
  1004.     return attemptRegister(self.name, self.psw)
  1005. end
  1006.  
  1007. function student:addLecture(lec)
  1008.     lec:setIndex(#self.lectures + 1)
  1009.     lec:setParent(self)
  1010.     table.insert(self.lectures, lec)
  1011. end
  1012.  
  1013. function student:removeLecture(i)
  1014.     table.remove(self.lectures, i)
  1015.  
  1016.     for l = i, #self.lectures do
  1017.         self.lectures[i]:setIndex(self.lectures[i]:getIndex() - 1)
  1018.     end
  1019. end
  1020.  
  1021. function student:getLectureList()
  1022.     return self.lectures
  1023. end
  1024.  
  1025.  
  1026.  
  1027. -------------------------------------------------------------
  1028. -------------------------------------------------------------
  1029. --                  General functions                      --
  1030. -------------------------------------------------------------
  1031. -------------------------------------------------------------
  1032. local function initLectureList()
  1033.     local dat = getLines(http.get(url .. "lectures.php?act=list").readAll())
  1034.     local data = dat[1]
  1035.  
  1036.     if data:find("listing failure") then
  1037.         return false
  1038.     end
  1039.  
  1040.     local l = string.split(data, ",")
  1041.  
  1042.     for _, id in ipairs(l) do
  1043.         local lec = newLecture(id)
  1044.         -- print("Adding new lecture with ID #" .. id)
  1045.         lec:load()
  1046.         table.insert(lectures, lec)
  1047.     end
  1048. end
  1049.  
  1050. local arg = {...}
  1051.  
  1052. initLectureList()
  1053.  
  1054. --[[
  1055. if arg[1] == "login" then
  1056.     local s = newStudent(arg[2], arg[3])
  1057.     local lo, msg = s:login()
  1058.  
  1059.     if lo then
  1060.         print("Success!")
  1061.  
  1062.         print("The student needs to take the following lectures:")
  1063.  
  1064.         for _, l in ipairs(s:getLectureList()) do
  1065.             print(l:getName())
  1066.         end
  1067.     else
  1068.         print("Failure.\n" .. msg)
  1069.     end
  1070. elseif arg[1] == "register" then
  1071.     local s = newStudent(arg[2], arg[3])
  1072.     local r, msg = s:register()
  1073.  
  1074.     if r then
  1075.         print("Success!")
  1076.     else
  1077.         print("Failure.\n" .. msg)
  1078.     end
  1079. else
  1080.     local x = createGUIServer(0, 0, 50, 50)
  1081.     x:redraw()
  1082.     print("\n")
  1083. end
  1084. --]]
  1085.  
  1086. local x = gui_server.new(0, 0, 100, 100)
  1087.  
  1088. local userLabel = gui_label.new()
  1089. userLabel:init("Username: ", 10, 50, 30, 100 / S_HEIGHT)
  1090. userLabel.hasBgColor = false
  1091.  
  1092. local userField = gui_textfield.new()
  1093. userField:init("", 50, 50, 30, 100 / S_HEIGHT)
  1094.  
  1095. local passLabel = gui_label.new()
  1096. passLabel:init("Password: ", 10, 70, 30, 100 / S_HEIGHT)
  1097. passLabel.hasBgColor = false
  1098.  
  1099. local passField = gui_textfield.new()
  1100. passField:init("", 50, 70, 30, 100 / S_HEIGHT)
  1101. passField.replaceChar = "*"
  1102.  
  1103. x:addChild(userLabel)
  1104. x:addChild(userField)
  1105. x:addChild(passLabel)
  1106. x:addChild(passField)
  1107.  
  1108. x:redraw()
  1109.  
  1110. while true do
  1111.     local ev, p1, p2, p3, p4 = os.pullEvent()
  1112.  
  1113.     local gui_ev = gui_event.new()
  1114.  
  1115.     if ev == "mouse_click" then
  1116.         gui_ev.button = p1
  1117.         if p1 == 3 then
  1118.             gui_ev.button = BUTTON_W
  1119.         end
  1120.         gui_ev.x = p2
  1121.         gui_ev.y = p3
  1122.         x:onMouseEvent(gui_ev)
  1123.     elseif ev == "mouse_scroll" then
  1124.         gui_ev.button = p1 == 1 and BUTTON_W_DOWN or BUTTON_W_UP
  1125.         gui_ev.x = p2
  1126.         gui_ev.y = p3
  1127.         x:onMouseEvent(gui_ev)
  1128.     elseif ev == "key" then
  1129.         gui_ev.key = p1
  1130.         x:onKeyEvent(gui_ev)
  1131.     end
  1132.    
  1133.     x:redraw()
  1134. end
Advertisement
Add Comment
Please, Sign In to add comment