/// <summary> /// It reads an xml config file and deserializes into collection of ItemPriceCollection /// </summary> /// <returns>Collection of item price</returns> private static ItemPriceCollection GetItemsPriceCollectionFromConfig() { const string configCategory = "ItemPrice"; const string configRuleData = "ItemPriceData"; ItemPriceCollection itemsPriceColl = null; try { IConfigurationSet configurationSet = ConfigurationFactory.GetConfigurationSet(typeof(ItemsPriceCollectionManager)); IConfigurationCollection configurations = configurationSet.GetConfigurations(configCategory); IConfiguration itemConfig = configurations.GetConfiguration(configRuleData); if (itemConfig != null) { itemsPriceColl = SerializerHelper.Deserialize( itemConfig.ConfigurationBasis.InnerXml, typeof(ItemPriceCollection) ) as ItemPriceCollection; } } catch (Exception ex) { throw new Exception(String.Format ("Error occured while retrieving ItemPrice configuration and ItemPricecollection in ItemsPriceCollectionManager.GetItemsPriceCollectionFromConfig method : {0}", ex.Message)); } return(itemsPriceColl); }
/// <summary> /// GetPrice method will get the configured values for all items in the system and start /// calculating the price depending on the matching barcodes between the request barcodes /// and the one exist in the returned collections. /// </summary> /// <param name="items"></param> /// <returns></returns> public PriceCalculationStatusResource GetPrice(List <Item> items) { var priceCalculationStatusResource = new PriceCalculationStatusResource(); double total = 0; var totalStr = "0"; //Apples cost 60p and oranges cost 25p. //111 apple barcode //222 orange barcode try { //This Collection can be stored in database and returned values from it. //Or save this static values in configuration files and returned these values from it. //ItemsPriceCollection is static and will be loads once in the static constructor. ItemPriceCollection itemsPriceColl = ItemsPriceCollectionManager.GetItemPriceCollection(); foreach (Item item in items) { foreach (ItemPrice itemPrice in itemsPriceColl) { if (item.BarCodeId == itemPrice.BarCodeId) { double price = itemPrice.Price; total += price; break; } } } } catch (Exception ex) { throw new Exception(String.Format("Error in calculating price in PriceCalculationMapper.CalcualtePrice method : {0}", ex.Message)); } //set total price to the data contrat that will be returen in the response body priceCalculationStatusResource.TotalPrice = total; return(priceCalculationStatusResource); }
protected override void Setup() { try { Instance = this; this.InitLogging(); log.InfoFormat("Setup: serverId={0}", ServerId); Clients = new ClientCollection(); Notifications = new NotificationService(this); #if LOCAL string databaseConnectionFile = System.IO.Path.Combine(BinaryPath, "assets/database_connection_local.txt"); #else string databaseConnectionFile = System.IO.Path.Combine(BinaryPath, "assets/database_connection.txt"); #endif string databaseConnectionString = File.ReadAllText(databaseConnectionFile).Trim(); this.DB = new DbReader(); this.DB.Setup(databaseConnectionString, SelectCharacterSettings.Default.DatabaseName, SelectCharacterSettings.Default.DatabaseCollectionName); Mail = new MailManager(this); Guilds = new GuildService(this); Chat = new ChatService(this); Groups = new GroupService(this); Stores = new PlayerStoreService(this); Auction = new AuctionService(this); RaceCommands = new RaceCommandService(this); this.Players = new PlayerService(this); this.StartModules = new StartPlayerModuleRes(); this.StartModules.LoadFromFile(Path.Combine(this.BinaryPath, "assets/start_player_modules.xml")); bankSlotPrices = new BankSlotPriceCollection(); bankSlotPrices.LoadFromFile(Path.Combine(this.BinaryPath, "assets/bank_slot_price.xml")); leveling = new Leveling(); leveling.Load(Path.Combine(BinaryPath, "assets")); itemPriceCollection = new ItemPriceCollection(); itemPriceCollection.Load(BinaryPath); consumableItems = new ConsumableItemCollection(); consumableItems.Load(BinaryPath); StartLocations = new StartLocationCollection(); StartLocations.LoadFromFile(Path.Combine(BinaryPath, "assets/start_locations.xml")); log.Info(StartLocations.GetWorld(Race.Humans, Workshop.DarthTribe)); pvpStoreItems = new PvpStoreItemCollection(); pvpStoreItems.Load(Path.Combine(BinaryPath, "assets/pvp_store.xml")); log.InfoFormat("store items loaded = {0} [red]", pvpStoreItems.count); serverSettings = new ServerInputsRes(); serverSettings.Load(BinaryPath, "Data/server_inputs.xml"); resource = new Res(BinaryPath); resource.Load(); Election = new CommanderElection(this); raceStats = new RaceStatsService(this); friends = new FriendService(this); pvpStore = new PvpStoreManager(this); achievmentCache = new Achievments.AchievmentCache(this); Protocol.AllowRawCustomValues = true; this.PublicIpAddress = PublicIPAddressReader.ParsePublicIpAddress(SelectCharacterSettings.Default.PublicIPAddress); this.ConnectToMaster(); this.executionFiber = new PoolFiber(); this.executionFiber.Start(); this.executionFiber.ScheduleOnInterval(this.Update, SelectCharacterSettings.Default.DatabaseSaveInterval * 1000, SelectCharacterSettings.Default.DatabaseSaveInterval * 1000); }catch (Exception ex) { log.Error(ex); log.Error(ex.StackTrace); } }
/// <summary> /// Constructor that loads the collection /// </summary> static ItemsPriceCollectionManager() { //load configurations form xml file _itemsPriceCollection = GetItemsPriceCollectionFromConfig(); }
/// <summary> /// GetPriceWithOffer method will get the configured values for all items in the system and start /// calculating the price depending on the matching barcodes between the request barcodes /// and the one exist in the returned collections and depending on the below offer rule: /// buy one, get one free on Apples /// 3 for the price of 2 on Oranges /// </summary> /// <param name="items"></param> /// <returns></returns> public PriceCalculationStatusResource GetPriceWithOffer(List <Item> items) { var priceCalculationStatusResource = new PriceCalculationStatusResource(); double total = 0; int appleCount = 0; int orangeCont = 0; double applePrice = 0.0; double orangePrice = 0.0; double totalApplePrice = 0.0; double totalOrangePrice = 0.0; //Apples cost 60p and oranges cost 25p. //111 apple barcode //222 orange barcode try { //This Collection can be stored in database and returned values from it. //Or save this static values in configuration files and returned these values from it. //ItemsPriceCollection is static and will be loads once in the static constructor. ItemPriceCollection itemsPriceColl = ItemsPriceCollectionManager.GetItemPriceCollection(); //Calculate Apples appleCount += items.Count(item => item.BarCodeId == 111); foreach (ItemPrice itemPrice in itemsPriceColl.Cast <ItemPrice>().Where(itemPrice => itemPrice.BarCodeId == 111)) { applePrice = itemPrice.Price; } //buy one, get one free on Apples double partialApplePrice = 0; if (appleCount % 2 > 0) { partialApplePrice = appleCount % 2 * applePrice; } totalApplePrice = partialApplePrice + Math.Floor((double)appleCount / 2) * applePrice; //Calculate Oranges orangeCont += items.Count(item => item.BarCodeId == 222); foreach (ItemPrice itemPrice in itemsPriceColl.Cast <ItemPrice>().Where(itemPrice => itemPrice.BarCodeId == 222)) { orangePrice = itemPrice.Price; } // 3 for the price of 2 on Oranges double partialOrangePrice = 0; if (orangeCont % 3 > 0) { partialOrangePrice = orangeCont % 3 * orangePrice; } totalOrangePrice = partialOrangePrice + Math.Floor((double)orangeCont / 3) * orangePrice; } catch (Exception ex) { throw new Exception(String.Format("Error in calculating price in PriceCalculationMapper.CalcualtePriceWithOffer method : {0}", ex.Message)); } //set total price to the data contrat that will be returen in the response body priceCalculationStatusResource.TotalPrice = totalApplePrice + totalOrangePrice; return(priceCalculationStatusResource); }