public static void SaveToPool(Battlegroup bg) { using (var db = new MinionWarsEntities()) { EvolutionPool ep = new EvolutionPool(); Random r = new Random(); List <BattlegroupAssignment> bga = db.BattlegroupAssignment.Where(x => x.battlegroup_id == bg.id).ToList(); ep.minion_id = bga.OrderBy(x => r.Next()).First().minion_id; ep.last_location = bg.location; ep.stored_date = DateTime.Now; db.EvolutionPool.Add(ep); db.SaveChanges(); } }
public static Minion generateGeneticMinion(EvolutionPool ep1, EvolutionPool ep2) { using (var db = new MinionWarsEntities()) { Minion minion = new Minion(); Minion parent1 = db.Minion.Find(ep1.minion_id); Minion parent2 = db.Minion.Find(ep2.minion_id); Random r = new Random(); int p1gene = -1; int p2gene = -1; do { p1gene = r.Next(0, 5); p2gene = r.Next(0, 5); } while (p1gene == p2gene); if (p1gene == 0) { minion.mtype_id = parent1.mtype_id; } else if (p2gene == 0) { minion.mtype_id = parent2.mtype_id; } else { minion.mtype_id = r.Next(1, 16); } if (p1gene == 1) { minion.somatotype = parent1.somatotype; } else if (p2gene == 1) { minion.somatotype = parent2.somatotype; } else { minion.somatotype = SomatotypeGenerator(r); } minion.strength = Convert.ToInt32(db.ModifierCoeficients.Find(18).value); minion.dexterity = Convert.ToInt32(db.ModifierCoeficients.Find(19).value); minion.vitality = Convert.ToInt32(db.ModifierCoeficients.Find(20).value); CalculateStatsByType(minion); CalculateStatsByBuild(minion); CalculateSpeed(minion); //TODO: BEHAVIOR GENE minion.behaviour = r.Next(1, 16); if (p1gene == 2) { minion.melee_ability = parent1.melee_ability; } else if (p2gene == 2) { minion.melee_ability = parent2.melee_ability; } else { minion.melee_ability = r.Next(1, 16) + 2; } if (p1gene == 3) { minion.ranged_ability = parent1.ranged_ability; } else if (p2gene == 3) { minion.ranged_ability = parent2.ranged_ability; } else { minion.ranged_ability = r.Next(1, 16) + 2; } if (p1gene == 4) { minion.passive = parent1.passive; } else if (p2gene == 4) { minion.passive = parent2.passive; } else { minion.passive = r.Next(0, 15); } minion.speed = 1; List <Minion> checkMinion = null; checkMinion = db.Minion.Where(x => x.behaviour == minion.behaviour && x.melee_ability == minion.melee_ability && x.ranged_ability == minion.ranged_ability && x.passive == minion.passive && x.mtype_id == minion.mtype_id && x.somatotype.Equals(minion.somatotype)).ToList(); if (checkMinion.Count > 0) { return(checkMinion.First()); } else { db.Minion.Add(minion); db.SaveChanges(); } return(minion); } }