Unofficial CardLife revival project, pronounced like "celery"
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.

63 lines
1.9KB

  1. using System;
  2. using System.Collections.Generic;
  3. using GameNetworkLayer.Shared;
  4. namespace CLre_server.API.Synergy
  5. {
  6. public static class Message
  7. {
  8. internal const NetworkDispatcherCode CLre_MESSAGE_NETCODE = (NetworkDispatcherCode) 219;
  9. private static readonly Dictionary<uint, Action<ReceiveMessageArgs>> handlers =
  10. new Dictionary<uint, Action<ReceiveMessageArgs>>();
  11. private static readonly ServerMessagingEngine msgEngine = new ServerMessagingEngine();
  12. private static readonly List<int> clrePlayers = new List<int>();
  13. public static void SendToAllCLreClients(ref SerializedCLreMessage message)
  14. {
  15. foreach (var playerId in clrePlayers)
  16. {
  17. SendCLreMessage(playerId, ref message);
  18. }
  19. }
  20. public static void SendCLreMessage(int playerId, ref SerializedCLreMessage message)
  21. {
  22. msgEngine.EnqueueMessage(playerId, ref message);
  23. }
  24. public static void RegisterListener(uint id, Action<ReceiveMessageArgs> handler)
  25. {
  26. if (handlers.TryGetValue(id, out Action<ReceiveMessageArgs> existing))
  27. {
  28. existing += handler;
  29. handlers[id] = existing;
  30. }
  31. else
  32. {
  33. handlers[id] = handler;
  34. }
  35. }
  36. internal static void HandleMessageReceive(int playerId, ref SerializedCLreMessage msg)
  37. {
  38. ReceiveMessageArgs payload = new ReceiveMessageArgs
  39. {
  40. Message = msg,
  41. PlayerId = playerId,
  42. };
  43. if (handlers.TryGetValue(msg.Id, out Action<ReceiveMessageArgs> h))
  44. {
  45. h(payload);
  46. }
  47. }
  48. }
  49. public struct ReceiveMessageArgs
  50. {
  51. public SerializedCLreMessage Message;
  52. public int PlayerId;
  53. }
  54. }