/// <summary>
        /// Building assets takes a number of gameticks until finished.
        /// </summary>
        public void BuildAsset(BuildAssetCommand command)
        {
            // TODO: synchronize
            var assetDef = gameDef.GetAssetDef(command.AssetDefId);

            if (assetDef == null)
            {
                throw new AssetNotFoundException(command.AssetDefId);
            }
            if (assetRepository.HasAsset(command.PlayerId, assetDef.Id))
            {
                throw new AssetAlreadyBuiltException(assetDef.Id);
            }
            if (assetRepository.IsBuildQueued(command.PlayerId, command.AssetDefId))
            {
                throw new AssetAlreadyQueuedException(assetDef.Id);
            }
            if (!assetRepository.PrerequisitesMet(command.PlayerId, assetDef))
            {
                throw new PrerequisitesNotMetException("too bad");
            }
            resourceRepositoryWrite.DeductCost(command.PlayerId, assetDef.Cost);
            var dueTick = world.GetTargetGameTick(assetDef.BuildTimeTicks);

            actionQueueRepository.AddAction(new GameAction {
                Name       = AssetBuildActionConstants.Name,
                PlayerId   = command.PlayerId,
                DueTick    = dueTick,
                Properties = new Dictionary <string, string> {
                    { AssetBuildActionConstants.AssetDefId, command.AssetDefId.Id }
                }
            });
        }
Beispiel #2
0
        /// <summary>
        /// Building units happens immediately. Throws exceptions if prerequisites are not met.
        /// </summary>
        public void BuildUnit(BuildUnitCommand command)
        {
            // TODO: synchronize
            var unitDef = gameDef.GetUnitDef(command.UnitDefId);

            if (unitDef == null)
            {
                throw new UnitDefNotFoundException(command.UnitDefId);
            }
            if (!unitRepository.PrerequisitesMet(command.PlayerId, unitDef))
            {
                throw new PrerequisitesNotMetException("too bad");
            }

            resourceRepositoryWrite.DeductCost(command.PlayerId, unitDef.Cost.Multiply(command.Count));
            AddUnit(command.PlayerId, command.UnitDefId, command.Count);
        }