Advertisement
Alyssa

Quest-Server

Aug 5th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.53 KB | None | 0 0
  1. slash = "/"
  2. connectnum = 0
  3. cnum = 0
  4. valid = {}
  5. reg = {}
  6. side = "top"
  7. versionf = fs.open("version", "r")
  8. versionraw = versionf.readLine()
  9. version = versionraw -- I might have to unserialize...
  10. versionf.close()
  11. print("QUEST-SERVER-"..version.." Online!")
  12. print("To reply I must receive QUEST-CLIENT-"..version.."")
  13. os.loadAPI("modem")
  14. modem.open(side, 782)
  15. connected = {} -- Data for connected will be saved, soon
  16. totalusers = "totalusers"
  17. if fs.exists(totalusers) then
  18.  nc = fs.open(totalusers, "r")
  19.  cnum = nc.readLine()
  20.  print("Data for " .. cnum.. " Users")
  21.  nc.close()
  22. end
  23. stayon = true
  24. connectnum = cnum
  25. action = {}
  26. pass = {}
  27. equip = {}
  28. inventory = {}
  29. level = {}
  30. name = {}
  31. areax = {}
  32. areay = {}
  33. userposx = {}
  34. userposy = {}
  35. -- rank = {} Coming soon
  36. message = {}
  37.  
  38. function seperate(themsg)
  39.  sep1, sep2, sep3, sep4=themsg:match("(%S+) (%S+) (%S+) (%S+)")
  40.  if sep1 and sep2 and sep3 and sep4 then
  41.   message1 = sep1
  42.   message2 = sep2
  43.   message3 = sep3
  44.   message4 = sep4
  45.  else
  46.   sep1, sep2, sep3=themsg:match("(%S+) (%S+) (%S+)")
  47.   if sep1 and sep2 and sep3 then
  48.    message1 = sep1
  49.    message2 = sep2
  50.    message3 = sep3
  51.   else
  52.    sep1, sep2=themsg:match("(%S+) (%S+)")
  53.    if sep1 and sep2 then
  54.     message1 = sep1
  55.     message2 = sep2
  56.    else
  57.     sep1=themsg:match("(%S+)")
  58.     if sep1 then
  59.      message1 = sep1
  60.     else
  61.     end
  62.    end
  63.   end
  64.  end
  65.  if sep4 then
  66.   print("seperated message: " .. sep1 .. " " .. sep2 .. " " .. sep3 .. " " .. sep4 .. "")
  67.  elseif sep3 then
  68.   print("seperated message: " .. sep1 .. " " .. sep2 .. " " .. sep3 .. "")
  69.  elseif sep2 then
  70.   print("seperated message: " .. sep1 .. " " .. sep2 .. "")
  71.  elseif sep1 then
  72.   print("seperated message: " .. sep1 .. "")
  73.  else
  74.   print("No seperated message")
  75.  end
  76. end
  77.  
  78. function newarea(direction, clientno)
  79.  if direction == "right" then
  80.   areax[clientno] = areax[clientno] +1
  81.  end
  82.  if direction == "left" then
  83.   areax[clientno] = areax[clientno] -1
  84.  end
  85.  if direction == "up" then
  86.   areay[clientno] = areay[clientno] +1
  87.  end
  88.  if direction == "down" then
  89.   areay[clientno] = areay[clientno] -1
  90.  end
  91. end
  92.  
  93. function savefiles()
  94. for r = 1, connectnum do
  95.  t = connected[t]
  96.  adress = dist .. slash .. "userposx"
  97.  mklevel = fs.open(adress, "w")
  98.  mklevel.write(userposx[t])
  99.  mklevel.close()
  100.  adress = dist .. slash .. "register"
  101.  mkreg = fs.open(adress, "w")
  102.  mkreg.write(reg[t])
  103.  mkreg.close()
  104.  adress = "totalusers"
  105.  mktu = fs.open(adress)
  106.  mktu.write(connectnum)
  107.  mktu.close()
  108.  adress = dist .. slash .. "userposy"
  109.  mklevel = fs.open(adress, "w")
  110.  mklevel.write(userposy[t])
  111.  mklevel.close()
  112.  adress = dist .. slash .. "areax"
  113.  mklevel = fs.open(adress, "w")
  114.  mklevel.write(areax[t])
  115.  mklevel.close()
  116.  adress = dist .. slash .. "areay"
  117.  mklevel = fs.open(adress, "w")
  118.  mklevel.write(areay[t])
  119.  mklevel.close()
  120.  adress = dist .. slash .. "level"
  121.  mklevel = fs.open(adress, "w")
  122.  mklevel.write(level[t])
  123.  mklevel.close()
  124.  adress = dist .. slash .. "inventory"
  125.  mkinv = fs.open(adress, "w")
  126.  mkinv.write(inventory[t])
  127.  mkinv.close()
  128.  adress = dist .. slash .. "equip"
  129.  mkequip = fs.open(adress, "w")
  130.  mkequip.write(equip[t])
  131.  mkequip.close()
  132.  adress = dist .. slash .. "name"
  133.  mkname = fs.open(adress, "w")
  134.  mkname.write(name[t])
  135.  mkname.close()
  136.  adress = dist .. slash .. ".pass"
  137.  mkpass = fs.open(adress, "w")
  138.  mkpass.write(pass[t])
  139.  mkpass.close()
  140.  end
  141.  print("files saved!")
  142. end
  143.  
  144. function loadfiles()
  145.  for y = 1, connectnum do
  146.   loadloc = connected[y]
  147.   adrtoload = loadloc .. slash .. "userposx"
  148.   loading = fs.open(adrtoload, "r")
  149.   userposx[loadloc] = loading.readLine()
  150.   loading.close()
  151.   adrtoload = loadloc .. slash .. "userposy"
  152.   loading = fs.open(adrtoload, "r")
  153.   userposy[loadloc] = loading.readLine()
  154.   loading.close()
  155.   adrtoload = loadloc .. slash .. "register"
  156.   loading = fs.open(adrtoload, "r")
  157.   reg[loadloc] = loading.readLine()
  158.   loading.close()
  159.   adrtoload = loadloc .. slash .. "areay"
  160.   loading = fs.open(adrtoload, "r")
  161.   areay[loadloc] = loading.readLine()
  162.   loading.close()
  163.   adrtoload = loadloc .. slash .. "areax"
  164.   loading = fs.open(adrtoload, "r")
  165.   areax[loadloc] = loading.readLine()
  166.   loading.close()
  167.   adrtoload = loadloc .. slash .. "name"
  168.   loading = fs.open(adrtoload, "r")
  169.   name[loadloc] = loading.readLine()
  170.   loading.close()
  171.   adrtoload = loadloc .. slash .. ".pass"
  172.   loading = fs.open(adrtoload, "r")
  173.   pass[loadloc] = loading.readLine()
  174.   loading.close()
  175.   adrtoload = loadloc .. slash .. "equip"
  176.   loading = fs.open(adrtoload, "r")
  177.   equip[loadloc] = loading.readLine()
  178.   loading.close()
  179.   adrtoload = loadloc .. slash .. "level"
  180.   loading = fs.open(adrtoload, "r")
  181.   level[loadloc] = loading.readLine()
  182.   loading.close()
  183.   adrtoload = loadloc .. slash .. "inventory"
  184.   loading = fs.open(adrtoload, "r")
  185.   inventory[loadloc] = loading.readLine()
  186.   loading.close()
  187.  end
  188.  print("loaded files")
  189. end
  190.  
  191. loadfiles()
  192.  
  193. function newuser(dist, name, pass)
  194.  dist = tostring(dist)
  195.  fs.makeDir(dist)
  196.  adress = dist .. slash .. "areax"
  197.  mkarea = fs.open(adress, "w")
  198.  mkarea.write("1")
  199.  mkarea.close()
  200.  adress = dist .. slash .. "register"
  201.  mkreg = fs.open(adress, "w")
  202.  mkreg.write("true")
  203.  mkreg.close()
  204.  adress = dist .. slash .. "areay"
  205.  mkarea = fs.open(adress, "w")
  206.  mkarea.write("1")
  207.  mkarea.close()
  208.  adress = dist .. slash .. "userposy"
  209.  mkposy = fs.open(adress, "w")
  210.  mkposy.write("2")
  211.  mkposy.close()
  212.  adress = dist .. slash .. "userposx"
  213.  mkposx = fs.open(adress, "w")
  214.  mkposx.write("2")
  215.  mkposx.close()
  216.  adress = dist .. slash .. "level"
  217.  mklevel = fs.open(adress, "w")
  218.  mklevel.write("1")
  219.  mklevel.close()
  220.  adress = dist .. slash .. "inventory"
  221.  mkinv = fs.open(adress, "w")
  222.  mkinv.write("1 2 3")
  223.  mkinv.close()
  224.  adress = dist .. slash .. "equip"
  225.  mkequip = fs.open(adress, "w")
  226.  mkequip.write()
  227.  mkequip.close()
  228.  adress = dist .. slash .. "name"
  229.  mkname = fs.open(adress, "w")
  230.  mkname.write(name)
  231.  mkname.close()
  232.  adress = dist .. slash .. ".pass"
  233.  mkpass = fs.open(adress, "w")
  234.  mkpass.write(pass)
  235.  mkpass.close()
  236.  print("Created user files for " .. dist .."")
  237.  loadfiles()  
  238. end
  239.  
  240. function newconnect(dist)
  241.  dist = tostring(dist)
  242.  for i = 1, connectnum+1 do
  243.   if not connected[i] then
  244.    connected[i] = dist
  245.    action[i] = "created"
  246.    print("User No." .. dist .. " Created!")
  247.    fs.makeDir(dist)
  248.    if i == connectnum+1 then
  249.     connectnum = connectnum +1
  250.    end
  251.   end
  252.  end
  253. end
  254.  
  255. function disconnect(dist)
  256.  for o = 1, connectnum do
  257.   if connected[o] == dist then
  258.    connected[o] = 0
  259.   end
  260.  end
  261. end
  262.  
  263. while stayon == true do
  264.  old = false
  265.  validmsg = false
  266.  if modem.receive() then
  267.   seperate(modem.getmsg())
  268.   validmsg = true
  269.  else
  270.   stayon = false
  271.  end
  272.  if message1 == "QUEST-CLIENT-"..version.."" then
  273.   print("Recieved valid message")
  274.   comp = modem.getdistance()
  275.   connectnum1 = connectnum +1
  276.   ksearch = 1
  277.   for u = 1, connectnum1 do
  278.    if comp == connected[u] then
  279.     old = true
  280.     clientnum = u
  281.     ksearch = 0
  282.    elseif u == connectnum1 and ksearch == 1  then
  283.     old = false
  284.     newconnect(comp)
  285.     clientnum = u
  286.     ksearch = 0
  287.    else
  288.    end
  289.   end
  290.   if validmsg == true then
  291.    if old == false then
  292.     tosend = "QUEST-SERVER-"..version.." SEND-USER-DATA " ..comp
  293.     action[clientnum] = "userdata"
  294.     print("set "..clientnum.."'s action to userdata")
  295.    else
  296.     tosend = "QUEST-SERVER-"..version.." SEND-LOGIN-DATA " ..comp
  297.     action[clientnum] = "logindata"
  298.     print("set "..clientnum.."'s action to login data")
  299.    end
  300.   end
  301.   if message2 == "move" and action[clientnum] == "active" then
  302.    if message3 == "up" and action[clientnum] == "active" then -- oops, I forgot I don't need
  303.     if not userposy <= 2 then
  304.      userposy[clientnum] = userposy[clientnum] -1
  305.     elseif userposy == 2 then
  306.      newarea("up")
  307.     end
  308.    elseif message3 == "down" and action[clientnum] == "active" then --^ this part
  309.     if not userposy >= 21 then
  310.      userposy[clientnum] = userposy[clientnum] +1
  311.     elseif userposy == 21 then
  312.      newarea("down")
  313.     end
  314.    elseif message3 == "right" and action[clientnum] == "active" then
  315.     if not userposx >= 21 then
  316.      userposx[clientnum] = userposx[clientnum] +1
  317.     elseif userposx == 21 then
  318.      newarea("right")
  319.     end
  320.    elseif message3 == "left" and action[clientnum] == "active" then
  321.     if not userposx <= 2 then
  322.      userposx[clientnum] = userposx[clientnum] -1
  323.     elseif userposx == 2 then
  324.      newarea("left")
  325.     end
  326.    end
  327.   end
  328.   if message2 == "login" and action[clientnum] == "logindata" then
  329.    username = message3
  330.    passentered = message4
  331.    if passentered == pass[clientnum] and username == name[clientnum] then
  332.     valid[clientnum] = true
  333.     action[clientnum] = "active"
  334.    print("set "..clientnum.."'s action to active, because of login")
  335.     tosend = "QUEST-SERVER-"..version.." LOGIN-CORRECT " .. comp
  336.    else
  337.     tosend = "QUEST-SERVER-"..version.." LOGIN-INCORRECT " .. comp
  338.    end
  339.   end
  340.   if message2 == "register" and action[clientnum] == "userdata" and reg[clientnum] ~= "true" then
  341.    if action[clientnum] == "userdata" then
  342.     username = message3
  343.     thepass = message4
  344.     name[clientnum] = username
  345.     pass[clientnum] = thepass
  346.     valid[clientnum] = true
  347.     reg[clientnum] = "true"
  348.     action[clientnum] = "active"
  349.     print("set "..clientnum.."'s action to active, because of registering")
  350.     newuser(comp, name, pass)
  351.     tosend = "QUEST-SERVER-"..version.." REGISTERED " .. comp
  352.    end
  353.   end
  354.  end
  355.  modem.transmit(784, 782, tosend)
  356.  if tosend then
  357.   print("Sent: " .. tosend .. "")
  358.  end
  359. end
  360.  
  361. savefiles()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement