Exemplo n.º 1
0
        public async Task StartBuildingAsync(string username, int buildingId)
        {
            var country = await _context.Countries.Include(c => c.InProgressBuildings)
                          .SingleAsync(c => c.ParentUser.UserName == username);

            if (country.InProgressBuildings.Count > 0)
            {
                throw new InProgressException("The country already has a building in progress.");
            }

            var buildingType = await _context.BuildingTypes.FindAsync(buildingId);

            if (buildingType == null)
            {
                throw new ArgumentOutOfRangeException(nameof(buildingId), "No such building id.");
            }

            if (buildingType.CostPearl > country.Pearls || buildingType.CostCoral > country.Corals)
            {
                throw new InvalidOperationException("Not enough money");
            }

            country.Pearls -= buildingType.CostPearl;
            country.Corals -= buildingType.CostCoral;

            var inProgressBuilding = new InProgressBuilding
            {
                ParentCountry = country,
                Building      = buildingType,
                TimeLeft      = KnownValues.DefaultBuildingTime
            };

            _context.InProgressBuildings.Add(inProgressBuilding);
            await _context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        public async Task StartBuildingAsync(string username, int buildingId, CancellationToken turnEndWaitToken)
        {
            using (var lck = await _turnEndLock.ReaderLockAsync(turnEndWaitToken))
            {
                var country = await _context.Countries.Include(c => c.InProgressBuildings)
                              .ThenInclude(b => b.Building)
                              .SingleAsync(c => c.ParentUser.UserName == username);

                if (country.InProgressBuildings.Any(b => b.Building.Id == buildingId))
                {
                    throw new InProgressException("There is already a building of this type in progress.");
                }

                var buildingType = await _context.BuildingTypes.FindAsync(buildingId);

                if (buildingType == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(buildingId), "No such building id.");
                }

                if (buildingType.CostPearl > country.Pearls || buildingType.CostCoral > country.Corals)
                {
                    throw new InvalidOperationException("Not enough money");
                }

                country.Pearls -= buildingType.CostPearl;
                country.Corals -= buildingType.CostCoral;

                var inProgressBuilding = new InProgressBuilding
                {
                    ParentCountry = country,
                    Building      = buildingType,
                    TimeLeft      = KnownValues.DefaultBuildingTime
                };
                _context.InProgressBuildings.Add(inProgressBuilding);
                await _context.SaveChangesAsync();
            }
        }