SHOW:
|
|
- or go back to the newest paste.
1 | local function init_server() | |
2 | rednet.open("back") | |
3 | rednet.host("123", "server") | |
4 | write("Server is "..rednet.lookup("123").."\n") | |
5 | end | |
6 | ||
7 | local function get_id_list() | |
8 | local file = fs.open("server/info", "r") | |
9 | local data = file.readAll() | |
10 | file.close() | |
11 | return textutils.unserialise(data) | |
12 | end | |
13 | ||
14 | local function check_field(id_list, id, field) | |
15 | for i=1,#id_list do | |
16 | if id==id_list[i][field] then | |
17 | return i | |
18 | end | |
19 | end | |
20 | return 0 | |
21 | end | |
22 | ||
23 | local function check_id(id_list) | |
24 | local sender, message, protocol | |
25 | local offset | |
26 | ||
27 | sender, message, protocol = rednet.receive() | |
28 | write("Received Login\n") | |
29 | i = check_field(id_list, message, 1) | |
30 | if i~=0 then | |
31 | rednet.send(sender, "OK") | |
32 | sender, message = rednet.receive() | |
33 | write("Received Pass\n") | |
34 | if id_list[i][2]==message then | |
35 | write(id_list[i][1].." connected!\n") | |
36 | rednet.send(sender, "OK") | |
37 | message = "" | |
38 | while message ~= "Received" do | |
39 | sender, message = rednet.receive() | |
40 | end | |
41 | rednet.send(sender, id_list[i][3]) | |
42 | return true | |
43 | else | |
44 | write("Pass incorrect\n") | |
45 | end | |
46 | else | |
47 | write("Login incorrect\n") | |
48 | end | |
49 | rednet.send(sender, "KO") | |
50 | return false | |
51 | end | |
52 | ||
53 | local function loop(id_list) | |
54 | while true do | |
55 | if check_id(id_list) then | |
56 | break | |
57 | end | |
58 | end | |
59 | end | |
60 | ||
61 | local function end_server() | |
62 | rednet.close("back") | |
63 | rednet.unhost("123", "server") | |
64 | write("Stopped the server\n") | |
65 | end | |
66 | ||
67 | local function main() | |
68 | local id_list | |
69 | ||
70 | init_server() | |
71 | id_list = get_id_list() | |
72 | loop(id_list) | |
73 | end_server() | |
74 | end | |
75 | ||
76 | main() |