From 5d32b6a3e7d2bda8efbfffb4f3df1720d6ba80f4 Mon Sep 17 00:00:00 2001 From: Oshisaure Date: Sun, 11 Oct 2020 02:17:48 +0100 Subject: [PATCH] Discord RPC cleanup - Loading Discord RPC is now handled by `load/rpc.lua` - Removed `presence` global, call `DiscordRPC:update()` directly with what needs updating - Game doesn't crash anymore if the Discord RPC fails to load - Added RPC variables in the gamemode superclass to let each gamemode handle its special case --- load/rpc.lua | 56 +++++++++++++++++++++++++++++++++++++ main.lua | 41 +-------------------------- scene/game.lua | 11 +++----- scene/game_config.lua | 9 +++--- scene/input_config.lua | 7 +++-- scene/mode_select.lua | 7 +++-- scene/title.lua | 22 ++++++++++++--- tetris/modes/demon_mode.lua | 6 ++-- tetris/modes/gamemode.lua | 1 + 9 files changed, 96 insertions(+), 64 deletions(-) create mode 100644 load/rpc.lua diff --git a/load/rpc.lua b/load/rpc.lua new file mode 100644 index 0000000..ba4ab2f --- /dev/null +++ b/load/rpc.lua @@ -0,0 +1,56 @@ +print("Loading discord RPC...") +DiscordRPC = { + loaded = false +} +local success, RPC = pcall(require, "libs.discordRPC") +if success then + DiscordRPC.loaded = true + DiscordRPC.appId = "599778517789573120" + + function RPC.ready(userId, username, discriminator, avatar) + print(string.format("Discord: ready (%s, %s, %s, %s)", userId, username, discriminator, avatar)) + end + + function RPC.disconnected(errorCode, message) + print(string.format("Discord: disconnected (%d: %s)", errorCode, message)) + end + + function RPC.errored(errorCode, message) + print(string.format("Discord: error (%d: %s)", errorCode, message)) + end + + function RPC.joinGame(joinSecret) + print(string.format("Discord: join (%s)", joinSecret)) + end + + function RPC.spectateGame(spectateSecret) + print(string.format("Discord: spectate (%s)", spectateSecret)) + end + + function RPC.joinRequest(userId, username, discriminator, avatar) + print(string.format("Discord: join request (%s, %s, %s, %s)", userId, username, discriminator, avatar)) + RPC.respond(userId, "yes") + end + + RPC.initialize(DiscordRPC.appId, true) + local now = os.time(os.date("*t")) + RPC.updatePresence({ + startTimestamp = now, + details = "Loading game...", + state = "", + largeImageKey = "icon2", + largeImageText = "Original game by Joe Zeng", + smallImageKey = "", + smallImageText = "" + }) + + DiscordRPC.RPC = RPC + print("DiscordRPC successfully loaded.") +else + print("DiscordRPC failed to load!") + print(RPC) +end + +function DiscordRPC:update(newstuff) + if self.loaded then self.RPC.updatePresence(newstuff) end +end \ No newline at end of file diff --git a/main.lua b/main.lua index aca1972..8879507 100644 --- a/main.lua +++ b/main.lua @@ -1,46 +1,7 @@ function love.load() - discordRPC = require("libs.discordRPC") - discordRPC.appId = "599778517789573120" - - function discordRPC.ready(userId, username, discriminator, avatar) - print(string.format("Discord: ready (%s, %s, %s, %s)", userId, username, discriminator, avatar)) - end - - function discordRPC.disconnected(errorCode, message) - print(string.format("Discord: disconnected (%d: %s)", errorCode, message)) - end - - function discordRPC.errored(errorCode, message) - print(string.format("Discord: error (%d: %s)", errorCode, message)) - end - - function discordRPC.joinGame(joinSecret) - print(string.format("Discord: join (%s)", joinSecret)) - end - - function discordRPC.spectateGame(spectateSecret) - print(string.format("Discord: spectate (%s)", spectateSecret)) - end - - function discordRPC.joinRequest(userId, username, discriminator, avatar) - print(string.format("Discord: join request (%s, %s, %s, %s)", userId, username, discriminator, avatar)) - discordRPC.respond(userId, "yes") - end - - discordRPC.initialize(discordRPC.appId, true) - local now = os.time(os.date("*t")) - presence = { - startTimestamp = now, - details = "Loading game...", - state = "", - largeImageKey = "icon2", - largeImageText = "Original game by Joe Zeng", - smallImageKey = "", - smallImageText = "" - } - math.randomseed(os.time()) highscores = {} + require "load.rpc" require "load.graphics" require "load.fonts" require "load.sounds" diff --git a/scene/game.lua b/scene/game.lua index e4e4fce..8ecff33 100644 --- a/scene/game.lua +++ b/scene/game.lua @@ -5,13 +5,10 @@ function GameScene:new(game_mode, ruleset) self.game = game_mode() self.ruleset = ruleset() self.game:initialize(self.ruleset) - if game_mode.name == "Demon Mode" and math.random(1, 7) == 7 then - presence.details = "Suffering" - else - presence.details = "In game" - end - presence.state = game_mode.name - discordRPC.updatePresence(presence) + DiscordRPC:update({ + details = self.game.rpc_details, + state = self.game.name, + }) end function GameScene:update() diff --git a/scene/game_config.lua b/scene/game_config.lua index d71b38c..1fe67e8 100644 --- a/scene/game_config.lua +++ b/scene/game_config.lua @@ -16,10 +16,11 @@ function ConfigScene:new() -- load current config self.config = config.input self.highlight = 1 - - presence.details = "In menus" - presence.state = "Changing game config" - discordRPC.updatePresence(presence) + + DiscordRPC:update({ + details = "In menus", + state = "Changing game settings", + }) end function ConfigScene:update() diff --git a/scene/input_config.lua b/scene/input_config.lua index 9f6a9da..6170d9f 100644 --- a/scene/input_config.lua +++ b/scene/input_config.lua @@ -23,9 +23,10 @@ function ConfigScene:new() self.config = config.input self.input_state = 1 - presence.details = "In menus" - presence.state = "Changing input config" - discordRPC.updatePresence(presence) + DiscordRPC:update({ + details = "In menus", + state = "Changing input config", + }) end function ConfigScene:update() diff --git a/scene/mode_select.lua b/scene/mode_select.lua index 0a92dd3..a1aa5a7 100755 --- a/scene/mode_select.lua +++ b/scene/mode_select.lua @@ -47,9 +47,10 @@ function ModeSelectScene:new() ruleset = current_ruleset, select = "mode", } - presence.details = "In menus" - presence.state = "Choosing a mode" - discordRPC.updatePresence(presence) + DiscordRPC:update({ + details = "In menus", + state = "Choosing a mode", + }) end function ModeSelectScene:update() diff --git a/scene/title.lua b/scene/title.lua index f8a314e..adcb688 100644 --- a/scene/title.lua +++ b/scene/title.lua @@ -7,13 +7,27 @@ local main_menu_screens = { ExitScene, } -local mainmenuidle = {"Idle", "Twiddling their thumbs", "Admiring the main menu's BG", "Waiting for spring to come"} +local mainmenuidle = { + "Idle", + "On title screen", + "On main menu screen", + "Twiddling their thumbs", + "Admiring the main menu's BG", + "Waiting for spring to come", + "Actually not playing", + "Contemplating collecting stars", + "Preparing to put the block!!", + "Having a nap", + "In menus", + "Bottom text", +} function TitleScene:new() self.main_menu_state = 1 - presence.details = "In menus" - presence.state = mainmenuidle[math.random(#mainmenuidle)] - discordRPC.updatePresence(presence) + DiscordRPC:update({ + details = "In menus", + state = mainmenuidle[math.random(#mainmenuidle)], + }) end function TitleScene:update() diff --git a/tetris/modes/demon_mode.lua b/tetris/modes/demon_mode.lua index d2d888d..1833a8b 100644 --- a/tetris/modes/demon_mode.lua +++ b/tetris/modes/demon_mode.lua @@ -11,9 +11,6 @@ DemonModeGame.name = "Demon Mode" DemonModeGame.hash = "DemonMode" DemonModeGame.tagline = "Can you handle the ludicrous speed past level 20?" - - - function DemonModeGame:new() DemonModeGame.super:new() self.roll_frames = 0 @@ -29,6 +26,9 @@ function DemonModeGame:new() self.enable_hold = true self.lock_drop = true self.next_queue_length = 3 + if math.random() < 1/6.66 then + self.rpc_details = "Suffering" + end end function DemonModeGame:getARE() diff --git a/tetris/modes/gamemode.lua b/tetris/modes/gamemode.lua index c2ec87d..5ceeb62 100644 --- a/tetris/modes/gamemode.lua +++ b/tetris/modes/gamemode.lua @@ -40,6 +40,7 @@ function GameMode:new() self.draw_section_times = false self.draw_secondary_section_times = false self.big_mode = false + self.rpc_details = "In game" -- variables related to configurable parameters self.drop_locked = false self.hard_drop_locked = false