Advertisement
Guest User

server.lua

a guest
Jan 25th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. local ServerID = os.getComputerID()
  2. rednet.open("back")
  3. root = "disk/Data/"
  4.  
  5. function WaitMsg()
  6.  
  7. WriteConsole("[SERVER] Server started !")
  8.  
  9.  while true do
  10.   UnserializeMsg = {}
  11.   a,b,c = rednet.receive()
  12.   msg = textutils.unserialize(b)
  13.   WriteConsole("["..a.."] : CMD = "..msg[1])
  14.   if msg[1]=="Connection" then
  15.    if fs.exists(root..msg[3])==false then
  16.     UnserializeMsg[1] = "UnExistAccount"
  17.    else
  18.     UnserializeMsg[1] = "ExistingAccount"
  19.    end
  20.   elseif msg[1]=="VerifingPassword" then
  21.  
  22.     DataMDPV = fs.open(root..msg[2].."/MDP.txt","r")
  23.  
  24.     if (DataMDPV.readAll() == msg[3]) then
  25.       UnserializeMsg[1] = "Accepted"
  26.     else
  27.       UnserializeMsg[1] = "Refused"
  28.     end
  29.     DataMDPV.close()
  30.   elseif msg[1]=="ChangePassword" then
  31.  
  32.     DataMDP = fs.open(root..msg[2].."/MDP.txt","w")
  33.     DataMDP.write(msg[3])
  34.     DataMDP.close()
  35.     UnserializeMsg[1] = "PasswordChanged"
  36.    
  37.   elseif msg[1]=="CreatAccount" then
  38.  
  39.    WriteConsole("Creation du compte pour : "..msg[2])
  40.    fs.makeDir(root..msg[2])
  41.    DataMDP = fs.open(root..msg[2].."/MDP.txt","a")
  42.    DataMDP.write(msg[3])
  43.    DataMDP.close()
  44.    DataMoney = fs.open(root..msg[2].."/Money.txt","a")
  45.    DataMoney.write(1000)
  46.    DataMoney.close()  
  47.    UnserializeMsg[1] = "AccountCreated"
  48.  
  49.   elseif msg[1]=="Somme" then
  50.  
  51.    DataMoneyReader = fs.open(root..msg[2].."/Money.txt","r")
  52.    Money = DataMoneyReader.readAll()
  53.    DataMoneyReader.close()
  54.    UnserializeMsg[1] = Money
  55.  
  56.   elseif msg[1]=="addMoney" then
  57.  
  58.    DataMoneyReader = fs.open(root..msg[2].."/Money.txt","r")
  59.    Money = DataMoneyReader.readAll()
  60.    DataMoneyReader.close()
  61.    DataMoneyWriter = fs.open(root..msg[2].."/Money.txt","w")
  62.    Money = tonumber(Money) + tonumber(msg[3])
  63.    DataMoneyWriter.write(Money)
  64.    DataMoneyWriter.close()
  65.  
  66.   elseif msg[1]=="PING" then
  67.  
  68.     UnserializeMsg[1] = "PONG"
  69.  
  70.   else
  71.    WriteConsole("Commande Inconnue (\""..msg[1].."\")")
  72.   end
  73.    Msg = textutils.serialize(UnserializeMsg)
  74.    rednet.send(tonumber(a),Msg)
  75.  end
  76. end
  77.  
  78. function Environement()
  79.  if fs.isDir("Data")==false then
  80.  WriteConsole("Creating the space for they account")
  81.  fs.makeDir("Data")
  82.  end
  83. end
  84.  
  85.  
  86. local histoCons = {}
  87.  
  88. function Console()
  89.                
  90.         local x, y = 0, 0
  91.         term.clear()
  92.         for x=1,51 do
  93.                 paintutils.drawPixel(x,1,colors.blue)
  94.                 paintutils.drawPixel(x,16,colors.blue)
  95.                 paintutils.drawPixel(x,19,colors.blue)
  96.         end
  97.         for y=2,18 do
  98.                 paintutils.drawPixel(1,y,colors.blue)
  99.                 paintutils.drawPixel(51,y,colors.blue)
  100.         end
  101.         term.setTextColor(colors.white)
  102.         term.setBackgroundColor(colors.blue)
  103.         term.setCursorPos(2,1)
  104.         term.write("IP : "..ServerID)
  105.         term.setCursorPos(23,1)
  106.         term.write("Console")
  107.         term.setBackgroundColor(colors.black)
  108.         term.setTextColor(colors.white)
  109.         term.setCursorPos(2,18)
  110.         term.write(">")
  111. end
  112.  
  113. function WriteConsole(msg)
  114.  
  115.   table.insert(histoCons,msg)
  116.     yCons = 2
  117.     local x, y = 0,0            
  118.    
  119.     if #histoCons > 14 then
  120.         table.remove(histoCons, 1)
  121.     end
  122.  
  123.   term.setBackgroundColor(colors.black)
  124.   term.setTextColor(colors.white)
  125.         for x = 2, 50 do
  126.           for y = 2, 15 do
  127.           term.setCursorPos(x,y)
  128.           term.write(" ")
  129.           end
  130.         end
  131.         term.setCursorPos(3,2)
  132.         for key,value in pairs( histoCons ) do
  133.             term.setCursorPos(3,yCons)
  134.             term.write(value)
  135.             yCons = yCons+1
  136.         end
  137.          
  138. end
  139.  
  140. function InputCmd()
  141.   sleep(0.5)
  142.   command = ""
  143.  
  144.   while true do
  145.    
  146.     local event, p1 = os.pullEvent()
  147.  
  148.     if event == "char" then
  149.  
  150.       command = command..p1
  151.  
  152.     elseif (event == "key") and (p1==14) then
  153.  
  154.       command = string.sub( command, 1, string.len( command ) - 1 )
  155.  
  156.     elseif (event == "key") and (p1==28) then
  157.  
  158.       Treatment(command)
  159.       command = ""
  160.  
  161.     end
  162.    
  163.     term.setCursorPos(3,18)
  164.     term.write("                                       ")
  165.     term.setCursorPos(3,18)
  166.     term.write(command)
  167.   end
  168. end
  169.  
  170. function Treatment(tCommand)
  171.  
  172.   if string.sub(tCommand, 1,1) == "/" then
  173.  
  174.     tCommand = string.sub(tCommand, 2,string.len( command ))
  175.  
  176.     cmd2 = string.sub(tCommand,1,2)
  177.     cmd4 = string.sub(tCommand,1,4)
  178.     cmd5 = string.sub(tCommand,1,5)
  179.     cmd6 = string.sub(tCommand,1,6)
  180.     cmd7 = string.sub(tCommand,1,7)
  181.  
  182.     if cmd2 == "rm" then
  183.  
  184.       notaspace = true
  185.       while notaspace do
  186.  
  187.        letter = string.sub(cmd1,1,1)
  188.        cmd1 = string.sub(cmd1,2,string.len(cmd1))
  189.        --WriteConsole("Args1 = "..args1.." lenght : "..string.len(cmd1))
  190.        args1 = args1..letter
  191.        if (letter == " ") or (string.len(cmd1) == 0) then notaspace = false end
  192.        sleep(0.5)
  193.       end
  194.       if fs.isDir(root..args1) then
  195.         fs.delete(root..args1)
  196.         WriteConsole("Repertoire suprimer ("..args1..")")
  197.       else
  198.         WriteConsole("Repertoire non-trouver")
  199.       end
  200.  
  201.     elseif cmd4 == "help" then
  202.  
  203.       --liste des cmd
  204.       WriteConsole("Commande possible :")
  205.       WriteConsole("/stop")
  206.       WriteConsole("/reboot")
  207.       WriteConsole("/creat (pseudo)")
  208.       WriteConsole("/account -permet de savoir tout les comptes")
  209.       WriteConsole("/rm")
  210.  
  211.     elseif cmd4 == "stop" then
  212.       os.shutdown()
  213.     elseif cmd5 == "creat" then
  214.      
  215.       args1 = ""
  216.       letter = ""
  217.       cmd1 = string.sub(tCommand,7,string.len(tCommand))
  218.       sleep(1)
  219.       notaspace = true
  220.       while notaspace do
  221.  
  222.        letter = string.sub(cmd1,1,1)
  223.        cmd1 = string.sub(cmd1,2,string.len(cmd1))
  224.        --WriteConsole("Args1 = "..args1.." lenght : "..string.len(cmd1))
  225.        args1 = args1..letter
  226.        if (letter == " ") or (string.len(cmd1) == 0) then notaspace = false end
  227.        sleep(0.5)
  228.       end
  229.       if fs.isDir(root..args1) == false then
  230.        fs.makeDir(root..args1)
  231.        DataMDP = fs.open(root..args1.."/MDP.txt","a")
  232.        DataMDP.write("0000")
  233.        DataMDP.close()
  234.        DataMoney = fs.open(root..args1.."/Money.txt","a")
  235.        DataMoney.write("1000")
  236.        DataMoney.close()
  237.        WriteConsole("Compte pour "..args1.." creer")
  238.       else
  239.         WriteConsole("Ce compte existe deja !")
  240.       end
  241.     elseif cmd6 == "reboot" then
  242.       os.reboot()
  243.    
  244.     elseif cmd7 == "account" then
  245.  
  246.       a = fs.list(root)
  247.  
  248.      TAccount = nil
  249.  
  250.      for k,v in pairs(fs.list(root)) do
  251.       if TAccount == nil then
  252.        TAccount = v
  253.       else
  254.        TAccount = TAccount.." , "..v
  255.       end
  256.      end
  257.      WriteConsole(TAccount)
  258.     end
  259.    end
  260. end
  261.  
  262. Console()
  263.  
  264. Environement()
  265. parallel.waitForAll(WaitMsg,InputCmd)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement