From 5e6c8fe87837dd279bbb13254fc6adf2f6178b07 Mon Sep 17 00:00:00 2001 From: gsy1519 <614054460@qq.com> Date: Sun, 26 Mar 2023 01:57:13 +0800 Subject: [PATCH] feat: :art: improve argument options --- logic/Server/ArgumentOption.cs | 17 ++++++++++------- logic/Server/GameServer.cs | 21 +++++++++++---------- logic/Server/Properties/launchSettings.json | 2 +- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/logic/Server/ArgumentOption.cs b/logic/Server/ArgumentOption.cs index fdb86a1..f735f31 100644 --- a/logic/Server/ArgumentOption.cs +++ b/logic/Server/ArgumentOption.cs @@ -12,20 +12,23 @@ namespace Server public class ArgumentOptions { - [Option("ip", Required = true, HelpText = "Server listening port")] + [Option("ip", Required = false, HelpText = "Server listening port")] public string ServerIP { get; set; } = "0.0.0.0"; [Option('p', "port", Required = true, HelpText = "Server listening port")] public ushort ServerPort { get; set; } = 8888; - [Option('n', "playerNum", Required = false, HelpText = "The number of players, 1 by defualt")] - public ushort playerNum { get; set; } = 1; - - [Option('t', "teamCount", Required = false, HelpText = "The number of teams, 1 by defualt")] + [Option("teamCount", Required = false, HelpText = "The number of teams, 2 by defualt")] public ushort TeamCount { get; set; } = 2; - [Option('c', "playerCount", Required = false, HelpText = "The number of students, 1 by default")] - public ushort PlayerCountPerTeam { get; set; } = 4; + [Option('s', "studentCount", Required = false, HelpText = "The number of students, 4 by default")] + public ushort StudentCount { get; set; } = 4; + + [Option('t', "trickerCount", Required = false, HelpText = "The number of trickers, 1 by default")] + public ushort TrickerCount { get; set; } = 1; + + [Option("maxStudentCount", Required = false, HelpText = "The max number of students, 4 by default")] + public ushort MaxStudentCount { get; set; } = 4; [Option('g', "gameTimeInSecond", Required = false, HelpText = "The time of the game in second, 10 minutes by default")] public uint GameTimeInSecond { get; set; } = 10 * 60; diff --git a/logic/Server/GameServer.cs b/logic/Server/GameServer.cs index 8c45d45..82694f2 100644 --- a/logic/Server/GameServer.cs +++ b/logic/Server/GameServer.cs @@ -20,7 +20,6 @@ namespace Server protected readonly ArgumentOptions options; private HttpSender? httpSender; private object gameLock = new(); - public int PlayerNum => options.playerNum; // 注意修改 private MessageToClient currentGameInfo = new(); private MessageOfObj currentMapMsg = new(); private object newsLock = new(); @@ -29,6 +28,7 @@ namespace Server protected readonly Game game; private uint spectatorMinPlayerID = 2023; private List spectatorList = new List(); + public int playerNum; public int TeamCount => options.TeamCount; protected long[] communicationToGameID; // 通信用的ID映射到游戏内的ID,通信中0-3为Student,4为Tricker private readonly object messageToAllClientsLock = new(); @@ -185,7 +185,7 @@ namespace Server private int PlayerIDToTeamID(long playerID) { - if (0 <= playerID && playerID < options.PlayerCountPerTeam) return 0; + if (0 <= playerID && playerID < options.StudentCount) return 0; if (playerID == 4) return 1; return -1; } @@ -202,7 +202,7 @@ namespace Server } private bool ValidPlayerID(long playerID) { - if ((0 <= playerID && playerID < options.PlayerCountPerTeam) || playerID == 4) + if ((0 <= playerID && playerID < options.StudentCount) || (options.MaxStudentCount <= playerID && playerID < options.MaxStudentCount + 1)) return true; return false; } @@ -276,7 +276,7 @@ namespace Server var onConnection = new BoolRes(); lock (gameLock) { - if (0 <= request.PlayerId && request.PlayerId < PlayerNum) // 注意修改 + if (0 <= request.PlayerId && request.PlayerId < playerNum) // 注意修改 { onConnection.ActSuccess = true; Console.WriteLine(onConnection.ActSuccess); @@ -331,7 +331,7 @@ namespace Server lock (semaDict) { semaDict.Add(request.PlayerId, temp); - start = semaDict.Count == PlayerNum; + start = semaDict.Count == playerNum; } if (start) StartGame(); } @@ -564,9 +564,9 @@ namespace Server public GameServer(ArgumentOptions options) { this.options = options; - //if (options.mapResource == DefaultArgumentOptions.MapResource) - // this.game = new Game(MapInfo.defaultMap, options.TeamCount); - //else + if (options.mapResource == DefaultArgumentOptions.MapResource) + this.game = new Game(MapInfo.defaultMap, options.TeamCount); + else { uint[,] map = new uint[GameData.rows, GameData.cols]; try @@ -609,8 +609,9 @@ namespace Server } finally { this.game = new Game(map, options.TeamCount); } } + playerNum = options.StudentCount + options.TrickerCount; currentMapMsg = MapMsg(game.GameMap.ProtoGameMap); - communicationToGameID = new long[options.PlayerCountPerTeam + 1]; + communicationToGameID = new long[options.MaxStudentCount + options.TrickerCount]; //创建server时先设定待加入人物都是invalid for (int i = 0; i < communicationToGameID.GetLength(0); i++) { @@ -621,7 +622,7 @@ namespace Server { try { - mwr = new MessageWriter(options.FileName, options.TeamCount, options.PlayerCountPerTeam); + mwr = new MessageWriter(options.FileName, options.TeamCount, options.StudentCount); } catch { diff --git a/logic/Server/Properties/launchSettings.json b/logic/Server/Properties/launchSettings.json index f1ad4af..61e0160 100644 --- a/logic/Server/Properties/launchSettings.json +++ b/logic/Server/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "Server": { "commandName": "Project", - "commandLineArgs": "--ip 0.0.0.0\r\n-p 8888\r\n-f playback\r\n-g 600\r\n-b true\r\n-c 4\r\n-t 2\r\n-n 1" + "commandLineArgs": "--ip 0.0.0.0 -p 8888" } } } \ No newline at end of file