void IJob.Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; string tenantId = dataMap.GetString("tenantId"); int interactionId = System.Int32.Parse(dataMap.GetString("interactionId")); init(tenantId);//Set Up api tenant //current = new Ataque.Clases.Ataque();//we must do this trought refelection searching by interaction.intName Interaction interaction = api.getInteractionHandler().GetInteraction(interactionId); if (interaction.intName.Equals("Ataque")) { current = new Ataque.Clases.Ataque(); } else if (interaction.intName.Equals("Comercio")) { current = new Comercio.Clases.Comercio(); } IntState intState = api.getIntStateHandler().GetIntStateByInteraction(interactionId); requester = GetIntFromMeta(intState.requester); if (intState.receiverId == -1) { receiver = GetIntFromMeta(intState.receiver); } else { receiver = GetIntFromId(intState.receiverId); } if (intState.state == SharedEntities.Enum.InteractionState.EXECUTING)//look for interaction state { int round = 1; List <IInteractionable> list = current.exec(requester, receiver); ApplyChanges(list); IntState state = GetIntState(interactionId, receiver, requester); state.state = SharedEntities.Enum.InteractionState.FINISHING; api.getIntStateHandler().SaveIntState(state); float time = 1; IInteractionable i = list.Where(c => c.mustSend() || c.getReturn()).OrderByDescending(c => c.getReturn()).FirstOrDefault(); if (i != null) { IDestacamento des = i.GetFlota().OrderByDescending(c => c.GetVelocidad()).FirstOrDefault(); time = GetDistanceBetweenColony(requester.GetID(), receiver.GetID()) / des.GetVelocidad(); } Scheduler.Scheduler.ScheduleInteraction(interactionId, time, tenantId, round); // testExec(interactionId); } else { List <IInteractionable> list = current.finalize(requester, receiver); IntState state = GetIntState(interactionId, receiver, requester); state.state = SharedEntities.Enum.InteractionState.FINISH; api.getIntStateHandler().SaveIntState(state); ApplyChanges(list); } }
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); }
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); } } }
private int FlotaAmount(IInteractionable i) { var amount = 0; i.GetFlota().ForEach((flota) => { amount += flota.GetAmount(); }); i.GetDefensas().ForEach((def) => { amount += def.GetAmount(); }); return(amount); }
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); }
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); }
internal void start() { List <IInteractionable> list = current.initialize(requester, receiver); int interactionId = InsertInteraction(requester.GetID(), receiver.GetID()); ApplyChanges(list); IntState state = GetIntState(interactionId, receiver, requester); api.getIntStateHandler().SaveIntState(state); float time = 1; IInteractionable i = list.Where(c => c.mustSend()).FirstOrDefault(); if (i != null) { /// calculo tiempos IDestacamento des = i.GetFlota().OrderByDescending(c => c.GetVelocidad()).FirstOrDefault(); time = GetDistanceBetweenColony(requester.GetID(), receiver.GetID()) / des.GetVelocidad(); } int round = 0; Scheduler.Scheduler.ScheduleInteraction(interactionId, time, tenantId, round); }
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 }); }