public string ToActionMessage(int player, Position target, Action action, Direction dir)
        {
            var message = "battleaction:";
            message += actionId;
            message += "=";
            message += CurrentAvailableActionsMessage(player);
            message += "=";
            message += ActionMessage(target, action, dir);
            message += "=";
            message += StateMessage();

            return message;
        }
        public bool PlayAction(Position target, Action action, Direction dir)
        {
            var entity = turns[currentTurn];
            if (entity.HP > 0 && !entity.ComingBack)
            {
                bool inRange = action.Range.InRange(arena, entity, target);
                if (action.Range2 != null && action.Range2.InRange(arena, entity, target))
                {
                    inRange = true;
                }

                if (inRange)
                {
                    if (action.ActionCost != null)
                    {
                        action.ActionCost.ApplyCost(entity, target);
                    }

                    foreach (GroundEffect effect in action.GroundEffects)
                    {
                        effect.apply(entity, target, dir, arena);
                    }

                    foreach (Position aoe in action.AoeTiles(entity, target, dir, arena))
                    {
                        foreach (BattleEntity pokemon in turns)
                        {
                            //todo ne pas toucher soi-même
                            if (aoe.Equals(pokemon.CurrentPos))
                            {
                                foreach (HitEffect effect in action.HitEffects)
                                {
                                    effect.apply(entity, pokemon, dir, arena);
                                }
                            }
                        }
                    }

                    actionId++;

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

                    foreach (int id in players)
                    {
                        var message = ToActionMessage(id, target, action, dir);
                        GlobalServer.Instance.SendMessage(id, message);
                    }

                    playIATurns();

                    return true;
                }
            }

            return false;
        }
        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;
        }
        public string ActionMessage(Position target, Action action, Direction dir)
        {
            string message = "";

            message += target.ToString() + ",";
            message += action.Id + ",";
            message += DirectionUtils.ToString(dir);

            return message;
        }