summaryrefslogtreecommitdiff
path: root/main.lua
blob: 238b20b95207b37708dc2e63539b217dc8a446bd (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function dump(x, idt)
    local p = {}
    local idt = idt or 0
    for k, v in pairs(x) do
        local s
        if type(v) == "table" then
            s = dump(v, idt+1)
        elseif type(v) == "string" then
            s = ("\"%s\""):format(v)
        else
            s = tostring(v)
        end
        table.insert(p, ("%s%s = %s"):format((" "):rep(4*(idt+1)), k, s))
    end
    return ("{\n%s\n%s}"):format(table.concat(p, ", \n"), (" "):rep(4*idt))
end

local client = require("client")
local server = require("server")
local main_menu = require("main_menu")
local ui = require("ui")
local color = ui.color

local mm
local clt
local active_ui
local loading_text, loading_ui
local error_text, error_ui
local connected = false
local last_status

local function show_error(msg)
    error_text.text:set(msg)
    active_ui = error_ui
end

local function join_game(invite)
    local c, err = client.join(invite)
    if err then
        show_error("Invalid invite")
    else
        clt = c
        active_ui = loading_ui
    end
end

local function disconnect(msg)
    client.close(clt)
    clt = nil
    connected = false
    if msg then
        show_error(msg)
    else
        active_ui = mm
    end
end

local function loading_status(status)
    loading_text.text:set(status)
end

local function update_client()
    client.update(clt)

    local status = client.status(clt)
    if status == "timeout_match" then
        disconnect("Failed to connect to match server (match server down?)")
    elseif status == "fail_match" then
        disconnect("Game not found (invalid invite?)")
    elseif status == "timeout_server" then
        disconnect("Failed to connect to server (NAT punch failure?)")
    elseif status == "fail_server" then
        disconnect("Incorrect secret (invalid invite?)")
    elseif status == "disco" then
        disconnect("Lost connection to server (Server shut down?)")
    elseif status ~= last_status then
        if status == "active" then
            loading_status("Connected to server!")
            connected = true
        elseif status == "wait_match" then
            loading_status("Waiting for match server...")
        elseif status == "wait_server" then
            loading_status("Waiting for server...")
        end
    end

    last_status = status
end

function love.load()
    love.graphics.setBackgroundColor(color(0xffffff))
    love.window.setTitle("RAINBOW SIX: PANOPTICON")

    mm = main_menu.create({
        join_game = join_game,
    })

    local loading_box = main_menu.box(
        "Loading...",
        ui.button("Cancel", function()
            disconnect()
        end)
    )
    loading_text = loading_box.box_title
    loading_ui = ui.new(ui.center_x(ui.center_y(loading_box)))

    local error_box = main_menu.box(
        "Error",
        ui.button("Back", function()
            active_ui = mm
        end)
    )
    error_text = error_box.box_title
    error_ui = ui.new(ui.center_x(ui.center_y(error_box)))

    active_ui = mm
end

function love.mousepressed(x, y, button)
    if active_ui then
        ui.mousepressed(active_ui, x, y, button)
    end
end

function love.mousereleased(x, y, button)
    if active_ui then
        ui.mousereleased(active_ui, x, y, button)
    end
end

function love.textinput(text)
    if active_ui then
        ui.textinput(active_ui, text)
    end
end

function love.keypressed(key)
    if active_ui then
        ui.keypressed(active_ui, key)
    end
end

function love.update()
    if clt then
        update_client()
    end
end

function love.draw()
    if active_ui then
        ui.update(active_ui)
        ui.render(active_ui)
    end
end