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

DelChat v1.0.0

Started by deljr15, 07 December 2012 - 04:43 PM
deljr15 #1
Posted 07 December 2012 - 05:43 PM
Hi I have been playing computer craft for about a week now and have come up with some interisting programs. I just thought I would share something back.

DelChat features
2 way and conference chat.
the ability to send and recieve multiple messages (dosent require a send then recieve)
auto adjusting screen size
user names

v1.1.0
SpoilerCode
Spoiler

local functionStack = {}
local chat = {}
local users = {}

-- config
chat[0] = "Welcome To DelChat"
local bar = "-" --character the header/footer of input section
-- end

local hScreen, vScreen = term.getSize()
local update = true
local loc = vScreen - 1
local cache = 1
local devider = bar
local localuser

function menu()
	term.clear()
	term.setCursorPos(1, 1)
	print("Enter Your Username:")
	localuser = read()
end

function clear()
	for i=2, hScreen do
		devider = devider .. bar
	end
	header = vScreen - 2
	term.clear()
	term.setCursorPos(1, header)
	term.write(devider)
	term.setCursorPos(1, vScreen)
	term.write(devider)
	term.setCursorPos(1, loc)
end

function openModem()
	sides = rs.getSides()
	for index, side in pairs(sides) do
		
		if peripheral.isPresent(sides[index]) and peripheral.getType(sides[index]) == "modem" then
			rednet.open(sides[index])
			return
		end
	end
	error("No modem found to perform rednet reception.")
end

function getUser(i)
	if users[i] == nil then
		rednet.send(i, "/userRequest")
		id, msg = rednet.receive(10)
		
		if id == nil then
			error("No response from client")
		else
			users[i] = msg
		end
	end
	return users[i]
end

function receiveChat()
	while true do
		id, message = rednet.receive()
		
		if message == "/userRequest" then
			rednet.send(id, tostring(localuser))
		else
			uName = getUser(id)
			if uName == nil then
				chat[cache] = "From " .. id .. ": " .. message
			else
				chat[cache] = uName .. ": " .. message
			end
			cache = cache + 1
			update = true
		end
	end
end

function sendChat()
	while true do
		term.setCursorPos(1, loc)
		term.clearLine()
		send = read()
		rednet.broadcast(send)
		chat[cache] = localuser .. ": " .. send
		cache = cache + 1
		update = true
	end
end

function display()
	while true do
		if update == true then
			term.setCursorPos(1, 1)
			screen = vScreen - 4
			line = cache - screen
			
			if line < 0 then
				line = 0
			end
			
			for i=0, screen do
				term.clearLine()
				print(chat[line + i])
			end
			
			term.setCursorPos(1, loc)
			update = false
		else
			sleep(0.25)
		end
	end
end

table.insert(functionStack, receiveChat)
table.insert(functionStack, sendChat)
table.insert(functionStack, display)

menu()
openModem()
clear()

while true do
		parallel.waitForAny(unpack(functionStack))
end

Download
Spoilerhttp://pastebin.com/79YqqanF

Screenshots
Spoiler
v1.0.0
Spoilercode
Spoiler

local functionStack = {}
local chat = {}

-- config
chat[0] = "Welcome To DelChat"
local bar = "-" --character the header/footer of input section
-- end

local screensizeh, screensizev = term.getSize()
local update = true
local location = screensizev - 1
local cache = 1
local devider = bar

function clear()
		for i=2, screensizeh do
				devider = devider .. bar
		end
		header = screensizev - 2
		term.clear()
		term.setCursorPos(1, header)
		term.write(devider)
		term.setCursorPos(1, screensizev)
		term.write(devider)
end

function openModem()
		local sides = rs.getSides()
		for index, side in pairs(sides) do
				if peripheral.isPresent(sides[index]) and peripheral.getType(sides[index]) == "modem" then
						rednet.open(sides[index])
						return
				end
		end

		error("No modem found to perform rednet reception.")
end


function receiveChat()
		while true do
				id, message = rednet.receive()
				chat[cache] = "From " .. id .. ": " .. message
				cache = cache + 1
				update = true
		end
end

function sendChat()
		while true do
				term.setCursorPos(1, location)
				term.clearLine()
				send = read()
				rednet.broadcast(send)
				chat[cache] = "Sent: " .. send
				cache = cache + 1
				update = true
		end
end

function display()
		while true do
				if update == true then
						term.setCursorPos(1, 1)
						screen = screensizev - 4
						line = cache - screen
						
						if line &amp;lt; 0 then
								line = 0
						end
						
						for i=0, screen do
								term.clearLine()
								print(chat[line + i])
						end
						
						term.setCursorPos(1, location)
						update = false
				else
						sleep(0.25)
				end
		end
end
--table.insert(functionStack, function() shell.run("shell") end)
table.insert(functionStack, receiveChat)
table.insert(functionStack, sendChat)
table.insert(functionStack, display)

clear()
openModem()
while true do
		parallel.waitForAny(unpack(functionStack))
end
Edit: Added Code Tags :)/>/&amp;gt; looks a bit better (not quite as good as it does in programmers notepad)

Screenshot

Spoiler
Plan to add
Secure private chat
Username server –implemented without server
A exit button (any tips on this would be helpful)
Advanced computer compatibility
Dlcruz129 #2
Posted 07 December 2012 - 07:02 PM
You made this in your first week? Impressive. Just a tip though, please indent your code.
deljr15 #3
Posted 07 December 2012 - 07:05 PM
Just a tip though, please indent your code.

Ya I do that the post killed my formating :(/>
bjornir90 #4
Posted 07 December 2012 - 07:08 PM
For the exit button, you could draw it, then make a function that check if it is clicked and finally run this function with parallel with the main functions. Also you can do it without parallel, simply by adding an event to the qeue when the button is clicked, and you check if the event come up with os.pullEvent(). Hope this is comprehensible…
rhyleymaster #5
Posted 07 December 2012 - 07:48 PM
Man, this is nice. Possibly add a function to set up a table for usernames?
Cruor #6
Posted 07 December 2012 - 07:50 PM
Tip - if you use code tags to store code they don't kill indention :)/>
deljr15 #7
Posted 08 December 2012 - 01:54 AM
Man, this is nice. Possibly add a function to set up a table for usernames?
I plan to do this. tho im not too sure how to go about it in the local program… I was thinking add a server when the program starts up it pings and registers its self with a username.

Tip - if you use code tags to store code they don't kill indention :)/>

fixed thanks :)/>

For the exit button, you could draw it, then make a function that check if it is clicked and finally run this function with parallel with the main functions. Also you can do it without parallel, simply by adding an event to the qeue when the button is clicked, and you check if the event come up with os.pullEvent(). Hope this is comprehensible…
sounds intriguing i havent played with clickable programs yet :)/>
bjornir90 #8
Posted 08 December 2012 - 02:48 AM
Look here :
Spoilerhttp://www.computercraft.info/forums2/index.php?/topic/6355-api-mouse-and-graphic-api-v13-add-a-buffer-and-color-customisation-beta-14-released/page__pid__56403#entry56403
at line 8 of the presented API on pastebin if you want to see an example or even use it :)/>
deljr15 #9
Posted 08 December 2012 - 07:47 PM
ill look into that next just updated to v1.1.0 adding usernames without the need for a server
mcgodinater #10
Posted 13 April 2013 - 01:09 PM
U should make it so that in the upper-right corner, there's an X that u click on to exit
Cranium #11
Posted 13 April 2013 - 07:16 PM
Can people stop reviving topics? KTHXBAI