This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Lamdax's profile picture

[1.58+] HYPE Instant Messager

Started by Lamdax, 20 October 2014 - 07:06 AM
Lamdax #1
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:
  • 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.
That's all I can think of for now. YET, I feel it is good to point out some flaws, so here you go:
  • Not very pretty compared to other programs.
  • Cannot receive messages whilst sending one (by the way, I would love some help with this one!).
Now that you know if you want or not want this program, here are some screenshots and the code and pastebin at the bottom. Also, thank you to CCEmuRedux for the ComputerCraft emulation.

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
Saldor010 #2
Posted 20 October 2014 - 04:49 PM
Well, just a little tip for you, Lua's proper spelling is "Lua" not "LUA". Just thought you might want to know before someone yells at you ;)/>
Edited on 20 October 2014 - 02:49 PM
KingofGamesYami #3
Posted 20 October 2014 - 05:02 PM
Welcome to the forums! I'd like to point out, this program is much better than a lot of "new users" first posts.

However, here are some formatting improvements:
code tags

print( "I'm in code tags!" )
Additionally, comments are a little derped with them:

--I'm a normal lua comment!
print( "this line looks bad" )
--Just adding another ' to stop it
--#I'm a forums safe comment - and I work in a program as well!
Lamdax #4
Posted 21 October 2014 - 10:40 AM
-snip-
-snippy snap-

Thank you both for the advice. I'll edit the OP to put them into effect.
Also, KingofGamesYami, thank you for the compliment, I'm glad that my program is a notch above the other ones!
Edited on 22 October 2014 - 10:21 AM
ShadowLamp #5
Posted 10 November 2014 - 04:53 PM
This doesn't work on the "non-advanced" computers D: :unsure:/>
civilwargeeky #6
Posted 13 November 2014 - 05:12 AM
if you don't mind overwriting things (or you could rename them), you could use this somewhere at the top of the program:

term.setTextColor = function(color) --#Accepts raw color
  if color and term.isColor() then
	  term.setTextColor(color)
  end
end
term.setBackgroundColor = function(color) --#Accepts raw color
  if color and term.isColor() then
	term.setBackgroundColor(color)
  end
end
This just says that if you can properly change the color, then change it.

Disregard, I didn't even bother looking at the code, the person said it didn't work on non-advanced monitors, and I just assumed it didn't check for color.
Your program already does this :D/>
Edited on 13 November 2014 - 04:15 AM
DannySMc #7
Posted 21 November 2014 - 11:16 AM
A nice idea, a good start to make it widely used give it a graphical interface with colours, I have an API that has box drawing and others if needed, and then make it compatible for normal and advanced computers/tablets/turtles :D/>
deletedededed #8
Posted 22 November 2014 - 02:58 AM
How long did it take you to create this code? Did you go from scratch or base it off a previous user? Did you have any past coding experience?
(Sorry about all the questions, I've been trying to create this for almost a week and have gotten almost no-where)

Edit: Spell-checked: expereince
Edited on 22 November 2014 - 01:58 AM