using System; using GamecraftModdingAPI.Tasks; using GamecraftModdingAPI.Engines; using Svelto.ECS; namespace GamecraftRPC.Engines { class PlayerCountEngine : IApiEngine { public string Name => "GamecraftRPCPlayerCountGameEngine"; public EntitiesDB entitiesDB { set; private get; } public bool isRemovable => false; private bool Ok = false; public void Dispose() { Ok = false; PresenceUtility.PlayerCount = 0; } public void Ready() { Ok = true; Scheduler.Schedule(new Repeatable(updatePlayerCount, () => { return Ok; }, delay: 5f)); } private void updatePlayerCount() { uint count = GamecraftModdingAPI.Player.Count(); if (count != PresenceUtility.PlayerCount && count > 0) { if (PresenceUtility.Lobby.HasValue && Plugin.DiscordRPC != null) { Discord.LobbyManager lm = Plugin.DiscordRPC.GetLobbyManager(); Discord.LobbyTransaction lt = lm.GetLobbyUpdateTransaction(PresenceUtility.Lobby.Value.Id); lt.SetCapacity(count * 2); lm.UpdateLobby(PresenceUtility.Lobby.Value.Id, lt, CallbackUtility.NobodyCares); } PresenceUtility.PlayerCount = count; PresenceUtility.Activity.Party.Size.CurrentSize = (int)count; PresenceUtility.Activity.Party.Size.MaxSize = (int)count * 2; Plugin.SetDiscordActivity(debug: "PlayerCountEngine-updatePlayerCount"); #if DEBUG GamecraftModdingAPI.Utility.Logging.MetaLog($"Updated Player Count: {count}"); #endif } } } }