Discord integration for Gamecraft
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.7KB

  1. using System;
  2. using GamecraftModdingAPI.Tasks;
  3. using GamecraftModdingAPI.Engines;
  4. using Svelto.ECS;
  5. namespace GamecraftRPC.Engines
  6. {
  7. class PlayerCountEngine : IApiEngine
  8. {
  9. public string Name => "GamecraftRPCPlayerCountGameEngine";
  10. public EntitiesDB entitiesDB { set; private get; }
  11. public bool isRemovable => false;
  12. private bool Ok = false;
  13. public void Dispose()
  14. {
  15. Ok = false;
  16. PresenceUtility.PlayerCount = 0;
  17. }
  18. public void Ready()
  19. {
  20. Ok = true;
  21. Scheduler.Schedule(new Repeatable(updatePlayerCount, () => { return Ok; }, delay: 5f));
  22. }
  23. private void updatePlayerCount()
  24. {
  25. uint count = GamecraftModdingAPI.Player.Count();
  26. if (count != PresenceUtility.PlayerCount && count > 0)
  27. {
  28. if (PresenceUtility.Lobby.HasValue && Plugin.DiscordRPC != null)
  29. {
  30. Discord.LobbyManager lm = Plugin.DiscordRPC.GetLobbyManager();
  31. Discord.LobbyTransaction lt = lm.GetLobbyUpdateTransaction(PresenceUtility.Lobby.Value.Id);
  32. lt.SetCapacity(count * 2);
  33. lm.UpdateLobby(PresenceUtility.Lobby.Value.Id, lt, CallbackUtility.NobodyCares);
  34. }
  35. PresenceUtility.PlayerCount = count;
  36. PresenceUtility.Activity.Party.Size.CurrentSize = (int)count;
  37. PresenceUtility.Activity.Party.Size.MaxSize = (int)count * 2;
  38. Plugin.SetDiscordActivity(debug: "PlayerCountEngine-updatePlayerCount");
  39. #if DEBUG
  40. GamecraftModdingAPI.Utility.Logging.MetaLog($"Updated Player Count: {count}");
  41. #endif
  42. }
  43. }
  44. }
  45. }