Exemplo n.º 1
0
 /// <summary>
 /// Ajout d'un nouveau client avec sa websocket
 /// </summary>
 /// <param name="webSocket"></param>
 /// <returns></returns>
 public static async Task AddBot(WebSocket webSocket)
 {
     OneBot client = new OneBot(webSocket);
     List<OneBot> toRemove = new List<OneBot>();
     //Console.WriteLine("un peu de ménage");
     lock (lockListBot)
     {
         // au cas où, on en profite pour faire le ménage
         foreach (OneBot o in AllBot)
         {
             if (o.State == BotState.Error || o.State == BotState.Disconnect)
                 toRemove.Add(o);
         }
         AllBot.Add(client);
     };
     // fin du ménage
     RefreshViewer();
     //Console.WriteLine("Do it!");
     foreach (OneBot o in toRemove)
         RemoveBot(o.ClientGuid);
     Console.WriteLine($"#bots: {AllBot.Count}");
   
     // on se met à l'écoute des messages de ce client
     await client.WaitReceive();
     // arrivé ici, c'est que le client s'est déconnecté
     // on se retire de la liste des clients websocket
     RemoveBot(client.ClientGuid);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Retrait d'un client
        /// on a surement perdu sa conenction
        /// </summary>
        /// <param name="guid">l'id du client qu'il faut enlever</param>
        public static void RemoveBot(Guid guid)
        {
            OneBot toRemove = null;

            lock (lockListBot)
            {
                foreach (OneBot o in AllBot)
                {
                    if (o.ClientGuid == guid)
                    {
                        toRemove = o;
                        break;
                    }
                }
                if (toRemove != null)
                {
                    AllBot.Remove(toRemove);
                }
            }
            if (toRemove != null)
            {
                ViewerRemovePlayer(toRemove.bot.X, toRemove.bot.Y);
                //RefreshViewer();
            }
            Console.WriteLine($"#bots: {AllBot.Count}");
        }
Exemplo n.º 3
0
 /// <summary>
 /// Exécute la simulation dans son ensemble !
 /// </summary>
 public static async void DoTurns()
 {
     if (turnRunning) return;
     turnRunning = true;
     Console.WriteLine("Running simulator...");
     int turnCount = 0;
     while (turnRunning)
     {
         //System.Diagnostics.Debug.WriteLine("One turns...");
         OneBot[] bots = null;
         int count = 0;
         lock (lockListBot)
         {
             count = AllBot.Count;
             if (count > 0)
             {
                 bots = new OneBot[count];
                 AllBot.CopyTo(bots);
             }
         }
         if (count == 0)
         {
             if (Settings.EndlessMode)
  
             {
                 // Disabled: Will spam the console until a bot joins.
                 // Console.WriteLine("Last bot left. Endless mode is active, continuing");
             }
             else
             {
                 Console.WriteLine("No more BOT, ending simulator.");
                 turnRunning = false;
             }
         }
         else
         {
             for (int i = 0; i < bots.Length; i++)
             {
                 Console.WriteLine($"Turn #{turnCount} Bot {bots[i].bot.Name}");
                 await bots[i].StartNewTurn();
                 DateTime start = DateTime.UtcNow;
                 while ((bots[i].State != BotState.Ready) && (DateTime.UtcNow - start).TotalSeconds < Settings.MaxDelaySecondByTurn)
                 {
                     Thread.Sleep(2);
                 }
                 if (bots[i].State != BotState.Ready)
                 {
                     // trop long, ajout pénalité !
                     // TODO: rien pour le moment
                 }
                 Thread.Sleep(Settings.DelayBetweenEachBotTurn);
             }
             // on génère de l'énergie si nécessaire
             MainGame.RefuelMap();
             turnCount++;
             if (turnCount % MainGame.Settings.EnergyPodLessEvery == 0)
             {
                 if (Settings.EnergyPodMax > Settings.EnergyPodMin)
                     Settings.EnergyPodMax--;
             }
         }
     }
     Console.WriteLine("End of running.");
 }