-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce ClaimGifts
#2982
Merged
Merged
Introduce ClaimGifts
#2982
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fa763ce
create ClaimOneTimeGift(action), OneTimeGiftSheet(sheet)
tyrosine1153 5abb70f
craete GiftModule
tyrosine1153 53766de
move ClaimGiftsSheet
tyrosine1153 2635d7f
create ClaimableGiftsSheetTest
tyrosine1153 53544d3
create ClaimGiftsTest
tyrosine1153 2af6ba7
refactor to replace avatarState -> Inventory
tyrosine1153 b6d63ea
refactor claimedGiftIds serialize
tyrosine1153 fb30233
add xml comment
tyrosine1153 ddf552b
fix item tradable
tyrosine1153 0d3b171
add CheckAvatarAddrIsContainedInAgent
tyrosine1153 ea9329e
edit field to property
tyrosine1153 03380eb
refactor to replace SetAccount -> MutateAccount
tyrosine1153 d54a4c8
fix exception name
tyrosine1153 49680cb
add column items tradable in ClaimableGiftsSheet
tyrosine1153 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
namespace Lib9c.Tests.Action | ||
{ | ||
using System; | ||
using System.Linq; | ||
using Libplanet.Action.State; | ||
using Libplanet.Crypto; | ||
using Libplanet.Mocks; | ||
using Nekoyume; | ||
using Nekoyume.Action; | ||
using Nekoyume.Model.Item; | ||
using Nekoyume.Model.State; | ||
using Nekoyume.Module; | ||
using Xunit; | ||
|
||
public class ClaimGiftsTest | ||
{ | ||
private readonly TableSheets _tableSheets; | ||
private readonly IWorld _state; | ||
|
||
public ClaimGiftsTest() | ||
{ | ||
_state = new World(MockUtil.MockModernWorldState); | ||
|
||
var tableCsv = TableSheetsImporter.ImportSheets(); | ||
foreach (var (key, value) in tableCsv) | ||
{ | ||
_state = _state.SetLegacyState(Addresses.GetSheetAddress(key), value.Serialize()); | ||
} | ||
|
||
_tableSheets = new TableSheets(tableCsv); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1)] | ||
[InlineData(300)] | ||
[InlineData(600)] | ||
[InlineData(1200)] | ||
public void Execute_Success(long blockIndex) | ||
{ | ||
var agentAddress = new PrivateKey().Address; | ||
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0); | ||
|
||
var avatarState = AvatarState.Create( | ||
avatarAddress, | ||
agentAddress, | ||
0, | ||
_tableSheets.GetAvatarSheets(), | ||
default); | ||
var state = _state.SetAvatarState(avatarAddress, avatarState); | ||
|
||
if (!_tableSheets.ClaimableGiftsSheet.TryFindRowByBlockIndex(blockIndex, out var row)) | ||
{ | ||
throw new Exception(); | ||
} | ||
|
||
Execute( | ||
state, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
blockIndex, | ||
row.Items.ToArray() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Execute_ClaimableGiftsNotAvailableException() | ||
{ | ||
var agentAddress = new PrivateKey().Address; | ||
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0); | ||
|
||
var avatarState = AvatarState.Create( | ||
avatarAddress, | ||
agentAddress, | ||
0, | ||
_tableSheets.GetAvatarSheets(), | ||
default); | ||
var state = _state.SetAvatarState(avatarAddress, avatarState); | ||
var sheet = _tableSheets.ClaimableGiftsSheet; | ||
|
||
Assert.Throws<ClaimableGiftsNotAvailableException>(() => | ||
{ | ||
var row = sheet.Values.OrderBy(row => row.StartedBlockIndex).First(); | ||
Execute( | ||
state, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
row.StartedBlockIndex - 1, | ||
row.Items.ToArray() | ||
); | ||
}); | ||
Assert.Throws<ClaimableGiftsNotAvailableException>(() => | ||
{ | ||
var row = sheet.Values.OrderByDescending(row => row.EndedBlockIndex).First(); | ||
Execute( | ||
state, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
row.EndedBlockIndex + 1, | ||
row.Items.ToArray() | ||
); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Execute_AlreadyClaimedGiftsException() | ||
{ | ||
var agentAddress = new PrivateKey().Address; | ||
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0); | ||
|
||
var avatarState = AvatarState.Create( | ||
avatarAddress, | ||
agentAddress, | ||
0, | ||
_tableSheets.GetAvatarSheets(), | ||
default); | ||
var state = _state.SetAvatarState(avatarAddress, avatarState); | ||
|
||
var row = _tableSheets.ClaimableGiftsSheet.Values.First(); | ||
var blockIndex = row.StartedBlockIndex; | ||
|
||
var nextState = Execute( | ||
state, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
blockIndex, | ||
row.Items.ToArray() | ||
); | ||
Assert.Throws<AlreadyClaimedGiftsException>(() => | ||
{ | ||
Execute( | ||
nextState, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
blockIndex + 1, | ||
row.Items.ToArray() | ||
); | ||
}); | ||
} | ||
|
||
private IWorld Execute( | ||
IWorld previousState, | ||
Address avatarAddress, | ||
Address agentAddress, | ||
int giftId, | ||
long blockIndex, | ||
(int itemId, int quantity, bool tradable)[] expected) | ||
{ | ||
var prevClaimedGifts = _state.GetClaimedGifts(avatarAddress); | ||
|
||
var action = new ClaimGifts(avatarAddress, giftId); | ||
var actionContext = new ActionContext | ||
{ | ||
PreviousState = previousState, | ||
Signer = agentAddress, | ||
BlockIndex = blockIndex, | ||
}; | ||
|
||
var nextState = action.Execute(actionContext); | ||
|
||
// Check claimed gifts. | ||
var nextClaimedGifts = nextState.GetClaimedGifts(avatarAddress); | ||
Assert.Equal(prevClaimedGifts.Count + 1, nextClaimedGifts.Count); | ||
|
||
// Check Inventory. | ||
var inventory = nextState.GetInventoryV2(avatarAddress); | ||
foreach (var (itemId, quantity, tradable) in expected) | ||
{ | ||
Assert.True(inventory.TryGetItem(itemId, out var inventoryItem)); | ||
Assert.Equal(quantity, inventoryItem.count); | ||
Assert.Equal(tradable, inventoryItem.item is ITradableItem); | ||
} | ||
|
||
return nextState; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Lib9c.Tests.TableData.Event | ||
{ | ||
using Nekoyume.TableData; | ||
using Xunit; | ||
|
||
public class ClaimableGiftsSheetTest | ||
{ | ||
[Fact] | ||
public void Set() | ||
{ | ||
const string csv = @"id,started_block_index,ended_block_index,item_1_id,item_1_quantity,item_1_tradable,item_2_id,item_2_quantity,item_2_tradable,item_3_id,item_3_quantity,item_3_tradable,item_4_id,item_4_quantity,item_4_tradable,item_5_id,item_5_quantity,item_5_tradable | ||
1,1,250,600402,5,true,,,,,,,,,,,, | ||
2,251,500,40100030,1,true,,,,,,,,,,,, | ||
3,501,1000,49900022,1,true,,,,,,,,,,,, | ||
4,1001,1500,40100028,1,true,,,,,,,,,,,, | ||
5,1501,2000,400000,5,false,,,,,,,,,,,,"; | ||
|
||
var sheet = new ClaimableGiftsSheet(); | ||
sheet.Set(csv); | ||
Assert.Equal(5, sheet.Count); | ||
Assert.NotNull(sheet.First); | ||
Assert.NotNull(sheet.Last); | ||
var row = sheet.First; | ||
Assert.Equal(1, row.Id); | ||
Assert.Equal(1, row.StartedBlockIndex); | ||
Assert.Equal(250, row.EndedBlockIndex); | ||
Assert.Single(row.Items); | ||
Assert.Equal(600402, row.Items[0].itemId); | ||
Assert.Equal(5, row.Items[0].quantity); | ||
Assert.True(row.Items[0].tradable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Nekoyume.Action | ||
{ | ||
[Serializable] | ||
public class AlreadyClaimedGiftsException : Exception | ||
{ | ||
public AlreadyClaimedGiftsException() | ||
{ | ||
} | ||
|
||
public AlreadyClaimedGiftsException(string msg) : base(msg) | ||
{ | ||
} | ||
|
||
public AlreadyClaimedGiftsException(SerializationInfo info, StreamingContext context) : base(info, context) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Action.State; | ||
using Libplanet.Crypto; | ||
using Nekoyume.Exceptions; | ||
using Nekoyume.Extensions; | ||
using Nekoyume.Model.Item; | ||
using Nekoyume.Model.State; | ||
using Nekoyume.Module; | ||
using Nekoyume.TableData; | ||
using static Lib9c.SerializeKeys; | ||
|
||
namespace Nekoyume.Action | ||
{ | ||
/// <summary> | ||
/// An action to claim gifts using the Gift Id. <br/> | ||
/// Refer <see cref="ClaimableGiftsSheet"/> to find gifts data.<br/> | ||
/// It can only claim once at the specified block index. | ||
/// </summary> | ||
[ActionType(ActionTypeText)] | ||
public class ClaimGifts : GameAction | ||
{ | ||
private const string ActionTypeText = "claim_gifts"; | ||
|
||
/// <summary> | ||
/// The address of the avatar claiming the gift. | ||
/// </summary> | ||
public Address AvatarAddress { get; private set; } | ||
/// <summary> | ||
/// The ID of the gift to be claimed. This ID is used in the <see cref="ClaimableGiftsSheet"/>. | ||
/// </summary> | ||
public int GiftId { get; private set; } | ||
private const string GiftIdKey = "gi"; | ||
|
||
public ClaimGifts() | ||
{ | ||
} | ||
|
||
public ClaimGifts(Address avatarAddress, int giftId) | ||
{ | ||
AvatarAddress = avatarAddress; | ||
GiftId = giftId; | ||
} | ||
|
||
protected override IImmutableDictionary<string, IValue> PlainValueInternal => | ||
ImmutableDictionary<string, IValue>.Empty | ||
.Add(AvatarAddressKey, AvatarAddress.Serialize()) | ||
ipdae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.Add(GiftIdKey, GiftId.Serialize()); | ||
|
||
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue) | ||
{ | ||
AvatarAddress = plainValue[AvatarAddressKey].ToAddress(); | ||
GiftId = plainValue[GiftIdKey].ToInteger(); | ||
} | ||
|
||
/// <exception cref="FailedLoadStateException">Thrown when the inventory could not be loaded.</exception> | ||
/// <exception cref="SheetRowNotFoundException">Thrown when the gift ID is not found in the <see cref="ClaimableGiftsSheet"/>.</exception> | ||
/// <exception cref="ClaimableGiftsNotAvailableException">Thrown when the gift is not available at the current block index.</exception> | ||
/// <exception cref="AlreadyClaimedGiftsException">Thrown when the gift has already been claimed.</exception> | ||
public override IWorld Execute(IActionContext context) | ||
{ | ||
context.UseGas(1); | ||
var states = context.PreviousState; | ||
var random = context.GetRandom(); | ||
var addressesHex = GetSignerAndOtherAddressesHex(context, AvatarAddress); | ||
|
||
// NOTE: The `AvatarAddress` must contained in `Signer`'s `AgentState.avatarAddresses`. | ||
if (!Addresses.CheckAvatarAddrIsContainedInAgent(context.Signer, AvatarAddress)) | ||
{ | ||
throw new InvalidActionFieldException( | ||
ActionTypeText, | ||
addressesHex, | ||
nameof(AvatarAddress), | ||
$"Signer({context.Signer}) is not contained in" + | ||
$" AvatarAddress({AvatarAddress})."); | ||
} | ||
|
||
var inventory = states.GetInventoryV2(AvatarAddress); | ||
if (inventory is null) | ||
{ | ||
throw new FailedLoadStateException( | ||
ActionTypeText, | ||
addressesHex, | ||
typeof(Inventory), | ||
AvatarAddress); | ||
} | ||
|
||
var sheetTypes = new [] | ||
{ | ||
typeof(ClaimableGiftsSheet), | ||
}; | ||
var sheets = states.GetSheets( | ||
containItemSheet: true, | ||
sheetTypes: sheetTypes); | ||
|
||
var claimableGiftsSheet = sheets.GetSheet<ClaimableGiftsSheet>(); | ||
if (!claimableGiftsSheet.TryGetValue(GiftId, out var giftRow)) | ||
{ | ||
throw new SheetRowNotFoundException( | ||
addressesHex, | ||
nameof(claimableGiftsSheet), | ||
GiftId); | ||
} | ||
|
||
if (!giftRow.Validate(context.BlockIndex)) | ||
{ | ||
throw new ClaimableGiftsNotAvailableException( | ||
$"[{addressesHex}] Claimable gift is not available at block index: {context.BlockIndex}" | ||
); | ||
} | ||
|
||
var claimedGiftIds = states.GetClaimedGifts(AvatarAddress); | ||
if (claimedGiftIds.Contains(giftRow.Id)) | ||
{ | ||
U-lis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new AlreadyClaimedGiftsException( | ||
$"[{addressesHex}] Already claimed gift. You can only claim gift once : {giftRow.Id}" | ||
); | ||
} | ||
|
||
var itemSheet = sheets.GetItemSheet(); | ||
foreach (var (itemId, quantity, tradable) in giftRow.Items) | ||
{ | ||
var itemRow = itemSheet[itemId]; | ||
if (itemRow is MaterialItemSheet.Row materialRow) | ||
{ | ||
var item = tradable | ||
? ItemFactory.CreateTradableMaterial(materialRow) | ||
: ItemFactory.CreateMaterial(materialRow); | ||
inventory.AddItem(item, quantity); | ||
} | ||
else | ||
{ | ||
foreach (var _ in Enumerable.Range(0, quantity)) | ||
{ | ||
var item = ItemFactory.CreateItem(itemRow, random); | ||
inventory.AddItem(item); | ||
} | ||
} | ||
} | ||
|
||
claimedGiftIds.Add(giftRow.Id); | ||
|
||
return states | ||
.SetClaimedGifts(AvatarAddress, claimedGiftIds) | ||
.SetInventory(AvatarAddress, inventory); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
액션 xml주석 추가 부탁드리겠습니다