diff options
| author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2026-06-06 01:57:58 +0200 |
|---|---|---|
| committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2026-06-06 02:01:11 +0200 |
| commit | ec10eebd1d61f43c3d4f9c479d2f7041b352a777 (patch) | |
| tree | 480136f51a14885ae04de8cddb587d7038e94c18 | |
| parent | 341af788ffb60b1066f7735c10a2ef8480ec0aa9 (diff) | |
| download | r6p-ec10eebd1d61f43c3d4f9c479d2f7041b352a777.tar.xz | |
implement create save & load save buttons
| -rw-r--r-- | client.lua | 2 | ||||
| -rw-r--r-- | main.lua | 157 | ||||
| -rw-r--r-- | main_menu.lua | 4 | ||||
| -rw-r--r-- | server.lua | 4 | ||||
| -rwxr-xr-x | standalone_server.lua | 2 |
5 files changed, 143 insertions, 26 deletions
@@ -69,7 +69,7 @@ local function handle_server(clt, pkt) end function client.update(clt) - local event = clt.host:service(20) + local event = clt.host:service() while event do if event.type == "receive" then local pkt = util.json_dec(event.data) @@ -32,24 +32,52 @@ local function show(dg, ...) end end -local function connect(invite) +local function quit_game(msg) + if clt then + client.close(clt) + clt = nil + last_status = nil + end + if srv then + server.close(srv) + srv = nil + end + if msg then + show(dialog.error, msg) + else + show(dialog.main_menu, msg) + end +end + +local function save_location(save) + return love.filesystem.getSaveDirectory().."/saves/"..save..".json" +end + +local function start_client(invite) local c, err = client.join(invite) if err then - show(dialog.error, "Invalid invite") + quit_game("Invalid invite") else clt = c end end -local function disconnect(msg) - client.close(clt) - clt = nil - last_status = nil - if msg then - show(dialog.error, msg) +local function start_server(save) + local s, err = server.create(save_location(save)) + if err then + quit_game(err) -- TODO: better message else - show(dialog.main_menu, msg) + srv = s + end +end + +local function create_save(save) + if love.filesystem.getInfo("saves/"..save..".json") then + show(dialog.error, "Savefile with this name already exists", dialog.create_save) + return end + love.filesystem.createDirectory("saves") + start_server(save) end local function select_player(name, create) @@ -57,20 +85,28 @@ local function select_player(name, create) show(dialog.loading, "Joining Lobby...") end +local function show_create_save() + show(dialog.create_save) +end + +local function show_load_save() + show(dialog.load_save) +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?)") + quit_game("Failed to connect to match server (match server down?)") elseif status == "fail_match" then - disconnect("Game not found (invalid invite?)") + quit_game("Game not found (invalid invite?)") elseif status == "timeout_server" then - disconnect("Failed to connect to server (NAT punch failure?)") + quit_game("Failed to connect to server (NAT punch failure?)") elseif status == "fail_server" then - disconnect("Incorrect secret (invalid invite?)") + quit_game("Incorrect secret (invalid invite?)") elseif status == "disco" then - disconnect("Lost connection to server (Server shut down?)") + quit_game("Lost connection to server (Server shut down?)") elseif status ~= last_status then if status == "active" then show(dialog.loading, "Waiting for info...") @@ -84,9 +120,9 @@ local function update_client() if not clt then return end if clt.player_error then - show(dialog.error, clt.player_error, dialog.select_player) + show(dialog.error, clt.player_error, dialog.select_player) -- TODO: better message clt.player_error = nil - elseif clt.player and (not in_lobby or (in_lobby and last_info ~= clt.info)) then + elseif clt.player and (dialog.active ~= dialog.lobby or last_info ~= clt.info) then show(dialog.lobby) elseif not clt.player and last_info ~= clt.info and (not last_info or dialog.active == dialog.select_player) then show(dialog.select_player) @@ -96,6 +132,16 @@ local function update_client() last_status = status end +local function update_server() + server.update(srv, 0) + local status, invite = server.match_status(srv) + if status == "fail" then + quit_game("Failed to register match (match server down?)") + elseif status == "active" and not clt then + start_client(invite) + end +end + local function create_dialog_box(title, elem, width) return ui.new(ui.center_y(ui.stack_x( ui.flex(1), @@ -116,7 +162,7 @@ end local function create_loading() return create_info_box("Loading...", "Cancel", - function() disconnect() end, + function() quit_game() end, function(self, msg) self:set_title(msg) end @@ -210,14 +256,31 @@ local function create_create_player() end local function create_lobby() + local invite = {} local player_list = {} - local dg = create_dialog_box("Lobby", ui.stack_y( + local dg = create_dialog_box("Lobby", ui.pad_y(10, + invite, player_list, ui.center_x(ui.button("Disconnect", function() - disconnect() + quit_game() end)) ), 3) function dg.show(self) + if srv then + local copied = {} + invite[1] = ui.pad_x(10, + ui.center_y(ui.text("Invite: " .. srv.invite, 25, color(0x000000))), + ui.button("Copy", function() + love.system.setClipboardText(srv.invite) + local ts = love.timer.getTime() + copied[1] = ui.text("Copied", 25, function() + local col = color(0x000000) + col[4] = math.max(0, ts+1-love.timer.getTime()) + return col + end) + end), + ui.center_y(copied)) + end local players = ui.stack_y() for _, p in ipairs(clt.info.players) do table.insert(players, create_player_indicator(p, 20)) @@ -227,16 +290,64 @@ local function create_lobby() return dg end +local function back_button() + return ui.center_x(ui.button("Back", function() + show(dialog.main_menu) + end)) +end + +local function create_load_save() + local contents = {} + local dg = create_dialog_box("Load Save", ui.pad_y(15, contents, back_button())) + function dg.show(self) + local stack = ui.stack_y() + local ext = ".json" + local items = love.filesystem.getDirectoryItems("saves") + for _, item in ipairs(items) do + if item:sub(-#ext) == ext then + local name = item:sub(1, -#ext-1) + table.insert(stack, ui.button(name, function() + start_server(name) + end)) + end + end + + table.insert(stack, ui.button("+", function() + show_create_save() + end)) + + contents[1] = stack + end + return dg +end + +local function create_create_save() + local input_name = ui.input("Save name") + return create_dialog_box("Create Save", ui.pad_y(15, + ui.pad_x(10, + ui.flex(3, input_name), + ui.flex(1, ui.button("Create", function() + if input_name.input_value ~= "" then + create_save(input_name.input_value) + end + end)) + ), + back_button() + )) +end + function love.load() love.graphics.setBackgroundColor(color(0xffffff)) love.window.setTitle("RAINBOW SIX: PANOPTICON") - dialog.main_menu = main_menu.create({ join_game = connect }) + dialog.main_menu = main_menu.create({ join_game = start_client, load_save = show_load_save, create_save = show_create_save }) dialog.loading = create_loading() dialog.error = create_error() dialog.create_player = create_create_player() dialog.select_player = create_select_player() dialog.lobby = create_lobby() + dialog.load_save = create_load_save() + dialog.create_save = create_create_save() show(dialog.main_menu) end @@ -269,6 +380,9 @@ function love.update() if clt then update_client() end + if srv then + update_server() + end end function love.draw() @@ -283,4 +397,7 @@ function love.quit() if clt then client.close(clt) end + if srv then + server.close(srv) + end end diff --git a/main_menu.lua b/main_menu.lua index 73ba7b1..e84c673 100644 --- a/main_menu.lua +++ b/main_menu.lua @@ -12,8 +12,8 @@ end local function mm_create(actions) local host_game = menubox("Host Game", ui.pad_x(10, - ui.flex(1, ui.button("Create Save")), - ui.flex(1, ui.button("Load Save")) + ui.flex(1, ui.button("Create Save", actions.create_save)), + ui.flex(1, ui.button("Load Save", actions.load_save)) )) local invite_code = ui.input("Invite Code") @@ -196,8 +196,8 @@ local function handle_client(srv, peer, pkt) end end -function server.update(srv) - local event = srv.host:service(20) +function server.update(srv, wait) + local event = srv.host:service(wait) while event do if event.type == "receive" then local pkt = util.json_dec(event.data) diff --git a/standalone_server.lua b/standalone_server.lua index 9221984..f399ad1 100755 --- a/standalone_server.lua +++ b/standalone_server.lua @@ -16,7 +16,7 @@ end local function server_loop() local started = false while true do - server.update(srv) + server.update(srv, 100) local status, invite = server.match_status(srv) if status == "fail" then |
