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.

67 lines
2.1KB

  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.Utility;
  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. private Discord.Discord discordRPC = null;
  18. private bool Ok = false;
  19. public void Dispose()
  20. {
  21. Ok = false;
  22. PresenceUtility.PlayerCount = 0;
  23. PresenceUtility.Activity.Party.Size.CurrentSize = 0;
  24. PresenceUtility.Activity.Party.Size.MaxSize = 0;
  25. PresenceUtility.Activity.Party.Id = "";
  26. }
  27. public void Ready()
  28. {
  29. Ok = true;
  30. Scheduler.Schedule(new Repeatable(updatePlayerCount, () => { return Ok; }, delay: 2f));
  31. PresenceUtility.Activity.Party.Size.CurrentSize = 1;
  32. PresenceUtility.Activity.Party.Size.MaxSize = PresenceUtility.LobbySize;
  33. PresenceUtility.Activity.Party.Id = "";//PresenceUtility.PartyId.ToString();
  34. }
  35. private void updatePlayerCount()
  36. {
  37. if (entitiesDB == null) return;
  38. uint count = 0;
  39. for (uint i = 0; i < CharacterExclusiveGroups.AllCharacters.Length; i++)
  40. {
  41. count += entitiesDB.Count<RigidBodyEntityStruct>(CharacterExclusiveGroups.AllCharacters[i]);
  42. }
  43. GamecraftModdingAPI.Utility.Logging.MetaLog($"Player Count: {count}");
  44. if ((int)count != PresenceUtility.PlayerCount && count > 0)
  45. {
  46. PresenceUtility.PlayerCount = (int)count;
  47. PresenceUtility.Activity.Party.Size.CurrentSize = (int)count;
  48. Plugin.SetDiscordActivity(discordRPC, debug: "PlayerCountEngine-updatePlayerCount");
  49. }
  50. }
  51. public PlayerCountEngine(Discord.Discord discordRPC)
  52. {
  53. this.discordRPC = discordRPC;
  54. }
  55. }
  56. }