private MapMessage GetMapUpdate(Player player)
        {
            Position segment = player.Position.GetSegment(Settings.Default.ChunkSize);

            var startx = (segment.X - 1) * 20;
            var starty = (segment.Y - 1) * 20;
            if (startx < 0) startx = 0;
            if (starty < 0) starty = 0;

            Position startPos = new Position(startx, starty);
            string message = String.Empty;

            var endx = (segment.X + 2) * 20;
            var endy = (segment.Y + 2) * 20;

            for (int y = starty; y < endy - 1; y++)
            {
                for (int x = startx; x < endx - 1; x++)
                {
                    Position pos = new Position(x, y);
                    message += (int)World.Instance.Map.GetTile(pos);
                    message += ".";
                    message += (int)World.Instance.Map.GetObject(pos);
                    message += ",";
                }
            }

            message = message.Remove(message.Length - 1, 1);

            return new MapMessage(startPos, message);
        }
        private static bool ConnectionRequested(IRemoteConnection sender, string request, out string response)
        {
            Player player = new Player(request, sender);
            //player.Position = new Position(200, 150);
            player.Position = new Position(200, 175);
            //player.Position = new Position(35, 35);

            PLAYERS.Add(sender, player);
            World.Instance.AddPlayer(player);
            GlobalServer.Instance.AddPlayer(player.Id, sender);

            response = "OK:" + player.Id;

            return true;
        }
        private string GetTeamUpdate(Player player)
        {
            string message = "team:";

            foreach (BattleEntity pokemon in player.Team)
            {
                message += pokemon.PokedexId;
                message += ".";
                message += pokemon.Level;
                message += ",";
            }

            message = message.Remove(message.Length - 1, 1);

            return message;
        }
        public bool AddPlayer(Player player)
        {
            if (!_players.ContainsKey(player.Id))
            {
                _players.Add(player.Id, player);

                while (!VisibleEntities.Add(player, player.Position))
                {
                    player.Position = new Position(player.Position, DirectionUtils.RandomDirection());
                }

                return true;
            }

            return false;
        }
 protected void SendMessage(Player player, BaseMessage message)
 {
     player.RemoteConnection.Send(message.ToString());
 }
        private List<PositionEntity> GetVisibleEntities(Player player)
        {
            var lineOfSight = Settings.Default.LineOfSight;

            var visibleEntites = new List<PositionEntity>();

            for (int x = Math.Max(player.Position.X - lineOfSight, 0); x < Math.Min(player.Position.X + lineOfSight, World.Instance.Map.Size); x++)
            {
                for (int y = Math.Max(player.Position.Y - lineOfSight, 0); y < Math.Min(player.Position.Y + lineOfSight, World.Instance.Map.Size); y++)
                {
                    var entity = World.Instance.VisibleEntities[x, y];

                    if (entity != null)
                    {
                        if (entity.Type == EntityType.Player)
                        {
                            visibleEntites.Add(new PositionEntity(entity.Id, entity.Position, entity.Type, entity.Direction, 0, entity.State));
                        }
                        else if (entity.Type == EntityType.Pokemon)
                        {
                            var pokedexId = (entity as Pokemon).PokedexId;
                            visibleEntites.Add(new PositionEntity(entity.Id, entity.Position, entity.Type, entity.Direction, pokedexId, entity.State));
                        }
                    }
                }
            }

            return visibleEntites;
        }
        public bool PlayTrainerAction(Player player, Position target, Action action, int index)
        {
            if (action.Id == (int)TrainerAction.End_Battle && GetPlayerNbPokemons(player.Id) == 0)
            {
                actionId++;

                EndPlayerBattle(player.Id);

                if (action.NextTurn)
                {
                    NextTurn();
                }

                foreach (int id in players)
                {
                    var message = ToNoActionMessage(id);//TODO
                    GlobalServer.Instance.SendMessage(id, message);
                }

                playIATurns();

                return true;
            }
            else if (action.Id == (int)TrainerAction.Pokemon_Go)
            {
                if (!player.Team[index].InBattle)
                {
                    BattleEntity battleEntity = player.Team[index];
                    battleEntity.BattleId = entityIdSequence++;
                    battleEntity.CurrentPos = target;
                    battleEntity.Ready = false;
                    turns.Add(battleEntity);

                    bool ok = true;
                    foreach (int id in players)
                    {
                        if (!spectators.Contains(id))
                        {
                            if (GetPlayerNbPokemons(id) == 0)
                            {
                                ok = false;
                            }
                        }
                    }

                    if (ok)
                    {
                        actionId++;
                        WaitingPokemonGo = false;

                        NextTurn();

                        foreach (int id in players)
                        {
                            var message = ToNoActionMessage(id);//TODO
                            GlobalServer.Instance.SendMessage(id, message);
                        }

                        playIATurns();
                    }

                    return true;
                }
            }
            else if (action.Id == (int)TrainerAction.Pokemon_Come_Back)
            {

                var entity = GetEntity(target);
                if (entity != null && entity.PlayerId == player.Id)
                {
                    entity.ComingBack = true;

                    actionId++;
                    if (GetPlayerNbPokemons(player.Id) == 0)
                    {
                        WaitingPokemonGo = true;
                    }

                    foreach (int id in players)
                    {
                        var message = ToNoActionMessage(id);//TODO
                        GlobalServer.Instance.SendMessage(id, message);
                    }

                    return true;
                }
            }
            else if (action.Id == (int)TrainerAction.Pokeball)
            {
                var entity = GetEntity(target);
                if (entity != null && entity.PlayerId < 0)
                {
                    var indexof = turns.IndexOf(entity);
                    turns.Remove(entity);
                    if (indexof <= currentTurn)
                    {
                        currentTurn--;
                        if (currentTurn < 0)
                        {
                            currentTurn = 0;
                        }
                    }

                    player.Team.AddPokemon(new BattleEntity(-1, entity.PokedexId, player.Id)); //TODO boîte
                    player.TeamToUpdate = true;

                    World.Instance.Population.Remove(entity.WorldId);
                }

                actionId++;

                NextTurn();

                foreach (int id in players)
                {
                    var message = ToNoActionMessage(id);//TODO
                    GlobalServer.Instance.SendMessage(id, message);
                }

                playIATurns();
            }
            else if (action.Id == (int)TrainerAction.End_Turn)
            {
                actionId++;

                NextTurn();

                foreach (int id in players)
                {
                    var message = ToNoActionMessage(id);
                    GlobalServer.Instance.SendMessage(id, message);
                }

                playIATurns();
            }

            return false;
        }