-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmrc.lua
More file actions
183 lines (150 loc) · 5.39 KB
/
mrc.lua
File metadata and controls
183 lines (150 loc) · 5.39 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
-- Minenet Relay Chat client
-- Requires server connection
-- Requires basic wireless modem
local modem = peripheral.find("modem")
local computerId = os.getComputerID()
local computerName = os.getComputerLabel()
local mrcServerId = 2 -- Must be manually set
local w, h = term.getSize()
if (computerName == nil) then
error("Error: Please set a label for the computer.", 0)
end
modem.open(1)
local function requestChatHistory() -- Requests chatlog from server
local packet = {
protocol = "mrc",
type = "historyRequest",
sender = computerId,
receiver = mrcServerId,
data = nil
}
modem.transmit(1, 1 , packet)
-- 2-second timeout
local timerId = os.startTimer(2)
while (true) do
local event, p1, p2, _, p4 = os.pullEvent()
if (event == "timer") then
local eventTimerId = p1
if (eventTimerId == timerId) then
error("Error: Connection to server timed out.", 0)
end
elseif (event == "modem_message") then
local channel = p2
local data = p4
if (channel == 1) then
if (data["protocol"] == "mrc") and (data["type"] == "historyResponse") and (data["sender"] == mrcServerId) and (data["receiver"] == computerId) then
return data["data"]
end
end
end
end
end
-- Print chat history
local chatHistory = requestChatHistory()
term.clear()
if not (chatHistory == nil) then
for _, line in ipairs(chatHistory) do
local name = line["name"]
if (name == computerName) then
name = "You"
end
local message = name .. "> " .. line["content"]
term.setCursorPos(1, h)
term.write(message)
term.scroll(1)
end
end
term.setCursorPos(1, 1)
term.clearLine()
term.write("Minenet Relay Chat Client v1.0")
term.setCursorPos(1, h)
term.write("You> ")
term.setCursorBlink(true)
local input = ""
local function reDraw() -- For redrawing text input
term.setCursorPos(1, 1)
term.clearLine()
term.write("Minenet Relay Chat Client v1.0")
term.setCursorPos(1, h)
term.clearLine()
term.write("You> " .. input)
term.setCursorBlink(true)
end
while true do
local event, p1, p2, p3, p4 = os.pullEvent()
-- Handle incoming modem messages
if (event == "modem_message") then
local channel = p2
local data = p4
if (channel == 1) then
-- Check if message is of valid protocol and type and that it comes from the correct server
if (data["protocol"] == "mrc") and (data["type"] == "messageReceive") and (data["sender"] == mrcServerId) then
local message = data["data"]["name"] .. "> " .. data["data"]["content"]
-- Check that message did not come from self
if not (data["receiver"] == computerId) then
-- Print to terminal with correct formatting
term.clearLine()
term.setCursorPos(1, h)
term.write(message)
term.scroll(1)
reDraw()
end
end
end
-- Handle alphanumeric character keypresses
elseif (event == "char") then
local character = p1
input = input .. character
reDraw()
-- Handle keypresses other than alphanumeric characters
elseif event == "key" then
local key = p1
-- Delete character from input
if key == keys.backspace then
input = input:sub(1, -2)
reDraw()
-- Send message over MRC
elseif key == keys.enter then
if not (input == "") then
local message = {name = computerName, content = input}
local packet = {
protocol = "mrc",
type = "messageSend",
sender = computerId,
receiver = mrcServerId,
data = message
}
modem.transmit(1, 1, packet)
-- 2-second timeout
local timerId = os.startTimer(2)
term.setCursorBlink(false)
while (true) do
local eventMsg, a1, a2, _, a4 = os.pullEvent()
if (eventMsg == "timer") then
local eventTimerId = a1
if (eventTimerId == timerId) then
term.scroll(1)
term.setCursorPos(1, h)
error("Error: Connection to server timed out.", 0)
end
elseif (eventMsg == "modem_message") then
local channel = a2
local data = a4
if (channel == 1) then
if (data["protocol"] == "mrc") and (data["type"] == "messageReceive") and (data["sender"] == mrcServerId) and (data["receiver"] == computerId) then
break
end
end
end
end
-- Display your own message locally
term.clearLine()
term.setCursorPos(1, h)
term.write("You> " .. input)
term.scroll(1)
input = ""
reDraw()
end
end
end
end