aboutsummaryrefslogtreecommitdiffstats
path: root/Gamestates/Multiplayer/Local.lua
blob: aaa13f6b6a8a315c36a963279914316afe7f7f61 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
local Local = {}

require "enet"

local Gui = require "Quickie"
local Utils = require "Utils"

local connect, connected = false, false
Local.server_host = { text = "" }
Local.myservice = { name = "Mastermind", ip = "*", port = "5678" }

if love.system.getOS() == "Android" then
	Local.services = {}


	function love.handlers.serviceregistered(a,b,c,d)
		SDL.log("ServiceRegistered: " .. a)
	end

	function love.handlers.discoverystarted(a,b,c,d)
		SDL.log("Discovery Started: " .. a)
	end

	function love.handlers.serviceresolved(a,b,c,d)
		SDL.log("Service Discovered: " .. a .. b .. c .. d)
		a = a:gsub("\\032", " ")
		if a == Local.myservice.name then
			Local.myservice.ip = c
			Local.myservice.port = d
		else
			Local.services[a] = { ip = c, port = d}
		end
	end

	function love.handlers.servicelost(a,b,c,d)
		SDL.log("Service Lost: " .. a .. b)
		Local.services[a] = nil
	end
end

function Local:init()
	Local.host = enet.host_create(Local.myservice.ip .. ":" .. Local.myservice.port)

	if love.system.getOS() == "Android" then
		love.android.registerService(Local.myservice.name, "_http._tcp.", Local.myservice.port)
	    love.android.discoverServices("_http._tcp.")
	end
end

function Local:update(dt)

	if not connect then
		Gui.group.push{grow = "right"}

		Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}}
			if love.system.getOS() == "Android" then
				for name, host in pairs(Local.services) do
					if Gui.Button{text = name .. " " .. host.ip .. ":" .. host.port, size = {Utils.percentCoordinates(60, 10)}} then
						Local.server_host.text = host.ip.. ":" .. host.port
						connect = true
						return
					end
				end
			end

			Gui.Label{text = "Host", size = {Utils.percentCoordinates(10, 10)}}
			Gui.Input{info = Local.server_host, size = {Utils.percentCoordinates(50, 10)}}
			if Gui.Button{text = "Connect", size = {Utils.percentCoordinates(60, 10)}} then
				connect = true
			end
		Gui.group.pop{}

		Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}}
			Gui.Label{text = "My infos"}
			Gui.Label{text = "Name: " .. Local.myservice.name}
			Gui.Label{text = "Host: " .. Local.myservice.ip .. ":" .. Local.myservice.port}
		Gui.group.pop{}

		Gui.group.pop{}
	elseif not connected then
		Local.host:connect(Local.server_host.text)
		connected = true
	else
		local event = Local.host:service()

		while event ~= nil do
			if event.type == "receive" then
				print("Receive ", event.data, event.peer)
			elseif event.type == "connect" then
				if event.peer:index() > 1 then
					print("Already connected")
				else
					print("Local " .. tostring(event.peer) .. " " .. event.peer:state() .. " " .. tostring(event.peer:connect_id()))
				end
			elseif event.type == "disconnect" then
					print("Disconnect " .. tostring(event.peer))
			end

			event = Local.host:service()
		end
	end
end

function Local:draw()
	Gui.core.draw()
end

function Local:keypressed(key, code)
    Gui.keyboard.pressed(key)
end

-- LÖVE 0.9
function love.textinput(str)
    Gui.keyboard.textinput(str)
end

return Local