FlagRoom.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X.Entities.Data;
  5. public class FlagRoom : BARoom
  6. {
  7. public const int MAX_FLAG_CAPTURE = 3;
  8. private int lastResetFlagTime = -1;
  9. private bool isFirstTimeFlagSet = false;
  10. public FlagRoom(BAServer server) : base(server)
  11. {
  12. // AttempAddStation (0, TeamUtil.Team.Blue.GetHashCode ());
  13. // AttempAddStation (1, TeamUtil.Team.Red.GetHashCode());
  14. }
  15. private void ResetFlags()
  16. {
  17. flagDict.Add (TeamUtil.Team.Blue.GetHashCode(), new BAFlag(TeamUtil.Team.Blue.GetHashCode(), 0, 0));
  18. flagDict.Add (TeamUtil.Team.Red.GetHashCode(), new BAFlag(TeamUtil.Team.Red.GetHashCode(), 0, 0));
  19. }
  20. private void SendFlagAdded()
  21. {
  22. ISFSObject data = new SFSObject ();
  23. data.PutSFSArray ("l", GetFlagDataArray());
  24. server.Send (new Message(Command.AddFlag, data));
  25. }
  26. private ISFSArray GetFlagDataArray()
  27. {
  28. SFSArray arr = new SFSArray();
  29. foreach(KeyValuePair<int, BAFlag> kvp in flagDict)
  30. {
  31. BAFlag flag = kvp.Value;
  32. ISFSObject obj = new SFSObject ();
  33. obj.PutInt ("t", flag.teamId);
  34. obj.PutInt ("x", flag.x);
  35. obj.PutInt ("y", flag.y);
  36. obj.PutInt ("c", flag.craftId);
  37. obj.PutBool ("b", flag.inBase);
  38. arr.AddSFSObject (obj);
  39. }
  40. return arr;
  41. }
  42. public override bool AttempGetFlag (int userId, int craftId)
  43. {
  44. BAUser user = GetUser (userId);
  45. if(user != null)
  46. {
  47. int teamId = TeamUtil.GetOpponentTeam (user.teamId).GetHashCode();
  48. BAFlag flag = null;
  49. flagDict.TryGetValue (teamId, out flag);
  50. if(flag != null && flag.craftId == -1)
  51. {
  52. flag.craftId = craftId;
  53. flag.x = flag.y = 0;
  54. flag.inBase = false;
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. public override bool AttempPutFlag (int userId, int craftId, int flagTeamId, int x, int y)
  61. {
  62. BAUser user = GetUser (userId);
  63. BAFlag flag = null;
  64. flagDict.TryGetValue (flagTeamId, out flag);
  65. if(flag == null)
  66. {
  67. return false;
  68. }
  69. if (flagTeamId == user.teamId) {
  70. flag.craftId = -1;
  71. flag.x = flag.y = 0;
  72. flag.inBase = true;
  73. return true;
  74. }
  75. else if(flagTeamId != user.teamId && craftId == flag.craftId)
  76. {
  77. flag.craftId = -1;
  78. flag.x = x;
  79. flag.y = y;
  80. flag.inBase = false;
  81. return true;
  82. }
  83. return false;
  84. }
  85. public override bool AttempCaptureFlag (int userId, int craftId)
  86. {
  87. BAUser user = GetUser (userId);
  88. if (user == null)
  89. return false;
  90. BAFlag myFlag = null;
  91. BAFlag oppFlag = null;
  92. flagDict.TryGetValue (user.teamId, out myFlag);
  93. flagDict.TryGetValue (TeamUtil.GetOpponentTeam(user.teamId).GetHashCode(), out oppFlag);
  94. if(myFlag == null || oppFlag == null)
  95. return false;
  96. if(myFlag.craftId == -1 && myFlag.inBase && oppFlag.craftId == craftId)
  97. {
  98. flagDict.Clear ();
  99. if(user.teamId == TeamUtil.Team.Blue.GetHashCode())
  100. {
  101. blueScore++;
  102. if(blueScore >= MAX_FLAG_CAPTURE)
  103. {
  104. SendBattleInfo(GetScoreData(TeamUtil.Team.Blue.GetHashCode()));
  105. }
  106. }
  107. else
  108. {
  109. redScore++;
  110. if(redScore >= MAX_FLAG_CAPTURE)
  111. {
  112. SendBattleInfo (GetScoreData(TeamUtil.Team.Red.GetHashCode()));
  113. }
  114. }
  115. lastResetFlagTime = GetGameTime () + scoreTaskInterval;
  116. return true;
  117. }
  118. return false;
  119. }
  120. public SFSObject GetScoreData(int winTeam)
  121. {
  122. if(isGameOver)
  123. return null;
  124. int freeTime = GetFreeTime();
  125. int time = roundTime-GetGameTime();
  126. if(time<=0)
  127. {
  128. if(blueScore>redScore)
  129. {
  130. winTeam = TeamUtil.Team.Blue.GetHashCode();
  131. }
  132. else if(blueScore<redScore)
  133. {
  134. winTeam = TeamUtil.Team.Red.GetHashCode();
  135. }
  136. else
  137. {
  138. winTeam = TeamUtil.Team.Yellow.GetHashCode();
  139. }
  140. }
  141. SFSObject data = new SFSObject();
  142. data.PutInt("t", time);
  143. data.PutInt("f", freeTime);
  144. data.PutInt("b", blueScore);
  145. data.PutInt("r", redScore);
  146. if(!isFirstTimeFlagSet)
  147. {
  148. if(GetFreeTime() == 0)
  149. {
  150. isFirstTimeFlagSet = true;
  151. ResetFlags();
  152. SendFlagAdded();
  153. }
  154. }
  155. else if(lastResetFlagTime != -1 && GetGameTime() > lastResetFlagTime)
  156. {
  157. lastResetFlagTime = -1;
  158. ResetFlags();
  159. SendFlagAdded();
  160. }
  161. else if(flagDict.Count > 0)
  162. {
  163. ISFSArray l = new SFSArray();
  164. PutFlagData(flagDict[TeamUtil.Team.Blue.GetHashCode()], l);
  165. PutFlagData(flagDict[TeamUtil.Team.Red.GetHashCode()], l);
  166. data.PutSFSArray("fl", l);
  167. }
  168. if(winTeam > 0)
  169. {
  170. DealGameOver(data, winTeam);
  171. }
  172. return data;
  173. }
  174. private void PutFlagData(BAFlag flag, ISFSArray arr)
  175. {
  176. if(flag != null)
  177. {
  178. ISFSObject obj = new SFSObject();
  179. obj.PutInt("t", flag.teamId);
  180. obj.PutInt("x", flag.x);
  181. obj.PutInt("y", flag.y);
  182. obj.PutInt("c", flag.craftId);
  183. arr.AddSFSObject(obj);
  184. }
  185. }
  186. public override void Update ()
  187. {
  188. base.Update ();
  189. if(scoreTaskRunning)
  190. {
  191. if (GetTime () - lastSendScoreTime >= scoreTaskInterval) {
  192. lastSendScoreTime = GetTime ();
  193. SendBattleInfo (GetScoreData (-1));
  194. }
  195. }
  196. }
  197. }