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.

Program.cs 4.1 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using CommandLine;
  2. using Grpc.Core;
  3. using Protobuf;
  4. namespace Server
  5. {
  6. public class Program
  7. {
  8. /// <summary>
  9. /// Generated by http://www.network-science.de/ascii/ with font "standard"
  10. /// </summary>
  11. const string welcome =
  12. @"
  13. _____ _ _ _ _ _ ___ __
  14. |_ _| | | | | | | / \ |_ _/ /_
  15. | | | |_| | | | |/ _ \ | | '_ \
  16. | | | _ | |_| / ___ \ | | (_) |
  17. |_| |_| |_|\___/_/ \_\___\___/
  18. ____ _ _ ____ _ _ _
  19. / ___|_ __ __ _ __| |_ _ __ _| |_ ___ / ___(_)_ __| |___| |
  20. | | _| '__/ _` |/ _` | | | |/ _` | __/ _ \ | | _| | '__| / __| |
  21. | |_| | | | (_| | (_| | |_| | (_| | || __/_ | |_| | | | | \__ \_|
  22. \____|_| \__,_|\__,_|\__,_|\__,_|\__\___( ) \____|_|_| |_|___(_)
  23. ";
  24. // 特别说明:在 .NET 7 及以上,C# 支持新的多行字符串,允许多行前面缩进,因此可以不必再定格写字符串,
  25. // 即升级 .NET 版本后可以改为如下的:
  26. // const string welcome = """
  27. //
  28. // _____ _ _ _ _ _ ___ __
  29. // |_ _| | | | | | | / \ |_ _/ /_
  30. // | | | |_| | | | |/ _ \ | | '_ \
  31. // | | | _ | |_| / ___ \ | | (_) |
  32. // |_| |_| |_|\___/_/ \_\___\___/
  33. //
  34. // ____ _ _ ____ _ _ _
  35. // / ___|_ __ __ _ __| |_ _ __ _| |_ ___ / ___(_)_ __| |___| |
  36. // | | _| '__/ _` |/ _` | | | |/ _` | __/ _ \ | | _| | '__| / __| |
  37. // | |_| | | | (_| | (_| | |_| | (_| | || __/_ | |_| | | | | \__ \_|
  38. // \____|_| \__,_|\__,_|\__,_|\__,_|\__\___( ) \____|_|_| |_|___(_)
  39. //
  40. //
  41. // """; // 将以此结束符为基准开始缩进(但 Python 没有这个 feature,差评 x)
  42. static ServerBase CreateServer(ArgumentOptions options)
  43. {
  44. return options.Playback ? new PlaybackServer(options) : new GameServer(options);
  45. }
  46. static int Main(string[] args)
  47. {
  48. foreach (var arg in args)
  49. {
  50. Console.Write($"{arg} ");
  51. }
  52. Console.WriteLine();
  53. ArgumentOptions? options = null;
  54. _ = Parser.Default.ParseArguments<ArgumentOptions>(args).WithParsed(o => { options = o; });
  55. if (options == null)
  56. {
  57. Console.WriteLine("Argument parsing failed!");
  58. return 1;
  59. }
  60. if (options.StartLockFile == DefaultArgumentOptions.FileName)
  61. {
  62. Console.WriteLine(welcome);
  63. }
  64. Console.WriteLine("Server begins to run: " + options.ServerPort.ToString());
  65. try
  66. {
  67. var server = CreateServer(options);
  68. Grpc.Core.Server rpcServer = new Grpc.Core.Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
  69. {
  70. Services = { AvailableService.BindService(server) },
  71. Ports = { new ServerPort(options.ServerIP, options.ServerPort, ServerCredentials.Insecure) }
  72. };
  73. rpcServer.Start();
  74. Console.WriteLine("Server begins to listen!");
  75. server.WaitForEnd();
  76. Console.WriteLine("Server end!");
  77. rpcServer.ShutdownAsync().Wait();
  78. Thread.Sleep(50);
  79. Console.WriteLine("");
  80. Console.WriteLine("=================== Final Score ====================");
  81. Console.WriteLine($"Studnet: {server.GetScore()[0]}");
  82. Console.WriteLine($"Tricker: {server.GetScore()[1]}");
  83. }
  84. catch (Exception ex)
  85. {
  86. Console.WriteLine(ex.ToString());
  87. }
  88. return 0;
  89. }
  90. }
  91. }