Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

EnvThread.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Threading;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Options;
  5. using uqac_ia_aspirobot.Common;
  6. using uqac_ia_aspirobot.Extensions;
  7. using uqac_ia_aspirobot.Interfaces;
  8. namespace uqac_ia_aspirobot.Environment
  9. {
  10. public class EnvThread
  11. {
  12. private readonly IEnvironment _environment;
  13. private readonly ArServer _server;
  14. private readonly EnvConfig _config;
  15. private readonly Random _rand;
  16. private enum Actions
  17. {
  18. NoAction = 0,
  19. AddDust = 1,
  20. AddJewel = 2,
  21. RemoveJewel = 3
  22. }
  23. private static Thread _thread;
  24. public static void Start(EnvConfig envConfig, ArConfig arConfig)
  25. {
  26. var envServices = new ServiceCollection();
  27. envServices.AddOptions();
  28. envServices.AddSingleton<EnvThread>();
  29. envServices.AddSingleton<ArServer>();
  30. envServices.AddSingleton<IEnvironment, EnvEnvironment>();
  31. envServices.AddTransient<IRoom, EnvRoom>();
  32. envServices.Configure<EnvConfig>(envConfig.CopyTo);
  33. envServices.Configure<ArConfig>(arConfig.CopyTo);
  34. var envProvider = envServices.BuildServiceProvider();
  35. var envThread = envProvider.GetService<EnvThread>();
  36. _thread = new Thread(envThread.Run)
  37. {
  38. Name = nameof(EnvThread)
  39. };
  40. _thread.Start();
  41. }
  42. public static void Join()
  43. {
  44. _thread.Join();
  45. }
  46. public EnvThread(IOptions<EnvConfig> config, IEnvironment environment, ArServer server)
  47. {
  48. _environment = environment;
  49. _server = server;
  50. _config = config.Value;
  51. _rand = new Random();
  52. }
  53. private bool GenerateProba(float proba)
  54. {
  55. return _rand.NextDouble() * 100.0 <= proba;
  56. }
  57. private Actions GenerateAction()
  58. {
  59. if (GenerateProba(_config.ActionPropability))
  60. {
  61. if (GenerateProba(_config.AddDustProbability))
  62. {
  63. return Actions.AddDust;
  64. }
  65. if (GenerateProba(_config.RemoveJewelProbability))
  66. {
  67. return Actions.RemoveJewel;
  68. }
  69. if (GenerateProba(_config.AddJewelProbability))
  70. {
  71. return Actions.AddJewel;
  72. }
  73. }
  74. return Actions.NoAction;
  75. }
  76. private void Run()
  77. {
  78. var running = true;
  79. _environment.Setup();
  80. _server.Setup();
  81. while (running)
  82. {
  83. Thread.Sleep(_config.SleepTime);
  84. var action = GenerateAction();
  85. if (action == Actions.AddDust || action == Actions.AddJewel)
  86. {
  87. var x = _rand.Next(0, _environment.GetWidth());
  88. var y = _rand.Next(0, _environment.GetWidth());
  89. if (action == Actions.AddDust)
  90. {
  91. _environment.AddDust(x, y);
  92. }
  93. else if (action == Actions.AddJewel)
  94. {
  95. _environment.AddJewel(x, y);
  96. }
  97. }
  98. else if (action == Actions.RemoveJewel)
  99. {
  100. _environment.ForeachRoom(room =>
  101. {
  102. if (room.State.HasFlag(RoomState.Jewel))
  103. {
  104. room.RemoveJewel();
  105. return false;
  106. }
  107. return true;
  108. });
  109. }
  110. }
  111. }
  112. }
  113. }