public void Create10000Players() { string baseEmail = "*****@*****.**"; string baseUserName = "******"; string basePlayerName = "stressTestPlayer"; CosmoMongerDbDataContext db = CosmoManager.GetDbContext(); CosmoMongerMembershipProvider provider = new CosmoMongerMembershipProvider(); MembershipCreateStatus status; Race skummRace = (from r in db.Races where r.Name == "Skumm" select r).SingleOrDefault(); for (int i = 0; i < 50000; i++) { CosmoMongerMembershipUser testUser = (CosmoMongerMembershipUser)provider.CreateUser(i + baseUserName, "test1000", i + baseEmail, null, null, true, null, out status); Assert.IsNotNull(testUser, "Test User was created. status = {0}", new object[] { status }); User testUserModel = testUser.GetUserModel(); Assert.IsNotNull(testUserModel, "Able to get model object for user"); Player testPlayer = testUserModel.CreatePlayer(i + basePlayerName, skummRace); foreach (Good good in db.Goods) { ShipGood shipGood = new ShipGood(); shipGood.Ship = testPlayer.Ship; shipGood.Good = good; shipGood.Quantity = 0; db.ShipGoods.InsertOnSubmit(shipGood); } db.SubmitChanges(); } }
/// <summary> /// Updates the system good price. /// </summary> private void UpdateSystemGoodPrice() { CosmoMongerDbDataContext db = CosmoManager.GetDbContext(); foreach (SystemGood good in db.SystemGoods) { // Get the total number of this good type avaiable in all systems int targetTotal = good.Good.TargetCount; int systemsWithGood = (from s in good.Good.SystemGoods where s.Quantity > 0 select s).Count(); double newPriceMultipler; // pre 2-21-09 way // newPriceMultipler = this.CalculatePriceMultipler(targetTotal, systemsWithGood, good.Quantity); // new way post 2-21-09 newPriceMultipler = this.CalculatePriceMultipler(targetTotal, systemsWithGood, good.Quantity); double oldPriceMultipler = good.PriceMultiplier; // Take average of previous and current price multipler good.PriceMultiplier = (oldPriceMultipler + newPriceMultipler) / 2.0; Dictionary <string, object> props = new Dictionary <string, object> { { "SystemId", good.SystemId }, { "GoodId", good.GoodId }, { "PriceMultiplier", good.PriceMultiplier }, { "NewPriceMultipler", newPriceMultipler }, { "OldPriceMultipler", oldPriceMultipler } }; Logger.Write("Adjusting Good Price", "NPC", 400, 0, TraceEventType.Verbose, "Adjusting Good Price", props); try { // Send changes to database db.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException ex) { ExceptionPolicy.HandleException(ex, "SQL Policy"); // Another thread has made changes to this SystemGood row, // which could be from someone buying or selling the good at a system // Best case to resolve this would be to simply start over in the price calculatation, // the previous price no longer valid. foreach (ObjectChangeConflict occ in db.ChangeConflicts) { // Refresh current values from database occ.Resolve(RefreshMode.OverwriteCurrentValues); } continue; } } }
public void SellNotSold() { CosmoMongerDbDataContext db = CosmoManager.GetDbContext(); // Setup player Player testPlayer = this.CreateTestPlayer(); Ship testShip = testPlayer.Ship; CosmoSystem startingSystem = testShip.CosmoSystem; GameManager manager = new GameManager(testPlayer.User.UserName); // Store the player starting cash int playerStartingCash = testPlayer.Ship.Credits; // Add some water to this ship for us to sell Good water = (from g in db.Goods where g.Name == "Water" select g).SingleOrDefault(); Assert.That(water, Is.Not.Null, "We should have a Water good"); ShipGood shipGood = new ShipGood(); shipGood.Good = water; shipGood.Quantity = 10; shipGood.Ship = testShip; testShip.ShipGoods.Add(shipGood); // Verify that the good is for sell in the current system SystemGood systemWater = startingSystem.GetGood(water.GoodId); if (systemWater != null) { // Remove the good from the system db.SystemGoods.DeleteOnSubmit(systemWater); db.SubmitChanges(); } try { shipGood.Sell(testShip, 10, systemWater.Price); } catch (InvalidOperationException ex) { // Correct Assert.That(ex, Is.Not.Null, "InvalidOperationException should be valid"); return; } Assert.Fail("Player should not been able to sell in the system when the good was not sold in the system"); }
public void SellNotEnoughGoods() { CosmoMongerDbDataContext db = CosmoManager.GetDbContext(); // Setup player Player testPlayer = this.CreateTestPlayer(); Ship testShip = testPlayer.Ship; CosmoSystem startingSystem = testShip.CosmoSystem; GameManager manager = new GameManager(testPlayer.User.UserName); // Add some water to this ship for us to sell Good water = (from g in db.Goods where g.Name == "Water" select g).SingleOrDefault(); Assert.That(water, Is.Not.Null, "We should have a Water good"); ShipGood shipGood = new ShipGood(); shipGood.Good = water; shipGood.Quantity = 10; shipGood.Ship = testShip; testShip.ShipGoods.Add(shipGood); db.SubmitChanges(); // Verify that the good is for sell in the current system SystemGood systemWater = startingSystem.GetGood(water.GoodId); if (systemWater == null) { startingSystem.AddGood(water.GoodId, 0); systemWater = startingSystem.GetGood(water.GoodId); } try { shipGood.Sell(testShip, 20, systemWater.Price); } catch (ArgumentOutOfRangeException ex) { Assert.That(ex.ParamName, Is.EqualTo("quantity"), "Quantity to sell should be the invalid argument"); return; } Assert.Fail("Player should not been able to sell more goods than aboard"); }
/// <summary> /// Updates the system good count, causing systems to produce goods as needed. /// </summary> private void UpdateSystemGoodCount() { CosmoMongerDbDataContext db = CosmoManager.GetDbContext(); Dictionary <string, object> props; foreach (Good good in db.Goods) { // Get the total number of this good type available in all systems int totalSystemGoodCount = good.SystemGoods.Sum(x => x.Quantity); double targetBreak = (((double)good.TargetCount) / 10.0); // Check if we need to add some of this good to the galaxy while (totalSystemGoodCount < good.TargetCount) { // Randomly select a system with equal to or fewer than targetBreak number of goods to produce. var goodProducingSystems = (from g in good.SystemGoods where g.ProductionFactor > 0 && g.Quantity <= targetBreak select g); if (goodProducingSystems.Count() == 0) { // No systems produce this good? // Continue on to the next good type break; } SystemGood selectedProducingSystemGood = goodProducingSystems.ElementAt(rnd.Next(goodProducingSystems.Count())); // Produce the good, using the count needed and the production factor double adjustedProductionFactor = (rnd.NextDouble() + selectedProducingSystemGood.ProductionFactor) / 2; int lackingGoodCount = (int)(rnd.Next(10) * adjustedProductionFactor); selectedProducingSystemGood.Quantity += lackingGoodCount; props = new Dictionary <string, object> { { "SystemId", selectedProducingSystemGood.SystemId }, { "GoodId", selectedProducingSystemGood.GoodId }, { "Quantity", selectedProducingSystemGood.Quantity }, { "AddedQuantity", lackingGoodCount } }; Logger.Write("Producing Goods", "NPC", 400, 0, TraceEventType.Verbose, "Producing Goods", props); // Update the total good count totalSystemGoodCount += lackingGoodCount; try { // Send changes to database db.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException ex) { ExceptionPolicy.HandleException(ex, "SQL Policy"); // Another thread has made changes to this SystemGood row, // which could be from someone buying or selling the good at a system // Best case to resolve this would be to simply start over in the good production, // because the good quantity has been changed by another method. foreach (ObjectChangeConflict occ in db.ChangeConflicts) { // Refresh values from database occ.Resolve(RefreshMode.OverwriteCurrentValues); } continue; } } // Now consume some of this good in the galaxy // Randomly select a good at a system to consume where the quantity is equal to or higher // than targetBreak number of goods var goodConsumingSystems = (from g in good.SystemGoods where g.ConsumptionFactor > 0 && g.Quantity >= targetBreak && g.Quantity > 0 select g); if (goodConsumingSystems.Count() == 0) { // No systems consume this good? // Continue on to the next good type continue; } SystemGood selectedConsumingSystemGood = goodConsumingSystems.ElementAt(rnd.Next(goodConsumingSystems.Count())); // Consuming the good, using the count needed and the consumption factor double adjustedConsumptionFactor = (rnd.NextDouble() + selectedConsumingSystemGood.ConsumptionFactor) / 2; int usageGoodCount = (int)(rnd.Next(10) * adjustedConsumptionFactor); usageGoodCount = Math.Min(usageGoodCount, selectedConsumingSystemGood.Quantity); selectedConsumingSystemGood.Quantity -= usageGoodCount; props = new Dictionary <string, object> { { "SystemId", selectedConsumingSystemGood.SystemId }, { "GoodId", selectedConsumingSystemGood.GoodId }, { "Quantity", selectedConsumingSystemGood.Quantity }, { "RemovedQuantity", usageGoodCount } }; Logger.Write("Consuming Goods", "NPC", 400, 0, TraceEventType.Verbose, "Consuming Goods", props); try { // Send changes to database db.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException ex) { ExceptionPolicy.HandleException(ex, "SQL Policy"); // Another thread has made changes to this SystemGood row, // which could be from someone buying or selling the good at a system // Best case to resolve this would be to simply ignore the good consumption, // we like to produce more than we consume foreach (ObjectChangeConflict occ in db.ChangeConflicts) { // Refresh values from database occ.Resolve(RefreshMode.OverwriteCurrentValues); } } } }