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.

GamePresenceHandler.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GamecraftModdingAPI.Events;
  7. using Svelto.ECS;
  8. using RobocraftX.Common;
  9. using Discord;
  10. namespace GamecraftRPC.Events
  11. {
  12. class GamePresenceHandler : SimpleEventHandlerEngine
  13. {
  14. public static readonly string HandlerName = "GamecraftRPCGamePresenceEventHandler";
  15. //private Discord.Discord discordRPC = null;
  16. public static void OnAdd(Discord.Discord discordRPC)
  17. {
  18. PresenceUtility.LastGameStart = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
  19. LobbyTransaction ltxn = discordRPC.GetLobbyManager().GetLobbyCreateTransaction();
  20. UserManager um = discordRPC.GetUserManager();
  21. um.OnCurrentUserUpdate += () =>
  22. {
  23. ltxn.SetOwner(um.GetCurrentUser().Id);
  24. ltxn.SetCapacity((uint)PresenceUtility.LobbySize);
  25. ltxn.SetType(LobbyType.Private);
  26. ltxn.SetMetadata("ownerSteamId", Steamworks.SteamClient.SteamId.AccountId.ToString());
  27. discordRPC.GetLobbyManager().CreateLobby(ltxn,
  28. (Result result, ref Lobby lobby) =>
  29. {
  30. GamecraftModdingAPI.Utility.Logging.LogDebug($"Lobby status: {result}");
  31. PresenceUtility.LobbyId = lobby.Id;
  32. PresenceUtility.GameSecret = lobby.Secret;
  33. });
  34. };
  35. Plugin.SetDiscordActivity(discordRPC, state: "In-Game", details: $"Editing {GameMode.SaveGameDetails.Name}", start: PresenceUtility.LastGameStart, partyId: PresenceUtility.PartyId.ToString(), partyCurrentSize: 1, partyMaxSize: PresenceUtility.LobbySize, debug: "GamePresence-OnAdd");
  36. }
  37. public GamePresenceHandler(Discord.Discord _discordRPC) : base((db) => { OnAdd(_discordRPC); }, (db) => { }, EventType.GameSwitchedTo, HandlerName)
  38. {
  39. //this.discordRPC = _discordRPC;
  40. }
  41. }
  42. }