Exemple #1
0
        // TODO: this will also need to be called from a scheduler rountine, that runs perhaps once an hour to once a day.
        public static void CheckAccountExpiry(EconDataStruct data)
        {
            // Instance and Config are both defined before Data, so this is safe to call here.
            var expireDate           = DateTime.Now - EconomyScript.Instance.Config.AccountExpiry;
            var newAccountExpireDate = DateTime.Now.AddDays(-1);

            var deadAccounts = data.Accounts.Where(a =>
                                                   (a.SteamId != EconomyConsts.NpcMerchantId &&
                                                    data.Markets.All(m => m.MarketId != a.SteamId)) && // Exclude accounts which run markets.
                                                   (a.Date < expireDate ||
                                                    a.Date == a.OpenedDate && a.BankBalance == EconomyScript.Instance.Config.DefaultStartingBalance && a.Date < newAccountExpireDate)
                                                   ).ToArray();

            foreach (var account in deadAccounts)
            {
                EconomyScript.Instance.ServerLogger.WriteInfo("Removing Dead Account '{0}' with {1} {2}.", account.NickName, account.BankBalance, EconomyScript.Instance.Config.CurrencyName);
                data.Accounts.Remove(account);

                // EconomyScript.Instance.Data would not have been set for the first run though.
                var marketAccount = data.Accounts.FirstOrDefault(a => a.SteamId == EconomyConsts.NpcMerchantId);

                if (account.BankBalance > 0)
                {
                    // TODO: do something with the BankBalance if in a faction.
                    marketAccount.BankBalance += account.BankBalance;
                }
            }
        }
Exemple #2
0
        private static void ValidateAndUpdateData(EconDataStruct data, List <MarketItemStruct> defaultPrices)
        {
            EconomyScript.Instance.ServerLogger.WriteInfo("Validating and Updating Data.");

            // Add missing items that are covered by Default items.
            foreach (var defaultItem in defaultPrices)
            {
                foreach (var market in data.Markets)
                {
                    var item = market.MarketItems.FirstOrDefault(e => e.TypeId.Equals(defaultItem.TypeId) && e.SubtypeName.Equals(defaultItem.SubtypeName));
                    if (item == null)
                    {
                        market.MarketItems.Add(new MarketItemStruct {
                            TypeId = defaultItem.TypeId, SubtypeName = defaultItem.SubtypeName, BuyPrice = defaultItem.BuyPrice, SellPrice = defaultItem.SellPrice, IsBlacklisted = defaultItem.IsBlacklisted, Quantity = defaultItem.Quantity
                        });
                        EconomyScript.Instance.ServerLogger.WriteVerbose("MarketItem Adding Default item: {0} {1}.", defaultItem.TypeId, defaultItem.SubtypeName);
                    }
                    else
                    {
                        // Disable any blackmarket items.
                        if (!defaultItem.IsBlacklisted)
                        {
                            item.IsBlacklisted = false;
                        }
                    }
                }
            }

            // Buy/Sell - check we have our NPC banker ready
            NpcMerchantManager.VerifyAndCreate(data);

            // Initial check of account on server load.
            AccountManager.CheckAccountExpiry(data);
        }
        private static void CheckDefaultMarket(EconDataStruct data, List <MarketItemStruct> defaultPrices)
        {
            EconomyScript.Instance.ServerLogger.WriteInfo("Checking Default Market Data.");

            var market = data.Markets.FirstOrDefault(m => m.MarketId == EconomyConsts.NpcMerchantId);

            if (market == null)
            {
                market = new MarketStruct
                {
                    Open             = true,
                    MarketId         = EconomyConsts.NpcMerchantId,
                    MarketZoneType   = MarketZoneType.FixedSphere,
                    DisplayName      = EconomyConsts.NpcMarketName,
                    MarketZoneSphere = new BoundingSphereD(Vector3D.Zero, EconomyScript.Instance.ServerConfig.DefaultTradeRange), // Center of the game world.
                    MarketItems      = new List <MarketItemStruct>()
                };
                data.Markets.Add(market);
            }

            if (string.IsNullOrEmpty(market.DisplayName))
            {
                market.DisplayName = EconomyConsts.NpcMarketName;
            }

            AddMissingItemsToMarket(market, defaultPrices);
        }
Exemple #4
0
        /// <summary>
        /// Check we have our NPC banker ready.
        /// </summary>
        public static void VerifyAndCreate(EconDataStruct data)
        {
            // we look up our bank record based on our bogus NPC Steam Id/
            BankAccountStruct myNpcAccount = data.Accounts.FirstOrDefault(
                a => a.SteamId == EconomyConsts.NpcMerchantId);

            // Do it have an account already?
            if (myNpcAccount == null)
            {
                //nope, lets construct our bank record with a new balance
                myNpcAccount = AccountManager.CreateNewDefaultAccount(EconomyConsts.NpcMerchantId, EconomyScript.Instance.ServerConfig.NpcMerchantName, 0);

                //ok lets apply it
                data.Accounts.Add(myNpcAccount);
                data.CreditBalance -= myNpcAccount.BankBalance;
                EconomyScript.Instance.ServerLogger.WriteInfo("Banker Account Created.");
            }
            else
            {
                EconomyScript.Instance.ServerLogger.WriteInfo("Banker Account Exists.");
            }

            // ShipSale was added recently, so this makes sure the list is created in existing data stores.
            if (data.ShipSale == null)
            {
                data.ShipSale = new List <ShipSaleStruct>();
            }
        }
Exemple #5
0
        public static void SaveData(EconDataStruct data)
        {
            TextWriter writer = MyAPIGateway.Utilities.WriteFileInWorldStorage(WorldStorageDataFilename, typeof(EconDataStruct));

            writer.Write(MyAPIGateway.Utilities.SerializeToXML <EconDataStruct>(data));
            writer.Flush();
            writer.Close();
        }
Exemple #6
0
        public static void SaveData(EconDataStruct data)
        {
            string     filename = GetDataFilename();
            TextWriter writer   = MyAPIGateway.Utilities.WriteFileInLocalStorage(filename, typeof(EconDataStruct));

            writer.Write(MyAPIGateway.Utilities.SerializeToXML <EconDataStruct>(data));
            writer.Flush();
            writer.Close();
        }
Exemple #7
0
        private static EconDataStruct InitData()
        {
            EconomyScript.Instance.ServerLogger.WriteInfo("Creating new EconDataStruct.");
            EconDataStruct data = new EconDataStruct();

            data.Accounts  = new List <BankAccountStruct>();
            data.Markets   = new List <MarketStruct>();
            data.OrderBook = new List <OrderBookStruct>();
            return(data);
        }
Exemple #8
0
        private static EconDataStruct InitData()
        {
            EconomyScript.Instance.ServerLogger.WriteInfo("Creating new EconDataStruct.");
            EconDataStruct data = new EconDataStruct();

            data.Clients   = new List <ClientAccountStruct>();
            data.Markets   = new List <MarketStruct>();
            data.OrderBook = new List <OrderBookStruct>();
            data.ShipSale  = new List <ShipSaleStruct>();
            data.Missions  = new List <MissionBaseStruct>();
            return(data);
        }
        private static void ValidateAndUpdateData(EconDataStruct data, List <MarketItemStruct> defaultPrices)
        {
            EconomyScript.Instance.ServerLogger.WriteInfo("Validating and Updating Data.");

            if (data.Accounts != null)
            {
                if (data.Accounts.Count != 0)
                {
                    if (data.Clients == null)
                    {
                        data.Clients = new List <ClientAccountStruct>();
                    }
                    foreach (BankAccountStruct account in data.Accounts)
                    {
                        data.Clients.Add(new ClientAccountStruct
                        {
                            SteamId     = account.SteamId,
                            BankBalance = account.BankBalance,
                            Date        = account.Date,
                            OpenedDate  = account.OpenedDate,
                            MissionId   = account.MissionId,
                            Language    = account.Language,
                            NickName    = account.NickName
                        });
                    }
                }
                data.Accounts = null;
            }

            foreach (ClientAccountStruct client in data.Clients)
            {
                if (client.ClientHudSettings == null)
                {
                    client.ClientHudSettings = new ClientHudSettingsStruct();
                }
            }

            foreach (var market in data.Markets)
            {
                AddMissingItemsToMarket(market, defaultPrices);
            }

            if (data.Missions == null)
            {
                data.Missions = new List <MissionBaseStruct>();
            }

            // Buy/Sell - check we have our NPC banker ready
            NpcMerchantManager.VerifyAndCreate(data);

            // Initial check of account on server load.
            AccountManager.CheckAccountExpiry(data);
        }
Exemple #10
0
        private static void ValidateAndUpdateData(EconDataStruct data, List <MarketItemStruct> defaultPrices)
        {
            EconomyScript.Instance.ServerLogger.WriteInfo("Validating and Updating Data.");

            // Add missing items that are covered by Default items.
            foreach (var defaultItem in defaultPrices)
            {
                foreach (var market in data.Markets)
                {
                    var item          = market.MarketItems.FirstOrDefault(e => e.TypeId.Equals(defaultItem.TypeId) && e.SubtypeName.Equals(defaultItem.SubtypeName));
                    var isNpcMerchant = market.MarketId == EconomyConsts.NpcMerchantId; // make sure no stock is added to player markets.

                    // TODO: remove this later. It's a temporary fix to setup the new Open property.
                    // Added 01.125.
                    if (isNpcMerchant)
                    {
                        market.Open = true;
                    }

                    if (item == null)
                    {
                        market.MarketItems.Add(new MarketItemStruct {
                            TypeId = defaultItem.TypeId, SubtypeName = defaultItem.SubtypeName, BuyPrice = defaultItem.BuyPrice, SellPrice = defaultItem.SellPrice, IsBlacklisted = defaultItem.IsBlacklisted, Quantity = isNpcMerchant ? defaultItem.Quantity : 0
                        });
                        EconomyScript.Instance.ServerLogger.WriteVerbose("MarketItem Adding Default item: {0} {1}.", defaultItem.TypeId, defaultItem.SubtypeName);
                    }
                    else
                    {
                        // Disable any blackmarket items.
                        if (defaultItem.IsBlacklisted)
                        {
                            item.IsBlacklisted = true;
                        }
                    }
                }
            }

            if (data.Missions == null)
            {
                data.Missions = new List <MissionBaseStruct>();
            }

            // Buy/Sell - check we have our NPC banker ready
            NpcMerchantManager.VerifyAndCreate(data);

            // Initial check of account on server load.
            AccountManager.CheckAccountExpiry(data);
        }
Exemple #11
0
        private static void CheckDefaultMarket(EconDataStruct data, List <MarketItemStruct> defaultPrices)
        {
            EconomyScript.Instance.ServerLogger.WriteInfo("Checking Default Market Data.");

            var market = data.Markets.FirstOrDefault(m => m.MarketId == EconomyConsts.NpcMerchantId);

            if (market == null)
            {
                market = new MarketStruct
                {
                    MarketId         = EconomyConsts.NpcMerchantId,
                    MarketZoneType   = MarketZoneType.FixedSphere,
                    DisplayName      = EconomyConsts.NpcMarketName,
                    MarketZoneSphere = new BoundingSphereD(Vector3D.Zero, EconomyScript.Instance.Config.DefaultTradeRange), // Center of the game world.
                    MarketItems      = new List <MarketItemStruct>()
                };
                data.Markets.Add(market);
            }

            if (string.IsNullOrEmpty(market.DisplayName))
            {
                market.DisplayName = EconomyConsts.NpcMarketName;
            }

            // Add missing items that are covered by Default items.
            foreach (var defaultItem in defaultPrices)
            {
                var item = market.MarketItems.FirstOrDefault(e => e.TypeId.Equals(defaultItem.TypeId) && e.SubtypeName.Equals(defaultItem.SubtypeName));
                if (item == null)
                {
                    market.MarketItems.Add(new MarketItemStruct {
                        TypeId = defaultItem.TypeId, SubtypeName = defaultItem.SubtypeName, BuyPrice = defaultItem.BuyPrice, SellPrice = defaultItem.SellPrice, IsBlacklisted = defaultItem.IsBlacklisted, Quantity = defaultItem.Quantity
                    });
                    EconomyScript.Instance.ServerLogger.WriteVerbose("MarketItem Adding Default item: {0} {1}.", defaultItem.TypeId, defaultItem.SubtypeName);
                }
                else
                {
                    // Disable any blackmarket items.
                    if (!defaultItem.IsBlacklisted)
                    {
                        item.IsBlacklisted = false;
                    }
                }
            }
        }
Exemple #12
0
        public static EconDataStruct LoadData(List <MarketItemStruct> defaultPrices)
        {
            string         filename = GetDataFilename();
            EconDataStruct data     = null;

            if (!MyAPIGateway.Utilities.FileExistsInLocalStorage(filename, typeof(EconDataStruct)))
            {
                data = InitData();
                CheckDefaultMarket(data, defaultPrices);
                ValidateAndUpdateData(data, defaultPrices);
                return(data);
            }

            TextReader reader = MyAPIGateway.Utilities.ReadFileInLocalStorage(filename, typeof(EconDataStruct));

            var xmlText = reader.ReadToEnd();

            reader.Close();

            if (string.IsNullOrWhiteSpace(xmlText))
            {
                data = InitData();
                CheckDefaultMarket(data, defaultPrices);
                ValidateAndUpdateData(data, defaultPrices);
                return(data);
            }

            try
            {
                data = MyAPIGateway.Utilities.SerializeFromXML <EconDataStruct>(xmlText);
                EconomyScript.Instance.ServerLogger.WriteInfo("Loading existing EconDataStruct.");
            }
            catch
            {
                // data failed to deserialize.
                EconomyScript.Instance.ServerLogger.WriteError("Failed to deserialize EconDataStruct. Creating new EconDataStruct.");
                data = InitData();
            }

            CheckDefaultMarket(data, defaultPrices);
            ValidateAndUpdateData(data, defaultPrices);
            return(data);
        }
        /// <summary>
        /// Check we have our NPC banker ready.
        /// </summary>
        public static void VerifyAndCreate(EconDataStruct data)
        {
            // we look up our bank record based on our bogus NPC Steam Id/
            var myNpcAccount = data.Accounts.FirstOrDefault(
                a => a.SteamId == EconomyConsts.NpcMerchantId);

            // Do it have an account already?
            if (myNpcAccount == null)
            {
                //nope, lets construct our bank record with a new balance
                myNpcAccount = AccountManager.CreateNewDefaultAccount(EconomyConsts.NpcMerchantId, EconomyScript.Instance.Config.NpcMerchantName, 0);

                //ok lets apply it
                data.Accounts.Add(myNpcAccount);
                EconomyScript.Instance.ServerLogger.WriteInfo("Banker Account Created.");
            }
            else
            {
                EconomyScript.Instance.ServerLogger.WriteInfo("Banker Account Exists.");
            }
        }
Exemple #14
0
 public static void SaveData(EconDataStruct data)
 {
     string filename = GetDataFilename();
     TextWriter writer = MyAPIGateway.Utilities.WriteFileInLocalStorage(filename, typeof(EconDataStruct));
     writer.Write(MyAPIGateway.Utilities.SerializeToXML<EconDataStruct>(data));
     writer.Flush();
     writer.Close();
 }
Exemple #15
0
        private static void ValidateAndUpdateData(EconDataStruct data, List<MarketStruct> defaultPrices)
        {
            EconomyScript.Instance.ServerLogger.Write("Проверки и обновления данных.");

            // Add missing items that are covered by Default items.
            foreach (var defaultItem in defaultPrices)
            {
                if (!data.MarketItems.Any(e => e.TypeId.Equals(defaultItem.TypeId) && e.SubtypeName.Equals(defaultItem.SubtypeName)))
                {
                    data.MarketItems.Add(new MarketStruct { TypeId = defaultItem.TypeId, SubtypeName = defaultItem.SubtypeName, BuyPrice = defaultItem.BuyPrice, SellPrice = defaultItem.SellPrice, IsBlacklisted = defaultItem.IsBlacklisted, Quantity = defaultItem.Quantity });
                    EconomyScript.Instance.ServerLogger.Write("MarketItem Добавление пункт По умолчанию: {0} {1}.", defaultItem.TypeId, defaultItem.SubtypeName);
                }
            }

            // Don't have to call this anymore, as the Config will have called it for the Default item prices.
            //MarketManager.SyncMarketItems(ref data.MarketItems);

            // Buy/Sell - check we have our NPC banker ready
            NpcMerchantManager.VerifyAndCreate(data);
        }
Exemple #16
0
        private static bool LoadOldData(EconDataStruct data)
        {
            string filename;
            bool oldLoaded = false;

            filename = MarketManager.GetContentFilename();
            if (MyAPIGateway.Utilities.FileExistsInLocalStorage(filename, typeof(MarketManager)))
            {
                EconomyScript.Instance.ServerLogger.Write("Загрузка старую версию файла MarketManagement: {0}", filename);
                var marketConfigData = MarketManager.LoadContent();
                data.MarketItems = marketConfigData.MarketItems;
                oldLoaded = true;
                MyAPIGateway.Utilities.DeleteFileInLocalStorage(filename, typeof(MarketManager));
            }

            filename = BankManagement.GetContentFilename();
            if (MyAPIGateway.Utilities.FileExistsInLocalStorage(filename, typeof(BankConfig)))
            {
                EconomyScript.Instance.ServerLogger.Write("Загрузка старую версию файла BankConfig: {0}", filename);
                var bankConfigData = BankManagement.LoadContent();
                data.Accounts = bankConfigData.Accounts;
                oldLoaded = true;
                MyAPIGateway.Utilities.DeleteFileInLocalStorage(filename, typeof(BankConfig));
            }

            return oldLoaded;
        }
Exemple #17
0
 private static EconDataStruct InitData()
 {
     EconomyScript.Instance.ServerLogger.Write("Создание нового EconDataStruct.");
     EconDataStruct data = new EconDataStruct();
     data.Accounts = new List<BankAccountStruct>();
     data.MarketItems = new List<MarketStruct>();
     return data;
 }