-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmrcServer.lua
More file actions
78 lines (68 loc) · 2.25 KB
/
mrcServer.lua
File metadata and controls
78 lines (68 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- Minenet Relay Chat server
-- Requires ender modem
local modem = peripheral.find("modem")
local computerId = os.getComputerID()
modem.open(1)
local function loadHistory() -- Load last 20 chat messages
local history = {}
if fs.exists("chatlog.txt") then
local file = fs.open("chatlog.txt", "r")
while true do
local line = file.readLine()
if not line then break end
local message = textutils.unserializeJSON(line)
table.insert(history, message)
end
file.close()
else
return nil
end
-- Only return the last 20 messages
local total = #history
if total > 20 then
local recent = {}
for i = total - 19, total do
table.insert(recent, history[i])
end
return recent
else
return history
end
end
term.clear()
term.setCursorPos(1, 1)
print("Minenet Relay Chat")
print("Powered by minenet")
print("-------------------")
while true do
local _, _, channel, replyChannel, data = os.pullEvent("modem_message")
if channel == 1 then
if (data["protocol"] == "mrc") and (data["receiver"] == computerId) then
if (data["type"] == "historyRequest") then
local packet = {
protocol = "mrc",
type = "historyResponse",
sender = os.getComputerID(),
receiver = data["sender"],
data = loadHistory()
}
modem.transmit(replyChannel, 1, packet)
elseif (data["type"] == "messageSend") then
local packet = {
protocol = "mrc",
type = "messageReceive",
sender = os.getComputerID(),
receiver = data["sender"],
data = data["data"]
}
print(data["data"]["name"] .. "> " .. data["data"]["content"])
-- Save to logs
local message = data["data"]
local file = fs.open("chatlog.txt", "a")
file.writeLine(textutils.serializeJSON(message))
file.close()
modem.transmit(replyChannel, 1, packet)
end
end
end
end