Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- slash = "/"
- connectnum = 0
- cnum = 0
- valid = {}
- reg = {}
- side = "top"
- versionf = fs.open("version", "r")
- versionraw = versionf.readLine()
- version = versionraw -- I might have to unserialize...
- versionf.close()
- print("QUEST-SERVER-"..version.." Online!")
- print("To reply I must receive QUEST-CLIENT-"..version.."")
- os.loadAPI("modem")
- modem.open(side, 782)
- connected = {} -- Data for connected will be saved, soon
- totalusers = "totalusers"
- if fs.exists(totalusers) then
- nc = fs.open(totalusers, "r")
- cnum = nc.readLine()
- print("Data for " .. cnum.. " Users")
- nc.close()
- end
- stayon = true
- connectnum = cnum
- action = {}
- pass = {}
- equip = {}
- inventory = {}
- level = {}
- name = {}
- areax = {}
- areay = {}
- userposx = {}
- userposy = {}
- -- rank = {} Coming soon
- message = {}
- function seperate(themsg)
- sep1, sep2, sep3, sep4=themsg:match("(%S+) (%S+) (%S+) (%S+)")
- if sep1 and sep2 and sep3 and sep4 then
- message1 = sep1
- message2 = sep2
- message3 = sep3
- message4 = sep4
- else
- sep1, sep2, sep3=themsg:match("(%S+) (%S+) (%S+)")
- if sep1 and sep2 and sep3 then
- message1 = sep1
- message2 = sep2
- message3 = sep3
- else
- sep1, sep2=themsg:match("(%S+) (%S+)")
- if sep1 and sep2 then
- message1 = sep1
- message2 = sep2
- else
- sep1=themsg:match("(%S+)")
- if sep1 then
- message1 = sep1
- else
- end
- end
- end
- end
- if sep4 then
- print("seperated message: " .. sep1 .. " " .. sep2 .. " " .. sep3 .. " " .. sep4 .. "")
- elseif sep3 then
- print("seperated message: " .. sep1 .. " " .. sep2 .. " " .. sep3 .. "")
- elseif sep2 then
- print("seperated message: " .. sep1 .. " " .. sep2 .. "")
- elseif sep1 then
- print("seperated message: " .. sep1 .. "")
- else
- print("No seperated message")
- end
- end
- function newarea(direction, clientno)
- if direction == "right" then
- areax[clientno] = areax[clientno] +1
- end
- if direction == "left" then
- areax[clientno] = areax[clientno] -1
- end
- if direction == "up" then
- areay[clientno] = areay[clientno] +1
- end
- if direction == "down" then
- areay[clientno] = areay[clientno] -1
- end
- end
- function savefiles()
- for r = 1, connectnum do
- t = connected[t]
- adress = dist .. slash .. "userposx"
- mklevel = fs.open(adress, "w")
- mklevel.write(userposx[t])
- mklevel.close()
- adress = dist .. slash .. "register"
- mkreg = fs.open(adress, "w")
- mkreg.write(reg[t])
- mkreg.close()
- adress = "totalusers"
- mktu = fs.open(adress)
- mktu.write(connectnum)
- mktu.close()
- adress = dist .. slash .. "userposy"
- mklevel = fs.open(adress, "w")
- mklevel.write(userposy[t])
- mklevel.close()
- adress = dist .. slash .. "areax"
- mklevel = fs.open(adress, "w")
- mklevel.write(areax[t])
- mklevel.close()
- adress = dist .. slash .. "areay"
- mklevel = fs.open(adress, "w")
- mklevel.write(areay[t])
- mklevel.close()
- adress = dist .. slash .. "level"
- mklevel = fs.open(adress, "w")
- mklevel.write(level[t])
- mklevel.close()
- adress = dist .. slash .. "inventory"
- mkinv = fs.open(adress, "w")
- mkinv.write(inventory[t])
- mkinv.close()
- adress = dist .. slash .. "equip"
- mkequip = fs.open(adress, "w")
- mkequip.write(equip[t])
- mkequip.close()
- adress = dist .. slash .. "name"
- mkname = fs.open(adress, "w")
- mkname.write(name[t])
- mkname.close()
- adress = dist .. slash .. ".pass"
- mkpass = fs.open(adress, "w")
- mkpass.write(pass[t])
- mkpass.close()
- end
- print("files saved!")
- end
- function loadfiles()
- for y = 1, connectnum do
- loadloc = connected[y]
- adrtoload = loadloc .. slash .. "userposx"
- loading = fs.open(adrtoload, "r")
- userposx[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. "userposy"
- loading = fs.open(adrtoload, "r")
- userposy[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. "register"
- loading = fs.open(adrtoload, "r")
- reg[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. "areay"
- loading = fs.open(adrtoload, "r")
- areay[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. "areax"
- loading = fs.open(adrtoload, "r")
- areax[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. "name"
- loading = fs.open(adrtoload, "r")
- name[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. ".pass"
- loading = fs.open(adrtoload, "r")
- pass[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. "equip"
- loading = fs.open(adrtoload, "r")
- equip[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. "level"
- loading = fs.open(adrtoload, "r")
- level[loadloc] = loading.readLine()
- loading.close()
- adrtoload = loadloc .. slash .. "inventory"
- loading = fs.open(adrtoload, "r")
- inventory[loadloc] = loading.readLine()
- loading.close()
- end
- print("loaded files")
- end
- loadfiles()
- function newuser(dist, name, pass)
- dist = tostring(dist)
- fs.makeDir(dist)
- adress = dist .. slash .. "areax"
- mkarea = fs.open(adress, "w")
- mkarea.write("1")
- mkarea.close()
- adress = dist .. slash .. "register"
- mkreg = fs.open(adress, "w")
- mkreg.write("true")
- mkreg.close()
- adress = dist .. slash .. "areay"
- mkarea = fs.open(adress, "w")
- mkarea.write("1")
- mkarea.close()
- adress = dist .. slash .. "userposy"
- mkposy = fs.open(adress, "w")
- mkposy.write("2")
- mkposy.close()
- adress = dist .. slash .. "userposx"
- mkposx = fs.open(adress, "w")
- mkposx.write("2")
- mkposx.close()
- adress = dist .. slash .. "level"
- mklevel = fs.open(adress, "w")
- mklevel.write("1")
- mklevel.close()
- adress = dist .. slash .. "inventory"
- mkinv = fs.open(adress, "w")
- mkinv.write("1 2 3")
- mkinv.close()
- adress = dist .. slash .. "equip"
- mkequip = fs.open(adress, "w")
- mkequip.write()
- mkequip.close()
- adress = dist .. slash .. "name"
- mkname = fs.open(adress, "w")
- mkname.write(name)
- mkname.close()
- adress = dist .. slash .. ".pass"
- mkpass = fs.open(adress, "w")
- mkpass.write(pass)
- mkpass.close()
- print("Created user files for " .. dist .."")
- loadfiles()
- end
- function newconnect(dist)
- dist = tostring(dist)
- for i = 1, connectnum+1 do
- if not connected[i] then
- connected[i] = dist
- action[i] = "created"
- print("User No." .. dist .. " Created!")
- fs.makeDir(dist)
- if i == connectnum+1 then
- connectnum = connectnum +1
- end
- end
- end
- end
- function disconnect(dist)
- for o = 1, connectnum do
- if connected[o] == dist then
- connected[o] = 0
- end
- end
- end
- while stayon == true do
- old = false
- validmsg = false
- if modem.receive() then
- seperate(modem.getmsg())
- validmsg = true
- else
- stayon = false
- end
- if message1 == "QUEST-CLIENT-"..version.."" then
- print("Recieved valid message")
- comp = modem.getdistance()
- connectnum1 = connectnum +1
- ksearch = 1
- for u = 1, connectnum1 do
- if comp == connected[u] then
- old = true
- clientnum = u
- ksearch = 0
- elseif u == connectnum1 and ksearch == 1 then
- old = false
- newconnect(comp)
- clientnum = u
- ksearch = 0
- else
- end
- end
- if validmsg == true then
- if old == false then
- tosend = "QUEST-SERVER-"..version.." SEND-USER-DATA " ..comp
- action[clientnum] = "userdata"
- print("set "..clientnum.."'s action to userdata")
- else
- tosend = "QUEST-SERVER-"..version.." SEND-LOGIN-DATA " ..comp
- action[clientnum] = "logindata"
- print("set "..clientnum.."'s action to login data")
- end
- end
- if message2 == "move" and action[clientnum] == "active" then
- if message3 == "up" and action[clientnum] == "active" then -- oops, I forgot I don't need
- if not userposy <= 2 then
- userposy[clientnum] = userposy[clientnum] -1
- elseif userposy == 2 then
- newarea("up")
- end
- elseif message3 == "down" and action[clientnum] == "active" then --^ this part
- if not userposy >= 21 then
- userposy[clientnum] = userposy[clientnum] +1
- elseif userposy == 21 then
- newarea("down")
- end
- elseif message3 == "right" and action[clientnum] == "active" then
- if not userposx >= 21 then
- userposx[clientnum] = userposx[clientnum] +1
- elseif userposx == 21 then
- newarea("right")
- end
- elseif message3 == "left" and action[clientnum] == "active" then
- if not userposx <= 2 then
- userposx[clientnum] = userposx[clientnum] -1
- elseif userposx == 2 then
- newarea("left")
- end
- end
- end
- if message2 == "login" and action[clientnum] == "logindata" then
- username = message3
- passentered = message4
- if passentered == pass[clientnum] and username == name[clientnum] then
- valid[clientnum] = true
- action[clientnum] = "active"
- print("set "..clientnum.."'s action to active, because of login")
- tosend = "QUEST-SERVER-"..version.." LOGIN-CORRECT " .. comp
- else
- tosend = "QUEST-SERVER-"..version.." LOGIN-INCORRECT " .. comp
- end
- end
- if message2 == "register" and action[clientnum] == "userdata" and reg[clientnum] ~= "true" then
- if action[clientnum] == "userdata" then
- username = message3
- thepass = message4
- name[clientnum] = username
- pass[clientnum] = thepass
- valid[clientnum] = true
- reg[clientnum] = "true"
- action[clientnum] = "active"
- print("set "..clientnum.."'s action to active, because of registering")
- newuser(comp, name, pass)
- tosend = "QUEST-SERVER-"..version.." REGISTERED " .. comp
- end
- end
- end
- modem.transmit(784, 782, tosend)
- if tosend then
- print("Sent: " .. tosend .. "")
- end
- end
- savefiles()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement