public bool BuildHut(int villageId, int amount)
        {
            using (var uow = UnitOfWorkProvider.Create())
            {
                var village = villageRepository.GetById(villageId, s => s.Player);

                if (village == null)
                {
                    throw new NullReferenceException("Building service - BuildHut(...) village cant be null");
                }

                resourceListQuery.Filter = new ResourceFilter {
                    ResourceType = "Wood", VillageId = villageId
                };
                var wood = resourceRepository.GetById(resourceListQuery.Execute().SingleOrDefault().ID, r => r.ResourceType, r => r.Village);

                /*if (wood == null)
                 * {
                 *  throw new NullReferenceException("Building Service - BuildHut(...) wood cant be null");
                 * }*/

                resourceListQuery.Filter = new ResourceFilter {
                    ResourceType = "Stone", VillageId = villageId
                };
                var stone = resourceRepository.GetById(resourceListQuery.Execute().SingleOrDefault().ID, r => r.ResourceType, r => r.Village);

                /*if (stone == null)
                 * {
                 *  throw new NullReferenceException("Building Service - BuildHut(...) stone cant be null");
                 * }*/

                if ((wood.Amount < (village.Huts * 100 + 100) * amount) || (stone.Amount < (village.Huts * 60 + 60) * amount))
                {
                    return(false);
                }

                wood.Amount             -= (village.Huts * 100 + 100) * amount;
                stone.Amount            -= (village.Huts * 60 + 60) * amount;
                village.Huts            += amount;
                village.AvailableWorkers = village.AvailableWorkers += 4 * amount;
                resourceRepository.Update(wood);
                resourceRepository.Update(stone);
                villageRepository.Update(village);
                uow.Commit();
                return(true);
            }
        }
Ejemplo n.º 2
0
        //vytvori village, resources, buildings, products, units pre village
        //pre kazdy typ budovy budovu, pre kazdy typ res res atd

        public void CreateVillage(int playerID)
        {
            var village = new Village();

            using (var uow = UnitOfWorkProvider.Create())
            {
                var player = playerRepository.GetById(playerID, p => p.Villages, p => p.Account);
                player.NumOfVillages++;

                village.Player           = player;
                village.Huts             = 1;
                village.AvailableWorkers = 4;
                if (player.Villages.Count() == 0)
                {
                    village.Number = 1;
                }
                else
                {
                    village.Number = player.Villages.Last().Number + 1;
                }
                villageRepository.Insert(village);
                playerRepository.Update(player);
                uow.Commit();
            }

            using (var uow = UnitOfWorkProvider.Create())
            {
                var v = villageRepository.GetById(village.ID);

                var        resourceTypesCount = resourceTypeListQuery.GetTotalRowCount();
                Resource[] resources          = new Resource[resourceTypesCount];

                //takto, alebo i = 0  inicializovat, i++ inicializovat a for od 2 //opakovanie kodu, ci efektivita?
                for (int i = 0; i < resourceTypesCount; ++i)
                {
                    resources[i] = new Resource();
                    resources[i].ResourceType = resourceTypeRepository.GetById(i + 1);

                    if (i == 0)
                    {
                        resources[i].Amount = 90;
                    }

                    if (i == 1)
                    {
                        resources[i].Amount = 50;
                    }

                    resources[i].Village = v;
                    resourceRepository.Insert(resources[i]);
                }

                var        buildingTypesCount = buildingTypeListQuery.GetTotalRowCount();
                var        buildingTypes      = buildingTypeRepository.GetByIds(Enumerable.Range(1, buildingTypesCount).ToArray()); // 2. Sposob na priradovanie type
                Building[] buildings          = new Building[buildingTypesCount];

                for (int i = 0; i < buildingTypesCount; ++i)
                {
                    buildings[i] = new Building();
                    buildings[i].BuildingType = buildingTypes[i];   //2. sposob
                    buildings[i].Village      = v;
                    buildingRepository.Insert(buildings[i]);
                }


                var       productTypesCount = productTypeListQuery.GetTotalRowCount();
                Product[] products          = new Product[productTypesCount];

                for (int i = 0; i < productTypesCount; ++i)
                {
                    products[i]             = new Product();
                    products[i].ProductType = productTypeRepository.GetById(i + 1);
                    products[i].Village     = v;
                    productRepository.Insert(products[i]);
                }


                var unitTypesCount = unitTypeListQuery.Execute().Where(u => u.Role.Equals(UnitRole.Friendly)).Count();    //friendly/hostile units

                Unit[] units = new Unit[unitTypesCount];
                for (int i = 0; i < unitTypesCount; ++i)
                {
                    units[i]          = new Unit();
                    units[i].UnitType = unitTypeRepository.GetById(i + 1);
                    units[i].Village  = v;
                    unitRepository.Insert(units[i]);
                }

                var         adventureTypesCount = adventureTypeListQuery.GetTotalRowCount();
                var         adventureTypes      = adventureTypeRepository.GetByIds(Enumerable.Range(1, adventureTypesCount).ToArray()); // 2. Sposob na priradovanie type
                Adventure[] adventures          = new Adventure[adventureTypesCount];

                for (int i = 0; i < adventureTypesCount; ++i)
                {
                    adventures[i] = new Adventure();
                    adventures[i].AdventureType = adventureTypes[i];   //2. sposob
                    adventures[i].Village       = v;
                    adventureRepository.Insert(adventures[i]);
                }

                uow.Commit();
            }
        }