238 lines
6.5 KiB
C#
238 lines
6.5 KiB
C#
using System.Collections.Generic;
|
|
using asap.core;
|
|
using cfg;
|
|
using game;
|
|
using GameCore;
|
|
using UniRx;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
public interface IDeferredRewardStashService
|
|
{
|
|
public void DisposeSubscriptions();
|
|
public void Reset();
|
|
public void Flush();
|
|
public void UploadData();
|
|
public void LoadData();
|
|
public IReadOnlyList<ItemData> Items { get; }
|
|
public bool IsEmpty { get; }
|
|
}
|
|
|
|
public class DeferredRewardStashService : IDeferredRewardStashService
|
|
{
|
|
private readonly TbItem _itemTable = GContext.container.Resolve<Tables>().TbItem;
|
|
private readonly PlayerItemData _playerItemData = GContext.container.Resolve<PlayerItemData>();
|
|
private CompositeDisposable _disposables;
|
|
private List<ItemData> _items;
|
|
public IReadOnlyList<ItemData> Items => _items;
|
|
public bool IsEmpty => _items is not { Count: > 0 };
|
|
private const string Key = "DeferredRewardStashService", Comma = ",", Splitter = "|";
|
|
|
|
// private readonly HashSet<int> _excludeList = new HashSet<int>
|
|
// {
|
|
// 310020000,
|
|
// 310030000,
|
|
// 310040000,
|
|
// 320020000,
|
|
// 320030000,
|
|
// 320040000,
|
|
// };
|
|
|
|
/// <summary>
|
|
/// Temporary usage to exclude handbag from showing, and showing only.
|
|
/// </summary>
|
|
// private bool IsItemHandBag(ItemData item)
|
|
// {
|
|
// var itemLine = _itemTable.GetOrDefault(item.id);
|
|
// if (itemLine == null)
|
|
// return false;
|
|
// return itemLine.Type == 9 && itemLine.SubType == 7;
|
|
// }
|
|
|
|
public DeferredRewardStashService()
|
|
{
|
|
_disposables = new CompositeDisposable();
|
|
GContext.OnEvent<EventStashDrop>().Subscribe(StashItemsByDrop).AddTo(_disposables);
|
|
GContext.OnEvent<EventStashItems>().Subscribe(StashItems).AddTo(_disposables);
|
|
GContext.OnEvent<EventStashDropList>().Subscribe(StashItemsByDropList).AddTo(_disposables);
|
|
GContext.OnEvent<EventStashItem>().Subscribe(e => StashItem(e.Item)).AddTo(_disposables);
|
|
}
|
|
|
|
public void DisposeSubscriptions()
|
|
{
|
|
_disposables.Dispose();
|
|
_disposables = null;
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
_items = new List<ItemData>();
|
|
//_items.Clear();
|
|
}
|
|
|
|
public void Flush()
|
|
{
|
|
Debug.Log("[DeferredRewardStashService] Flush.");
|
|
string textValue = LocalizationMgr.GetText("UI_MiniBattlePass_DivePopupPanel_8");
|
|
RewardType rewardType = RewardType.FishBoxFly;
|
|
if (IsEmpty)
|
|
{
|
|
rewardType = RewardType.FishBoxFlyRestore;
|
|
textValue = LocalizationMgr.GetText("UI_FishingRewardPanel_101042");
|
|
LoadData();
|
|
}
|
|
if (IsEmpty)
|
|
return;
|
|
ShowData showData = new ShowData(_items, rewardType);
|
|
showData.textValue = textValue;
|
|
//UI_FishingRewardPanel_101041
|
|
GContext.Publish(showData);
|
|
_playerItemData.AddItem(_items);
|
|
Reset();
|
|
UploadData();
|
|
}
|
|
|
|
private void StashItemsByDropList(EventStashDropList e)
|
|
{
|
|
foreach (var dropId in e.DropList)
|
|
{
|
|
var items = _playerItemData.GetItemDataByDropId(dropId);
|
|
foreach (var item in items)
|
|
{
|
|
StashItem(item);
|
|
}
|
|
}
|
|
UploadData();
|
|
}
|
|
|
|
private void StashItemsByDrop(EventStashDrop e)
|
|
{
|
|
var items = _playerItemData.GetItemDataByDropId(e.DropId);
|
|
foreach (var item in items)
|
|
{
|
|
StashItem(item);
|
|
}
|
|
UploadData();
|
|
}
|
|
|
|
private void StashItems(EventStashItems e)
|
|
{
|
|
var items = e.Items;
|
|
foreach (var item in items)
|
|
{
|
|
StashItem(item);
|
|
}
|
|
UploadData();
|
|
}
|
|
|
|
private void StashItem(ItemData item)
|
|
{
|
|
if (!_itemTable.DataMap.ContainsKey(item.id))
|
|
{
|
|
//Debug.LogError($"Item id {item.id} not found in item table.");
|
|
return;
|
|
}
|
|
// Debug.Log($"[RewardStash] Item {item.id} * {item.count} Stashed.");
|
|
var itemDropPackageItems = GetItemDropPackageItemList(item);
|
|
if (itemDropPackageItems != null && itemDropPackageItems.Count > 0)
|
|
{
|
|
foreach (var dropItem in itemDropPackageItems)
|
|
{
|
|
_items.Add(dropItem);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_items.Add(item);
|
|
}
|
|
|
|
UploadData();
|
|
// _playerItemData.AddItem(item);
|
|
}
|
|
List<ItemData> GetItemDropPackageItemList(ItemData itemData)
|
|
{
|
|
Item item = _itemTable.GetOrDefault(itemData.id);
|
|
if (item.Type == 9)
|
|
{
|
|
switch (item.SubType)
|
|
{
|
|
case 1:
|
|
case 3:
|
|
case 4:
|
|
case 7:
|
|
return _playerItemData.GetItemDropPackageItemList(item.RedirectID, itemData.count);
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
private string Serialize()
|
|
{
|
|
var sb = new StringBuilder();
|
|
foreach (var item in _items)
|
|
sb.Append(item.id + Comma + (int)item.count + Splitter);
|
|
if (sb.Length > 0)
|
|
sb.Remove(sb.Length - 1, 1);
|
|
return sb.ToString();
|
|
}
|
|
|
|
private List<ItemData> Deserialize(string s)
|
|
{
|
|
if (s == null || s == "")
|
|
return new List<ItemData>();
|
|
var items = s.Split(Splitter);
|
|
var res = new List<ItemData>();
|
|
foreach (var item in items)
|
|
{
|
|
var split = item.Split(Comma);
|
|
if (split.Length != 2)
|
|
{
|
|
Debug.LogWarning($"[DeferredRewardStashService]Wrong amount of parameters in \"{item}\".");
|
|
continue;
|
|
}
|
|
var id = int.Parse(split[0]);
|
|
var count = int.Parse(split[1]);
|
|
var itemData = new ItemData(id, count);
|
|
res.Add(itemData);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public void UploadData()
|
|
{
|
|
var s = Serialize();
|
|
PlayFabMgr.Instance.UpdateUserDataValue(Key, s);
|
|
}
|
|
|
|
public void LoadData()
|
|
{
|
|
Reset();
|
|
var s = PlayFabMgr.Instance.GetLocalData(Key);
|
|
if (string.IsNullOrEmpty(s))
|
|
return;
|
|
_items = Deserialize(s);
|
|
}
|
|
|
|
public class EventStashItems
|
|
{
|
|
public List<ItemData> Items;
|
|
}
|
|
|
|
public class EventStashDrop
|
|
{
|
|
public int DropId;
|
|
}
|
|
|
|
public class EventStashDropList
|
|
{
|
|
public List<int> DropList;
|
|
}
|
|
|
|
public class EventStashItem
|
|
{
|
|
public ItemData Item;
|
|
}
|
|
}
|