diff --git a/logic/Client/Client.csproj b/logic/Client/Client.csproj index d284dda..352b45b 100644 --- a/logic/Client/Client.csproj +++ b/logic/Client/Client.csproj @@ -12,6 +12,7 @@ + diff --git a/logic/Client/CommandLineArgs.cs b/logic/Client/CommandLineArgs.cs new file mode 100644 index 0000000..77b3f1f --- /dev/null +++ b/logic/Client/CommandLineArgs.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using CommandLine; + +namespace Client +{ + static class DefaultArgumentOptions + { + public static string FileName = "CLGG!@#$%^&*()_+"; // An impossible name of the playback file to indicate -f is not sepcified. + } + + public class ArgumentOptions + { + [Option('u', "cl", Required = false, HelpText = "Whether to use command line")] + public bool cl { get; set; } = false; + + [Option('i', "ip", Required = false, HelpText = "Client connected ip")] + public string Ip { get; set; } = "127.0.0.1"; + + [Option('p', "port", Required = false, HelpText = "Client listening port")] + public string Port { get; set; } = "7777"; + + [Option('t', "teamID", Required = false, HelpText = "Client teamID")] + public string TeamID { get; set; } = "0"; + + [Option('c', "characterID", Required = false, HelpText = "Client playerID")] + public string PlayerID { get; set; } = "0"; + + [Option('y', "type", Required = false, HelpText = "Client playerType")] + public string PlayerType { get; set; } = "0"; + + [Option('o', "occupation", Required = false, HelpText = "Client occupation")] + public string Occupation { get; set; } = "0"; + + [Option('f', "playbackFile", Required = false, HelpText = "The playback file name.")] + public string PlaybackFile { get; set; } = DefaultArgumentOptions.FileName; + + [Option("playbackSpeed", Required = false, HelpText = "The speed of the playback, between 0.25 and 4.0")] + public double PlaybackSpeed { get; set; } = 1.0; + + } +} diff --git a/logic/Client/MainWindow.xaml.cs b/logic/Client/MainWindow.xaml.cs index 544502b..13506af 100644 --- a/logic/Client/MainWindow.xaml.cs +++ b/logic/Client/MainWindow.xaml.cs @@ -17,12 +17,11 @@ using System.Windows.Threading; using Grpc.Core; using Protobuf; using Playback; +using CommandLine; // 目前MainWindow还未复现的功能: -// 部分errordisplayer // private void ReactToCommandline(), -// private void Playback(string fileName, double pbSpeed = 2.0) -// 交互:private void ClickToSetMode(object sender, RoutedEventArgs e) +// private void ClickToSetMode(object sender, RoutedEventArgs e) // private void Bonus() namespace Client @@ -58,27 +57,17 @@ namespace Client listOfDoor = new List(); listOfGate = new List(); WindowStartupLocation = WindowStartupLocation.CenterScreen; - comInfo[0] = "127.0.0.1"; - comInfo[1] = "8888"; - comInfo[2] = "0"; - comInfo[3] = "1"; - comInfo[4] = "1"; - ConnectToServer(comInfo); - OnReceive(); - //DrawMap(); - //ZoomMap(); - //MessageOfStudent kurei = new MessageOfStudent(); - //kurei.X = 10000; - //kurei.Y = 20000; - //kurei.Speed = 1000; - //kurei.PlayerId = 0; - //listOfHuman.Add(kurei); - //MessageOfAll all= new MessageOfAll(); - //all.HiddenGateRefreshed = false; - //listOfAll.Add(all); - // ReactToCommandline(); + //comInfo[0] = "127.0.0.1"; + //comInfo[1] = "8888"; + //comInfo[2] = "0"; + //comInfo[3] = "1"; + //comInfo[4] = "1"; + //ConnectToServer(comInfo); + //OnReceive(); + ReactToCommandline(); } + private void SetStatusBar() { StatusBarsOfSurvivor = new StatusBarOfSurvivor[4]; @@ -114,24 +103,63 @@ namespace Client mapFlag = true; } } - - //private void Playback(string fileName, double pbSpeed = 2.0) - //{ - // var pbClient = new PlaybackClient(fileName, pbSpeed); - // int[,]? map; - // if ((map = pbClient.ReadDataFromFile(dataDict, drawPicLock)) != null) - // { - // isClientStocked = false; - // isPlaybackMode = true; - // defaultMap = map; - // mapFlag = true; - // } - // else - // { - // MessageBox.Show("Failed to read the playback file!"); - // isClientStocked = true; - // } - //} + private void ReactToCommandline() + { + string[] args = Environment.GetCommandLineArgs(); + if (args.Length == 2) + { + Playback(args[1]); + return; + } + _ = Parser.Default.ParseArguments(args).WithParsed(o => + { options = o; }); + if (options == null || options.cl == false) + { + OnReceive(); + } + else + { + if (options.PlaybackFile == DefaultArgumentOptions.FileName) + { + try + { + OnReceive(); + string[] comInfo = new string[5]; + comInfo[0] = options.Ip; + comInfo[1] = options.Port; + comInfo[2] = options.PlayerID; + comInfo[3] = options.PlayerType; + comInfo[4] = options.Occupation; + ConnectToServer(comInfo); + } + catch + { + OnReceive(); + } + } + else + { + Playback(options.PlaybackFile, options.PlaybackSpeed); + } + } + } + private void Playback(string fileName, double pbSpeed = 2.0) + { + var pbClient = new PlaybackClient(fileName, pbSpeed); + int[,]? map; + if ((map = pbClient.ReadDataFromFile(listOfProp, listOfHuman, listOfButcher, listOfBullet,listOfBombedBullet, listOfAll, listOfChest, listOfClassroom,listOfDoor, listOfGate, drawPicLock)) != null) + { + isClientStocked = false; + isPlaybackMode = true; + defaultMap = map; + mapFlag = true; + } + else + { + MessageBox.Show("Failed to read the playback file!"); + isClientStocked = true; + } + } // 连接Server,comInfo[]的格式:0-ip 1- port 2-playerID 3-human/TrickerType 4-occupation private void ConnectToServer(string[] comInfo) @@ -1398,5 +1426,6 @@ namespace Client { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 }}; private string[] comInfo = new string[5]; + ArgumentOptions? options = null; } } diff --git a/logic/Client/PlaybackClient.cs b/logic/Client/PlaybackClient.cs index 8dbf1f4..5b9d999 100644 --- a/logic/Client/PlaybackClient.cs +++ b/logic/Client/PlaybackClient.cs @@ -16,12 +16,14 @@ namespace Client private readonly double playbackSpeed; private readonly int frameTimeInMilliseconds; public MessageReader? Reader; + private SemaphoreSlim sema; + public SemaphoreSlim Sema => sema; public PlaybackClient(string fileName, double playbackSpeed = 1.0, int frameTimeInMilliseconds = 50) { this.fileName = fileName; this.playbackSpeed = playbackSpeed; this.frameTimeInMilliseconds = frameTimeInMilliseconds; - //this.sema = new SemaphoreSlim(1, 1); + this.sema = new SemaphoreSlim(1, 1); try { Reader = new MessageReader(this.fileName); @@ -40,7 +42,7 @@ namespace Client { if (Reader == null) return null; - //Sema.Wait(); + Sema.Wait(); bool endFile = false; bool mapFlag = false; // 是否获取了地图 int[,] map = new int[50, 50]; @@ -239,7 +241,7 @@ namespace Client frame, () => { - //Sema.Release(); + Sema.Release(); //MessageBox.Show("Game Over!"); return 1; } @@ -248,7 +250,6 @@ namespace Client }) { IsBackground = true }.Start(); return map; - } } }