mirror of
https://github.com/MikuLeaks/MikuSB.git
synced 2026-06-04 09:23:57 +00:00
SupporterCard Affix can now be changed.
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
using MikuSB.Data;
|
||||
using MikuSB.Data.Excel;
|
||||
using MikuSB.Database;
|
||||
using MikuSB.Database.Inventory;
|
||||
using MikuSB.Database.Player;
|
||||
using MikuSB.GameServer.Game.Support;
|
||||
using MikuSB.Proto;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.SupporterCard;
|
||||
|
||||
internal static class SupporterCardAffixShared
|
||||
{
|
||||
public const uint BaseGid = 150;
|
||||
public const uint FixedResetSid = 1;
|
||||
|
||||
public static SupportCardExcel? GetExcel(GameSupportCardInfo card)
|
||||
{
|
||||
return GameData.SupportCardData.FirstOrDefault(x => x.TemplateId == card.TemplateId);
|
||||
}
|
||||
|
||||
public static async Task SendResetResponse(Connection connection, NtfSyncPlayer? sync = null)
|
||||
{
|
||||
await CallGSRouter.SendScript(connection, "SupporterCard_ResetAffix", "null", sync!);
|
||||
}
|
||||
|
||||
public static async Task SendSelectResponse(Connection connection, NtfSyncPlayer? sync = null)
|
||||
{
|
||||
await CallGSRouter.SendScript(connection, "SupporterCard_SelectAffix", "null", sync!);
|
||||
}
|
||||
|
||||
public static List<Item> ConsumeCostItems(Connection connection, IEnumerable<IReadOnlyList<uint>> costs)
|
||||
{
|
||||
var player = connection.Player!;
|
||||
var syncItems = new List<Item>();
|
||||
|
||||
foreach (var cost in costs)
|
||||
{
|
||||
if (cost.Count < 5)
|
||||
continue;
|
||||
|
||||
var templateId = GameResourceTemplateId.FromGdpl(cost);
|
||||
var item = player.InventoryManager.InventoryData.Items.Values.FirstOrDefault(x => x.TemplateId == templateId);
|
||||
if (item == null || item.ItemCount < cost[4])
|
||||
throw new InvalidOperationException("support affix material not enough");
|
||||
|
||||
item.ItemCount -= cost[4];
|
||||
var proto = item.ToProto();
|
||||
if (item.ItemCount == 0)
|
||||
{
|
||||
player.InventoryManager.InventoryData.Items.Remove(item.UniqueId);
|
||||
proto.Count = 0;
|
||||
}
|
||||
syncItems.Add(proto);
|
||||
}
|
||||
|
||||
return syncItems;
|
||||
}
|
||||
|
||||
public static bool HasEnoughItems(Connection connection, IEnumerable<IReadOnlyList<uint>> costs)
|
||||
{
|
||||
var items = connection.Player!.InventoryManager.InventoryData.Items.Values;
|
||||
return costs.All(cost =>
|
||||
{
|
||||
if (cost.Count < 5)
|
||||
return false;
|
||||
|
||||
var templateId = GameResourceTemplateId.FromGdpl(cost);
|
||||
var item = items.FirstOrDefault(x => x.TemplateId == templateId);
|
||||
return item != null && item.ItemCount >= cost[4];
|
||||
});
|
||||
}
|
||||
|
||||
public static PlayerAttr GetOrCreateAttr(PlayerGameData data, uint gid, uint sid)
|
||||
{
|
||||
var attr = data.Attrs.FirstOrDefault(x => x.Gid == gid && x.Sid == sid);
|
||||
if (attr != null)
|
||||
return attr;
|
||||
|
||||
attr = new PlayerAttr { Gid = gid, Sid = sid, Val = 0 };
|
||||
data.Attrs.Add(attr);
|
||||
return attr;
|
||||
}
|
||||
|
||||
public static void SetAttr(Connection connection, NtfSyncPlayer sync, uint gid, uint sid, uint value)
|
||||
{
|
||||
var player = connection.Player!;
|
||||
var attr = GetOrCreateAttr(player.Data, gid, sid);
|
||||
attr.Val = value;
|
||||
sync.Custom[player.ToPackedAttrKey(gid, sid)] = value;
|
||||
sync.Custom[player.ToShiftedAttrKey(gid, sid)] = value;
|
||||
}
|
||||
|
||||
public static IEnumerable<uint> GetActiveAffixIds(GameSupportCardInfo card, params int[] ignoreSlots)
|
||||
{
|
||||
var ignored = ignoreSlots.ToHashSet();
|
||||
for (var slot = 1; slot <= SupportAffixStateService.ActiveThirdAffixSlot; slot++)
|
||||
{
|
||||
if (ignored.Contains(slot))
|
||||
continue;
|
||||
|
||||
var (affixId, _) = SupportAffixStateService.GetAffix(card, slot);
|
||||
if (affixId > 0)
|
||||
yield return affixId;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Save(Connection connection)
|
||||
{
|
||||
var player = connection.Player!;
|
||||
DatabaseHelper.SaveDatabaseType(player.InventoryManager.InventoryData);
|
||||
DatabaseHelper.SaveDatabaseType(player.Data);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class SupporterCardIdParam
|
||||
{
|
||||
[JsonPropertyName("Id")]
|
||||
public int SupportCardUid { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class SupporterCardSelectParam
|
||||
{
|
||||
[JsonPropertyName("Id")]
|
||||
public int SupportCardUid { get; set; }
|
||||
|
||||
[JsonPropertyName("SelectNew")]
|
||||
public bool SelectNew { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class SupporterCardResetInitialParam
|
||||
{
|
||||
[JsonPropertyName("Id")]
|
||||
public int SupportCardUid { get; set; }
|
||||
|
||||
[JsonPropertyName("Index")]
|
||||
public int Index { get; set; }
|
||||
|
||||
[JsonPropertyName("FixedId")]
|
||||
public uint FixedId { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class SupporterCardSelectInitialParam
|
||||
{
|
||||
[JsonPropertyName("Id")]
|
||||
public int SupportCardUid { get; set; }
|
||||
|
||||
[JsonPropertyName("Index")]
|
||||
public int Index { get; set; }
|
||||
|
||||
[JsonPropertyName("SelectNew")]
|
||||
public bool SelectNew { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.SupporterCard;
|
||||
|
||||
[CallGSApi("SupporterCard_FixedResetInitialAffix")]
|
||||
public class SupporterCard_FixedResetInitialAffix : ICallGSHandler
|
||||
{
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
await SupporterCard_ResetInitialAffix.Reset(connection, param, fixedMode: true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using MikuSB.Data;
|
||||
using MikuSB.Proto;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.SupporterCard;
|
||||
|
||||
[CallGSApi("SupporterCard_ReceiveFixedItem")]
|
||||
public class SupporterCard_ReceiveFixedItem : ICallGSHandler
|
||||
{
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var player = connection.Player!;
|
||||
if (!GameData.SupportFixedData.TryGetValue(1, out var fixedCfg) || fixedCfg.Item.Count < 5 || fixedCfg.Num <= 0)
|
||||
{
|
||||
await CallGSRouter.SendScript(connection, "SupporterCard_ReceiveFixedItem", "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
var attr = SupporterCardAffixShared.GetOrCreateAttr(player.Data, SupporterCardAffixShared.BaseGid, SupporterCardAffixShared.FixedResetSid);
|
||||
var claimCount = attr.Val / (uint)fixedCfg.Num;
|
||||
if (claimCount == 0)
|
||||
{
|
||||
await CallGSRouter.SendScript(connection, "SupporterCard_ReceiveFixedItem", "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
attr.Val %= (uint)fixedCfg.Num;
|
||||
|
||||
var rewardTemplateId = (uint)GameResourceTemplateId.FromGdpl(fixedCfg.Item);
|
||||
var rewardItem = GameData.SuppliesData.GetValueOrDefault(rewardTemplateId);
|
||||
if (rewardItem == null)
|
||||
{
|
||||
await CallGSRouter.SendScript(connection, "SupporterCard_ReceiveFixedItem", "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
var granted = await player.InventoryManager.AddSuppliesItem(rewardItem, claimCount * fixedCfg.Item[4], sendPacket: false);
|
||||
|
||||
var sync = new NtfSyncPlayer();
|
||||
if (granted != null)
|
||||
sync.Items.Add(granted.ToProto());
|
||||
SupporterCardAffixShared.SetAttr(connection, sync, SupporterCardAffixShared.BaseGid, SupporterCardAffixShared.FixedResetSid, attr.Val);
|
||||
SupporterCardAffixShared.Save(connection);
|
||||
|
||||
var arg = new JsonObject
|
||||
{
|
||||
["tbRewards"] = new JsonArray(
|
||||
(int)fixedCfg.Item[0],
|
||||
(int)fixedCfg.Item[1],
|
||||
(int)fixedCfg.Item[2],
|
||||
(int)fixedCfg.Item[3],
|
||||
(int)(claimCount * fixedCfg.Item[4]))
|
||||
}.ToJsonString();
|
||||
|
||||
await CallGSRouter.SendScript(connection, "SupporterCard_ReceiveFixedItem", arg, sync);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using MikuSB.GameServer.Game.Support;
|
||||
using MikuSB.Proto;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.SupporterCard;
|
||||
|
||||
[CallGSApi("SupporterCard_ResetAffix")]
|
||||
public class SupporterCard_ResetAffix : ICallGSHandler
|
||||
{
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var req = JsonSerializer.Deserialize<SupporterCardIdParam>(param);
|
||||
var card = req == null ? null : connection.Player!.InventoryManager.GetSupportCardItem((uint)req.SupportCardUid);
|
||||
var excel = card == null ? null : SupporterCardAffixShared.GetExcel(card);
|
||||
if (card == null || excel == null || excel.AffixCost.Count < 5 || !SupportAffixStateService.HasAffix(card, SupportAffixStateService.ActiveThirdAffixSlot))
|
||||
{
|
||||
await SupporterCardAffixShared.SendResetResponse(connection);
|
||||
return;
|
||||
}
|
||||
|
||||
var costs = new[] { excel.AffixCost };
|
||||
if (!SupporterCardAffixShared.HasEnoughItems(connection, costs))
|
||||
{
|
||||
await SupporterCardAffixShared.SendResetResponse(connection);
|
||||
return;
|
||||
}
|
||||
|
||||
var sync = new NtfSyncPlayer();
|
||||
sync.Items.AddRange(SupporterCardAffixShared.ConsumeCostItems(connection, costs));
|
||||
var excluded = SupporterCardAffixShared.GetActiveAffixIds(card, SupportAffixStateService.ActiveThirdAffixSlot);
|
||||
var (affixId, tier) = SupportAffixService.GenerateRandomAffix(excel.AffixPool[SupportAffixStateService.ActiveThirdAffixSlot - 1], excluded);
|
||||
SupportAffixStateService.SetAffix(card, SupportAffixStateService.PendingMaxAffixSlot, affixId, tier);
|
||||
sync.Items.Add(card.ToProto());
|
||||
|
||||
SupporterCardAffixShared.Save(connection);
|
||||
await SupporterCardAffixShared.SendResetResponse(connection, sync);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using MikuSB.GameServer.Game.Support;
|
||||
using MikuSB.Proto;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.SupporterCard;
|
||||
|
||||
[CallGSApi("SupporterCard_ResetInitialAffix")]
|
||||
public class SupporterCard_ResetInitialAffix : ICallGSHandler
|
||||
{
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
await Reset(connection, param, fixedMode: false);
|
||||
}
|
||||
|
||||
internal static async Task Reset(Connection connection, string param, bool fixedMode)
|
||||
{
|
||||
var req = JsonSerializer.Deserialize<SupporterCardResetInitialParam>(param);
|
||||
var card = req == null ? null : connection.Player!.InventoryManager.GetSupportCardItem((uint)req.SupportCardUid);
|
||||
var excel = card == null ? null : SupporterCardAffixShared.GetExcel(card);
|
||||
if (req == null || card == null || excel == null || req.Index is < 1 or > 2 || excel.AffixPool.Count < req.Index)
|
||||
{
|
||||
await SupporterCardAffixShared.SendResetResponse(connection);
|
||||
return;
|
||||
}
|
||||
|
||||
var costs = fixedMode ? new[] { excel.FixedAffixCost } : excel.InitialAffixCost;
|
||||
if (!costs.Any() || !SupporterCardAffixShared.HasEnoughItems(connection, costs))
|
||||
{
|
||||
await SupporterCardAffixShared.SendResetResponse(connection);
|
||||
return;
|
||||
}
|
||||
|
||||
var sync = new NtfSyncPlayer();
|
||||
sync.Items.AddRange(SupporterCardAffixShared.ConsumeCostItems(connection, costs));
|
||||
|
||||
uint affixId;
|
||||
uint tier;
|
||||
if (fixedMode && req.FixedId > 0)
|
||||
{
|
||||
affixId = req.FixedId;
|
||||
tier = SupportAffixService.GenerateTier(affixId);
|
||||
}
|
||||
else
|
||||
{
|
||||
var excluded = SupporterCardAffixShared.GetActiveAffixIds(card, req.Index);
|
||||
(affixId, tier) = SupportAffixService.GenerateRandomAffix(excel.AffixPool[req.Index - 1], excluded);
|
||||
}
|
||||
|
||||
SupportAffixStateService.SetAffix(card, SupportAffixStateService.PendingInitialAffixSlot, affixId, tier);
|
||||
card.AffixId = (uint)req.Index;
|
||||
|
||||
var attr = SupporterCardAffixShared.GetOrCreateAttr(connection.Player!.Data, SupporterCardAffixShared.BaseGid, SupporterCardAffixShared.FixedResetSid);
|
||||
attr.Val += 1;
|
||||
SupporterCardAffixShared.SetAttr(connection, sync, SupporterCardAffixShared.BaseGid, SupporterCardAffixShared.FixedResetSid, attr.Val);
|
||||
|
||||
sync.Items.Add(card.ToProto());
|
||||
SupporterCardAffixShared.Save(connection);
|
||||
await SupporterCardAffixShared.SendResetResponse(connection, sync);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using MikuSB.GameServer.Game.Support;
|
||||
using MikuSB.Proto;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.SupporterCard;
|
||||
|
||||
[CallGSApi("SupporterCard_SelectAffix")]
|
||||
public class SupporterCard_SelectAffix : ICallGSHandler
|
||||
{
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var req = JsonSerializer.Deserialize<SupporterCardSelectParam>(param);
|
||||
var card = req == null ? null : connection.Player!.InventoryManager.GetSupportCardItem((uint)req.SupportCardUid);
|
||||
if (card == null || !SupportAffixStateService.HasAffix(card, SupportAffixStateService.PendingMaxAffixSlot))
|
||||
{
|
||||
await SupporterCardAffixShared.SendSelectResponse(connection);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req!.SelectNew)
|
||||
SupportAffixStateService.CopyAffix(card, SupportAffixStateService.PendingMaxAffixSlot, SupportAffixStateService.ActiveThirdAffixSlot);
|
||||
|
||||
SupportAffixStateService.ClearAffix(card, SupportAffixStateService.PendingMaxAffixSlot);
|
||||
|
||||
var sync = new NtfSyncPlayer();
|
||||
sync.Items.Add(card.ToProto());
|
||||
SupporterCardAffixShared.Save(connection);
|
||||
await SupporterCardAffixShared.SendSelectResponse(connection, sync);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using MikuSB.GameServer.Game.Support;
|
||||
using MikuSB.Proto;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.SupporterCard;
|
||||
|
||||
[CallGSApi("SupporterCard_SelectInitialAffix")]
|
||||
public class SupporterCard_SelectInitialAffix : ICallGSHandler
|
||||
{
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var req = JsonSerializer.Deserialize<SupporterCardSelectInitialParam>(param);
|
||||
var card = req == null ? null : connection.Player!.InventoryManager.GetSupportCardItem((uint)req.SupportCardUid);
|
||||
if (req == null || card == null || req.Index is < 1 or > 2 || card.AffixId != req.Index || !SupportAffixStateService.HasAffix(card, SupportAffixStateService.PendingInitialAffixSlot))
|
||||
{
|
||||
await SupporterCardAffixShared.SendSelectResponse(connection);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.SelectNew)
|
||||
SupportAffixStateService.CopyAffix(card, SupportAffixStateService.PendingInitialAffixSlot, req.Index);
|
||||
|
||||
SupportAffixStateService.ClearAffix(card, SupportAffixStateService.PendingInitialAffixSlot);
|
||||
card.AffixId = 0;
|
||||
|
||||
var sync = new NtfSyncPlayer();
|
||||
sync.Items.Add(card.ToProto());
|
||||
SupporterCardAffixShared.Save(connection);
|
||||
await SupporterCardAffixShared.SendSelectResponse(connection, sync);
|
||||
}
|
||||
}
|
||||
@@ -86,17 +86,15 @@ public class SupporterCard_Upgrade : ICallGSHandler
|
||||
// Unlock next affix slot when reaching max level for the first time
|
||||
if (supportCardExcel != null)
|
||||
{
|
||||
var currentSlots = supportCard.Affixs.Count / 2;
|
||||
var currentSlots = Enumerable.Range(1, SupportAffixStateService.ActiveThirdAffixSlot)
|
||||
.Count(slot => SupportAffixStateService.HasAffix(supportCard, slot));
|
||||
var totalSlots = supportCardExcel.TotalAffixCount;
|
||||
if (currentSlots < totalSlots && currentSlots < supportCardExcel.AffixPool.Count)
|
||||
{
|
||||
var poolId = supportCardExcel.AffixPool[currentSlots];
|
||||
var (affixId, tier) = SupportAffixService.GenerateRandomAffix(poolId);
|
||||
if (affixId > 0)
|
||||
{
|
||||
supportCard.Affixs.Add(affixId);
|
||||
supportCard.Affixs.Add(tier);
|
||||
}
|
||||
SupportAffixStateService.SetAffix(supportCard, currentSlots + 1, affixId, tier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user