public InteractuableMetadata GetInteractionableMeta(IInteractionable r)
        {
            InteractuableMetadata meta = new InteractuableMetadata();

            meta.capacidad       = r.GetCapacidad();
            meta.defensa         = GetDestacamentoArrays(r.GetDefensas());
            meta.flota           = GetDestacamentoArrays(r.GetFlota());
            meta.recursos        = GetResourceArrays(r.GetRecursos());
            meta.interactuableID = r.GetID();
            return(meta);
        }
Beispiel #2
0
        private void PerformAttack(IDestacamento d, IInteractionable i, bool isReq)
        {
            int           AmountAttacks  = GetAtaquesEfectivos(d);
            List <Unidad> damagedUnits   = isReq ? FlotaAtacadaRequester : FlotaAtacadaReceiver;
            int           targetselector = AttackDamaged(damagedUnits, AmountAttacks, d.GetAtaque(), i);
            int           nexToAttack    = targetselector;

            if (targetselector > 0)
            {
                IEnumerable <IDestacamento> it      = i.GetFlota().Concat(i.GetDefensas()).OrderBy(c => c.GetEscudo());
                List <IDestacamento>        targets = it.TakeWhile((flota) =>
                {
                    if (targetselector == 0)
                    {
                        return(false);
                    }

                    if (targetselector <= flota.GetAmount())
                    {
                        targetselector = 0;
                        return(true);
                    }
                    else
                    {
                        targetselector -= flota.GetAmount();
                        return(true);
                    }
                }).ToList();
                targets.ForEach((t) => {
                    int index = 0;
                    int large = t.GetAmount();
                    while (nexToAttack > 0 && index < large)
                    {
                        Unidad u       = new Unidad(t);
                        bool destroyed = u.Hit(d.GetAtaque());
                        if (!destroyed)
                        {
                            damagedUnits.Add(u);
                        }
                        else
                        {
                            t.SetAmount(t.GetAmount() - 1);
                        }
                        index++;
                        nexToAttack--;
                    }
                });
                if (nexToAttack != 0)
                {
                    AttackDamaged(damagedUnits, nexToAttack, d.GetAtaque(), i);
                }
            }
        }
Beispiel #3
0
        private int FlotaAmount(IInteractionable i)
        {
            var amount = 0;

            i.GetFlota().ForEach((flota) => {
                amount += flota.GetAmount();
            });
            i.GetDefensas().ForEach((def) =>
            {
                amount += def.GetAmount();
            });
            return(amount);
        }
Beispiel #4
0
        private float GetInitiative(IInteractionable i)
        {
            Random rdm           = new Random();
            float  requesterDice = rdm.Next(1, 20);
            float  initiative    = 0;

            i.GetFlota().ForEach((flota) => {
                initiative += (flota.GetAmount() * flota.GetVelocidad() / 1000);
            });
            i.GetDefensas().ForEach((def) =>
            {
                initiative += (def.GetAmount() * def.GetEfectividad()) / 100;
            });
            return(requesterDice + initiative);
        }
Beispiel #5
0
        private int AttackDamaged(List <Unidad> DamagedUnits, int AmountAttacks, float damage, IInteractionable i)
        {
            int PerformedAttacks = 0;
            int index            = 0;

            while (DamagedUnits.Count > 0 && PerformedAttacks < AmountAttacks)
            {
                Unidad u         = DamagedUnits.ElementAt(index);
                bool   destroyed = u.Hit(damage);
                if (destroyed)
                {
                    IDestacamento d = i.GetDefensas().Concat(i.GetFlota()).Where(c => c.GetId() == u.GetId()).First <IDestacamento>();
                    d.SetAmount(d.GetAmount() - 1);
                    DamagedUnits.Remove(u);
                }
                else
                {
                    //DamagedUnits.Insert(index, u);
                }
                PerformedAttacks++;
                index = index + 1 > DamagedUnits.Count - 1 ? 0 : index + 1;
            }
            return(AmountAttacks - PerformedAttacks);
        }
Beispiel #6
0
        public List <IInteractionable> exec(IInteractionable requester, IInteractionable receiver)
        {
            receiver.GetFlota().ForEach((c) =>
            {
                destacamento.Add(c.GetId(), c.GetAmount());
            });
            receiver.GetDefensas().ForEach((c) =>
            {
                destacamento.Add(c.GetId(), c.GetAmount());
            });
            receiver.GetRecursos().ForEach((c) =>
            {
                recurso.Add(c.GetId(), c.GetAmount());
            });


            bool  requesterWin = FlotaAmount(receiver) == 0;
            float round        = 0;

            while (!requesterWin && FlotaAmount(requester) > 0)
            {
                System.Diagnostics.Debug.WriteLine("####Round" + round);
                round++;
                float receiverDice  = GetInitiative(receiver);
                float requesterDice = GetInitiative(requester);
                //restore shield
                FlotaAtacadaReceiver.ForEach((u) => { u.RestoreShield(); });
                FlotaAtacadaRequester.ForEach((u) => { u.RestoreShield(); });
                var FlotaRapidaRequester = requester.GetFlota().OrderBy(c => c.GetVelocidad() * c.GetAmount()).Where(c => c.GetAmount() != 0);
                var FlotaRapidaReceiver  = receiver.GetFlota().Concat(receiver.GetDefensas()).OrderBy(c => c.GetVelocidad() * c.GetAmount()).Where(c => c.GetAmount() != 0);;
                if (requesterDice >= receiverDice)
                {
                    FlotaRapidaRequester.ToList().ForEach((c) => {
                        PerformAttack(c, receiver, false);
                    });
                    FlotaRapidaReceiver.ToList().ForEach((c) => {
                        PerformAttack(c, requester, true);
                    });
                }
                else
                {
                    FlotaRapidaReceiver.ToList().ForEach((c) => {
                        PerformAttack(c, requester, true);
                    });
                    FlotaRapidaRequester.ToList().ForEach((c) => {
                        PerformAttack(c, receiver, false);
                    });
                }
                requesterWin = FlotaAmount(receiver) == 0;
            }
            if (requesterWin)
            {
                requester.Win();
                requester.GetFlota().ForEach((des) =>
                {
                    var capacidad = des.GetCapacidad() / receiver.GetRecursos().Count;
                    receiver.GetRecursos().ForEach((rec) =>
                    {
                        var setter = requester.GetRecursos().Where(c => c.GetId() == rec.GetId()).FirstOrDefault();
                        if (setter == null)
                        {
                            return;
                        }
                        if (rec.GetAmount() < capacidad)
                        {
                            setter.SetAmount(setter.GetAmount() + rec.GetAmount());
                            rec.SetAmount(0);
                        }
                        else
                        {
                            setter.SetAmount(setter.GetAmount() + capacidad);
                            rec.SetAmount(rec.GetAmount() - capacidad);
                        }
                    });
                });
                requester.Return();
            }
            else
            {
                receiver.Win();
            }

            destacamento.ToList().ForEach((r) => {
                IDestacamento defensa = receiver.GetDefensas().Where(c => c.GetId() == r.Key).FirstOrDefault();
                if (defensa != null)
                {
                    defensa.SetAmount(defensa.GetAmount() - r.Value);
                }
            });
            destacamento.ToList().ForEach((r) => {
                IDestacamento flota = receiver.GetFlota().Where(c => c.GetId() == r.Key).FirstOrDefault();
                if (flota != null)
                {
                    flota.SetAmount(flota.GetAmount() - r.Value);
                }
            });
            recurso.ToList().ForEach((r) => {
                IResources rec = receiver.GetRecursos().Where(c => c.GetId() == r.Key).FirstOrDefault();
                if (rec != null)
                {
                    rec.SetAmount(rec.GetAmount() - r.Value);
                }
            });
            receiver.SetMustUpdate(true);
            return(new List <IInteractionable> {
                requester, receiver
            });
        }