ChallengeService.cs 3.9 KB

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