You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GameServer.cs 25 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. using Grpc.Core;
  2. using Protobuf;
  3. using System.Threading;
  4. using Timothy.FrameRateTask;
  5. using System;
  6. using System.Net.Http.Headers;
  7. using Gaming;
  8. using GameClass.GameObj;
  9. using Preparation.Utility;
  10. using Playback;
  11. using Newtonsoft.Json;
  12. using Newtonsoft.Json.Linq;
  13. using Preparation.Interface;
  14. namespace Server
  15. {
  16. public class GameServer : AvailableService.AvailableServiceBase
  17. {
  18. private Dictionary<long, (SemaphoreSlim, SemaphoreSlim)> semaDict = new();
  19. protected readonly ArgumentOptions options;
  20. private HttpSender? httpSender;
  21. private object gameLock = new();
  22. private MessageToClient currentGameInfo = new();
  23. private MessageOfObj currentMapMsg = new();
  24. private object newsLock = new();
  25. private List<MessageOfNews> currentNews = new();
  26. private SemaphoreSlim endGameSem = new(0);
  27. protected readonly Game game;
  28. private uint spectatorMinPlayerID = 2023;
  29. private List<uint> spectatorList = new List<uint>();
  30. public int playerNum;
  31. public int TeamCount => options.TeamCount;
  32. protected long[] communicationToGameID; // 通信用的ID映射到游戏内的ID,通信中0-3为Student,4为Tricker
  33. private readonly object messageToAllClientsLock = new();
  34. public static readonly long SendMessageToClientIntervalInMilliseconds = 50;
  35. private MessageWriter? mwr = null;
  36. public void StartGame()
  37. {
  38. if (game.GameMap.Timer.IsGaming) return;
  39. /*foreach (var id in communicationToGameID)
  40. {
  41. if (id == GameObj.invalidID) return; //如果有未初始化的玩家,不开始游戏
  42. }*/ //测试时人数不够
  43. Console.WriteLine("Game starts!");
  44. game.StartGame((int)options.GameTimeInSecond * 1000);
  45. Thread.Sleep(1);
  46. new Thread(() =>
  47. {
  48. bool flag = true;
  49. new FrameRateTaskExecutor<int>
  50. (
  51. () => game.GameMap.Timer.IsGaming,
  52. () =>
  53. {
  54. if (flag == true)
  55. {
  56. ReportGame(GameState.GameStart);
  57. flag = false;
  58. }
  59. else ReportGame(GameState.GameRunning);
  60. },
  61. SendMessageToClientIntervalInMilliseconds,
  62. () =>
  63. {
  64. ReportGame(GameState.GameEnd); // 最后发一次消息,唤醒发消息的线程,防止发消息的线程由于有概率处在 Wait 状态而卡住
  65. OnGameEnd();
  66. return 0;
  67. }
  68. ).Start();
  69. })
  70. { IsBackground = true }.Start();
  71. }
  72. public void WaitForEnd()
  73. {
  74. this.endGameSem.Wait();
  75. mwr?.Dispose();
  76. }
  77. private void SaveGameResult(string path)
  78. {
  79. Dictionary<string, int> result = new Dictionary<string, int>();
  80. for (int i = 0; i < TeamCount; i++)
  81. {
  82. result.Add("Team" + i.ToString(), GetTeamScore(i)); //Team待修改
  83. }
  84. JsonSerializer serializer = new JsonSerializer();
  85. using (StreamWriter sw = new StreamWriter(path))
  86. {
  87. using (JsonWriter writer = new JsonTextWriter(sw))
  88. {
  89. serializer.Serialize(writer, result);
  90. }
  91. }
  92. }
  93. protected virtual void SendGameResult() // 天梯的 Server 给网站发消息记录比赛结果
  94. {
  95. var scores = new JObject[options.TeamCount];
  96. for (ushort i = 0; i < options.TeamCount; ++i)
  97. {
  98. scores[i] = new JObject { ["team_id"] = i.ToString(), ["score"] = GetTeamScore(i) };
  99. } // Team待修改
  100. httpSender?.SendHttpRequest
  101. (
  102. new JObject
  103. {
  104. ["result"] = new JArray(scores)
  105. }
  106. );
  107. }
  108. private void OnGameEnd()
  109. {
  110. game.ClearAllLists();
  111. mwr?.Flush();
  112. if (options.ResultFileName != DefaultArgumentOptions.FileName)
  113. SaveGameResult(options.ResultFileName + ".json");
  114. SendGameResult();
  115. this.endGameSem.Release();
  116. }
  117. public void ReportGame(GameState gameState, bool requiredGaming = true)
  118. {
  119. var gameObjList = game.GetGameObj();
  120. currentGameInfo = new();
  121. lock (messageToAllClientsLock)
  122. {
  123. switch (gameState)
  124. {
  125. case GameState.GameRunning:
  126. case GameState.GameEnd:
  127. case GameState.GameStart:
  128. currentGameInfo.ObjMessage.Add(currentMapMsg);
  129. foreach (GameObj gameObj in gameObjList)
  130. {
  131. MessageOfObj? msg = CopyInfo.Auto(gameObj);
  132. if (msg != null) currentGameInfo.ObjMessage.Add(CopyInfo.Auto(gameObj));
  133. }
  134. lock (newsLock)
  135. {
  136. foreach (var news in currentNews)
  137. {
  138. MessageOfObj? msg = CopyInfo.Auto(news);
  139. if (msg != null) currentGameInfo.ObjMessage.Add(CopyInfo.Auto(news));
  140. }
  141. currentNews.Clear();
  142. }
  143. currentGameInfo.GameState = gameState;
  144. currentGameInfo.AllMessage = GetMessageOfAll();
  145. mwr?.WriteOne(currentGameInfo);
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. foreach (var kvp in semaDict)
  152. {
  153. kvp.Value.Item1.Release();
  154. }
  155. foreach (var kvp in semaDict)
  156. {
  157. kvp.Value.Item2.Wait();
  158. }
  159. }
  160. public int GetTeamScore(long teamID)
  161. {
  162. return game.GetTeamScore(teamID);
  163. }
  164. private int PlayerIDToTeamID(long playerID)
  165. {
  166. if (0 <= playerID && playerID < options.StudentCount) return 0;
  167. if (options.MaxStudentCount <= playerID && playerID < options.MaxStudentCount + options.TrickerCount) return 1;
  168. return -1;
  169. }
  170. private int PlayerTypeToTeamID(Protobuf.PlayerType playerType)
  171. {
  172. if (playerType == PlayerType.StudentPlayer) return 0;
  173. if (playerType == PlayerType.TrickerPlayer) return 1;
  174. return -1;
  175. }
  176. private uint GetBirthPointIdx(long playerID) // 获取出生点位置
  177. {
  178. return (uint)playerID + 1; // ID从0-4,出生点从1-5
  179. }
  180. private bool ValidPlayerID(long playerID)
  181. {
  182. if ((0 <= playerID && playerID < options.StudentCount) || (options.MaxStudentCount <= playerID && playerID < options.MaxStudentCount + options.TrickerCount))
  183. return true;
  184. return false;
  185. }
  186. private MessageOfAll GetMessageOfAll()
  187. {
  188. MessageOfAll msg = new MessageOfAll();
  189. //msg.GameTime
  190. msg.SubjectFinished = (int)game.GameMap.NumOfRepairedGenerators;
  191. //msg.StudentGraduated
  192. //msg.StudentQuited
  193. msg.StudentScore = 0;
  194. msg.TrickerScore = 0;
  195. game.GameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
  196. try
  197. {
  198. foreach (Character character in game.GameMap.GameObjDict[GameObjType.Character])
  199. {
  200. if (!character.IsGhost()) msg.StudentScore += character.Score;
  201. else msg.TrickerScore += character.Score;
  202. }
  203. }
  204. finally
  205. {
  206. game.GameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  207. }
  208. //msg.GateOpened
  209. //msg.HiddenGateRefreshed
  210. //msg.HiddenGateOpened
  211. return msg;
  212. }
  213. private Protobuf.PlaceType IntToPlaceType(uint n)
  214. {
  215. switch (n)
  216. {
  217. case 0:
  218. case 1:
  219. case 2:
  220. case 3:
  221. case 4:
  222. case 5:
  223. return Protobuf.PlaceType.Land;
  224. case 6: return Protobuf.PlaceType.Wall;
  225. case 7: return Protobuf.PlaceType.Grass;
  226. case 8: return Protobuf.PlaceType.Classroom;
  227. case 9: return Protobuf.PlaceType.Gate;
  228. case 10: return Protobuf.PlaceType.HiddenGate;
  229. case 11: return Protobuf.PlaceType.Window;
  230. case 12: return Protobuf.PlaceType.Door3;
  231. case 13: return Protobuf.PlaceType.Door5;
  232. case 14: return Protobuf.PlaceType.Door6;
  233. case 15: return Protobuf.PlaceType.Chest;
  234. default: return Protobuf.PlaceType.NullPlaceType;
  235. }
  236. }
  237. private MessageOfObj MapMsg(uint[,] map)
  238. {
  239. MessageOfObj msgOfMap = new();
  240. msgOfMap.MapMessage = new();
  241. for (int i = 0; i < GameData.rows; i++)
  242. {
  243. msgOfMap.MapMessage.Row.Add(new MessageOfMap.Types.Row());
  244. for (int j = 0; j < GameData.cols; j++)
  245. {
  246. msgOfMap.MapMessage.Row[i].Col.Add(IntToPlaceType(map[i, j]));
  247. }
  248. }
  249. return msgOfMap;
  250. }
  251. public override Task<BoolRes> TryConnection(IDMsg request, ServerCallContext context)
  252. {
  253. #if DEBUG
  254. Console.WriteLine($"TryConnection ID: {request.PlayerId}");
  255. #endif
  256. var onConnection = new BoolRes();
  257. lock (gameLock)
  258. {
  259. if (0 <= request.PlayerId && request.PlayerId < playerNum) // 注意修改
  260. {
  261. onConnection.ActSuccess = true;
  262. Console.WriteLine(onConnection.ActSuccess);
  263. return Task.FromResult(onConnection);
  264. }
  265. }
  266. onConnection.ActSuccess = false;
  267. return Task.FromResult(onConnection);
  268. }
  269. protected readonly object addPlayerLock = new();
  270. public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<MessageToClient> responseStream, ServerCallContext context)
  271. {
  272. Console.WriteLine($"AddPlayer: {request.PlayerId}");
  273. if (request.PlayerId >= spectatorMinPlayerID)
  274. {
  275. // 观战模式
  276. uint tp = (uint)request.PlayerId;
  277. if (!spectatorList.Contains(tp))
  278. {
  279. spectatorList.Add(tp);
  280. Console.WriteLine("A new spectator comes to watch this game.");
  281. }
  282. return;
  283. }
  284. if (game.GameMap.Timer.IsGaming)
  285. return;
  286. if (!ValidPlayerID(request.PlayerId)) //玩家id是否正确
  287. return;
  288. if (communicationToGameID[request.PlayerId] != GameObj.invalidID) //是否已经添加了该玩家
  289. return;
  290. Preparation.Utility.CharacterType characterType = Preparation.Utility.CharacterType.Null;
  291. if (request.PlayerType == PlayerType.StudentPlayer)
  292. characterType = CopyInfo.ToStudentType(request.StudentType);
  293. else if (request.PlayerType == PlayerType.TrickerPlayer)
  294. characterType = CopyInfo.ToTrickerType(request.TrickerType);
  295. lock (addPlayerLock)
  296. {
  297. Game.PlayerInitInfo playerInitInfo = new(GetBirthPointIdx(request.PlayerId), PlayerTypeToTeamID(request.PlayerType), request.PlayerId, characterType);
  298. long newPlayerID = game.AddPlayer(playerInitInfo);
  299. if (newPlayerID == GameObj.invalidID)
  300. return;
  301. communicationToGameID[request.PlayerId] = newPlayerID;
  302. // 内容待修改
  303. var temp = (new SemaphoreSlim(0, 1), new SemaphoreSlim(0, 1));
  304. bool start = false;
  305. Console.WriteLine($"Id: {request.PlayerId} joins.");
  306. lock (semaDict)
  307. {
  308. semaDict.Add(request.PlayerId, temp);
  309. start = semaDict.Count == playerNum;
  310. }
  311. if (start) StartGame();
  312. }
  313. do
  314. {
  315. semaDict[request.PlayerId].Item1.Wait();
  316. if (currentGameInfo != null)
  317. {
  318. await responseStream.WriteAsync(currentGameInfo);
  319. //Console.WriteLine("Send!");
  320. }
  321. semaDict[request.PlayerId].Item2.Release();
  322. } while (game.GameMap.Timer.IsGaming);
  323. }
  324. public override Task<BoolRes> Attack(AttackMsg request, ServerCallContext context)
  325. {
  326. #if DEBUG
  327. Console.WriteLine($"Attack ID: {request.PlayerId}");
  328. #endif
  329. var gameID = communicationToGameID[request.PlayerId];
  330. game.Attack(gameID, request.Angle);
  331. BoolRes boolRes = new();
  332. boolRes.ActSuccess = true;
  333. return Task.FromResult(boolRes);
  334. }
  335. public override Task<MoveRes> Move(MoveMsg request, ServerCallContext context)
  336. {
  337. #if DEBUG
  338. Console.WriteLine($"Move ID: {request.PlayerId}, TimeInMilliseconds: {request.TimeInMilliseconds}");
  339. #endif
  340. var gameID = communicationToGameID[request.PlayerId];
  341. MoveRes moveRes = new();
  342. game.MovePlayer(gameID, (int)request.TimeInMilliseconds, request.Angle);
  343. // 之后game.MovePlayer可能改为bool类
  344. moveRes.ActSuccess = true;
  345. if (!game.GameMap.Timer.IsGaming) moveRes.ActSuccess = false;
  346. return Task.FromResult(moveRes);
  347. }
  348. public override Task<BoolRes> SendMessage(SendMsg request, ServerCallContext context)
  349. {
  350. var boolRes = new BoolRes();
  351. if (!ValidPlayerID(request.PlayerId) || !ValidPlayerID(request.ToPlayerId)
  352. || PlayerIDToTeamID(request.PlayerId) != PlayerIDToTeamID(request.ToPlayerId) || request.PlayerId == request.ToPlayerId)
  353. {
  354. boolRes.ActSuccess = false;
  355. return Task.FromResult(boolRes);
  356. }
  357. if (request.Message.Length > 256)
  358. {
  359. #if DEBUG
  360. Console.WriteLine("Message string is too long!");
  361. #endif
  362. boolRes.ActSuccess = false;
  363. return Task.FromResult(boolRes);
  364. }
  365. else
  366. {
  367. MessageOfNews news = new();
  368. news.News = request.Message;
  369. news.FromId = request.PlayerId;
  370. news.ToId = request.ToPlayerId;
  371. lock (newsLock)
  372. {
  373. currentNews.Add(news);
  374. }
  375. #if DEBUG
  376. Console.WriteLine(news.News);
  377. #endif
  378. }
  379. boolRes.ActSuccess = true;
  380. return Task.FromResult(boolRes);
  381. }
  382. public override Task<BoolRes> PickProp(PropMsg request, ServerCallContext context)
  383. {
  384. #if DEBUG
  385. Console.WriteLine($"PickProp ID: {request.PlayerId}");
  386. #endif
  387. BoolRes boolRes = new();
  388. var gameID = communicationToGameID[request.PlayerId];
  389. boolRes.ActSuccess = game.PickProp(gameID, CopyInfo.ToPropType(request.PropType));
  390. return Task.FromResult(boolRes);
  391. }
  392. public override Task<BoolRes> UseProp(PropMsg request, ServerCallContext context)
  393. {
  394. #if DEBUG
  395. Console.WriteLine($"UseProp ID: {request.PlayerId}");
  396. #endif
  397. BoolRes boolRes = new();
  398. var gameID = communicationToGameID[request.PlayerId];
  399. game.UseProp(gameID, CopyInfo.ToPropType(request.PropType));
  400. boolRes.ActSuccess = true;
  401. return Task.FromResult(boolRes);
  402. }
  403. public override Task<BoolRes> ThrowProp(PropMsg request, ServerCallContext context)
  404. {
  405. #if DEBUG
  406. Console.WriteLine($"ThrowProp ID: {request.PlayerId}");
  407. #endif
  408. BoolRes boolRes = new();
  409. var gameID = communicationToGameID[request.PlayerId];
  410. game.ThrowProp(gameID, CopyInfo.ToPropType(request.PropType));
  411. boolRes.ActSuccess = true;
  412. return Task.FromResult(boolRes);
  413. }
  414. public override Task<BoolRes> UseSkill(SkillMsg request, ServerCallContext context)
  415. {
  416. #if DEBUG
  417. Console.WriteLine($"UseSkill ID: {request.PlayerId}");
  418. #endif
  419. BoolRes boolRes = new();
  420. var gameID = communicationToGameID[request.PlayerId];
  421. boolRes.ActSuccess = game.UseActiveSkill(gameID, request.SkillId);
  422. return Task.FromResult(boolRes);
  423. }
  424. public override Task<BoolRes> Graduate(IDMsg request, ServerCallContext context)
  425. {
  426. #if DEBUG
  427. Console.WriteLine($"Graduate ID: {request.PlayerId}");
  428. #endif
  429. BoolRes boolRes = new();
  430. var gameID = communicationToGameID[request.PlayerId];
  431. boolRes.ActSuccess = game.Escape(gameID);
  432. return Task.FromResult(boolRes);
  433. }
  434. public override Task<BoolRes> StartRescueMate(TreatAndRescueMsg request, ServerCallContext context)
  435. {
  436. #if DEBUG
  437. Console.WriteLine($"StartRescueMate ID: {request.PlayerId}");
  438. #endif
  439. BoolRes boolRes = new();
  440. var gameID = communicationToGameID[request.PlayerId];
  441. var toGameID = communicationToGameID[request.ToPlayerId];
  442. boolRes.ActSuccess = game.Rescue(gameID, toGameID);
  443. return Task.FromResult(boolRes);
  444. }
  445. public override Task<BoolRes> StartTreatMate(TreatAndRescueMsg request, ServerCallContext context)
  446. {
  447. #if DEBUG
  448. Console.WriteLine($"StartTreatMate ID: {request.PlayerId}");
  449. #endif
  450. BoolRes boolRes = new();
  451. var gameID = communicationToGameID[request.PlayerId];
  452. var toGameID = communicationToGameID[request.ToPlayerId];
  453. boolRes.ActSuccess = game.Treat(gameID, toGameID);
  454. return Task.FromResult(boolRes);
  455. }
  456. public override Task<BoolRes> StartLearning(IDMsg request, ServerCallContext context)
  457. {
  458. #if DEBUG
  459. Console.WriteLine($"StartLearning ID: {request.PlayerId}");
  460. #endif
  461. BoolRes boolRes = new();
  462. var gameID = communicationToGameID[request.PlayerId];
  463. boolRes.ActSuccess = game.Fix(gameID);
  464. return Task.FromResult(boolRes);
  465. }
  466. public override Task<BoolRes> StartOpenChest(IDMsg request, ServerCallContext context)
  467. {
  468. #if DEBUG
  469. Console.WriteLine($"StartOpenChest ID: {request.PlayerId}");
  470. #endif
  471. BoolRes boolRes = new();
  472. var gameID = communicationToGameID[request.PlayerId];
  473. boolRes.ActSuccess = game.OpenChest(gameID);
  474. return Task.FromResult(boolRes);
  475. }
  476. public override Task<BoolRes> StartOpenGate(IDMsg request, ServerCallContext context)
  477. {
  478. #if DEBUG
  479. Console.WriteLine($"StartOpenGate ID: {request.PlayerId}");
  480. #endif
  481. BoolRes boolRes = new();
  482. var gameID = communicationToGameID[request.PlayerId];
  483. boolRes.ActSuccess = game.OpenDoorway(gameID);
  484. return Task.FromResult(boolRes);
  485. }
  486. public override Task<BoolRes> OpenDoor(IDMsg request, ServerCallContext context)
  487. {
  488. #if DEBUG
  489. Console.WriteLine($"OpenDoor ID: {request.PlayerId}");
  490. #endif
  491. BoolRes boolRes = new();
  492. var gameID = communicationToGameID[request.PlayerId];
  493. boolRes.ActSuccess = game.LockOrOpenDoor(gameID);
  494. return Task.FromResult(boolRes);
  495. }
  496. public override Task<BoolRes> CloseDoor(IDMsg request, ServerCallContext context)
  497. {
  498. #if DEBUG
  499. Console.WriteLine($"CloseDoor ID: {request.PlayerId}");
  500. #endif
  501. BoolRes boolRes = new();
  502. var gameID = communicationToGameID[request.PlayerId];
  503. boolRes.ActSuccess = game.LockOrOpenDoor(gameID);
  504. return Task.FromResult(boolRes);
  505. }
  506. public override Task<BoolRes> EndAllAction(IDMsg request, ServerCallContext context)
  507. {
  508. #if DEBUG
  509. Console.WriteLine($"EndAllAction ID: {request.PlayerId}");
  510. #endif
  511. BoolRes boolRes = new();
  512. var gameID = communicationToGameID[request.PlayerId];
  513. boolRes.ActSuccess = game.Stop(gameID);
  514. return Task.FromResult(boolRes);
  515. }
  516. public override Task<BoolRes> SkipWindow(IDMsg request, ServerCallContext context)
  517. {
  518. #if DEBUG
  519. Console.WriteLine($"SkipWindow ID: {request.PlayerId}");
  520. #endif
  521. BoolRes boolRes = new();
  522. var gameID = communicationToGameID[request.PlayerId];
  523. boolRes.ActSuccess = game.ClimbingThroughWindow(gameID);
  524. return Task.FromResult(boolRes);
  525. }
  526. public GameServer(ArgumentOptions options)
  527. {
  528. this.options = options;
  529. if (options.mapResource == DefaultArgumentOptions.MapResource)
  530. this.game = new Game(MapInfo.defaultMap, options.TeamCount);
  531. else
  532. {
  533. uint[,] map = new uint[GameData.rows, GameData.cols];
  534. try
  535. {
  536. string? line;
  537. int i = 0, j = 0;
  538. using (StreamReader sr = new StreamReader(options.mapResource))
  539. {
  540. while (!sr.EndOfStream && i < GameData.rows)
  541. {
  542. if ((line = sr.ReadLine()) != null)
  543. {
  544. string[] nums = line.Split(' ');
  545. foreach (string item in nums)
  546. {
  547. if (item.Length > 1)//以兼容原方案
  548. {
  549. map[i, j] = (uint)int.Parse(item);
  550. }
  551. else
  552. {
  553. //2022-04-22 by LHR 十六进制编码地图方案(防止地图编辑员瞎眼x
  554. map[i, j] = (uint)Preparation.Utility.MapEncoder.Hex2Dec(char.Parse(item));
  555. }
  556. j++;
  557. if (j >= GameData.cols)
  558. {
  559. j = 0;
  560. break;
  561. }
  562. }
  563. i++;
  564. }
  565. }
  566. }
  567. }
  568. catch
  569. {
  570. map = MapInfo.defaultMap;
  571. }
  572. finally { this.game = new Game(map, options.TeamCount); }
  573. }
  574. playerNum = options.StudentCount + options.TrickerCount;
  575. currentMapMsg = MapMsg(game.GameMap.ProtoGameMap);
  576. communicationToGameID = new long[options.MaxStudentCount + options.TrickerCount];
  577. //创建server时先设定待加入人物都是invalid
  578. for (int i = 0; i < communicationToGameID.GetLength(0); i++)
  579. {
  580. communicationToGameID[i] = GameObj.invalidID;
  581. }
  582. if (options.FileName != DefaultArgumentOptions.FileName)
  583. {
  584. try
  585. {
  586. mwr = new MessageWriter(options.FileName, options.TeamCount, options.StudentCount);
  587. }
  588. catch
  589. {
  590. Console.WriteLine($"Error: Cannot create the playback file: {options.FileName}!");
  591. }
  592. }
  593. if (options.Url != DefaultArgumentOptions.Url && options.Token != DefaultArgumentOptions.Token)
  594. {
  595. this.httpSender = new HttpSender(options.Url, options.Token, "PUT");
  596. }
  597. }
  598. }
  599. }