Jump to content

[HELP] How to fix bugs in this chat script?


Tokio

Recommended Posts

Why not see another players my messages?
And how to add built-in, and addCommandHandler commands to this chat system?

Client:

Spoiler

local sx, sy = guiGetScreenSize()

local chat = {}
local chatTimer = nil
chat.messages = {}
chat.inputActive = false
chat.maxShowedMessage = 11
chat.boxAlpha = 50
chat.input = ''

smileys = {
	['xd'] = 'xd.png',
	['Xd'] = 'xd.png',
	['XD'] = 'xd.png',
	['xD'] = 'xd.png',
	[':D'] = 'd.png',
	[':d'] = 'd.png',
	['D:'] = 'd.png',
	['d:'] = 'd.png',
	['<3'] = 'sziv.png',
	['szia'] = 'szia.png',
	['maci'] = 'maci.png',
	['medve'] = 'maci.png',
	['bocs'] = 'maci.png',
	['bomba'] = 'bomba.png',
	['bombi'] = 'bomba.png',
	['mosoly'] = 'smiley.png',
	['mosolyog'] = 'smiley.png',
}

badWords = {
	['seemta'] = 'látás',
	['hl'] = 'magyar élet',
	['hungary life'] = 'hl',
	['socialgaming'] = 'szociális közösség',
	['social gaming'] = 'szociális közösség'
	--['']
}

commands = {
	['cc'] = {
		function()
			chat.messages = {}
			playSound('sounds/info.mp3')
			triggerEvent('chat->NewMessage', root, 'info', 'Chatbox kiűrítve!')
		end
	},
}

local oTip = 0
local tips = {
	{'info', 'tip1'},
}

addEventHandler('onClientResourceStart', resourceRoot, 
	function()
   		createTipp()
   	end
)

function createTipp()
    local newTipp = math.random(1, #tips)

    if newTipp == oTip then
        createTipp()
    end

	local text = tips[newTipp]
	playSound('sounds/info2.mp3')
	triggerEvent('chat->NewMessage', root, text[1], text[2])
end

local seconds = 5
setTimer(createTipp, seconds * 10000, 0)

local quest = {}
quest.active = false
quest.success = false
quest.new = 0
quest.question = {
	{'szamologep', 'Mennyi #FF9A0010 #FFFFFF+ #FF9A0010 #FF9A00?', '20'},
}

addEventHandler('onClientResourceStart', resourceRoot, 
	function()
   		generateQuestion()
   	end
)

function generateQuestion()
   	quest.new = math.random(1, #quest.question)

    local text = quest.question[quest.new]
    quest.active = true

   	triggerEvent('chat->NewMessage', root, text[1], text[2])
end

addEventHandler('onClientResourceStart', resourceRoot, 
	function()
		TwCen14 = dxCreateFont('TwCen.ttf', 14)
		showChat(false)
	end
)

addEventHandler('onClientRender', root, function()
	if chat.inputActive then
		chat.boxAlpha = chat.boxAlpha + 15
		if chat.boxAlpha >= 100 then
			chat.boxAlpha = 100
		end

		dxDrawRectangle(sx / 2 - 450 - 230, sy / 2 - 300 - 75, 650, 320, tocolor(0, 0, 0, chat.boxAlpha))
		dxDrawRectangle(sx / 2 - 450 - 230, sy / 2 - 90, 650, 35, tocolor(0, 0, 0, chat.boxAlpha))
		dxDrawText(chat.input, sx / 2 - 450 - 220, sy / 2 - 300 - 60 + 25 * chat.maxShowedMessage, 450, 300, tocolor(255, 255, 255, 255), 1, TwCen14)
	else
		chat.boxAlpha = 0
	end

	local downY = 0
	for k, v in pairs(chat.messages) do
		noHexText = v[2]:gsub('#%x%x%x%x%x%x', '')

		if v[1] then
			local txtWithSmiley = nil
			local smileyIcon = nil

			for i, s in pairs(smileys) do
				i = string.lower(i) or string.upper(i)
				if string.find(noHexText, i) then
					txtWithSmiley = v[2]:gsub(i, '')
					smileyIcon = i
				end
			end

			dxDrawImage(sx / 2 - 450 - 220, sy / 2 - 300 - 60 + downY, 16, 16, 'images/' .. v[1] .. '.png')
			dxDrawText(noHexText, sx / 2 - 450 - 200 - 1, sy / 2 - 300 - 60 + downY - 5 + 1, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14)
			dxDrawText(noHexText, sx / 2 - 450 - 200 + 2, sy / 2 - 300 - 60 + downY - 5 + 2, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14)
			dxDrawText(noHexText, sx / 2 - 450 - 200 - 1, sy / 2 - 300 - 60 + downY - 5 + 1, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14)
			dxDrawText(noHexText, sx / 2 - 450 - 200 + 2, sy / 2 - 300 - 60 + downY - 5 + 2, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14)

			dxDrawText(v[2], sx / 2 - 450 - 200, sy / 2 - 300 - 60 + downY - 5, 450, 300, tocolor(255, 255, 255, 255), 1, TwCen14, _, _, false, false, false, true)
		elseif not v[1] then
			local txtWithSmiley2 = nil
			local smileyIcon2 = nil

			for i, s in pairs(smileys) do
				if string.find(noHexText, i) then
					txtWithSmiley2 = noHexText:gsub(i, '')
					txtWithSmiley3 = v[2]:gsub(i, '')
					smileyIcon2 = i
				end
			end

			if txtWithSmiley2 ~= nil then
				dxDrawText(txtWithSmiley2, sx / 2 - 450 - 220 - 1, sy / 2 - 300 - 60 + downY + 1, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14, _, _, false, false, false, true)
				dxDrawText(txtWithSmiley2, sx / 2 - 450 - 220 + 2, sy / 2 - 300 - 60 + downY + 2, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14, _, _, false, false, false, true)
				dxDrawText(txtWithSmiley2, sx / 2 - 450 - 220 - 1, sy / 2 - 300 - 60 + downY + 1, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14, _, _, false, false, false, true)
				dxDrawText(txtWithSmiley2, sx / 2 - 450 - 220 + 2, sy / 2 - 300 - 60 + downY + 2, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14, _, _, false, false, false, true)

				dxDrawText(txtWithSmiley3, sx / 2 - 450 - 220, sy / 2 - 300 - 60 + downY, 450, 300, tocolor(255, 255, 255, 255), 1, TwCen14, _, _, false, false, false, true)
		
				dxDrawImage(sx / 2 - 450 - 220 + dxGetTextWidth(txtWithSmiley2, 1, TwCen14, false) + 5, sy / 2 - 300 - 60 + downY + 5, 16, 16, 'images/' .. smileys[smileyIcon2])
			else
				dxDrawText(noHexText, sx / 2 - 450 - 220 - 1, sy / 2 - 300 - 60 + downY + 1, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14, _, _, false, false, false, true)
				dxDrawText(noHexText, sx / 2 - 450 - 220 + 2, sy / 2 - 300 - 60 + downY + 2, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14, _, _, false, false, false, true)
				dxDrawText(noHexText, sx / 2 - 450 - 220 - 1, sy / 2 - 300 - 60 + downY + 1, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14, _, _, false, false, false, true)
				dxDrawText(noHexText, sx / 2 - 450 - 220 + 2, sy / 2 - 300 - 60 + downY + 2, 450, 300, tocolor(0, 0, 0, 150), 1, TwCen14, _, _, false, false, false, true)

				dxDrawText(v[2], sx / 2 - 450 - 220, sy / 2 - 300 - 60 + downY, 450, 300, tocolor(255, 255, 255, 255), 1, TwCen14, _, _, false, false, false, true)
			end
		end
		
		downY = downY + 25
	end
end)

addEvent('chat->NewMessage', true)
addEventHandler('chat->NewMessage', root, function(icon, message)
	if #chat.messages >= chat.maxShowedMessage then
		table.remove(chat.messages, 1)
	end

	table.insert(chat.messages, {icon, message})
end)

bindKey('t', 'down', function()
	chat.inputActive = true
end)

addEventHandler('onClientCharacter', getRootElement(), function(character)
	if chat.inputActive then
		chat.input = chat.input .. character
		playSound('sounds/write.wav')

		if string.len(chat.input) > 100 then
			chat.input = string.sub(chat.input, 0, string.len(chat.input) - 1)
		end
	end
end)

addEventHandler('onClientKey', getRootElement(), function(key, ePress)
	if chat.inputActive then
		if key == 'enter' and ePress then
			if string.find(string.sub(chat.input, 1, 1), '/') then
				for k, v in pairs(commands) do
					if k == chat.input:gsub('/', '') then
						v[1]()
					end
				end

				chat.inputActive = false
				chat.input = ''
			else
 				if string.len(chat.input) > 0 then
 					if quest.active then
 						if string.find(chat.input, quest.question[quest.new][3]) then
 							local randomCoin = math.random(200, 450)
 							for i, v in pairs(getElementsByType('player')) do
								triggerEvent('chat->NewMessage', v, 'info', getPlayerName(localPlayer) .. ' jól válaszolt a kérdésre! Ezért kapott #FF9A00' .. randomCoin .. ' #FFFFFFcoint!')
							end

							playSound('sounds/info2.mp3')

 							quest.active = false
 							quest.success = false

 							setTimer(function()
 								if not quest.active then
 									generateQ()
 								end
 							end, 20 * 5000, 1)
 						else
 							playSound('sounds/info2.mp3')
							triggerEvent('chat->NewMessage', localPlayer, 'hiba', 'Rossz válasz!')
							quest.active = false
 							quest.success = false

 							setTimer(function()
 								if not quest.success and not quest.active then
 									for i, v in pairs(getElementsByType('player')) do
										triggerEvent('chat->NewMessage', v, quest.question[quest.new][1], '#ffffff[#FF9A00Teszt#ffffff] Senki nem tudta a helyes választ a kérdésre.')
									end

 									generateQuestion()
 								end
 							end, 20 * 5000, 1)
						end
 					end

					chat.input = getPlayerName(localPlayer) .. ': ' .. chat.input

					for k, v in pairs(badWords) do
						if string.find(chat.input, k) then
							chat.input = chat.input:gsub(k, v)
						end
					end

					for i, v in pairs(getElementsByType('player')) do
						triggerEvent('chat->NewMessage', v, false, chat.input)
					end

					chat.inputActive = false
					chat.input = ''
				elseif string.len(chat.input) == 0 then
					chat.inputActive = false
					chat.input = ''
				end
			end
		end

		if key == 'backspace' and ePress then
			chat.input = string.sub(chat.input, 0, string.len(chat.input) - 1)
		end
	end
end)

function calculateChat(cmd, ...)
	local message = table.concat({...}, ' ')
	if string.len(message) > 0 then
		message = getPlayerName(localPlayer) .. ': ' .. message
		for i, v in pairs(getElementsByType('player')) do
			triggerEvent('chat->NewMessage', v, false, message)
			playSound('sounds/info2.mp3')
		end
	end
end
addCommandHandler('say', calculateChat)
addCommandHandler('saY', calculateChat)
addCommandHandler('Say', calculateChat)
addCommandHandler('SAY', calculateChat)
addCommandHandler('SAY', calculateChat)

addEventHandler('onClientPlayerJoin', getRootElement(), function()
	for k, v in pairs(getElementsByType('player')) do
		playSound('sounds/info.mp3')
		triggerEvent('chat->NewMessage', v, 'csatlakozas', getPlayerName(source) .. ' csatlakozott!')
	end
end)

function createMessage(type, message)
	triggerEvent('chat->NewMessage', root, type, message)
end

 

Server:

Spoiler

addEventHandler('onPlayerChat', getRootElement(), function()
	cancelEvent()
end)

 

Thanks the help!

Link to comment

Individual clients don't have a peer-to-peer connection between each other, so you have to run everything via the server. When a client wants to send a message, relay it through the server via triggerServerEvent and then triggerClientEvent to all clients (with a single call). Anyway, if you're going to do something like that, as a kind of optimization, you may as well just have the /say handler on the serverside instead of the client if all the client does is send it to the server. If you want to play sounds/info2.mp3 when localPlayer uses /say you could just play the sound via chat->NewMessage if source == localPlayer

Edited by MrTasty
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...