private int PlaceShopItem(List <PlayerShopItem> shopItemList, PlayerShopItem shopItem) { using (TimedLock.Lock(shopItemList)) { for (var i = 0; i < shopItemList.Count; i++) { if (shopItemList[i].Price < shopItem.Price) { continue; } if (shopItemList[i].Price > shopItem.Price) { shopItemList.Insert(i, shopItem); return(i); } if (shopItemList[i].InsertTime > shopItem.InsertTime) { shopItemList.Insert(i, shopItem); return(i); } } shopItemList.Add(shopItem); return(shopItemList.Count - 1); } }
private ItemType GetItemType(PlayerShopItem shopItem) { var gameData = _manager.Resources.GameData; var item = gameData.Items[shopItem.ItemId]; if (item.Potion && item.ActivateEffects.Any(a => a.Effect == ActivateEffects.IncrementStat)) { return(ItemType.StatPot); } if (!gameData.SlotType2ItemType.ContainsKey(item.SlotType)) { return(ItemType.Other); } switch (gameData.SlotType2ItemType[item.SlotType]) { case ItemType.Weapon: return(ItemType.Weapon); case ItemType.Ability: return(ItemType.Ability); case ItemType.Armor: return(ItemType.Armor); case ItemType.Ring: return(ItemType.Ring); default: return(ItemType.Other); } }
private bool ValidateShopItem(PlayerShopItem shopItem) { if (shopItem == null || !_manager.Resources.GameData.Items.ContainsKey(shopItem.ItemId) || shopItem.Price < 0 || shopItem.AccountId <= 0) { return(false); } return(true); }
private int AddShopItem(PlayerShopItem shopItem) { if (!ValidateShopItem(shopItem)) { return(-1); } var market = GetMarket(shopItem); if (!market.ContainsKey(shopItem.ItemId)) { market.TryAdd(shopItem.ItemId, new List <PlayerShopItem>()); } return(PlaceShopItem(market[shopItem.ItemId], shopItem)); }
private int AddShopItem(PlayerShopItem shopItem) { if (!ValidateShopItem(shopItem)) { Log.Warn($"Invalid PlayerShopItem. {shopItem.AccountId}'s item ({shopItem.ItemId}) will not be merched."); return(-1); } var market = GetMarket(shopItem); if (!market.ContainsKey(shopItem.ItemId)) { market.TryAdd(shopItem.ItemId, new List <PlayerShopItem>()); } return(PlaceShopItem(market[shopItem.ItemId], shopItem)); }
private ConcurrentDictionary <ushort, List <PlayerShopItem> > GetMarket(PlayerShopItem shopItem) { switch (GetItemType(shopItem)) { case ItemType.Weapon: return(_weapons); case ItemType.Ability: return(_abilities); case ItemType.Armor: return(_armor); case ItemType.Ring: return(_rings); case ItemType.StatPot: return(_statPots); default: return(_other); } }
public void Remove(PlayerShopItem shopItem, ITransaction transaction = null) { var trans = transaction ?? _manager.Database.Conn.CreateTransaction(); var task = _dbMarket .RemoveAsync(shopItem, trans) .ContinueWith(t => { using (TimedLock.Lock(MarketLock)) { var success = !t.IsCanceled && t.Result; if (success) { var itemList = GetMarket(shopItem)[shopItem.ItemId]; using (TimedLock.Lock(itemList)) { itemList.Remove(shopItem); } var merchant = _merchants.Values.FirstOrDefault(m => m.PlayerShopItem == shopItem); merchant?.Reload(); } return(success); } }); task.ContinueWith(e => Log.Error(e.Exception.InnerException.ToString()), TaskContinuationOptions.OnlyOnFaulted); if (transaction == null) { trans.Execute(CommandFlags.FireAndForget); } }
public void Add(PlayerShopItem shopItem, ITransaction transaction = null) { var trans = transaction ?? _manager.Database.Conn.CreateTransaction(); if (!ValidateShopItem(shopItem)) { return; } var task = _dbMarket .InsertAsync(shopItem, trans) .ContinueWith(t => { using (TimedLock.Lock(MarketLock)) { if (t.IsCanceled || !t.Result) { return(false); } var index = AddShopItem(shopItem); if (index != 0) { return(true); } var merchant = _merchants .FirstOrDefault(m => m.Value.Item == shopItem.ItemId); if (merchant.Value != null) { merchant.Value.TimeLeft = 30000; merchant.Value.Reload(); return(true); } var itemType = GetItemType(shopItem); var shopItems = _items[itemType]; using (TimedLock.Lock(shopItems)) { if (!shopItems.Contains(shopItem.ItemId)) { shopItems.Add(shopItem.ItemId); _queues[itemType].Enqueue(shopItem.ItemId); } } if (_merchants.Count < _shopTypeLocations.Select(s => s.Value.Count).Sum()) { AddMerchants(); } return(true); } }); task.ContinueWith(e => Log.Error(e.Exception.InnerException.ToString()), TaskContinuationOptions.OnlyOnFaulted); if (transaction == null) { trans.Execute(CommandFlags.FireAndForget); } }