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