SHOW:
|
|
- or go back to the newest paste.
1 | local args = {...} | |
2 | local modem = peripheral.find("modem") | |
3 | local user = args[1] | |
4 | local server = tonumber(args[2]) | |
5 | local event, side, sChannel, id, data, dist, bg, chatBox, sid, H | |
6 | local debug = args[3] | |
7 | if debug then | |
8 | H = 2 | |
9 | else | |
10 | H = 1 | |
11 | end | |
12 | modem.open(os.getComputerID()) | |
13 | ||
14 | local function send(msg) | |
15 | modem.transmit(server, os.getComputerID(), msg) | |
16 | end | |
17 | ||
18 | local function receive() | |
19 | event, side, sChannel, id, data, dist = os.pullEvent("modem_message") | |
20 | end | |
21 | ||
22 | ||
23 | local function writeMsg() | |
24 | - | bg.write(data) |
24 | + | data = data .. " " |
25 | local recursive | |
26 | if #data > 50 and data:find("%s") then | |
27 | - | bg.scroll(1) |
27 | + | recursive = true |
28 | - | bg.setCursorPos(1, y) |
28 | + | local space = data:find("%s", 50) |
29 | - | else |
29 | + | local line = data:sub(0, space - 1) |
30 | - | bg.setCursorPos(1, y + 1) |
30 | + | data = data:sub(space + 1) |
31 | - | end |
31 | + | bg.write(line) |
32 | else | |
33 | recursive = false | |
34 | bg.write(data) | |
35 | end | |
36 | ||
37 | local x, y = bg.getCursorPos() | |
38 | if y >= 15 then | |
39 | bg.scroll(1) | |
40 | bg.setCursorPos(1, y) | |
41 | else | |
42 | bg.setCursorPos(1, y + 1) | |
43 | end | |
44 | if recursive then | |
45 | writeMsg() | |
46 | end | |
47 | end | |
48 | ||
49 | local function sendMsg(msg) | |
50 | send(sid .. ":" .. msg) | |
51 | receive() | |
52 | writeMsg() | |
53 | end | |
54 | ||
55 | local function init() | |
56 | term.clear() | |
57 | botBar = window.create(term.native(), 1, 17, 52, 3) | |
58 | botBar.setBackgroundColor(colors.gray) | |
59 | botBar.clear() | |
60 | bg = window.create(term.native(), 1, 2, 52, 15) | |
61 | bg.setBackgroundColor(colors.lightGray) | |
62 | bg.clear() | |
63 | bg.setCursorPos(1, 2) | |
64 | topBar = window.create(term.native(), 1, 1, 52, 1) | |
65 | topBar.setBackgroundColor(colors.blue) | |
66 | topBar.clear() | |
67 | topBar.setCursorPos(1, 1) | |
68 | topBar.write("Cloud Chat") | |
69 | if debug == "true" then | |
70 | topBar.setCursorPos(45,1) | |
71 | topBar.write("Debug") | |
72 | end | |
73 | chatBox = window.create(term.native(), 2, 18, 49, H) | |
74 | chatBox.setBackgroundColor(colors.black) | |
75 | chatBox.clear() | |
76 | end | |
77 | ||
78 | local function input() | |
79 | chatBox.setCursorPos(1, 1) | |
80 | term.redirect(chatBox) | |
81 | data = read() | |
82 | end | |
83 | ||
84 | init() | |
85 | - | event = parallel.waitForAny(input, receive) |
85 | + | |
86 | - | if event == 1 and sid ~= nil then |
86 | + | |
87 | receive() | |
88 | sid = tonumber(data) | |
89 | if sid == nil then | |
90 | writeMsg() | |
91 | sleep(2) | |
92 | os.pullEvent() | |
93 | term.clear() | |
94 | term.setCursorPos(1, 1) | |
95 | return | |
96 | else | |
97 | modem.open(id) | |
98 | end | |
99 | while true do | |
100 | chatBox.clear() | |
101 | event = parallel.waitForAny(input, receive) | |
102 | if event == 1 and sid ~= nil and data ~= "" then | |
103 | sendMsg(data) | |
104 | elseif event == 2 then | |
105 | writeMsg() | |
106 | end | |
107 | end |