GameRoomService.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X;
  5. using Sfs2X.Entities.Data;
  6. public class GameRoomService
  7. {
  8. public delegate void ResponseDelegate(string cmd, ISFSObject data);
  9. public int userId = 1;
  10. public int maxUsers = 6;
  11. public MapData.Mode mode;
  12. public int roundTime = 5*60*1000;
  13. public int freeTime = 1*15*1000;
  14. protected ServicePlayer myPlayer;
  15. public Dictionary<int, ServicePlayer> aiPlayerDict;
  16. public List<ServicePlayer> aiPlayerList;
  17. public Dictionary<int, ServiceStation> stationDict; //key is crystalBase id
  18. public Dictionary<int, ServiceFlag> flagDict;
  19. public Dictionary<int, ServiceDoor> doorDict;
  20. public Dictionary<int, ServiceItemPos> itemPosDict;
  21. protected int craftIdCount = 1;
  22. protected int aiPlayerIdCount = -2;
  23. public ServicePlayer mapPlayer;
  24. public int blueScore;
  25. public int redScore;
  26. protected int startTime;
  27. public bool isGameOver = false;
  28. protected float taskInterval = 5f;
  29. protected float lastTaskTime = 0;
  30. protected bool isTaskRunning = true;
  31. protected Dictionary<string, LocalRequestHandler> handlerDict;
  32. private ResponseDelegate responseCallBack;
  33. public GameRoomService (ResponseDelegate responseCallBack)
  34. {
  35. this.responseCallBack = responseCallBack;
  36. aiPlayerDict = new Dictionary<int, ServicePlayer>();
  37. aiPlayerList = new List<ServicePlayer>();
  38. stationDict = new Dictionary<int, ServiceStation>();
  39. flagDict = new Dictionary<int, ServiceFlag>();
  40. doorDict = new Dictionary<int, ServiceDoor>();
  41. itemPosDict = new Dictionary<int, ServiceItemPos>();
  42. mapPlayer = new ServicePlayer(-1, "");
  43. blueScore = 0;
  44. redScore = 0;
  45. startTime = (int)(GameTime.time*1000f);
  46. lastTaskTime = GameTime.time;
  47. //add request handlers
  48. handlerDict = new Dictionary<string, LocalRequestHandler>();
  49. // handlerDict.Add(Command.PlayerJoin, new PlayerJoinHandler());
  50. // handlerDict.Add(Command.Ready, new ReadyHandler());
  51. // handlerDict.Add(Command.CraftSelection, new CraftSelectionHandler());
  52. handlerDict.Add(Command.Attack, new AttackHandler());
  53. handlerDict.Add(Command.Radio, new RadioHandler());
  54. handlerDict.Add(Command.Station, new OccupyStationHandler());
  55. handlerDict.Add(Command.GetMapItem, new GetMapItemHandler());
  56. // handlerDict.Add(Command.GetFlag, new GetFlagHandler());
  57. // handlerDict.Add(Command.PutFlag, new PutFlagHandler());
  58. // handlerDict.Add(Command.CaptureFlag, new CaptureFlagHandler());
  59. handlerDict.Add(Command.SimpleSync, new SimpleSyncHandler());
  60. InitGame();
  61. }
  62. public ServicePlayer GetMyPlayer()
  63. {
  64. return myPlayer;
  65. }
  66. protected virtual void InitGame()
  67. {
  68. }
  69. public SFSObject GetMapPlayerObj()
  70. {
  71. SFSObject obj = new SFSObject();
  72. obj.PutInt("u", mapPlayer.userId);
  73. obj.PutUtfString("n", mapPlayer.nickName);
  74. obj.PutInt("t", mapPlayer.teamId);
  75. obj.PutInt("a", mapPlayer.aiTakerId);
  76. return obj;
  77. }
  78. public ServiceStation AttempAddStation(int crystalIndex, int teamId)
  79. {
  80. if(stationDict.ContainsKey(crystalIndex))
  81. {
  82. return null;
  83. }
  84. ServiceStation station = new ServiceStation(CreateCraftId(), crystalIndex, teamId);
  85. stationDict.Add(crystalIndex, station);
  86. return station;
  87. }
  88. public virtual void RemoveStation(int stationId)
  89. {
  90. }
  91. public int AttempGetItem(int id)
  92. {
  93. if(itemPosDict.ContainsKey(id))
  94. {
  95. ServiceItemPos itemPos = itemPosDict[id];
  96. int itemId = itemPos.itemId;
  97. itemPos.itemId = -1;
  98. return itemId;
  99. }
  100. return -1;
  101. }
  102. public virtual bool AttempGetFlag(int userId)
  103. {
  104. return false;
  105. }
  106. public virtual bool AttempPutFlag(int userId, int teamId, int x, int y)
  107. {
  108. return false;
  109. }
  110. public virtual bool AttempCaptureFlag(int userId)
  111. {
  112. return false;
  113. }
  114. public void AddDoor(int index, int teamId)
  115. {
  116. if(doorDict.ContainsKey(index))
  117. {
  118. return;
  119. }
  120. ServiceDoor door = new ServiceDoor(CreateCraftId(), index, teamId);
  121. doorDict.Add(index, door);
  122. }
  123. public virtual void RemoveDoor(int doorId)
  124. {
  125. foreach(KeyValuePair<int, ServiceDoor> kvp in doorDict)
  126. {
  127. ServiceDoor door = kvp.Value;
  128. if(door.id == doorId)
  129. {
  130. doorDict.Remove(door.index);
  131. return;
  132. }
  133. }
  134. }
  135. public SFSArray GetFlagDataArray()
  136. {
  137. SFSArray arr = new SFSArray();
  138. foreach(KeyValuePair<int, ServiceFlag> kvp in flagDict)
  139. {
  140. ServiceFlag flag = kvp.Value;
  141. SFSObject obj = new SFSObject();
  142. obj.PutInt("t", flag.teamId);
  143. obj.PutInt("x", flag.x);
  144. obj.PutInt("y", flag.y);
  145. obj.PutInt("c", flag.craftId);
  146. arr.AddSFSObject(obj);
  147. }
  148. return arr;
  149. }
  150. public int GetTime()
  151. {
  152. return (int)(GameTime.time*1000) - startTime;
  153. }
  154. public int GetGameTime()
  155. {
  156. int time = GetTime();
  157. if(time <= freeTime)
  158. {
  159. return 0;
  160. }
  161. return time-freeTime;
  162. }
  163. public int GetFreeTime()
  164. {
  165. int time = GetTime();
  166. if(time <= freeTime)
  167. {
  168. return freeTime-time;
  169. }
  170. return 0;
  171. }
  172. public ServicePlayer NewPlayerJoin(string nick, int userId)
  173. {
  174. mapPlayer.aiTakerId = userId;
  175. ServicePlayer player = new ServicePlayer(userId, nick);
  176. player.teamId = TeamUtil.Team.Blue.GetHashCode();
  177. myPlayer = player;
  178. return player;
  179. }
  180. public ServicePlayer NewAIPlayer(int teamId)
  181. {
  182. int userId = CreateAIPlayerId();
  183. ServicePlayer player = new ServicePlayer(userId, "AI"+userId);
  184. player.teamId = teamId;
  185. player.joinTime = GetGameTime();
  186. aiPlayerDict.Add(userId, player);
  187. aiPlayerList.Add(player);
  188. return player;
  189. }
  190. public ServicePlayer GetPlayer(int userId)
  191. {
  192. if(myPlayer.userId == userId)
  193. return myPlayer;
  194. if(aiPlayerDict.ContainsKey(userId))
  195. return aiPlayerDict[userId];
  196. return null;
  197. }
  198. public int CreateCraftId()
  199. {
  200. return craftIdCount++;
  201. }
  202. public int CreateAIPlayerId()
  203. {
  204. return aiPlayerIdCount--;
  205. }
  206. protected void SendBattleInfo(SFSObject scoreData, SFSObject playerData, SFSObject aiData)
  207. {
  208. SFSObject data = new SFSObject();
  209. if(scoreData != null)
  210. data.PutSFSObject("s", scoreData);
  211. if(aiData != null)
  212. data.PutSFSObject("a", aiData);
  213. if(playerData != null)
  214. data.PutSFSObject("p", playerData);
  215. Send(Command.BattleInfo.ToString(), data);
  216. }
  217. protected SFSObject GetAiTakerChangeData()
  218. {
  219. SFSArray arr = new SFSArray();
  220. for(int i=0; i<aiPlayerList.Count; i++)
  221. {
  222. ServicePlayer player = aiPlayerList[i];
  223. if(player.aiTakerId != myPlayer.userId)
  224. {
  225. player.aiTakerId = myPlayer.userId;
  226. SFSObject obj = new SFSObject();
  227. obj.PutInt("u", player.userId);
  228. obj.PutInt("t", player.aiTakerId);
  229. arr.AddSFSObject(obj);
  230. }
  231. }
  232. SFSObject res = new SFSObject();
  233. if(arr.Size() > 0)
  234. {
  235. res.PutSFSArray("a", arr);
  236. return res;
  237. }
  238. return null;
  239. }
  240. protected SFSObject GetPlayerChangeData()
  241. {
  242. if(aiPlayerList.Count >= maxUsers - 1)
  243. return null;
  244. SFSObject res = new SFSObject();
  245. if(GetFreeTime() > 5)
  246. return null;
  247. int blueCount = 1;
  248. int redCount = 0;
  249. for(int i=0; i<aiPlayerList.Count; i++)
  250. {
  251. ServicePlayer player = aiPlayerList[i];
  252. if(player.teamId == TeamUtil.Team.Blue.GetHashCode())
  253. blueCount++;
  254. else if(player.teamId == TeamUtil.Team.Red.GetHashCode())
  255. redCount++;
  256. }
  257. int maxTeamUserCount = maxUsers/2;
  258. SFSArray addArr = new SFSArray();
  259. while(blueCount < maxTeamUserCount)
  260. {
  261. SFSObject data = new SFSObject();
  262. ServicePlayer player = NewAIPlayer(TeamUtil.Team.Blue.GetHashCode());
  263. data.PutInt("u", player.userId);
  264. data.PutInt("t", player.teamId);
  265. data.PutUtfString("n", player.nickName);
  266. addArr.AddSFSObject(data);
  267. blueCount++;
  268. }
  269. while(redCount < maxTeamUserCount)
  270. {
  271. SFSObject data = new SFSObject();
  272. ServicePlayer player = NewAIPlayer(TeamUtil.Team.Red.GetHashCode());
  273. data.PutInt("u", player.userId);
  274. data.PutInt("t", player.teamId);
  275. data.PutUtfString("n", player.nickName);
  276. addArr.AddSFSObject(data);
  277. redCount++;
  278. }
  279. if(addArr.Size() > 0)
  280. {
  281. res.PutSFSArray("a", addArr);
  282. return res;
  283. }
  284. return null;
  285. }
  286. protected void DealGameOver(SFSObject data, int winTeam)
  287. {
  288. StopTask();
  289. isGameOver = true;
  290. data.PutInt("w", winTeam);
  291. SavePlayerScore(data, winTeam);
  292. }
  293. protected virtual void SavePlayerScore(SFSObject data, int winTeam)
  294. {
  295. }
  296. public void DealMessage(Message msg)
  297. {
  298. if(handlerDict.ContainsKey(msg.cmd))
  299. handlerDict[msg.cmd].HandleClientRequest(this, msg.data);
  300. else
  301. Debuger.LogError("can not find handler with command["+msg.cmd+"]");
  302. }
  303. public void Send(string cmd, ISFSObject data)
  304. {
  305. responseCallBack(cmd, data);
  306. }
  307. protected virtual void DoTask()
  308. {
  309. }
  310. public void StopTask()
  311. {
  312. isTaskRunning = false;
  313. }
  314. public void run()
  315. {
  316. if(isTaskRunning)
  317. {
  318. float currentTime = GameTime.time;
  319. if(currentTime-lastTaskTime >= taskInterval)
  320. {
  321. lastTaskTime = currentTime;
  322. DoTask();
  323. }
  324. }
  325. }
  326. }