Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local connectedIPs = {};
- local hasModem;
- local width, height = term.getSize();
- if(not fs.exists("./users/"))then
- fs.makeDir("./users/");
- end
- function print(...)
- local y=({term.getCursorPos()})[2];
- for i,text in pairs({...})do
- text = tostring(text);
- term.write(text);
- y = y + 1;
- if(y==height)then y=1; term.clear(); end
- term.setCursorPos(1, y);
- end
- end
- function getModem()
- local sides = {"left","right","front","back","bottom","top"};
- for i=1,#sides do
- hasModem = (peripheral.isPresent(sides[i]) and peripheral.getType(sides[i])=='modem') or false;
- if(hasModem)then
- hasModem = sides[i];
- rednet.open(hasModem);
- term.clear(); term.setCursorPos(1,1); print("Computer ID:"..tostring(os.computerID()));
- end
- end
- end
- getModem();
- function split(text, delimeter)
- delimeter = delimeter or "\28";
- local data = {};
- for key in text:gmatch("([^"..delimeter.."]+)"..delimeter) do
- table.insert(data, key);
- end
- return data;
- end
- function newUser(name, password, sender)
- local sFile = "./users/"..name;
- local bExist = fs.exists(sFile);
- if(bExist)then return "User already exists!"; end
- local hFile = fs.open(sFile, "w");
- hFile.write(password.."\28");
- hFile.close();
- for i = 1,#connectedIPs do
- if(connectedIPs[i][1] == sender)then
- connectedIPs[i][2]=name;
- print(connectedIPs[i][2].." created an account!");
- break;
- end
- end
- return 'success';
- end
- function login(name, password, sender)
- local sFile = "./users/"..name;
- local bExist = fs.exists(sFile);
- if(not bExist)then return "User doesn't exist!"; end
- local hFile = fs.open(sFile, "r");
- local password2 = unpack(split(hFile.readAll()));
- if(password2==password)then
- for i = 1,#connectedIPs do
- if(connectedIPs[i][1] == sender)then
- connectedIPs[i][2]=name;
- print(connectedIPs[i][2].." logged in!");
- break;
- end
- end
- return 'success';
- else
- return 'Incorrect Password!';
- end
- hFile.close();
- end
- function handleIncomingConnection(sender, response, distance)
- print("==DATA SENT : "..sender.."==");
- if(response == 'connect')then
- print("User Connecting");
- while(true)do
- rednet.send(sender, "handshake");
- local eventData = {os.pullEvent("rednet_message")};
- if(eventData[2]==sender and eventData[3]=='connected')then
- for i = 1,#connectedIPs do
- if(connectedIPs[i][1] == sender)then
- table.remove(connectedIPs, i);
- end
- end
- table.insert(connectedIPs,{sender, "Guest"});
- print("User Connected")
- break;
- end
- end
- elseif(response == 'logout')then
- for i = 1,#connectedIPs do
- if(connectedIPs[i][1] == sender)then
- print(connectedIPs[i][2].." logged out!");
- connectedIPs[i][2]="Guest"; break;
- end
- end
- elseif(response == 'disconnect')then
- for i = 1,#connectedIPs do
- if(connectedIPs[i][1] == sender)then
- table.remove(connectedIPs,i); print("Disconnected!"); break;
- end
- end
- elseif(response:sub(1,1)=='*')then
- response = response:sub(2);
- local data = split(response);
- local key = data[1];
- if(key == 'newaccount')then
- local username = data[2];
- local password = data[3];
- rednet.send(sender, newUser(username, password, sender));
- elseif(key == 'loginaccount')then
- local username = data[2];
- local password = data[3];
- rednet.send(sender, login(username, password, sender));
- end
- else
- print(response);
- end
- print("==END DATA SENT==");
- end
- local eventData;
- while(true)do
- eventData = {os.pullEvent()};
- if(eventData[1]=='rednet_message')then
- handleIncomingConnection(eventData[2], eventData[3], eventData[4]);
- elseif(eventData[1]=='key')then
- local key = eventData[2];
- if(key==25)then
- local cLogged = 0;
- for i = 1,#connectedIPs do
- if(connectedIPs[i][2]~="Guest")then
- cLogged = cLogged + 1;
- end
- end
- print("There is "..#connectedIPs.." player(s) connected!");
- print("There is "..cLogged.." player(s) logged in!");
- elseif(key==46)then
- term.clear(); term.setCursorPos(1,1);
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment