|
- using CommandLine;
- using Grpc.Core;
- using Protobuf;
-
- namespace Server
- {
- public class Program
- {
- /// <summary>
- /// Generated by http://www.network-science.de/ascii/ with font "standard"
- /// </summary>
- const string welcome =
- @"
-
- _____ _ _ _ _ _ ___ __
- |_ _| | | | | | | / \ |_ _/ /_
- | | | |_| | | | |/ _ \ | | '_ \
- | | | _ | |_| / ___ \ | | (_) |
- |_| |_| |_|\___/_/ \_\___\___/
-
- ____ _ _ ____ _ _ _
- / ___|_ __ __ _ __| |_ _ __ _| |_ ___ / ___(_)_ __| |___| |
- | | _| '__/ _` |/ _` | | | |/ _` | __/ _ \ | | _| | '__| / __| |
- | |_| | | | (_| | (_| | |_| | (_| | || __/_ | |_| | | | | \__ \_|
- \____|_| \__,_|\__,_|\__,_|\__,_|\__\___( ) \____|_|_| |_|___(_)
-
-
- ";
- // 特别说明:在 .NET 7 及以上,C# 支持新的多行字符串,允许多行前面缩进,因此可以不必再定格写字符串,
- // 即升级 .NET 版本后可以改为如下的:
- // const string welcome = """
- //
- // _____ _ _ _ _ _ ___ __
- // |_ _| | | | | | | / \ |_ _/ /_
- // | | | |_| | | | |/ _ \ | | '_ \
- // | | | _ | |_| / ___ \ | | (_) |
- // |_| |_| |_|\___/_/ \_\___\___/
- //
- // ____ _ _ ____ _ _ _
- // / ___|_ __ __ _ __| |_ _ __ _| |_ ___ / ___(_)_ __| |___| |
- // | | _| '__/ _` |/ _` | | | |/ _` | __/ _ \ | | _| | '__| / __| |
- // | |_| | | | (_| | (_| | |_| | (_| | || __/_ | |_| | | | | \__ \_|
- // \____|_| \__,_|\__,_|\__,_|\__,_|\__\___( ) \____|_|_| |_|___(_)
- //
- //
- // """; // 将以此结束符为基准开始缩进(但 Python 没有这个 feature,差评 x)
-
- static ServerBase CreateServer(ArgumentOptions options)
- {
- return options.Playback ? new PlaybackServer(options) : new GameServer(options);
- }
-
- static int Main(string[] args)
- {
- foreach (var arg in args)
- {
- Console.Write($"{arg} ");
- }
- Console.WriteLine();
-
- ArgumentOptions? options = null;
- _ = Parser.Default.ParseArguments<ArgumentOptions>(args).WithParsed(o => { options = o; });
- if (options == null)
- {
- Console.WriteLine("Argument parsing failed!");
- return 1;
- }
-
- if (options.StartLockFile == DefaultArgumentOptions.FileName)
- {
- Console.WriteLine(welcome);
- }
- Console.WriteLine("Server begins to run: " + options.ServerPort.ToString());
-
- try
- {
- var server = CreateServer(options);
- Grpc.Core.Server rpcServer = new Grpc.Core.Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
- {
- Services = { AvailableService.BindService(server) },
- Ports = { new ServerPort(options.ServerIP, options.ServerPort, ServerCredentials.Insecure) }
- };
- rpcServer.Start();
-
- Console.WriteLine("Server begins to listen!");
- server.WaitForEnd();
- Console.WriteLine("Server end!");
- rpcServer.ShutdownAsync().Wait();
-
- Thread.Sleep(50);
- Console.WriteLine("");
- Console.WriteLine("=================== Final Score ====================");
- Console.WriteLine($"Studnet: {server.GetScore()[0]}");
- Console.WriteLine($"Tricker: {server.GetScore()[1]}");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- Console.WriteLine(ex.StackTrace);
- }
- return 0;
- }
- }
- }
|