mirror of
https://github.com/MikuLeaks/MikuSB.git
synced 2026-06-04 18:23:58 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f81b5f6ec | ||
|
|
6063a3a0cd | ||
|
|
60101e75e2 | ||
|
|
6497bb1c66 |
20
Common/Data/Excel/DreamCardActivityExcel.cs
Normal file
20
Common/Data/Excel/DreamCardActivityExcel.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MikuSB.Data.Excel;
|
||||
|
||||
[ResourceEntity("dlc/DreamCard/activity.json")]
|
||||
public class DreamCardActivityExcel : ExcelResource
|
||||
{
|
||||
[JsonProperty("ID")] public uint ID { get; set; }
|
||||
[JsonProperty("StartTime")] public string StartTime { get; set; } = "";
|
||||
[JsonProperty("EndTime")] public string EndTime { get; set; } = "";
|
||||
[JsonProperty("Condition")] public string Condition { get; set; } = "";
|
||||
[JsonProperty("LevelListID")] public List<uint> LevelListID { get; set; } = [];
|
||||
|
||||
public override uint GetId() => ID;
|
||||
|
||||
public override void Loaded()
|
||||
{
|
||||
GameData.DreamCardActivityData[ID] = this;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ public class VirCaptureLevelListExcel : ExcelResource
|
||||
[JsonProperty("Exp")] public uint Exp { get; set; }
|
||||
[JsonProperty("Num")] public uint Num { get; set; }
|
||||
[JsonProperty("MaxCost")] public uint MaxCost { get; set; }
|
||||
[JsonProperty("Rewards")] public List<List<uint>> Rewards { get; set; } = [];
|
||||
[JsonProperty("ExpUp")] public double ExpUp { get; set; }
|
||||
|
||||
public override uint GetId() => Level;
|
||||
|
||||
@@ -58,6 +58,7 @@ public static class GameData
|
||||
public static Dictionary<ulong, MonsterCardExcel> MonsterCardData { get; private set; } = [];
|
||||
public static Dictionary<uint, FishingFoodExcel> FishingFoodData { get; private set; } = [];
|
||||
public static Dictionary<uint, VirCaptureTowerExcel> VirCaptureTowerData { get; private set; } = [];
|
||||
public static Dictionary<uint, DreamCardActivityExcel> DreamCardActivityData { get; private set; } = [];
|
||||
}
|
||||
|
||||
public static class GameResourceTemplateId
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
using MikuSB.GameServer.Game.BossPvp;
|
||||
using MikuSB.Proto;
|
||||
using MikuSB.GameServer.Server.CallGS.Handlers.DreamCard;
|
||||
using MikuSB.GameServer.Server.CallGS.Handlers.Tower;
|
||||
using MikuSB.GameServer.Server.CallGS.Handlers.VirCapture;
|
||||
|
||||
@@ -80,6 +81,13 @@ public class Chapter_DealLevelSettlement : ICallGSHandler
|
||||
return response;
|
||||
}
|
||||
|
||||
if (string.Equals(sCmd, "DreamCard_LevelSettlement", StringComparison.Ordinal))
|
||||
{
|
||||
var (response, sync) = DreamCard_LevelSettlement.HandleSettlement(connection.Player!, tbParam);
|
||||
extraSync = sync;
|
||||
return response;
|
||||
}
|
||||
|
||||
return tbParam?.DeepClone() ?? new JsonObject();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
using MikuSB.Data;
|
||||
using MikuSB.Data.Excel;
|
||||
using System.Globalization;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.DreamCard;
|
||||
|
||||
[CallGSApi("DreamCard_CheckOpen")]
|
||||
public class DreamCard_CheckOpen : ICallGSHandler
|
||||
{
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var ids = GameData.DreamCardActivityData.Values
|
||||
.Where(x => IsOpen(x, now))
|
||||
.OrderBy(x => x.ID)
|
||||
.Select(x => JsonValue.Create(x.ID))
|
||||
.ToArray();
|
||||
|
||||
var response = new JsonObject
|
||||
{
|
||||
["tbID"] = new JsonArray(ids)
|
||||
};
|
||||
|
||||
await CallGSRouter.SendScript(connection, "DreamCard_CheckOpen", response.ToJsonString());
|
||||
}
|
||||
|
||||
private static bool IsOpen(DreamCardActivityExcel config, DateTime now)
|
||||
{
|
||||
var start = ParseConfigTime(config.StartTime);
|
||||
if (!start.HasValue || start > now)
|
||||
return false;
|
||||
|
||||
var end = ParseConfigTime(config.EndTime);
|
||||
if (end.HasValue && now >= end.Value)
|
||||
return false;
|
||||
|
||||
return string.IsNullOrWhiteSpace(config.Condition);
|
||||
}
|
||||
|
||||
private static DateTime? ParseConfigTime(string? raw)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(raw))
|
||||
return null;
|
||||
|
||||
var normalized = raw.Trim().Trim('[', ']');
|
||||
if (normalized.Length != 12)
|
||||
return null;
|
||||
|
||||
return DateTime.TryParseExact(
|
||||
normalized,
|
||||
"yyyyMMddHHmm",
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None,
|
||||
out var value)
|
||||
? value
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
using MikuSB.Data;
|
||||
using MikuSB.Util;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.DreamCard;
|
||||
|
||||
[CallGSApi("DreamCard_EnterLevel")]
|
||||
public class DreamCard_EnterLevel : ICallGSHandler
|
||||
{
|
||||
private static readonly Random Random = new();
|
||||
private static readonly Lazy<DreamCardLevelIndex?> LevelIndex = new(LoadLevelIndex);
|
||||
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var req = JsonSerializer.Deserialize<DreamCardEnterLevelParam>(param);
|
||||
if (req == null || req.LevelId <= 0 || req.Diff <= 0 || req.Type is < 1 or > 3)
|
||||
{
|
||||
await CallGSRouter.SendScript(connection, "DreamCard_EnterLevel", "null");
|
||||
return;
|
||||
}
|
||||
|
||||
var now = DateTime.Now;
|
||||
if (!IsAllowed(req, now))
|
||||
{
|
||||
await CallGSRouter.SendScript(connection, "DreamCard_EnterLevel", "null");
|
||||
return;
|
||||
}
|
||||
|
||||
var response = new JsonObject
|
||||
{
|
||||
["nSeed"] = Random.Next(1, 1_000_000_000),
|
||||
["nID"] = req.LevelId,
|
||||
["nDiff"] = req.Diff,
|
||||
["nType"] = req.Type
|
||||
};
|
||||
|
||||
await CallGSRouter.SendScript(connection, "DreamCard_EnterLevel", response.ToJsonString());
|
||||
}
|
||||
|
||||
private static bool IsAllowed(DreamCardEnterLevelParam req, DateTime now)
|
||||
{
|
||||
var index = LevelIndex.Value;
|
||||
if (index == null)
|
||||
return true;
|
||||
|
||||
return req.Type switch
|
||||
{
|
||||
1 => index.OpenOrdinaryLevelIds(now).Contains((uint)req.LevelId),
|
||||
2 => index.IsChallengeOpen((uint)req.LevelId, now),
|
||||
3 => index.IsEndlessOpen((uint)req.LevelId, now),
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
private static DreamCardLevelIndex? LoadLevelIndex()
|
||||
{
|
||||
try
|
||||
{
|
||||
var resourceRoot = ConfigManager.Config.Path.ResourcePath;
|
||||
var dreamCardRoot = Path.Combine(resourceRoot, "dlc", "DreamCard");
|
||||
|
||||
var ordinaryLevels = LoadJson<List<DreamCardOrdinaryLevelEntry>>(Path.Combine(dreamCardRoot, "levellist.json")) ?? [];
|
||||
var challengeLevels = LoadJson<List<DreamCardChallengeLevelEntry>>(Path.Combine(dreamCardRoot, "challenge.json")) ?? [];
|
||||
var endlessLevels = LoadJson<List<DreamCardEndlessLevelEntry>>(Path.Combine(dreamCardRoot, "endless.json")) ?? [];
|
||||
|
||||
return new DreamCardLevelIndex(ordinaryLevels, challengeLevels, endlessLevels);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static T? LoadJson<T>(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
return default;
|
||||
|
||||
return JsonSerializer.Deserialize<T>(File.ReadAllText(path));
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DreamCardEnterLevelParam
|
||||
{
|
||||
[JsonPropertyName("nID")]
|
||||
public int LevelId { get; set; }
|
||||
|
||||
[JsonPropertyName("nDiff")]
|
||||
public int Diff { get; set; }
|
||||
|
||||
[JsonPropertyName("nType")]
|
||||
public int Type { get; set; }
|
||||
|
||||
[JsonPropertyName("nRoleId")]
|
||||
public int RoleId { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class DreamCardLevelIndex
|
||||
{
|
||||
private readonly HashSet<uint> ordinaryLevelIds;
|
||||
private readonly Dictionary<uint, DreamCardChallengeLevelEntry> challengeLevels;
|
||||
private readonly Dictionary<uint, DreamCardEndlessLevelEntry> endlessLevels;
|
||||
|
||||
public DreamCardLevelIndex(
|
||||
IEnumerable<DreamCardOrdinaryLevelEntry> ordinaryLevels,
|
||||
IEnumerable<DreamCardChallengeLevelEntry> challengeLevels,
|
||||
IEnumerable<DreamCardEndlessLevelEntry> endlessLevels)
|
||||
{
|
||||
ordinaryLevelIds = ordinaryLevels
|
||||
.Where(x => x.LevelListId > 0)
|
||||
.Select(x => x.LevelListId)
|
||||
.ToHashSet();
|
||||
|
||||
this.challengeLevels = challengeLevels
|
||||
.Where(x => x.ChallengeId > 0)
|
||||
.GroupBy(x => x.ChallengeId)
|
||||
.ToDictionary(x => x.Key, x => x.First());
|
||||
|
||||
this.endlessLevels = endlessLevels
|
||||
.Where(x => x.EndlessId > 0)
|
||||
.GroupBy(x => x.EndlessId)
|
||||
.ToDictionary(x => x.Key, x => x.First());
|
||||
}
|
||||
|
||||
public HashSet<uint> OpenOrdinaryLevelIds(DateTime now)
|
||||
{
|
||||
var ids = new HashSet<uint>();
|
||||
foreach (var activity in GameData.DreamCardActivityData.Values)
|
||||
{
|
||||
if (!IsActivityOpen(activity, now))
|
||||
continue;
|
||||
|
||||
foreach (var id in activity.LevelListID)
|
||||
{
|
||||
if (ordinaryLevelIds.Contains(id))
|
||||
ids.Add(id);
|
||||
}
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
public bool IsChallengeOpen(uint id, DateTime now)
|
||||
{
|
||||
return challengeLevels.TryGetValue(id, out var entry) && IsWithin(entry.StartTime, entry.EndTime, now);
|
||||
}
|
||||
|
||||
public bool IsEndlessOpen(uint id, DateTime now)
|
||||
{
|
||||
return endlessLevels.TryGetValue(id, out var entry) && IsWithin(entry.StartTime, entry.EndTime, now);
|
||||
}
|
||||
|
||||
private static bool IsActivityOpen(Data.Excel.DreamCardActivityExcel config, DateTime now)
|
||||
{
|
||||
var start = ParseConfigTime(config.StartTime);
|
||||
if (!start.HasValue || start > now)
|
||||
return false;
|
||||
|
||||
var end = ParseConfigTime(config.EndTime);
|
||||
if (end.HasValue && now >= end.Value)
|
||||
return false;
|
||||
|
||||
return string.IsNullOrWhiteSpace(config.Condition);
|
||||
}
|
||||
|
||||
private static bool IsWithin(string? startRaw, string? endRaw, DateTime now)
|
||||
{
|
||||
var start = ParseConfigTime(startRaw);
|
||||
if (!start.HasValue || now < start.Value)
|
||||
return false;
|
||||
|
||||
var end = ParseConfigTime(endRaw);
|
||||
return !end.HasValue || now < end.Value;
|
||||
}
|
||||
|
||||
private static DateTime? ParseConfigTime(string? raw)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(raw))
|
||||
return null;
|
||||
|
||||
var normalized = raw.Trim().Trim('[', ']');
|
||||
if (normalized.Length != 12)
|
||||
return null;
|
||||
|
||||
return DateTime.TryParseExact(
|
||||
normalized,
|
||||
"yyyyMMddHHmm",
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None,
|
||||
out var value)
|
||||
? value
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DreamCardOrdinaryLevelEntry
|
||||
{
|
||||
[JsonPropertyName("LevelListID")]
|
||||
public uint LevelListId { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class DreamCardChallengeLevelEntry
|
||||
{
|
||||
[JsonPropertyName("ChallengeId")]
|
||||
public uint ChallengeId { get; set; }
|
||||
|
||||
[JsonPropertyName("StartTime")]
|
||||
public string StartTime { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("EndTime")]
|
||||
public string EndTime { get; set; } = "";
|
||||
}
|
||||
|
||||
internal sealed class DreamCardEndlessLevelEntry
|
||||
{
|
||||
[JsonPropertyName("EndlessID")]
|
||||
public uint EndlessId { get; set; }
|
||||
|
||||
[JsonPropertyName("StartTime")]
|
||||
public string StartTime { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("EndTime")]
|
||||
public string EndTime { get; set; } = "";
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
using MikuSB.Database;
|
||||
using MikuSB.Database.Player;
|
||||
using MikuSB.GameServer.Game.Player;
|
||||
using MikuSB.Proto;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.DreamCard;
|
||||
|
||||
[CallGSApi("DreamCard_LevelSettlement")]
|
||||
public class DreamCard_LevelSettlement : ICallGSHandler
|
||||
{
|
||||
private const uint LevelGroupId = 152;
|
||||
private const uint LevelSubNum = 10;
|
||||
private const int OrdinaryType = 1;
|
||||
private const int ChallengeType = 2;
|
||||
private const int EndlessType = 3;
|
||||
|
||||
private static readonly Lazy<DreamCardSettlementIndex?> SettlementIndex = new(LoadIndex);
|
||||
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var (response, sync) = HandleSettlement(connection.Player!, JsonNode.Parse(param));
|
||||
await CallGSRouter.SendScript(connection, "DreamCard_LevelSettlement", response.ToJsonString(), sync);
|
||||
}
|
||||
|
||||
public static (JsonObject Response, NtfSyncPlayer Sync) HandleSettlement(PlayerInstance player, JsonNode? tbParam)
|
||||
{
|
||||
var req = tbParam?.Deserialize<DreamCardLevelSettlementParam>();
|
||||
if (req == null || req.LevelId <= 0 || req.Diff <= 0 || req.Type is < OrdinaryType or > EndlessType)
|
||||
return (new JsonObject { ["sErr"] = "error.BadParam" }, new NtfSyncPlayer());
|
||||
|
||||
var sync = new NtfSyncPlayer();
|
||||
var response = new JsonObject
|
||||
{
|
||||
["nID"] = req.LevelId,
|
||||
["nDiff"] = req.Diff,
|
||||
["nType"] = req.Type
|
||||
};
|
||||
|
||||
switch (req.Type)
|
||||
{
|
||||
case OrdinaryType:
|
||||
HandleOrdinary(player, sync, response, req);
|
||||
break;
|
||||
case ChallengeType:
|
||||
HandleChallenge(player, sync, response, req);
|
||||
break;
|
||||
case EndlessType:
|
||||
HandleEndless(response, req);
|
||||
break;
|
||||
}
|
||||
|
||||
DatabaseHelper.SaveDatabaseType(player.Data);
|
||||
return (response, sync);
|
||||
}
|
||||
|
||||
private static void HandleOrdinary(PlayerInstance player, NtfSyncPlayer sync, JsonObject response, DreamCardLevelSettlementParam req)
|
||||
{
|
||||
var baseSid = (uint)(LevelSubNum * req.LevelId);
|
||||
|
||||
var passAttr = GetOrCreateAttr(player.Data, LevelGroupId, baseSid + 1);
|
||||
passAttr.Val += 1;
|
||||
SyncAttr(sync, player, passAttr);
|
||||
|
||||
var diffAttr = GetOrCreateAttr(player.Data, LevelGroupId, baseSid + 2);
|
||||
diffAttr.Val = Math.Max(diffAttr.Val, (uint)req.Diff);
|
||||
SyncAttr(sync, player, diffAttr);
|
||||
|
||||
var starAttr = GetOrCreateAttr(player.Data, LevelGroupId, baseSid + 3);
|
||||
starAttr.Val = MergeDifficultyBits(starAttr.Val, req.Diff, req.StarValue);
|
||||
SyncAttr(sync, player, starAttr);
|
||||
|
||||
if (TryGetOrdinaryRewardId((uint)req.LevelId, (uint)req.Diff, out var rewardId) && rewardId > 0)
|
||||
response["nRewardID"] = rewardId;
|
||||
}
|
||||
|
||||
private static void HandleChallenge(PlayerInstance player, NtfSyncPlayer sync, JsonObject response, DreamCardLevelSettlementParam req)
|
||||
{
|
||||
var baseSid = (uint)(LevelSubNum * req.LevelId);
|
||||
var scoreSid = baseSid + (uint)req.Diff + 4;
|
||||
|
||||
var currentScore = (uint)Math.Max(0, req.Score);
|
||||
var scoreAttr = GetOrCreateAttr(player.Data, LevelGroupId, scoreSid);
|
||||
var newRecord = currentScore > scoreAttr.Val;
|
||||
scoreAttr.Val = Math.Max(scoreAttr.Val, currentScore);
|
||||
SyncAttr(sync, player, scoreAttr);
|
||||
|
||||
var challengePeriodId = ResolveCurrentChallengePeriodId(DateTime.Now);
|
||||
if (challengePeriodId > 0)
|
||||
{
|
||||
var periodAttr = GetOrCreateAttr(player.Data, LevelGroupId, 0);
|
||||
periodAttr.Val = challengePeriodId;
|
||||
SyncAttr(sync, player, periodAttr);
|
||||
}
|
||||
|
||||
response["NewRecord"] = newRecord;
|
||||
}
|
||||
|
||||
private static void HandleEndless(JsonObject response, DreamCardLevelSettlementParam req)
|
||||
{
|
||||
response["NewRecord"] = false;
|
||||
}
|
||||
|
||||
private static uint MergeDifficultyBits(uint currentValue, int diff, int starMask)
|
||||
{
|
||||
var bitStart = Math.Max(0, diff - 1) * 3;
|
||||
var result = currentValue;
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
if (((starMask >> i) & 1) == 0)
|
||||
continue;
|
||||
|
||||
result |= 1u << (bitStart + i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static bool TryGetOrdinaryRewardId(uint levelId, uint diff, out uint rewardId)
|
||||
{
|
||||
rewardId = 0;
|
||||
var index = SettlementIndex.Value;
|
||||
if (index == null)
|
||||
return false;
|
||||
|
||||
return index.TryGetOrdinaryRewardId(levelId, diff, out rewardId);
|
||||
}
|
||||
|
||||
private static uint ResolveCurrentChallengePeriodId(DateTime now)
|
||||
{
|
||||
var index = SettlementIndex.Value;
|
||||
return index?.ResolveCurrentChallengePeriodId(now) ?? 0;
|
||||
}
|
||||
|
||||
private static DreamCardSettlementIndex? LoadIndex()
|
||||
{
|
||||
try
|
||||
{
|
||||
var root = Path.Combine(MikuSB.Util.ConfigManager.Config.Path.ResourcePath, "dlc", "DreamCard");
|
||||
var ordinaryLevels = LoadJson<List<DreamCardOrdinarySettlementEntry>>(Path.Combine(root, "levellist.json")) ?? [];
|
||||
var challengeTimes = LoadJson<List<DreamCardChallengeTimeEntry>>(Path.Combine(root, "chall_time.json")) ?? [];
|
||||
return new DreamCardSettlementIndex(ordinaryLevels, challengeTimes);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static T? LoadJson<T>(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
return default;
|
||||
|
||||
return JsonSerializer.Deserialize<T>(File.ReadAllText(path));
|
||||
}
|
||||
|
||||
private 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
|
||||
};
|
||||
data.Attrs.Add(attr);
|
||||
return attr;
|
||||
}
|
||||
|
||||
private static void SyncAttr(NtfSyncPlayer sync, PlayerInstance player, PlayerAttr attr)
|
||||
{
|
||||
sync.Custom[player.ToPackedAttrKey(attr.Gid, attr.Sid)] = attr.Val;
|
||||
sync.Custom[player.ToShiftedAttrKey(attr.Gid, attr.Sid)] = attr.Val;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DreamCardLevelSettlementParam
|
||||
{
|
||||
[JsonPropertyName("nID")]
|
||||
public int LevelId { get; set; }
|
||||
|
||||
[JsonPropertyName("nDiff")]
|
||||
public int Diff { get; set; }
|
||||
|
||||
[JsonPropertyName("nType")]
|
||||
public int Type { get; set; }
|
||||
|
||||
[JsonPropertyName("nStarValue")]
|
||||
public int StarValue { get; set; }
|
||||
|
||||
[JsonPropertyName("nScore")]
|
||||
public int Score { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class DreamCardSettlementIndex
|
||||
{
|
||||
private readonly Dictionary<(uint LevelId, uint Diff), uint> ordinaryRewardIds;
|
||||
private readonly List<DreamCardChallengeTimeEntry> challengeTimes;
|
||||
|
||||
public DreamCardSettlementIndex(
|
||||
IEnumerable<DreamCardOrdinarySettlementEntry> ordinaryLevels,
|
||||
IEnumerable<DreamCardChallengeTimeEntry> challengeTimes)
|
||||
{
|
||||
ordinaryRewardIds = ordinaryLevels
|
||||
.Where(x => x.LevelListId > 0 && x.HardStage > 0)
|
||||
.GroupBy(x => (x.LevelListId, x.HardStage))
|
||||
.ToDictionary(x => x.Key, x => x.First().RewardId);
|
||||
|
||||
this.challengeTimes = challengeTimes.ToList();
|
||||
}
|
||||
|
||||
public bool TryGetOrdinaryRewardId(uint levelId, uint diff, out uint rewardId)
|
||||
{
|
||||
return ordinaryRewardIds.TryGetValue((levelId, diff), out rewardId);
|
||||
}
|
||||
|
||||
public uint ResolveCurrentChallengePeriodId(DateTime now)
|
||||
{
|
||||
foreach (var entry in challengeTimes.OrderBy(x => x.ChallTimeId))
|
||||
{
|
||||
var start = ParseConfigTime(entry.StartTime);
|
||||
var end = ParseConfigTime(entry.EndTime);
|
||||
if (!start.HasValue || !end.HasValue)
|
||||
continue;
|
||||
|
||||
if (start.Value <= now && now < end.Value)
|
||||
return entry.ChallTimeId;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static DateTime? ParseConfigTime(string? raw)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(raw))
|
||||
return null;
|
||||
|
||||
var normalized = raw.Trim().Trim('[', ']');
|
||||
if (normalized.Length != 12)
|
||||
return null;
|
||||
|
||||
return DateTime.TryParseExact(
|
||||
normalized,
|
||||
"yyyyMMddHHmm",
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None,
|
||||
out var value)
|
||||
? value
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DreamCardOrdinarySettlementEntry
|
||||
{
|
||||
[JsonPropertyName("LevelListID")]
|
||||
public uint LevelListId { get; set; }
|
||||
|
||||
[JsonPropertyName("HardStage")]
|
||||
public uint HardStage { get; set; }
|
||||
|
||||
[JsonPropertyName("RewardID")]
|
||||
public uint RewardId { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class DreamCardChallengeTimeEntry
|
||||
{
|
||||
[JsonPropertyName("ChallTimeID")]
|
||||
public uint ChallTimeId { get; set; }
|
||||
|
||||
[JsonPropertyName("StartTime")]
|
||||
public string StartTime { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("EndTime")]
|
||||
public string EndTime { get; set; } = "";
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using MikuSB.Database;
|
||||
using MikuSB.Proto;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.DreamCard;
|
||||
|
||||
[CallGSApi("DreamCard_UpdateData")]
|
||||
public class DreamCard_UpdateData : ICallGSHandler
|
||||
{
|
||||
private const uint DataGroupId = 62;
|
||||
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var player = connection.Player!;
|
||||
var sync = new NtfSyncPlayer();
|
||||
var dirty = false;
|
||||
|
||||
try
|
||||
{
|
||||
var entries = JsonSerializer.Deserialize<List<DreamCardUpdateDataEntry>>(param) ?? [];
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
if (entry.Id <= 0)
|
||||
continue;
|
||||
|
||||
var value = NormalizeJson(entry.Data);
|
||||
player.SetStrAttr(DataGroupId, (uint)entry.Id, value);
|
||||
sync.CustomStr[player.ToShiftedAttrKey(DataGroupId, (uint)entry.Id)] = value;
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore malformed payloads so the client-side save queue can continue.
|
||||
}
|
||||
|
||||
if (dirty)
|
||||
DatabaseHelper.SaveDatabaseType(player.Data);
|
||||
|
||||
await CallGSRouter.SendScript(connection, "DreamCard_UpdateData", "{}", sync);
|
||||
}
|
||||
|
||||
private static string NormalizeJson(JsonElement data)
|
||||
{
|
||||
return data.ValueKind == JsonValueKind.Undefined
|
||||
? "null"
|
||||
: data.GetRawText();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class DreamCardUpdateDataEntry
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[JsonPropertyName("data")]
|
||||
public JsonElement Data { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
using MikuSB.Data;
|
||||
using MikuSB.Data.Excel;
|
||||
using MikuSB.Database;
|
||||
using MikuSB.Database.Inventory;
|
||||
using MikuSB.Database.Player;
|
||||
using MikuSB.Enums.Item;
|
||||
using MikuSB.GameServer.Game.Player;
|
||||
using MikuSB.Proto;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MikuSB.GameServer.Server.CallGS.Handlers.VirCapture;
|
||||
|
||||
[CallGSApi("VirCapture_GetLevelAward")]
|
||||
public class VirCapture_GetLevelAward : ICallGSHandler
|
||||
{
|
||||
private const uint VirCaptureGroupId = 128;
|
||||
private const uint CurLevelSid = 3;
|
||||
private const uint LevelAwardFlagStartSid = 101;
|
||||
private const uint LevelAwardFlagEndSid = 120;
|
||||
|
||||
public async Task Handle(Connection connection, string param, ushort seqNo)
|
||||
{
|
||||
var player = connection.Player!;
|
||||
var req = JsonSerializer.Deserialize<VirCaptureGetLevelAwardParam>(param);
|
||||
if (req == null || req.IdList == null || req.IdList.Count == 0)
|
||||
{
|
||||
await CallGSRouter.SendScript(connection, "VirCapture_GetLevelAward", "{\"tbAwardList\":[]}");
|
||||
return;
|
||||
}
|
||||
|
||||
var curLevel = player.Data.Attrs.FirstOrDefault(x => x.Gid == VirCaptureGroupId && x.Sid == CurLevelSid)?.Val ?? 0;
|
||||
var requestedLevels = req.IdList
|
||||
.Where(x => x > 0)
|
||||
.Select(x => (uint)x)
|
||||
.Distinct()
|
||||
.OrderBy(x => x)
|
||||
.ToList();
|
||||
|
||||
var claimLevels = requestedLevels
|
||||
.Where(level => level <= curLevel && CanClaimLevel(player.Data, level))
|
||||
.ToList();
|
||||
|
||||
var sync = new NtfSyncPlayer();
|
||||
var responseAwards = new JsonArray();
|
||||
|
||||
foreach (var level in claimLevels)
|
||||
{
|
||||
if (!GameData.VirCaptureLevelListData.TryGetValue(level, out var levelCfg) ||
|
||||
levelCfg.Rewards.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
SetClaimed(player, sync, level);
|
||||
|
||||
foreach (var reward in levelCfg.Rewards)
|
||||
{
|
||||
if (reward.Count < 5)
|
||||
continue;
|
||||
|
||||
await GrantRewardAsync(player, sync, reward);
|
||||
responseAwards.Add(new JsonArray(
|
||||
(int)reward[0],
|
||||
(int)reward[1],
|
||||
(int)reward[2],
|
||||
(int)reward[3],
|
||||
(int)reward[4]));
|
||||
}
|
||||
}
|
||||
|
||||
DatabaseHelper.SaveDatabaseType(player.Data);
|
||||
DatabaseHelper.SaveDatabaseType(player.InventoryManager.InventoryData);
|
||||
DatabaseHelper.SaveDatabaseType(player.CharacterManager.CharacterData);
|
||||
|
||||
var rsp = new JsonObject
|
||||
{
|
||||
["tbAwardList"] = responseAwards
|
||||
};
|
||||
await CallGSRouter.SendScript(connection, "VirCapture_GetLevelAward", rsp.ToJsonString(), sync);
|
||||
}
|
||||
|
||||
private static bool CanClaimLevel(PlayerGameData data, uint level)
|
||||
{
|
||||
var sid = GetLevelAwardSid(level);
|
||||
if (sid < LevelAwardFlagStartSid || sid > LevelAwardFlagEndSid)
|
||||
return false;
|
||||
|
||||
var pos = GetLevelAwardBit(level);
|
||||
var attr = data.Attrs.FirstOrDefault(x => x.Gid == VirCaptureGroupId && x.Sid == sid);
|
||||
return ((attr?.Val ?? 0) & (1u << pos)) == 0;
|
||||
}
|
||||
|
||||
private static void SetClaimed(PlayerInstance player, NtfSyncPlayer sync, uint level)
|
||||
{
|
||||
var sid = GetLevelAwardSid(level);
|
||||
var pos = GetLevelAwardBit(level);
|
||||
var attr = GetOrCreateAttr(player.Data, VirCaptureGroupId, sid);
|
||||
attr.Val |= 1u << pos;
|
||||
sync.Custom[player.ToPackedAttrKey(VirCaptureGroupId, sid)] = attr.Val;
|
||||
sync.Custom[player.ToShiftedAttrKey(VirCaptureGroupId, sid)] = attr.Val;
|
||||
}
|
||||
|
||||
private static uint GetLevelAwardSid(uint level) => LevelAwardFlagStartSid + (level / 30);
|
||||
|
||||
private static int GetLevelAwardBit(uint level) => (int)(level % 30);
|
||||
|
||||
private 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;
|
||||
}
|
||||
|
||||
private static async Task GrantRewardAsync(PlayerInstance player, NtfSyncPlayer sync, IReadOnlyList<uint> reward)
|
||||
{
|
||||
var itemType = (ItemTypeEnum)reward[0];
|
||||
var detail = reward[1];
|
||||
var particular = reward[2];
|
||||
var level = reward[3];
|
||||
var count = Math.Max(1u, reward[4]);
|
||||
|
||||
switch (itemType)
|
||||
{
|
||||
case ItemTypeEnum.TYPE_CARD:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var character = await player.CharacterManager.AddCharacter(itemType, detail, particular, level, sendPacket: false);
|
||||
if (character != null)
|
||||
sync.Items.Add(character.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_WEAPON:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var weapon = await player.InventoryManager.AddWeaponItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (weapon != null)
|
||||
sync.Items.Add(weapon.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_SUPPORT:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var support = await player.InventoryManager.AddSupportCardItem(detail, particular, level, sendPacket: false);
|
||||
if (support != null)
|
||||
sync.Items.Add(support.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_SUPPLIES:
|
||||
{
|
||||
var templateId = (uint)GameResourceTemplateId.FromGdpl(reward[0], detail, particular, level);
|
||||
if (GameData.SuppliesData.TryGetValue(templateId, out var supplies))
|
||||
{
|
||||
var item = await player.InventoryManager.AddSuppliesItem(supplies, count, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ItemTypeEnum.TYPE_USEABLE:
|
||||
{
|
||||
var item = AddOtherItem(player.InventoryManager.InventoryData, reward[0], detail, particular, level, count);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
break;
|
||||
}
|
||||
case ItemTypeEnum.TYPE_WEAPON_PART:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddWeaponPartItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_CARD_SKIN:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddSkinItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_HOUSE:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddHouseFurnitureItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_PROFILE:
|
||||
case ItemTypeEnum.TYPE_FRAME:
|
||||
case ItemTypeEnum.TYPE_BADGE:
|
||||
case ItemTypeEnum.TYPE_COVER:
|
||||
case ItemTypeEnum.TYPE_NAMECARD:
|
||||
case ItemTypeEnum.TYPE_EXPRESSION:
|
||||
case ItemTypeEnum.TYPE_BUBBLE:
|
||||
case ItemTypeEnum.TYPE_ANALYST:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddProfileItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_WEAPON_SKIN:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddWeaponSkinItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_MANIFESTATION:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddManifestationItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_CARD_SKIN_PART:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddSkinPartItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_AR:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddArItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
case ItemTypeEnum.TYPE_CALL:
|
||||
for (var i = 0u; i < count; i++)
|
||||
{
|
||||
var item = await player.InventoryManager.AddCallItem(itemType, detail, particular, level, sendPacket: false);
|
||||
if (item != null)
|
||||
sync.Items.Add(item.ToProto());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static BaseGameItemInfo? AddOtherItem(InventoryData inventory, uint genre, uint detail, uint particular, uint level, uint count)
|
||||
{
|
||||
var templateId = (uint)GameResourceTemplateId.FromGdpl(genre, detail, particular, level);
|
||||
if (!GameData.OtherItemData.TryGetValue(templateId, out var otherItem))
|
||||
return null;
|
||||
|
||||
var maxCount = otherItem.GMnum > 0 ? otherItem.GMnum : 99999u;
|
||||
var existing = inventory.Items.Values.FirstOrDefault(x => x.TemplateId == templateId);
|
||||
if (existing != null)
|
||||
{
|
||||
existing.ItemCount = Math.Min(existing.ItemCount + count, maxCount);
|
||||
return existing;
|
||||
}
|
||||
|
||||
var item = new BaseGameItemInfo
|
||||
{
|
||||
TemplateId = templateId,
|
||||
UniqueId = inventory.NextUniqueUid++,
|
||||
ItemType = ItemTypeEnum.TYPE_USEABLE,
|
||||
ItemCount = Math.Min(count, maxCount)
|
||||
};
|
||||
inventory.Items[item.UniqueId] = item;
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class VirCaptureGetLevelAwardParam
|
||||
{
|
||||
[JsonPropertyName("nId")]
|
||||
public int ActId { get; set; }
|
||||
|
||||
[JsonPropertyName("tbIdList")]
|
||||
public List<int> IdList { get; set; } = [];
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
v=4.3
|
||||
v=4.4
|
||||
Reference in New Issue
Block a user