Posted 20 October 2014 - 09:06 AM
Hello CC Community!
I come presenting you HYPE, my very own Instant Messager.
I need to start off by saying that I am very new to Lua, so please forgive me if I make some rookie mistakes or some excessive complex code (even if not need be).
Anyway, HYPE has the following features:
Here is the code in a spoiler:
And finally, your good ol' pastebin link: http://pastebin.com/FzKZDeHL
I come presenting you HYPE, my very own Instant Messager.
I need to start off by saying that I am very new to Lua, so please forgive me if I make some rookie mistakes or some excessive complex code (even if not need be).
Anyway, HYPE has the following features:
- Usage of the Modem API to send and receive messages.
- The ability to select any port necessary to send and receive messages.
- Custom display names for users.
- Peer-to-peer; no server necessary!
- Colour support (if the computer has colour).
- Entering and exiting messages.
- Not very pretty compared to other programs.
- Cannot receive messages whilst sending one (by the way, I would love some help with this one!).
HYPE Login Screen.
As you can see, there are colours (woo) and two boxes to enter your display name and your channel.
HYPE General Use.
Two people conversing. This shows the general use. You can press control to access the 'send' and 'leave' commands, which explain themselves.
Here is the code in a spoiler:
Spoiler
--# HYPE v0.1
--# Dimensions 51x19
local sides = {'back', 'top', 'left', 'right', 'front', 'bottom'}
for side = 1, #sides do
if peripheral.getType(sides[side]) == 'modem' then
m_side = sides[side]
break
end
end
if m_side == nil then
error('No Wireless Modem attached! Please attach a Wireless Modem.')
end
modem = peripheral.wrap(m_side)
local function color(col)
if term.isColor() then
term.setTextColor(col)
else
term.setTextColor(colors.white)
end
end
repeat
term.clear()
term.setCursorPos(1,1)
color(colors.red)
print('|------------- HYPE Instant Messager -------------|')
print('')
color(colors.lime)
print('Found Wireless Modem on the '.. m_side ..' side.')
color(colors.orange)
print('IN CHAT, PRESS \'CTRL\' TO SEND/LEAVE.')
print('MAX MESSAGE LENGTH: 34')
color(colors.white)
print('')
print('Display Name (15 Chars Max): ')
print('Channel (Number Under 65535): ')
term.setCursorPos(30,7)
displayName = read()
term.setCursorPos(31,8)
channelNum = read()
until (#displayName <= 15 and #displayName >= 3) and (tonumber(channelNum) ~= nil and tonumber(channelNum) <= 65535 and tonumber(channelNum) >= 0)
channelNum = tonumber(channelNum)
modem.open(channelNum)
displayName = displayName:upper()
modem.transmit(channelNum, channelNum, displayName..' has joined the channel.')
local message_list = {}
local function open_menu()
term.setCursorPos(1,19)
term.clearLine()
term.write('[Send] Leave ')
local selected = 'send'
while true do
local event, key = os.pullEvent('key')
if key == keys.left or key == keys.right then
term.clearLine()
term.setCursorPos(1,19)
if selected == 'send' then
term.write(' Send [Leave]')
selected = 'leave'
elseif selected == 'leave' then
term.write('[Send] Leave ')
selected = 'send'
end
elseif key == 1 or key == keys.leftCtrl or key == keys.rightCtrl then
do_return = false
break
elseif key == keys.enter then
do_return = true
break
end
end
term.clearLine()
term.setCursorPos(1,19)
write('Press CTRL to access menu.')
if do_return then
return selected
elseif do_return ~= true then
return nil
end
end
local function main()
while true do
term.clear()
term.setCursorPos(1,1)
color(colors.red)
term.setCursorPos(1,1)
write('|------------- HYPE INSTANT MESSAGER -------------|')
term.setCursorPos(1,18)
write('|-------------------------------------------------|')
color(colors.white)
term.setCursorPos(1,19)
write('Press CTRL to access menu.')
for line = 2, 17 do
term.setCursorPos(1,line)
term.clearLine()
end
term.setCursorPos(1,17)
for item = #message_list, 1, -1 do
write(message_list[item])
local posx,posy = term.getCursorPos()
if posy <= 2 then
term.setCursorPos(1,1)
break
end
term.setCursorPos(1, posy-1)
end
local event, key, freq, replFreq, mes, dist = os.pullEvent()
if event == 'modem_message' then
table.insert(message_list, mes)
if #message_list > 17 then
table.remove(message_list, 1)
end
elseif event == 'key' then
if key == keys.leftCtrl or key == keys.rightCtrl then
local returned = open_menu()
if returned == 'send' then
term.setCursorPos(1,19)
term.clearLine()
term.setCursorPos(1,19)
write(displayName..': ')
message = read()
term.setCursorPos(1,1)
if #message <= 34 then
table.insert(message_list, displayName..': '..message)
modem.transmit(channelNum, channelNum, displayName..': '..message)
end
elseif returned == 'leave' then
modem.transmit(channelNum, channelNum, displayName..' has left the channel.')
modem.closeAll()
term.clear()
term.setCursorPos(1,10)
color(colors.red)
term.write('|----------- Thank you for using HYPE. -----------|')
color(colors.white)
os.sleep(1)
term.clear()
term.setCursorPos(1,1)
return
end
end
end
end
end
main()
And finally, your good ol' pastebin link: http://pastebin.com/FzKZDeHL
Constructive criticism is very welcome, and encouraged. I would like to know what I can do better for next time I write a program. I hope you enjoy, and thank you for using HYPE. :)/>
Edited on 21 October 2014 - 08:43 AM