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.

PlayerCountEngine.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GamecraftModdingAPI.Tasks;
  7. using GamecraftModdingAPI.Engines;
  8. using Svelto.ECS;
  9. using RobocraftX.Character;
  10. using RobocraftX.Physics;
  11. namespace GamecraftRPC.Engines
  12. {
  13. class PlayerCountEngine : IApiEngine
  14. {
  15. public string Name => "GamecraftRPCPlayerCountGameEngine";
  16. public EntitiesDB entitiesDB { set; private get; }
  17. public bool isRemovable => false;
  18. private Discord.Discord discordRPC = null;
  19. private bool Ok = false;
  20. public void Dispose()
  21. {
  22. Ok = false;
  23. PresenceUtility.PlayerCount = 0;
  24. PresenceUtility.Activity.Party.Size.CurrentSize = 0;
  25. PresenceUtility.Activity.Party.Size.MaxSize = 0;
  26. PresenceUtility.Activity.Party.Id = "";
  27. }
  28. public void Ready()
  29. {
  30. Ok = true;
  31. Scheduler.Schedule(new Repeatable(updatePlayerCount, () => { return Ok; }, delay: 5f));
  32. PresenceUtility.Activity.Party.Size.CurrentSize = 1;
  33. PresenceUtility.Activity.Party.Size.MaxSize = PresenceUtility.LobbySize;
  34. PresenceUtility.Activity.Party.Id = "";//PresenceUtility.PartyId.ToString();
  35. }
  36. private void updatePlayerCount()
  37. {
  38. if (entitiesDB == null) return;
  39. uint count = 0;
  40. for (uint i = 0; i < CharacterExclusiveGroups.AllCharacters.Length; i++)
  41. {
  42. count += entitiesDB.Count<RigidBodyEntityStruct>(CharacterExclusiveGroups.AllCharacters[i]);
  43. }
  44. #if DEBUG
  45. GamecraftModdingAPI.Utility.Logging.MetaLog($"Player Count: {count}");
  46. #endif
  47. if ((int)count != PresenceUtility.PlayerCount && count > 0)
  48. {
  49. PresenceUtility.PlayerCount = (int)count;
  50. PresenceUtility.Activity.Party.Size.CurrentSize = (int)count;
  51. Plugin.SetDiscordActivity(discordRPC, debug: "PlayerCountEngine-updatePlayerCount");
  52. }
  53. }
  54. public PlayerCountEngine(Discord.Discord discordRPC)
  55. {
  56. this.discordRPC = discordRPC;
  57. }
  58. }
  59. }