ProtoHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. HGHHComponent hghhComponent = room.GetComponent<HGHHComponent>();
  41. if (hghhComponent != null)
  42. {
  43. info.State = hghhComponent.State;
  44. // info.Time = hghhComponent.Time;
  45. info.CurRound = hghhComponent.CurrentRound;
  46. info.Rand1 = hghhComponent.Rand[0];
  47. info.Rand2 = hghhComponent.Rand[1];
  48. info.ZhuangPos = hghhComponent.ZhuangPos;
  49. info.CardNum = hghhComponent.CardList.Count;
  50. info.OpId = opPlayer?.Id ?? -1;
  51. info.OpPos = opPlayer?.Pos ?? -1;
  52. info.CurDisCard = hghhComponent.DisCard;
  53. }
  54. // 本人信息
  55. info.MyInfo = PlayerInfoToProto(room, player, true);
  56. // 其他玩家信息
  57. info.OtherInfo = new List<PlayerInfo>();
  58. foreach (Player p in room.GetAllPlayers().Values.Where(p => p != null && p.Id != player.Id))
  59. {
  60. info.OtherInfo.Add(PlayerInfoToProto(room, p, false));
  61. }
  62. return info;
  63. }
  64. /// <summary>
  65. /// 房间玩家信息转proto
  66. /// </summary>
  67. /// <param name="room">玩家</param>
  68. /// <param name="player">玩家</param>
  69. /// <param name="flag">是否本人</param>
  70. /// <returns></returns>
  71. public static PlayerInfo PlayerInfoToProto(Room room, Player player, bool flag)
  72. {
  73. PlayerInfo info = new ();
  74. info.id = player.Id;
  75. info.name = player.Name;
  76. info.sex = player.Sex;
  77. info.exp = player.Exp;
  78. info.level = player.Level;
  79. info.vip = player.Vip;
  80. info.diamond = player.Diamond;
  81. info.pos = player.Pos;
  82. info.state = player.State;
  83. info.isAuto = player.IsAuto;
  84. info.cardInfo = new CardInfo();
  85. if (flag)
  86. {
  87. info.cardInfo.RemainCards = new List<int>(player.RemainCards);
  88. HGHHComponent hghhComponent = room.GetComponent<HGHHComponent>();
  89. if (hghhComponent != null)
  90. {
  91. info.cardInfo.DrawCard = hghhComponent.DrawCard;
  92. }
  93. info.cardInfo.Acts = player.Act.ToList();
  94. info.cardInfo.ActInfo = new List<ActInfo>();
  95. if (player.ActInfo is { Count: > 0 })
  96. {
  97. foreach (Struct.Kezi kezi in player.ActInfo)
  98. {
  99. info.cardInfo.ActInfo.Add(new ActInfo(){Card = kezi.Card, Type = kezi.Type, PlayerId = kezi.PlayerId});
  100. }
  101. }
  102. }
  103. info.cardInfo.RemainCardsNum = player.RemainCards.Length;
  104. info.cardInfo.DisCards = new List<int>(player.DisCards);
  105. info.cardInfo.UsedInfo = new List<ActInfo>();
  106. if (player.KeZi is {Count: > 0})
  107. {
  108. foreach (Struct.Kezi kezi in player.KeZi.Where(kezi => kezi != null))
  109. {
  110. info.cardInfo.UsedInfo.Add(new ActInfo(){Card = kezi.Card, Type = kezi.Type, PlayerId = kezi.PlayerId});
  111. }
  112. }
  113. return info;
  114. }
  115. }
  116. }