コード例 #1
0
ファイル: Combat.cs プロジェクト: jcsston/CosmoMonger
        /// <summary>
        /// Sends all the cargo on the ship into space.
        /// </summary>
        /// <param name="sourceShip">The source ship to throw the cargo out of.</param>
        private void SendCargoIntoSpace(Ship sourceShip)
        {
            CosmoMongerDbDataContext db = CosmoManager.GetDbContext();

            // We will unload all the cargo off of the source ship and move it into the combat 'space'
            foreach (ShipGood shipGood in sourceShip.ShipGoods.Where(g => g.Quantity > 0))
            {
                CombatGood good = (from g in this.CombatGoods
                                   where g.Good == shipGood.Good
                                   select g).SingleOrDefault();
                if (good == null)
                {
                    good        = new CombatGood();
                    good.Combat = this;
                    good.Good   = shipGood.Good;
                    db.CombatGoods.InsertOnSubmit(good);
                }

                // Into space the good goes...
                good.Quantity += shipGood.Quantity;

                // The good is no longer on the ship
                shipGood.Quantity = 0;
            }

            // Update the player stats on lost cargo
            Player shipPlayer = sourceShip.Players.SingleOrDefault();

            if (shipPlayer != null)
            {
                int cargoWorth = this.CombatGoods.Sum(g => (g.Quantity * g.Good.BasePrice));
                shipPlayer.CargoLostWorth += cargoWorth;
            }

            // Save database changes
            db.SaveChanges();
        }
コード例 #2
0
ファイル: Combat.cs プロジェクト: jcsston/CosmoMonger
        /// <summary>
        /// Accepts the search of the current turn ship by the other ship
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown when combat is over or there is no search in-progress</exception>
        /// <exception cref="ArgumentException">Thrown when there is not enough credits to pay the fine. Combat continues.</exception>
        public virtual void AcceptSearch()
        {
            CosmoMongerDbDataContext db = CosmoManager.GetDbContext();

            // Check that the combat is still in-progress
            if (this.Status != CombatStatus.Incomplete)
            {
                throw new InvalidOperationException("Combat is over");
            }

            if (!this.Search)
            {
                throw new InvalidOperationException("No search in progress");
            }

            // Look for contraband cargo
            ShipGood[] goods = (from g in this.ShipTurn.GetGoods()
                                where g.Good.Contraband
                                select g).ToArray();
            int contrabandCargoWorth = goods.Sum(g => g.Quantity * g.Good.BasePrice);

            if (contrabandCargoWorth > 0)
            {
                // Ship has some contraband cargo

                // The fine is twice the worth of the cargo
                int contrabandFine = contrabandCargoWorth * 2;

                // Check if the ship has enough to pay the fine
                if (this.ShipTurn.Credits < contrabandFine)
                {
                    // Not enough credits to pay the fine, search is rejected
                    throw new ArgumentException("Not enough credits to pay the fine!");
                }
                else
                {
                    // Charge the fine
                    this.ShipTurn.Credits  -= contrabandFine;
                    this.ShipOther.Credits += contrabandFine;
                    this.CreditsLooted     += contrabandFine;
                }

                // Take the cargo away
                foreach (ShipGood good in goods)
                {
                    // Add a CombatGood record
                    CombatGood combatGood = new CombatGood();
                    combatGood.Combat   = this;
                    combatGood.Good     = good.Good;
                    combatGood.Quantity = good.Quantity;
                    db.CombatGoods.InsertOnSubmit(combatGood);

                    // Load it up on the police ship
                    this.ShipOther.AddGood(good.GoodId, good.Quantity);

                    // Take the cargo away
                    good.Quantity = 0;
                }
            }

            // Combat is now over
            this.Status = CombatStatus.ShipSearched;

            db.SaveChanges();
        }