public override string PostSalvageForFaction(List <ShopDefItem> salvage, string Faction) { lock (_salvageLock) { Faction realFaction = (Faction)Enum.Parse(typeof(Faction), Faction); if (Holder.factionInventories == null) { Holder.factionInventories = FactionInventoryStateManager.Build(); } if (!Holder.factionInventories.ContainsKey(realFaction)) { Holder.factionInventories.Add(realFaction, new List <ShopDefItem>()); } foreach (ShopDefItem item in salvage) { if (Holder.factionInventories[realFaction].FirstOrDefault(x => x.ID.Equals(item.ID)) == null) { Holder.factionInventories[realFaction].Add(item); } else { int index = Holder.factionInventories[realFaction].FindIndex(x => x.ID.Equals(item.ID)); Holder.factionInventories[realFaction][index].Count++; Holder.factionInventories[realFaction][index].DiscountModifier = Math.Max(Holder.factionInventories[realFaction][index].DiscountModifier - Helper.LoadSettings().DiscountPerItem, Helper.LoadSettings().DiscountFloor); } } logger.Info($"INV: Added {salvage.Count} items into inventory for faction ({Faction})"); return(salvage.Count + " items inserted into inventory for " + Faction); } }
private static void PeriodicBackup() { // Create the backup path if it doesn't exist (new FileInfo(StarMapStateManager.MapFileDirectory)).Directory.Create(); // Save the map var mapToSave = StarMapStateManager.Build(); string mapAsJson = JsonConvert.SerializeObject(mapToSave); logger.Info("Saving StarMap"); WriteBoth(StarMapStateManager.MapFileDirectory, mapAsJson); // Save faction inventories var inventoriesToSave = FactionInventoryStateManager.Build(); string inventoryAsJson = JsonConvert.SerializeObject(inventoriesToSave); logger.Info("Saving Faction Inventories"); WriteBoth(FactionInventoryStateManager.ShopFileDirectory, inventoryAsJson); // Save player histories var historiesToSave = PlayerStateManager.Build(); string historyAsJson = JsonConvert.SerializeObject(historiesToSave); logger.Info("Saving player history"); lastBackupTime = DateTime.UtcNow; }
public static List <ShopDefItem> GenerateNewShop(Faction realFaction) { List <ShopDefItem> newShop = new List <ShopDefItem>(); Random rand = new Random(); if (Holder.factionInventories == null) { Holder.factionInventories = FactionInventoryStateManager.Build(); } if (!Holder.factionInventories.ContainsKey(realFaction)) { Holder.factionInventories.Add(realFaction, new List <ShopDefItem>()); } if (Holder.factionInventories[realFaction].Count <= 0) { return(newShop); } int maxCount = Holder.factionInventories[realFaction].Max(x => x.Count); foreach (ShopDefItem item in Holder.factionInventories[realFaction].OrderByDescending(x => x.Count)) { if (newShop.Count >= Helper.LoadSettings().MaxItemsPerShop) { break; } int rolledNumber = rand.Next(0, maxCount + 1); if (rolledNumber <= item.Count) { while (rolledNumber < item.Count) { if (newShop.FirstOrDefault(x => x.ID.Equals(item.ID)) == null) { ShopDefItem newItem = new ShopDefItem(item); newItem.Count = 1; newShop.Add(newItem); } else { newShop.FirstOrDefault(x => x.ID.Equals(item.ID)).Count++; } item.Count--; item.DiscountModifier = Math.Min(item.DiscountModifier + Helper.LoadSettings().DiscountPerItem, Helper.LoadSettings().DiscountCeiling); } } } foreach (ShopDefItem item in newShop) { logger.Debug($"Added {item.ID} count {item.Count}"); } Holder.factionInventories[realFaction].RemoveAll(x => x.Count <= 0); logger.Info($"New shop generated for faction ({realFaction})"); return(newShop); }
/* * Application that uses Windows Communications Foundation (WCF) to provide a RESTful API that allows persistence of Morphyum's WarTech. * If you're unfamiliar with WCF, checkout the following: * * http://dotnetmentors.com/wcf/overview-on-wcf-service-architecture.aspx * https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/extending-dispatchers * * The client is the PersistentMapClient, in this repository. * This PersistentMapServer is the server. * * Note that WCF is no longer the preferred solution for REST endpoints, which has become ASP.NET5 w/ MVC6. * See https://blog.tonysneed.com/2016/01/06/wcf-is-dead-long-live-mvc-6/. */ static void Main(string[] args) { try { // Start a heart-beat monitor to check the server status BackgroundWorker heartbeatWorker = new BackgroundWorker { WorkerSupportsCancellation = true }; heartbeatWorker.DoWork += new DoWorkEventHandler(HeartBeatMonitor.DoWork); heartbeatWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(HeartBeatMonitor.RunWorkerCompleted); heartbeatWorker.RunWorkerAsync(); SettingsFileMonitor monitor = new SettingsFileMonitor(); monitor.enable(); BackgroundWorker playerHistoryPruner = new BackgroundWorker { WorkerSupportsCancellation = true }; playerHistoryPruner.DoWork += new DoWorkEventHandler(PlayerHistoryPruner.DoWork); playerHistoryPruner.RunWorkerCompleted += new RunWorkerCompletedEventHandler(PlayerHistoryPruner.RunWorkerCompleted); playerHistoryPruner.RunWorkerAsync(); BackgroundWorker backupWorker = new BackgroundWorker { WorkerSupportsCancellation = true }; backupWorker.DoWork += new DoWorkEventHandler(BackupWorker.DoWork); backupWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackupWorker.RunWorkerCompleted); backupWorker.RunWorkerAsync(); // Preload the data to allow any necessary initialization to happen StarMapStateManager.Build(); FactionInventoryStateManager.Build(); PlayerStateManager.Build(); WarServices warServices = new WarServices(); // Create an AOP proxy object that we can hang Castle.DynamicProxies upon. These are useful for operations across the whole // of the service, or for when we need to fail a message in a reasonable way. var proxy = new Castle.DynamicProxy.ProxyGenerator() .CreateClassProxyWithTarget <WarServices>(warServices, new Castle.DynamicProxy.IInterceptor[] { new UserQuotaInterceptor(), new AdminKeyRequiredInterceptor() }); // Create a RESTful service host. The service instance is automatically, through // the WarServiceInstanceProviderBehaviorAttribute. We create the singleton this way to give // us the chance to customize the binding WebServiceHost _serviceHost = new WebServiceHost(typeof(WarServices), new Uri(ServiceUrl)); AddServiceBehaviors(_serviceHost); // Create a binding that wraps the default WebMessageEncodingBindingElement with a BindingElement // that can GZip compress responses when a client requests it. WebMessageEncodingBindingElement innerEncoding = new WebMessageEncodingBindingElement { ContentTypeMapper = new ForceJsonWebContentMapper() }; GZipMessageEncodingBindingElement encodingWrapper = new GZipMessageEncodingBindingElement(innerEncoding); var transport = new HttpTransportBindingElement { ManualAddressing = true, KeepAliveEnabled = false, AllowCookies = false }; var customBinding = new CustomBinding(encodingWrapper, transport); // Create a default endpoint with the JSON/XML behaviors and the behavior to check the incoming headers for GZIP requests var endpoint = _serviceHost.AddServiceEndpoint(typeof(IWarServices), customBinding, ""); endpoint.Behaviors.Add(new WebHttpBehavior()); endpoint.Behaviors.Add(new GZipBehavior()); _serviceHost.Open(); Console.WriteLine("Open Press Key to close"); Console.ReadKey(); _serviceHost.Close(); Console.WriteLine("Connection Closed"); // Cleanup any outstanding processes monitor.disable(); heartbeatWorker.CancelAsync(); playerHistoryPruner.CancelAsync(); PlayerHistoryPruner.PruneOnExit(); backupWorker.CancelAsync(); BackupWorker.BackupOnExit(); } catch (Exception e) { Console.WriteLine(e); Console.ReadKey(); } }