ArenaService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X.Entities.Data;
  5. public class ArenaService : GameRoomService
  6. {
  7. public const int SEND_SCORE_INTERVAL = 5*1000;
  8. protected Dictionary<int, CrystalBaseData> crystalBaseMap;
  9. public ArenaService (ResponseDelegate callBack):base(callBack)
  10. {
  11. }
  12. override protected void InitGame()
  13. {
  14. List<int> list = new List<int>();
  15. list.Add(0);
  16. list.Add(1);
  17. AttempAddStation(0, TeamUtil.Team.Blue.GetHashCode());
  18. AttempAddStation(1, TeamUtil.Team.Red.GetHashCode());
  19. }
  20. public override void RemoveStation (int stationId)
  21. {
  22. int numBlue = 0;
  23. int numRed = 0;
  24. int needRemoveKey = -1;
  25. foreach(KeyValuePair<int, ServiceStation> kvp in stationDict)
  26. {
  27. ServiceStation station = kvp.Value;
  28. if(station.id == stationId)
  29. {
  30. needRemoveKey = station.crystalId;
  31. }
  32. else if(station.teamId == TeamUtil.Team.Blue.GetHashCode())
  33. {
  34. numBlue++;
  35. }
  36. else if(station.teamId == TeamUtil.Team.Red.GetHashCode())
  37. {
  38. numRed++;
  39. }
  40. }
  41. if(needRemoveKey != -1)
  42. {
  43. stationDict.Remove(needRemoveKey);
  44. }
  45. if(numBlue == 0)
  46. {
  47. SendBattleInfo(GetScoreData(TeamUtil.Team.Red.GetHashCode()), null, null);
  48. }
  49. else if(numRed == 0)
  50. {
  51. SendBattleInfo(GetScoreData(TeamUtil.Team.Blue.GetHashCode()), null, null);
  52. }
  53. }
  54. public SFSObject GetScoreData(int winTeam)
  55. {
  56. SFSObject data = new SFSObject();
  57. if(isGameOver)
  58. {
  59. return data;
  60. }
  61. int score = 0;
  62. if(GetFreeTime() == 0)
  63. score = SEND_SCORE_INTERVAL/1000;
  64. foreach(KeyValuePair<int, ServiceStation> kvp in stationDict)
  65. {
  66. ServiceStation station = kvp.Value;
  67. if(station.teamId == TeamUtil.Team.Blue.GetHashCode())
  68. {
  69. blueScore += score;
  70. }
  71. else if(station.teamId == TeamUtil.Team.Red.GetHashCode())
  72. {
  73. redScore += score;
  74. }
  75. }
  76. data.PutInt("b", blueScore);
  77. data.PutInt("r", redScore);
  78. int time = roundTime - GetGameTime();
  79. if(winTeam > 0)
  80. {
  81. data.PutInt("t", time);
  82. DealGameOver(data, winTeam);
  83. }
  84. else if(time <= 0)
  85. {
  86. data.PutInt("t", 0);
  87. winTeam = blueScore >= redScore ? TeamUtil.Team.Blue.GetHashCode() : TeamUtil.Team.Red.GetHashCode();
  88. DealGameOver(data, winTeam);
  89. }
  90. else
  91. {
  92. data.PutInt("t", time);
  93. }
  94. int freeTime = GetFreeTime();
  95. data.PutInt("f", freeTime);
  96. return data;
  97. }
  98. override protected void DoTask()
  99. {
  100. SendBattleInfo(GetScoreData(-1), GetPlayerChangeData(), GetAiTakerChangeData());
  101. }
  102. override protected void SavePlayerScore(SFSObject data, int winTeam)
  103. {
  104. SFSArray stats = new SFSArray();
  105. int roundMin = roundTime/60/1000;
  106. if(roundMin <= 0)
  107. {
  108. roundMin = 1;
  109. }
  110. List<ServicePlayer> list = new List<ServicePlayer>();
  111. list.Add(myPlayer);
  112. for(int i=0; i<list.Count; i++)
  113. {
  114. ServicePlayer player = list[i];
  115. SFSObject statsInfo = new SFSObject();
  116. statsInfo.PutInt("u", player.userId);
  117. statsInfo.PutInt("k", player.kill);
  118. statsInfo.PutInt("a", player.assists);
  119. statsInfo.PutInt("d", player.death);
  120. int rankScore = player.kill * 10 + player.assists * 2;
  121. int expScore = rankScore;
  122. int time = (roundTime - player.joinTime)/60/1000 + 1;
  123. int win = 0;
  124. int lose = 0;
  125. int coinGet = 0;
  126. if(time > roundMin)
  127. time = roundMin;
  128. if(player.teamId == winTeam)
  129. {
  130. rankScore += 300*time/roundMin;
  131. expScore += 300*time/roundMin;
  132. coinGet = 100*time/roundMin;
  133. win = 1;
  134. }
  135. else
  136. {
  137. rankScore -= 300*time/roundMin;
  138. expScore += 100*time/roundMin;
  139. coinGet = 80*time/roundMin;
  140. lose = 1;
  141. }
  142. statsInfo.PutInt("t", time);
  143. statsInfo.PutInt("r", rankScore);
  144. statsInfo.PutInt("e", expScore);
  145. statsInfo.PutInt("c", coinGet);
  146. stats.AddSFSObject(statsInfo);
  147. }
  148. data.PutSFSArray("s", stats);
  149. }
  150. }