aboutsummaryrefslogtreecommitdiffstats
path: root/Gamestates/Multiplayer/Protocol.lua
blob: 6f3b7399cd8592101c6fe68902e2680bf2295b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
--[[
	Gamestates/Multiplayer/Protocol.lua
	Some functions involved in talking between 2 clients
	by piernov

	Comment: maybe useless.
]]--
local Protocol = {}

function Protocol.acceptGame(host, peer_id, accept) -- Send an answer to a connection from another player
	local peer = host:get_peer(peer_id)
	if accept then
		peer:send("ACCEPT: YES", 0, "reliable")
	else
		peer:send("ACCEPT: NO", 0, "reliable")
		peer:disconnect_later()
	end
end

function Protocol.parseGlobalEvents(event) -- Shared function between local menu and game which disallow multiple connections and handle disconnect
	if event.type == "connect" then
		if event.peer:index() > 1 then
			print("Already connected")
			event.peer:disconnect(2)
		end
	elseif event.type == "disconnect" then
		print("Disconnect " .. tostring(event.peer))
		if event.data == 2 then
			print("Peer is busy")
			event.peer:disconnect()
		end
	end
end

return Protocol