/// <summary>
 /// Timer qui sert a disconnect les joueurs
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     try
     {
         using (HugoWorldEntities context = new HugoWorldEntities())
         {
             List<Hero> heroes = context.Heroes.Where(h => h.Connected).ToList();
         
             foreach (Hero hero in heroes)
                 if (hero.LastActivity.Value + new TimeSpan(00, 01, 00) < DateTime.Now)
                     hero.Connected = false;
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
        void IHeroController.ConnectHero(int heroId)
        {
            using (HugoWorldEntities db = new HugoWorldEntities())
            {
                CompteJoueur compte = db.CompteJoueurs.Include("Heroes").FirstOrNull(c => c.Heroes.Any(h => h.Id == heroId));
                if (compte == null)
                    throw new FaultException("Error: the account wasn't found in the database");

                Hero hero = db.Heroes.FirstOrNull(h => h.Id == heroId);
                if (hero == null)
                    throw new FaultException("Error: the hero wasn't found in the database");
                else if (compte.Heroes.Any(h => h.Connected))
                    throw new FaultException("Error: there's already a connected hero on that account");
                else
                {
                    //list hero avec meme position et connecté
                    List<Hero> lstHeroes = context.Heroes.Where(h => h.x == hero.x && h.y == hero.y && h.Connected && h.Id != hero.Id).ToList();
                    if (lstHeroes.Count == 0)
                    {
                        hero.Connected = true;
                        hero.LastActivity = DateTime.Now;
                        db.SaveChanges();    
                    }
                    else
                    {
                        //get first position AREA 0;0 that isnt full
                        for (int x = 0; x < 8; x++)
                            for (int y = 0; y < 8; y++)
                                if (lstHeroes.Count(h => h.x == x && h.y == y) == 0)
                                    lock (context.Items)
                                        if (context.Items.Count(i => i.x == x && i.y == y) == 0)
                                            lock (context.ObjetMondes)
                                                if (context.ObjetMondes.Count(o => o.x == x && o.y == y) == 0)
                                                    lock (context.Monstres)
                                                        if (context.Monstres.Count(m => m.x == x && m.y == y) == 0)
                                                        {
                                                            hero.x = x;
                                                            hero.y = y;
                                                            hero.Connected = true;
                                                            hero.LastActivity = DateTime.Now;
                                                            db.SaveChanges();
                                                            return;
                                                        }
                        throw new FaultException("Error: too many accounts are login...\nRetry later because the first area is currently full.");                       
                    }
                }
            }
            
        }
        /// <summary>
        /// Auteur Francis Lussier
        /// permet de crée un héro
        /// </summary>
        void IHeroController.CreateHero(int MondeID, int compteId, int classeId, int X, int Y, int niveau, int dex, int str, int stamina, int Int, long experience, decimal argent, string name)
        {
            var Monde = context.Mondes.FirstOrNull(c => c.Id == MondeID);
            if (Monde == null)
                return;

            Classe classe = context.Classes.FirstOrNull(c => c.Id == classeId);
            CompteJoueur compte = context.CompteJoueurs.FirstOrNull(c => c.Id == compteId);
            if (classe == null || compte == null)
                return;

            while (true)
            {
                try
                {


                    using (HugoWorldEntities db = new HugoWorldEntities())
                    {
                        Hero hero = new Hero()
                        {
                            //Id = id,
                            MondeId = MondeID,
                            Argent = argent,
                            ClasseId = classeId,
                            CompteJoueurId = compteId,
                            Experience = experience,
                            Niveau = niveau,
                            StatBaseDex = dex,
                            StatBaseInt = Int,
                            StatBaseStam = stamina,
                            StatBaseStr = str,
                            Connected = false,
                            Name = name,
                            x = X,
                            y = Y,
                            LastActivity = DateTime.Now,
                            PV = stamina*10
                        };

                        db.Heroes.Add(hero);
                        db.SaveChanges();
                        break;
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }