Browse Source

feat: 🎨 improve argument options

tags/0.1.0
gsy1519 2 years ago
parent
commit
5e6c8fe878
3 changed files with 22 additions and 18 deletions
  1. +10
    -7
      logic/Server/ArgumentOption.cs
  2. +11
    -10
      logic/Server/GameServer.cs
  3. +1
    -1
      logic/Server/Properties/launchSettings.json

+ 10
- 7
logic/Server/ArgumentOption.cs View File

@@ -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;


+ 11
- 10
logic/Server/GameServer.cs View File

@@ -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<uint> spectatorList = new List<uint>();
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
{


+ 1
- 1
logic/Server/Properties/launchSettings.json View File

@@ -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"
}
}
}

Loading…
Cancel
Save