public ActionResult New(InfrastructureForm form)
        {
            Debug.WriteLine(string.Format("POST: Infrastructure Controller: New - gameID={0}", GameState.GameID));
            var game = GameState.Game;

            DB_infrastructure infrastructure = new DB_infrastructure();

            infrastructure.game_id     = game.Info.id;
            infrastructure.name        = form.Name;
            infrastructure.description = form.Description;
            infrastructure.rp_cost     = form.RPCost;

            infrastructure.is_colony   = form.IsColony;
            infrastructure.is_military = form.IsMilitary;

            infrastructure.base_health         = form.BaseHealth;
            infrastructure.base_regeneration   = form.BaseRegeneration;
            infrastructure.base_attack         = form.BaseAttack;
            infrastructure.base_special_attack = form.BaseSpecialAttack;
            infrastructure.base_agility        = form.BaseAgility;

            infrastructure.influence_bonus = form.InfluenceBonus;

            infrastructure.rp_bonus                   = form.RPBonus;
            infrastructure.science_bonus              = form.ScienceBonus;
            infrastructure.ship_construction_bonus    = form.ShipConstructionBonus;
            infrastructure.colonial_development_bonus = form.ColonialDevelopmentBonus;
            infrastructure.unit_training_bonus        = form.UnitTrainingBonus;

            infrastructure.research_slots             = form.ResearchSlots;
            infrastructure.ship_construction_slots    = form.ShipConstructionSlots;
            infrastructure.colonial_development_slots = form.ColonialDevelopmentSlots;
            infrastructure.unit_training_slots        = form.UnitTrainingSlots;

            infrastructure.gmnotes = form.GMNotes;
            Database.Session.Save(infrastructure);

            foreach (var possibleUpgrade in form.PossibleUpgrades)
            {
                if (possibleUpgrade.IsChecked)
                {
                    DB_infrastructure_upgrades upgrade = new DB_infrastructure_upgrades();
                    upgrade.game_id       = game.Info.id;
                    upgrade.from_infra_id = infrastructure.id;
                    upgrade.to_infra_id   = possibleUpgrade.ID;
                    Database.Session.Save(upgrade);
                }
            }

            Database.Session.Flush();
            return(RedirectToRoute("Statistics"));
        }
        public ActionResult Edit(InfrastructureForm form)
        {
            Debug.WriteLine(string.Format("POST: Infrastructure Controller: Edit - infrastructureID={0}", form.ID));
            var game = GameState.Game;

            DB_infrastructure infrastructure = game.GameStatistics.InfrastructureRaw.Find(x => x.id == form.ID);

            if (infrastructure.game_id == null || infrastructure.game_id != game.Info.id)
            {
                return(RedirectToRoute("game", new { gameID = game.Info.id }));
            }

            infrastructure.name        = form.Name;
            infrastructure.description = form.Description;
            infrastructure.rp_cost     = form.RPCost;

            infrastructure.is_colony   = form.IsColony;
            infrastructure.is_military = form.IsMilitary;

            infrastructure.base_health         = form.BaseHealth;
            infrastructure.base_regeneration   = form.BaseRegeneration;
            infrastructure.base_attack         = form.BaseAttack;
            infrastructure.base_special_attack = form.BaseSpecialAttack;
            infrastructure.base_agility        = form.BaseAgility;

            infrastructure.influence_bonus = form.InfluenceBonus;

            infrastructure.rp_bonus                   = form.RPBonus;
            infrastructure.science_bonus              = form.ScienceBonus;
            infrastructure.ship_construction_bonus    = form.ShipConstructionBonus;
            infrastructure.colonial_development_bonus = form.ColonialDevelopmentBonus;
            infrastructure.unit_training_bonus        = form.UnitTrainingBonus;

            infrastructure.research_slots             = form.ResearchSlots;
            infrastructure.ship_construction_slots    = form.ShipConstructionSlots;
            infrastructure.colonial_development_slots = form.ColonialDevelopmentSlots;
            infrastructure.unit_training_slots        = form.UnitTrainingSlots;

            infrastructure.gmnotes = form.GMNotes;
            Database.Session.Update(infrastructure);

            var infrastructureUpgrades = Database.Session.Query <DB_infrastructure_upgrades>()
                                         .Where(x => x.from_infra_id == infrastructure.id)
                                         .ToList();

            var checkedUpgrades = form.PossibleUpgrades
                                  .Where(x => x.IsChecked)
                                  .ToList();

            List <DB_infrastructure_upgrades> toRemove = new List <DB_infrastructure_upgrades>();
            List <Checkbox> toAdd = new List <Checkbox>();

            foreach (var infrastructureUpgrade in infrastructureUpgrades)
            {
                bool foundMatch = false;

                // First determine what to remove
                foreach (var checkBox in checkedUpgrades)
                {
                    // Infrastructure Upgrade is already set
                    if (infrastructureUpgrade.to_infra_id == checkBox.ID)
                    {
                        foundMatch = true;
                        break;
                    }
                }

                // No longer an Upgrade to this Infrastructure
                if (!foundMatch)
                {
                    toRemove.Add(infrastructureUpgrade);
                }
            }

            // Next determine what is new
            foreach (var checkBox in checkedUpgrades)
            {
                bool foundMatch = false;
                foreach (var infrastructureUpgrade in infrastructureUpgrades)
                {
                    // Infrastructure Upgrade is already set
                    if (checkBox.ID == infrastructureUpgrade.to_infra_id)
                    {
                        foundMatch = true;
                        break;
                    }
                }

                // We have a new upgrade
                if (!foundMatch)
                {
                    toAdd.Add(checkBox);
                }
            }

            // Now apply the changes
            foreach (var remove in toRemove)
            {
                Database.Session.Delete(remove);
            }
            foreach (var add in toAdd)
            {
                Database.Session.Save(new DB_infrastructure_upgrades(infrastructure.id, add.ID, game.Info.id));
            }

            Database.Session.Flush();
            return(RedirectToRoute("Statistics"));
        }