ProtoHelper.cs 4.5 KB

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