From 3ec6713b683acd1ba52e9bd1cb6ac2b4e5fe7cb9 Mon Sep 17 00:00:00 2001 From: WorstAquaPlayer Date: Sat, 27 Feb 2021 18:59:18 -0300 Subject: [PATCH] Now shows P2 icon and time left when song starts --- source/Discord.hx | 16 +++++++++++++--- source/PlayState.hx | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/source/Discord.hx b/source/Discord.hx index 668ea85e6..39875a37d 100644 --- a/source/Discord.hx +++ b/source/Discord.hx @@ -57,16 +57,26 @@ class DiscordClient trace("Discord Client initialized"); } - public static function changePresence(details:String, state:Null, ?smallImageKey : String, ?startTimestamp: Int, ?endTimestamp: Int) + public static function changePresence(details:String, state:Null, ?smallImageKey : String, ?hasStartTimestamp : Bool, ?endTimestamp: Float) { + var startTimestamp:Float = if(hasStartTimestamp) Date.now().getTime() else 0; + + if (endTimestamp > 0) + { + endTimestamp = startTimestamp + endTimestamp; + } + DiscordRpc.presence({ details: details, state: state, largeImageKey: 'icon', largeImageText: "Friday Night Funkin'", smallImageKey : smallImageKey, - startTimestamp : startTimestamp, - endTimestamp : endTimestamp + // Obtained times are in milliseconds so they are divided so Discord can use it + startTimestamp : Std.int(startTimestamp / 1000), + endTimestamp : Std.int(endTimestamp / 1000) }); + + trace('Discord RPC Updated. Argument: $details, $state, $smallImageKey, $hasStartTimestamp, $endTimestamp'); } } diff --git a/source/PlayState.hx b/source/PlayState.hx index 0356dd58a..f191a1798 100644 --- a/source/PlayState.hx +++ b/source/PlayState.hx @@ -125,6 +125,13 @@ class PlayState extends MusicBeatState var inCutscene:Bool = false; + #if !html + // Discord RPC variables + var storyDifficultyText:String = ""; + var iconRPC:String = ""; + var songLength:Float = 0; + #end + override public function create() { if (FlxG.sound.music != null) @@ -178,7 +185,6 @@ class PlayState extends MusicBeatState #if !html // Making difficulty text for Discord Rich Presence. - var storyDifficultyText = ""; switch (storyDifficulty) { case 0: @@ -188,14 +194,28 @@ class PlayState extends MusicBeatState case 2: storyDifficultyText = "Hard"; } + + iconRPC = SONG.player2; + + // To avoid having duplicate images in Discord assets + switch (iconRPC) + { + case 'senpai-angry': + iconRPC = 'senpai'; + case 'monster-christmas': + iconRPC = 'monster'; + case 'mom-car': + iconRPC = 'mom'; + } + // Updating Discord Rich Presence. if (isStoryMode) { - DiscordClient.changePresence("Story Mode: Week " + storyWeek, SONG.song + " (" + storyDifficultyText + ")"); + DiscordClient.changePresence("Story Mode: Week " + storyWeek, SONG.song + " (" + storyDifficultyText + ")", iconRPC); } else { - DiscordClient.changePresence("Freeplay", SONG.song + " (" + storyDifficultyText + ")"); + DiscordClient.changePresence("Freeplay", SONG.song + " (" + storyDifficultyText + ")", iconRPC); } #end @@ -992,6 +1012,21 @@ class PlayState extends MusicBeatState FlxG.sound.playMusic(Paths.inst(PlayState.SONG.song), 1, false); FlxG.sound.music.onComplete = endSong; vocals.play(); + + #if !html + // Song duration in a float, useful for the time left feature + songLength = FlxG.sound.music.length; + + // Updating Discord Rich Presence (with Time Left) + if (isStoryMode) + { + DiscordClient.changePresence("Story Mode: Week " + storyWeek, SONG.song + " (" + storyDifficultyText + ")", iconRPC, true, songLength); + } + else + { + DiscordClient.changePresence("Freeplay", SONG.song + " (" + storyDifficultyText + ")", iconRPC, true, songLength); + } + #end } var debugNum:Int = 0;