ProtoHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace ET.Server
  4. {
  5. /// <summary>
  6. /// Proto工具类
  7. /// </summary>
  8. public static class ProtoHelper
  9. {
  10. /// <summary>
  11. /// 玩家信息转proto
  12. /// </summary>
  13. /// <param name="player"></param>
  14. /// <returns></returns>
  15. public static PlayerInfo PlayerToProto(Player player)
  16. {
  17. return new PlayerInfo()
  18. {
  19. id = player.Id,
  20. name = player.Name,
  21. sex = player.Sex,
  22. exp = player.Exp,
  23. level = player.Level,
  24. vip = 0
  25. };
  26. }
  27. /// <summary>
  28. /// 房间信息转proto
  29. /// </summary>
  30. /// <param name="room"></param>
  31. /// <param name="player"></param>
  32. /// <param name="opPlayer">当前操作玩家</param>
  33. /// <returns></returns>
  34. public static RoomInfo RoomToProto(Room room, Player player, Player opPlayer)
  35. {
  36. RoomInfo info = new ();
  37. info.RoomId = room.RoomId;
  38. info.Type = room.Type;
  39. info.OwnerId = room.OwnerId;
  40. HGHuangHuangComponent hgHuangHuangComponent = room.GetComponent<HGHuangHuangComponent>();
  41. if (hgHuangHuangComponent != null)
  42. {
  43. info.State = hgHuangHuangComponent.State;
  44. info.Time = hgHuangHuangComponent.Time;
  45. info.Rand1 = hgHuangHuangComponent.Rand[0];
  46. info.Rand2 = hgHuangHuangComponent.Rand[1];
  47. info.CardNum = hgHuangHuangComponent.CardList.Count;
  48. info.OpId = opPlayer?.Id ?? -1;
  49. info.OpPos = opPlayer?.Pos ?? -1;
  50. info.CurDisCard = hgHuangHuangComponent.DisCard;
  51. }
  52. // 本人信息
  53. info.MyInfo = PlayerInfoToProto(room, player, true);
  54. // 其他玩家信息
  55. info.OtherInfo = new List<PlayerInfo>();
  56. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null && p.Id != player.Id))
  57. {
  58. info.OtherInfo.Add(PlayerInfoToProto(room, p, false));
  59. }
  60. return info;
  61. }
  62. /// <summary>
  63. /// 房间玩家信息转proto
  64. /// </summary>
  65. /// <param name="room">玩家</param>
  66. /// <param name="player">玩家</param>
  67. /// <param name="flag">是否本人</param>
  68. /// <returns></returns>
  69. private static PlayerInfo PlayerInfoToProto(Room room, Player player, bool flag)
  70. {
  71. PlayerInfo info = new ();
  72. info.id = player.Id;
  73. info.name = player.Name;
  74. info.sex = player.Sex;
  75. info.exp = player.Exp;
  76. info.level = player.Level;
  77. info.vip = 0;
  78. info.pos = player.Pos;
  79. info.state = player.State;
  80. info.isAuto = player.IsAuto;
  81. info.cardInfo = new CardInfo();
  82. if (flag)
  83. {
  84. info.cardInfo.RemainCards = new List<int>(player.RemainCards);
  85. HGHuangHuangComponent hgHuangHuangComponent = room.GetComponent<HGHuangHuangComponent>();
  86. if (hgHuangHuangComponent != null)
  87. {
  88. info.cardInfo.DrawCard = hgHuangHuangComponent.DrawCard;
  89. }
  90. info.cardInfo.Acts = player.Act.ToList();
  91. info.cardInfo.ActInfo = new List<ActInfo>();
  92. if (player.ActInfo is { Count: > 0 })
  93. {
  94. foreach (Struct.Kezi kezi in player.ActInfo)
  95. {
  96. info.cardInfo.ActInfo.Add(new ActInfo(){Card = kezi.Card, Type = kezi.Type, PlayerId = kezi.PlayerId});
  97. }
  98. }
  99. }
  100. info.cardInfo.RemainCardsNum = player.RemainCards.Length;
  101. info.cardInfo.DisCards = new List<int>(player.DisCards);
  102. info.cardInfo.UsedInfo = new List<ActInfo>();
  103. if (player.KeZi is {Count: > 0})
  104. {
  105. foreach (Struct.Kezi kezi in player.KeZi.Where(kezi => kezi != null))
  106. {
  107. info.cardInfo.UsedInfo.Add(new ActInfo(){Card = kezi.Card, Type = kezi.Type, PlayerId = kezi.PlayerId});
  108. }
  109. }
  110. return info;
  111. }
  112. }
  113. }