Skip to content

Commit

Permalink
Tweak evt reward logic and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeFZ committed Jul 23, 2023
1 parent 6abb77d commit a9e0a63
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 29 deletions.
69 changes: 69 additions & 0 deletions DragaliaAPI.Integration.Test/Features/Event/Clb01EventTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using DragaliaAPI.Database.Entities;
using DragaliaAPI.Models;
using DragaliaAPI.Models.Generated;
using DragaliaAPI.Shared.Definitions.Enums.EventItemTypes;
using Microsoft.EntityFrameworkCore;

namespace DragaliaAPI.Integration.Test.Features.Event;

public class Clb01EventTest : TestFixture
{
public Clb01EventTest(
CustomWebApplicationFactory<Program> factory,
ITestOutputHelper outputHelper
)
: base(factory, outputHelper) { }

private const int EventId = 21401;
private const string Prefix = "clb01_event";

[Fact]
public async Task GetEventData_ReturnsEventData()
{
await Client.PostMsgpack<MemoryEventActivateData>(
"memory_event/activate",
new MemoryEventActivateRequest(EventId)
);

DragaliaResponse<Clb01EventGetEventDataData> evtData =
await Client.PostMsgpack<Clb01EventGetEventDataData>(
$"{Prefix}/get_event_data",
new Clb01EventGetEventDataRequest(EventId)
);

evtData.data.clb_01_event_user_data.Should().NotBeNull();
evtData.data.clb_01_event_reward_list.Should().NotBeNull();
}

[Fact]
public async Task ReceiveEventRewards_ReturnsEventRewards()
{
await Client.PostMsgpack<MemoryEventActivateData>(
"memory_event/activate",
new MemoryEventActivateRequest(EventId)
);

DbPlayerEventItem pointItem = await ApiContext.PlayerEventItems.SingleAsync(
x => x.EventId == EventId && x.Type == (int)Clb01EventItemType.Clb01EventPoint
);

pointItem.Quantity += 500;

ApiContext.PlayerEventRewards.RemoveRange(
ApiContext.PlayerEventRewards.Where(x => x.EventId == EventId)
);

await ApiContext.SaveChangesAsync();

DragaliaResponse<Clb01EventReceiveClb01PointRewardData> evtResp =
await Client.PostMsgpack<Clb01EventReceiveClb01PointRewardData>(
$"{Prefix}/receive_clb01_point_reward",
new Clb01EventReceiveClb01PointRewardRequest(EventId)
);

evtResp.data.clb_01_event_reward_entity_list.Should().NotBeNullOrEmpty();
evtResp.data.clb_01_event_reward_list.Should().NotBeNullOrEmpty();
evtResp.data.entity_result.Should().NotBeNull();
evtResp.data.update_data_list.Should().NotBeNull();
}
}
111 changes: 111 additions & 0 deletions DragaliaAPI.Integration.Test/Features/Event/CombatEventTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using DragaliaAPI.Database.Entities;
using DragaliaAPI.Models;
using DragaliaAPI.Models.Generated;
using DragaliaAPI.Shared.Definitions.Enums.EventItemTypes;
using Microsoft.EntityFrameworkCore;

namespace DragaliaAPI.Integration.Test.Features.Event;

public class CombatEventTest : TestFixture
{
public CombatEventTest(
CustomWebApplicationFactory<Program> factory,
ITestOutputHelper outputHelper
)
: base(factory, outputHelper) { }

private const int EventId = 22213;
private const string Prefix = "combat_event";

[Fact]
public async Task GetEventData_ReturnsEventData()
{
await Client.PostMsgpack<MemoryEventActivateData>(
"memory_event/activate",
new MemoryEventActivateRequest(EventId)
);

DragaliaResponse<CombatEventGetEventDataData> evtData =
await Client.PostMsgpack<CombatEventGetEventDataData>(
$"{Prefix}/get_event_data",
new CombatEventGetEventDataRequest(EventId)
);

evtData.data.combat_event_user_data.Should().NotBeNull();
evtData.data.user_event_location_reward_list.Should().NotBeNull();
evtData.data.event_reward_list.Should().NotBeNull();
evtData.data.event_trade_list.Should().NotBeNull();
}

[Fact]
public async Task ReceiveEventRewards_ReturnsEventRewards()
{
await Client.PostMsgpack<MemoryEventActivateData>(
"memory_event/activate",
new MemoryEventActivateRequest(EventId)
);

DbPlayerEventItem pointItem = await ApiContext.PlayerEventItems.SingleAsync(
x => x.EventId == EventId && x.Type == (int)Clb01EventItemType.Clb01EventPoint
);

pointItem.Quantity += 500;

ApiContext.PlayerEventRewards.RemoveRange(
ApiContext.PlayerEventRewards.Where(x => x.EventId == EventId)
);

await ApiContext.SaveChangesAsync();

DragaliaResponse<CombatEventReceiveEventPointRewardData> evtResp =
await Client.PostMsgpack<CombatEventReceiveEventPointRewardData>(
$"{Prefix}/receive_event_point_reward",
new CombatEventReceiveEventPointRewardRequest(EventId)
);

evtResp.data.event_reward_entity_list.Should().NotBeNullOrEmpty();
evtResp.data.event_reward_list.Should().NotBeNullOrEmpty();
evtResp.data.entity_result.Should().NotBeNull();
evtResp.data.update_data_list.Should().NotBeNull();
}

[Fact]
public async Task ReceiveEventLocationRewards_ReturnsEventLocationRewards()
{
await Client.PostMsgpack<MemoryEventActivateData>(
"memory_event/activate",
new MemoryEventActivateRequest(EventId)
);

DbPlayerEventItem pointItem = await ApiContext.PlayerEventItems.SingleAsync(
x => x.EventId == EventId && x.Type == (int)Clb01EventItemType.Clb01EventPoint
);

pointItem.Quantity += 500;

ApiContext.PlayerQuests.RemoveRange(
ApiContext.PlayerQuests.Where(x => x.DeviceAccountId == DeviceAccountId)
);

ApiContext.PlayerQuests.Add(
new DbQuest
{
DeviceAccountId = DeviceAccountId,
QuestId = 222130103,
State = 3
}
);

await ApiContext.SaveChangesAsync();

DragaliaResponse<CombatEventReceiveEventLocationRewardData> evtResp =
await Client.PostMsgpack<CombatEventReceiveEventLocationRewardData>(
$"{Prefix}/receive_event_location_reward",
new CombatEventReceiveEventLocationRewardRequest(EventId, 2221302)
);

evtResp.data.event_location_reward_entity_list.Should().NotBeNullOrEmpty();
evtResp.data.user_event_location_reward_list.Should().NotBeNullOrEmpty();
evtResp.data.update_data_list.Should().NotBeNull();
}
}
68 changes: 68 additions & 0 deletions DragaliaAPI.Integration.Test/Features/Event/RaidEventTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using DragaliaAPI.Database.Entities;
using DragaliaAPI.Models;
using DragaliaAPI.Models.Generated;
using DragaliaAPI.Shared.Definitions.Enums.EventItemTypes;
using Microsoft.EntityFrameworkCore;

namespace DragaliaAPI.Integration.Test.Features.Event;

public class RaidEventTest : TestFixture
{
public RaidEventTest(
CustomWebApplicationFactory<Program> factory,
ITestOutputHelper outputHelper
)
: base(factory, outputHelper) { }

private const int EventId = 20455;
private const string Prefix = "raid_event";

[Fact]
public async Task GetEventData_ReturnsEventData()
{
await Client.PostMsgpack<MemoryEventActivateData>(
"memory_event/activate",
new MemoryEventActivateRequest(EventId)
);

DragaliaResponse<RaidEventGetEventDataData> evtData =
await Client.PostMsgpack<RaidEventGetEventDataData>(
$"{Prefix}/get_event_data",
new RaidEventGetEventDataRequest(EventId)
);

evtData.data.raid_event_user_data.Should().NotBeNull();
evtData.data.raid_event_reward_list.Should().NotBeNull();
}

[Fact]
public async Task ReceiveEventRewards_ReturnsEventRewards()
{
await Client.PostMsgpack<MemoryEventActivateData>(
"memory_event/activate",
new MemoryEventActivateRequest(EventId)
);

DbPlayerEventItem pointItem = await ApiContext.PlayerEventItems.SingleAsync(
x => x.EventId == EventId && x.Type == (int)RaidEventItemType.RaidPoint1
);

pointItem.Quantity += 500;

ApiContext.PlayerEventRewards.RemoveRange(
ApiContext.PlayerEventRewards.Where(x => x.EventId == EventId)
);

await ApiContext.SaveChangesAsync();

DragaliaResponse<RaidEventReceiveRaidPointRewardData> evtResp =
await Client.PostMsgpack<RaidEventReceiveRaidPointRewardData>(
$"{Prefix}/receive_clb01_point_reward",
new RaidEventReceiveRaidPointRewardRequest(EventId, new[] { 1001 })
);

evtResp.data.raid_event_reward_list.Should().NotBeNullOrEmpty();
evtResp.data.entity_result.Should().NotBeNull();
evtResp.data.update_data_list.Should().NotBeNull();
}
}
2 changes: 2 additions & 0 deletions DragaliaAPI/Features/Event/EarnEventController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class EarnEventController(
ITradeService tradeService
) : DragaliaControllerBase
{
// TODO: This is not fully implemented. However, no compendium event uses it.

[HttpPost("get_event_data")]
public async Task<DragaliaResult> GetEventData(EarnEventGetEventDataRequest request)
{
Expand Down
24 changes: 0 additions & 24 deletions DragaliaAPI/Features/Event/EventDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,6 @@ or EventKindType.ExHunter
};
}

public static int GetEventRewardItem(this EventData data)
{
return data.EventKindType switch
{
EventKindType.Build => (int)BuildEventItemType.BuildEventPoint,
EventKindType.Raid => (int)RaidEventItemType.SummonPoint,
EventKindType.Combat => (int)CombatEventItemType.EventPoint,
EventKindType.BattleRoyal => (int)BattleRoyalEventItemType.EventPoint,
EventKindType.Clb01 => (int)Clb01EventItemType.Clb01EventPoint,
EventKindType.Earn => (int)EarnEventItemType.EarnPoint,
EventKindType.ExHunter => (int)ExHunterEventItemType.SummonPoint,
/*
EventKindType.Collect
=> Enum.GetValues<CollectEventItemType>().Where(x => x != 0).Cast<int>(),
EventKindType.ExRush
=> Enum.GetValues<ExRushEventItemType>().Where(x => x != 0).Cast<int>(),
EventKindType.Simple
=> Enum.GetValues<SimpleEventItemType>().Where(x => x != 0).Cast<int>(),*/
_
=> 0 /* maybe 10101 */
,
};
}

public static IEnumerable<int> GetEventPassiveIds(this EventData data)
{
int eventId = data.Id;
Expand Down
13 changes: 8 additions & 5 deletions DragaliaAPI/Features/Event/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ public async Task<IEnumerable<AtgenBuildEventRewardEntityList>> ReceiveEventRewa

List<AtgenBuildEventRewardEntityList> rewardEntities = new();

DbPlayerEventItem buildPointItem = await eventRepository.GetEventItemAsync(
eventId,
data.GetEventRewardItem()
);
Dictionary<int, int> eventItemQuantities = (
await eventRepository.GetEventItemsAsync(eventId)
).ToDictionary(x => x.Id, x => x.Quantity);

Dictionary<int, IEventReward> rewards = data.GetEventRewards();

Expand All @@ -84,7 +83,11 @@ public async Task<IEnumerable<AtgenBuildEventRewardEntityList>> ReceiveEventRewa
? rewardIds.Select(x => rewards[x])
: rewards.Values
.ExceptBy(alreadyObtainedRewardIds, x => x.Id)
.Where(x => buildPointItem.Quantity >= x.EventItemQuantity)
.Where(
x =>
eventItemQuantities.ContainsKey(x.EventItemId)
&& eventItemQuantities[x.EventItemId] >= x.EventItemQuantity
)
).ToList();

if (availableRewards.Count == 0)
Expand Down

0 comments on commit a9e0a63

Please sign in to comment.