357 lines
11 KiB
C#
Raw Permalink Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2022-01-12
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "WheelProxy" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace G
{
/// <summary>
/// 轮盘
/// </summary>
public class WheelProxy : F.GameProxy
{
public class WheelInfo
{
public readonly ItemWheel item;
public readonly SlotInfo[] slotInfos;
public WheelInfo(ItemWheel wheel)
{
this.item = wheel;
var slots = wheel.slots;
slotInfos = new SlotInfo[slots.Length];
for (int i = 0; i < slotInfos.Length; i++)
{
slotInfos[i] = new SlotInfo
{
index = i,
weight = slots[i][0],
boxId = slots[i][1],
};
}
}
public void SetSlotReward(int slot, int id, int count, int state)
{
slotInfos[slot].reward = new Item.ItemInfo
{
id = id,
count = count,
};
slotInfos[slot].rewardState = state;
}
public void RefreshSlot()
{
foreach (var slotInfo in slotInfos)
{
var boxInfo = BoxProxy.Instance.GetBoxInfo(slotInfo.boxId);
slotInfo.reward = boxInfo.GetRndItem();
slotInfo.rewardState = 0;
}
}
public bool AutoRefresh()
{
foreach (var slotInfo in slotInfos)
{
if (slotInfo.rewardState == 0)
return false;
}
RefreshSlot();
return true;
}
public SlotInfo GetRndSlot()
{
var rndList = F.ListPool<int>.Get();
for (int i = 0; i < slotInfos.Length; i++)
{
if (slotInfos[i].rewardState == 0)
{
rndList.Add(i);
rndList.Add(slotInfos[i].weight);
}
}
if (rndList.Count > 0)
{
int rndId = GlobalUtils.GetRandom(rndList);
if (rndId >= 0)
{
return slotInfos[rndId];
}
}
return null;
}
}
public class SlotInfo
{
public int index;
public Item.ItemInfo reward;
public int boxId;
public int weight;
public int rewardState;
}
#region Field
readonly Dictionary<int, WheelInfo> _wheelInfos = new Dictionary<int, WheelInfo>(1);
#endregion
#region Property
/// <summary>
///
/// </summary>
public int luckValue
{
get { return MoneyProxy.Instance.GetMoney(35); }
}
/// <summary>
///
/// </summary>
public int wheelMoney
{
get { return Mathf.Max(0, InviteProxy.Instance.inviteCount - MoneyProxy.Instance.GetMoney(34)); }
}
/// <summary>
///
/// </summary>
public ActivityInfo luckAccActivity => ActivityProxy.Instance.GetActivity(702);
#endregion
#region Method
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public WheelInfo GetWheelInfo(int id)
{
_wheelInfos.TryGetValue(id, out var result);
return result;
}
/// <summary>
///
/// </summary>
/// <param name="callback"></param>
public void Spin(int id, Callback3 callback)
{
var wheelInfo = GetWheelInfo(id);
var slotInfo = wheelInfo.GetRndSlot();
if (slotInfo != null && slotInfo.rewardState == 0)
{
slotInfo.rewardState = 1;
wheelInfo.AutoRefresh();
SaveWheel();
RewardProxy.Instance.GetReward(slotInfo.reward);
MoneyProxy.Instance.AddMoney(34, 1);
MoneyProxy.Instance.AddMoney(35, 10);
UpdateActivty();
}
callback.Invoke(0, "", slotInfo);
}
public void RefreshSlot(Callback2 callback)
{
var wheelInfo = _wheelInfos[1];
wheelInfo.RefreshSlot();
SaveWheel();
callback.Invoke(0, "");
}
public void GetWheelSlotId(int id, int slot, Callback2 callback)
{
var wheelInfo = GetWheelInfo(id);
var slotInfo = wheelInfo.slotInfos[slot];
UI.RewardWindow.ShowReward(slotInfo.reward, callback);
}
public void GetRewards(ActivityInfo activityInfo, Callback2 callback)
{
var activity = this.luckAccActivity;
if (activity != null)
{
if (activity.status == 0)
{
bool completed = true;
foreach (var subInfo in activity.subInfos)
{
if (subInfo.status == 0)
{
completed = false;
break;
}
}
if (completed)
activity.status = 1;
}
if (activity.status == 1)
{
activity.status = 2;
ActivityProxy.Instance.SaveActivity(activity);
RewardProxy.Instance.GetRewardsWithUI(activity.rewardInfos, callback);
}
else if (activity.status == 0)
callback?.Invoke(1, "未完成");
else
callback?.Invoke(2, "已领取");
}
}
public void GetRewards(ActivitySubInfo subInfo, Callback2 callback)
{
var activity = this.luckAccActivity;
if (activity != null)
{
if (subInfo.status == 1)
{
subInfo.status = 2;
ActivityProxy.Instance.SaveActivity(activity);
RewardProxy.Instance.GetRewardsWithUI(subInfo.rewardInfos, callback);
}
else if (subInfo.status == 0)
callback?.Invoke(1, $"还需邀请{subInfo.maxValue - subInfo.curValue}人");
else
callback?.Invoke(2, "已领取");
}
}
public void UpdateActivty()
{
var activtyInfo = this.luckAccActivity;
var subInfos = activtyInfo.subInfos;
foreach (var subInfo in subInfos)
{
if (subInfo.status == 0)
{
subInfo.curValue = this.luckValue;
if (subInfo.curValue >= subInfo.maxValue)
{
subInfo.status = 1;
//foreach (var subInfo2 in subInfos)
//{
// if (subInfo2.status == 2)
// subInfo2.status = 1;
//}
break;
}
}
}
activtyInfo.curValue = 0;
if (activtyInfo.status == 0)
foreach (var subInfo in subInfos)
{
if (subInfo.status > 0)
{
activtyInfo.curValue += 1;
}
}
else
{
activtyInfo.curValue = activtyInfo.maxValue;
}
ActivityProxy.Instance.SaveActivity(activtyInfo);
}
const string ARCHIVE_KEY_V1 = "wheel_v1";
void LoadWheel()
{
_wheelInfos.Clear();
var wheelItems = ItemProxy.Instance.GetStaticItems<ItemWheel>();
if (wheelItems != null)
foreach (var wheelItem in wheelItems)
{
var wheelInfo = new WheelInfo(wheelItem);
wheelInfo.RefreshSlot();
_wheelInfos.Add(wheelItem.id, wheelInfo);
}
var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1);
if (dataList != null && dataList.Count > 0)
{
for (int i = 0; i < dataList.Count;)
{
int id = dataList[i++];
_wheelInfos.TryGetValue(id, out var wheelInfo);
int slotCount = dataList[i++];
for (int j = 0; j < slotCount; j++)
{
int slotRewardId = dataList[i++];
int slotRewardCount = dataList[i++];
int slotRewardState = dataList[i++];
wheelInfo.SetSlotReward(j, slotRewardId, slotRewardCount, slotRewardState);
}
}
}
}
void SaveWheel()
{
var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1);
if (dataList != null)
{
dataList.Clear();
foreach (var kv in _wheelInfos)
{
dataList.Add(kv.Key);
var slotInfos = kv.Value.slotInfos;
dataList.Add(slotInfos.Length);
foreach (var slotInfo in slotInfos)
{
dataList.Add(slotInfo.reward.id);
dataList.Add(slotInfo.reward.count);
dataList.Add(slotInfo.rewardState);
}
}
}
}
#endregion
#region Proxy
public static WheelProxy Instance => GetInstance<WheelProxy>();
public override void ReadArchive()
{
LoadWheel();
}
#endregion
}
}