Exemplo n.º 1
0
        public CombinationConsumable3Test()
        {
            _agentAddress  = default;
            _avatarAddress = _agentAddress.Derive("avatar");
            _slotAddress   = _avatarAddress.Derive(
                string.Format(
                    CultureInfo.InvariantCulture,
                    CombinationSlotState.DeriveFormat,
                    0
                    )
                );
            _sheets      = TableSheetsImporter.ImportSheets();
            _random      = new TestRandom();
            _tableSheets = new TableSheets(_sheets);

            var agentState = new AgentState(_agentAddress);

            agentState.avatarAddresses[0] = _avatarAddress;
            var gameConfigState = new GameConfigState();

            _avatarState = new AvatarState(
                _avatarAddress,
                _agentAddress,
                1,
                _tableSheets.GetAvatarSheets(),
                gameConfigState,
                default
                );

            _initialState = new State()
                            .SetState(_agentAddress, agentState.Serialize())
                            .SetState(_avatarAddress, _avatarState.Serialize());

            foreach (var(key, value) in _sheets)
            {
                _initialState =
                    _initialState.SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }
        }
Exemplo n.º 2
0
        public void Execute()
        {
            var       privateKey           = new PrivateKey();
            PublicKey publicKey            = privateKey.PublicKey;
            var       prevRedeemCodesState = new RedeemCodeState(new Dictionary <PublicKey, Reward>()
            {
                [publicKey] = new Reward(1),
            });
            var gameConfigState = new GameConfigState();
            var agentState      = new AgentState(_agentAddress);

            agentState.avatarAddresses[0] = _avatarAddress;
            var avatarState = new AvatarState(
                _avatarAddress,
                _agentAddress,
                1,
                _tableSheets,
                gameConfigState
                );

            var goldState = new GoldCurrencyState(new Currency("NCG", 2, minter: null));

            var initialState = new State()
                               .SetState(_agentAddress, agentState.Serialize())
                               .SetState(_avatarAddress, avatarState.Serialize())
                               .SetState(TableSheetsState.Address, _tableSheetsState.Serialize())
                               .SetState(RedeemCodeState.Address, prevRedeemCodesState.Serialize())
                               .SetState(GoldCurrencyState.Address, goldState.Serialize())
                               .MintAsset(GoldCurrencyState.Address, goldState.Currency * 100000000);
            var redeemCode = new RedeemCode(
                ByteUtil.Hex(privateKey.ByteArray),
                _avatarAddress
                );

            IAccountStateDelta nextState = redeemCode.Execute(new ActionContext()
            {
                BlockIndex     = 1,
                Miner          = default,
Exemplo n.º 3
0
        public MimisbrunnrBattle5Test()
        {
            var sheets = TableSheetsImporter.ImportSheets();

            _tableSheets = new TableSheets(sheets);

            var privateKey = new PrivateKey();

            _agentAddress = privateKey.PublicKey.ToAddress();
            var agentState = new AgentState(_agentAddress);

            _avatarAddress     = _agentAddress.Derive("avatar");
            _rankingMapAddress = _avatarAddress.Derive("ranking_map");
            var avatarState = new AvatarState(
                _avatarAddress,
                _agentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                new GameConfigState(sheets[nameof(GameConfigSheet)]),
                _rankingMapAddress
                )
            {
                level = 400,
            };

            agentState.avatarAddresses.Add(0, _avatarAddress);

            _initialState = new State()
                            .SetState(_agentAddress, agentState.Serialize())
                            .SetState(_avatarAddress, avatarState.Serialize())
                            .SetState(_rankingMapAddress, new RankingMapState(_rankingMapAddress).Serialize());

            foreach (var(key, value) in sheets)
            {
                _initialState = _initialState
                                .SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }
        }
Exemplo n.º 4
0
        public WorldUnlockScenarioTest()
        {
            var sheets = TableSheetsImporter.ImportSheets();
            _tableSheets = new TableSheets(sheets);

            var privateKey = new PrivateKey();
            _agentAddress = privateKey.PublicKey.ToAddress();
            var agentState = new AgentState(_agentAddress);

            _avatarAddress = _agentAddress.Derive("avatar");
            _rankingMapAddress = _avatarAddress.Derive("ranking_map");
            var avatarState = new AvatarState(
                _avatarAddress,
                _agentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                new GameConfigState(sheets[nameof(GameConfigSheet)]),
                _rankingMapAddress
            )
            {
                level = 100,
            };
            agentState.avatarAddresses.Add(0, _avatarAddress);

            _weeklyArenaState = new WeeklyArenaState(0);

            _initialState = new Lib9c.Tests.Action.State()
                .SetState(_weeklyArenaState.address, _weeklyArenaState.Serialize())
                .SetState(_agentAddress, agentState.Serialize())
                .SetState(_avatarAddress, avatarState.Serialize())
                .SetState(_rankingMapAddress, new RankingMapState(_rankingMapAddress).Serialize());

            foreach (var (key, value) in sheets)
            {
                _initialState = _initialState
                    .SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }
        }
Exemplo n.º 5
0
        public SellCancellationTest(ITestOutputHelper outputHelper)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .WriteTo.TestOutput(outputHelper)
                         .CreateLogger();

            _initialState = new State();
            var sheets = TableSheetsImporter.ImportSheets();

            foreach (var(key, value) in sheets)
            {
                _initialState = _initialState
                                .SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }

            var tableSheets = new TableSheets(sheets);

            var currency          = new Currency("NCG", 2, minters: null);
            var goldCurrencyState = new GoldCurrencyState(currency);

            _agentAddress = new PrivateKey().ToAddress();
            var agentState = new AgentState(_agentAddress);

            _avatarAddress = new PrivateKey().ToAddress();
            var rankingMapAddress = new PrivateKey().ToAddress();
            var avatarState       = new AvatarState(
                _avatarAddress,
                _agentAddress,
                0,
                tableSheets.GetAvatarSheets(),
                new GameConfigState(),
                rankingMapAddress)
            {
                worldInformation = new WorldInformation(
                    0,
                    tableSheets.WorldSheet,
                    GameConfig.RequireClearedStageLevel.ActionsInShop),
            };

            agentState.avatarAddresses[0] = _avatarAddress;

            var equipment = ItemFactory.CreateItemUsable(
                tableSheets.EquipmentItemSheet.First,
                Guid.NewGuid(),
                0);
            var shopState = new ShopState();

            shopState.Register(new ShopItem(
                                   _agentAddress,
                                   _avatarAddress,
                                   Guid.NewGuid(),
                                   new FungibleAssetValue(goldCurrencyState.Currency, 100, 0),
                                   equipment));

            _initialState = _initialState
                            .SetState(GoldCurrencyState.Address, goldCurrencyState.Serialize())
                            .SetState(Addresses.Shop, shopState.Serialize())
                            .SetState(_agentAddress, agentState.Serialize())
                            .SetState(_avatarAddress, avatarState.Serialize());
        }
Exemplo n.º 6
0
        public Buy7Test(ITestOutputHelper outputHelper)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .WriteTo.TestOutput(outputHelper)
                         .CreateLogger();

            _initialState = new State();
            var sheets = TableSheetsImporter.ImportSheets();

            foreach (var(key, value) in sheets)
            {
                _initialState = _initialState
                                .SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }

            _tableSheets = new TableSheets(sheets);

            var currency = new Currency("NCG", 2, minters: null);

            _goldCurrencyState = new GoldCurrencyState(currency);

            _sellerAgentAddress = new PrivateKey().ToAddress();
            var sellerAgentState = new AgentState(_sellerAgentAddress);

            _sellerAvatarAddress = new PrivateKey().ToAddress();
            var rankingMapAddress = new PrivateKey().ToAddress();
            var sellerAvatarState = new AvatarState(
                _sellerAvatarAddress,
                _sellerAgentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                new GameConfigState(),
                rankingMapAddress)
            {
                worldInformation = new WorldInformation(
                    0,
                    _tableSheets.WorldSheet,
                    GameConfig.RequireClearedStageLevel.ActionsInShop),
            };

            sellerAgentState.avatarAddresses[0] = _sellerAvatarAddress;

            _buyerAgentAddress = new PrivateKey().ToAddress();
            var buyerAgentState = new AgentState(_buyerAgentAddress);

            _buyerAvatarAddress = new PrivateKey().ToAddress();
            _buyerAvatarState   = new AvatarState(
                _buyerAvatarAddress,
                _buyerAgentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                new GameConfigState(),
                rankingMapAddress)
            {
                worldInformation = new WorldInformation(
                    0,
                    _tableSheets.WorldSheet,
                    GameConfig.RequireClearedStageLevel.ActionsInShop),
            };
            buyerAgentState.avatarAddresses[0] = _buyerAvatarAddress;

            _productId    = new Guid("6d460c1a-755d-48e4-ad67-65d5f519dbc8");
            _initialState = _initialState
                            .SetState(GoldCurrencyState.Address, _goldCurrencyState.Serialize())
                            .SetState(_sellerAgentAddress, sellerAgentState.Serialize())
                            .SetState(_sellerAvatarAddress, sellerAvatarState.Serialize())
                            .SetState(_buyerAgentAddress, buyerAgentState.Serialize())
                            .SetState(_buyerAvatarAddress, _buyerAvatarState.Serialize())
                            .SetState(Addresses.Shop, new ShopState().Serialize())
                            .MintAsset(_buyerAgentAddress, _goldCurrencyState.Currency * 100);
        }
Exemplo n.º 7
0
        public Buy3Test(ITestOutputHelper outputHelper)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .WriteTo.TestOutput(outputHelper)
                         .CreateLogger();

            _initialState = new State();
            var sheets = TableSheetsImporter.ImportSheets();

            foreach (var(key, value) in sheets)
            {
                _initialState = _initialState
                                .SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }

            _tableSheets = new TableSheets(sheets);

            var currency = new Currency("NCG", 2, minters: null);

            _goldCurrencyState = new GoldCurrencyState(currency);

            _sellerAgentAddress = new PrivateKey().ToAddress();
            var sellerAgentState = new AgentState(_sellerAgentAddress);

            _sellerAvatarAddress = new PrivateKey().ToAddress();
            var rankingMapAddress = new PrivateKey().ToAddress();
            var sellerAvatarState = new AvatarState(
                _sellerAvatarAddress,
                _sellerAgentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                new GameConfigState(),
                rankingMapAddress)
            {
                worldInformation = new WorldInformation(
                    0,
                    _tableSheets.WorldSheet,
                    GameConfig.RequireClearedStageLevel.ActionsInShop),
            };

            sellerAgentState.avatarAddresses[0] = _sellerAvatarAddress;

            _buyerAgentAddress = new PrivateKey().ToAddress();
            var buyerAgentState = new AgentState(_buyerAgentAddress);

            _buyerAvatarAddress = new PrivateKey().ToAddress();
            _buyerAvatarState   = new AvatarState(
                _buyerAvatarAddress,
                _buyerAgentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                new GameConfigState(),
                rankingMapAddress)
            {
                worldInformation = new WorldInformation(
                    0,
                    _tableSheets.WorldSheet,
                    GameConfig.RequireClearedStageLevel.ActionsInShop),
            };
            buyerAgentState.avatarAddresses[0] = _buyerAvatarAddress;

            var equipment = ItemFactory.CreateItemUsable(
                _tableSheets.EquipmentItemSheet.First,
                Guid.NewGuid(),
                0);

            var consumable = ItemFactory.CreateItemUsable(
                _tableSheets.ConsumableItemSheet.First,
                Guid.NewGuid(),
                0);

            var costume = ItemFactory.CreateCostume(
                _tableSheets.CostumeItemSheet.First,
                Guid.NewGuid());

            var shopState = new ShopState();

            shopState.Register(new ShopItem(
                                   _sellerAgentAddress,
                                   _sellerAvatarAddress,
                                   Guid.NewGuid(),
                                   new FungibleAssetValue(_goldCurrencyState.Currency, ProductPrice, 0),
                                   equipment));

            shopState.Register(new ShopItem(
                                   _sellerAgentAddress,
                                   _sellerAvatarAddress,
                                   Guid.NewGuid(),
                                   new FungibleAssetValue(_goldCurrencyState.Currency, ProductPrice, 0),
                                   consumable));

            shopState.Register(new ShopItem(
                                   _sellerAgentAddress,
                                   _sellerAvatarAddress,
                                   Guid.NewGuid(),
                                   new FungibleAssetValue(_goldCurrencyState.Currency, ProductPrice, 0),
                                   costume));

            _initialState = _initialState
                            .SetState(GoldCurrencyState.Address, _goldCurrencyState.Serialize())
                            .SetState(Addresses.Shop, shopState.Serialize())
                            .SetState(_sellerAgentAddress, sellerAgentState.Serialize())
                            .SetState(_sellerAvatarAddress, sellerAvatarState.Serialize())
                            .SetState(_buyerAgentAddress, buyerAgentState.Serialize())
                            .SetState(_buyerAvatarAddress, _buyerAvatarState.Serialize())
                            .MintAsset(_buyerAgentAddress, shopState.Products
                                       .Select(pair => pair.Value.Price)
                                       .Aggregate((totalPrice, next) => totalPrice + next));
        }
        public WeeklyArenaStateUpdateScenarioTest()
        {
            _initialState = new Tests.Action.State();

            var sheets = TableSheetsImporter.ImportSheets();

            foreach (var(key, value) in sheets)
            {
                _initialState = _initialState.SetState(
                    Addresses.TableSheet.Derive(key),
                    value.Serialize());
            }

            var tableSheets = new TableSheets(sheets);

            var rankingMapAddress = new PrivateKey().ToAddress();

            _agent1Address  = new PrivateKey().ToAddress();
            _avatar1Address = new PrivateKey().ToAddress();

            var agentState  = new AgentState(_agent1Address);
            var avatarState = new AvatarState(
                _avatar1Address,
                _agent1Address,
                0,
                tableSheets.GetAvatarSheets(),
                new GameConfigState(sheets[nameof(GameConfigSheet)]),
                rankingMapAddress
                )
            {
                worldInformation = new WorldInformation(
                    0,
                    tableSheets.WorldSheet,
                    Math.Max(
                        tableSheets.StageSheet.First?.Id ?? 1,
                        GameConfig.RequireClearedStageLevel.ActionsInRankingBoard)),
                level = 100,
            };

            agentState.avatarAddresses.Add(0, _avatar1Address);

            var agent2Address = new PrivateKey().ToAddress();

            _avatar2Address = new PrivateKey().ToAddress();

            var agent2State  = new AgentState(agent2Address);
            var avatar2State = new AvatarState(
                _avatar2Address,
                agent2Address,
                0,
                tableSheets.GetAvatarSheets(),
                new GameConfigState(sheets[nameof(GameConfigSheet)]),
                rankingMapAddress
                )
            {
                worldInformation = new WorldInformation(
                    0,
                    tableSheets.WorldSheet,
                    Math.Max(
                        tableSheets.StageSheet.First?.Id ?? 1,
                        GameConfig.RequireClearedStageLevel.ActionsInRankingBoard)),
                level = 100,
            };

            agent2State.avatarAddresses.Add(0, _avatar2Address);

            var agent3Address = new PrivateKey().ToAddress();

            _avatar3Address = new PrivateKey().ToAddress();

            var agent3State  = new AgentState(agent3Address);
            var avatar3State = new AvatarState(
                _avatar3Address,
                agent2Address,
                0,
                tableSheets.GetAvatarSheets(),
                new GameConfigState(sheets[nameof(GameConfigSheet)]),
                rankingMapAddress
                )
            {
                worldInformation = new WorldInformation(
                    0,
                    tableSheets.WorldSheet,
                    Math.Max(
                        tableSheets.StageSheet.First?.Id ?? 1,
                        GameConfig.RequireClearedStageLevel.ActionsInRankingBoard)),
                level = 100,
            };

            agent3State.avatarAddresses.Add(0, _avatar3Address);

            var prevWeeklyArenaState = new WeeklyArenaState(RankingBattle.UpdateTargetWeeklyArenaIndex - 2);
            var weeklyArenaState     = new WeeklyArenaState(RankingBattle.UpdateTargetWeeklyArenaIndex - 1);

            weeklyArenaState.SetV2(avatarState, tableSheets.CharacterSheet, tableSheets.CostumeStatSheet);
            weeklyArenaState[_avatar1Address].Activate();
            weeklyArenaState.SetV2(avatar2State, tableSheets.CharacterSheet, tableSheets.CostumeStatSheet);
            weeklyArenaState[_avatar2Address].Activate();
            _weeklyArenaAddress = WeeklyArenaState.DeriveAddress(RankingBattle.UpdateTargetWeeklyArenaIndex);

            var gold = new GoldCurrencyState(new Currency("NCG", 2, minter: null));

            _initialState = _initialState
                            .SetState(_agent1Address, agentState.Serialize())
                            .SetState(_avatar1Address, avatarState.Serialize())
                            .SetState(agent2Address, agent2State.Serialize())
                            .SetState(_avatar2Address, avatar2State.Serialize())
                            .SetState(agent3Address, agent2State.Serialize())
                            .SetState(_avatar3Address, avatar2State.Serialize())
                            .SetState(Addresses.GameConfig, new GameConfigState(sheets[nameof(GameConfigSheet)]).Serialize())
                            .SetState(prevWeeklyArenaState.address, prevWeeklyArenaState.Serialize())
                            .SetState(weeklyArenaState.address, weeklyArenaState.Serialize())
                            .SetState(_weeklyArenaAddress, new WeeklyArenaState(RankingBattle.UpdateTargetWeeklyArenaIndex).Serialize())
                            .SetState(GoldCurrencyState.Address, gold.Serialize())
                            .SetState(Addresses.GoldDistribution, GoldDistributionTest.Fixture.Select(v => v.Serialize()).Serialize())
                            .MintAsset(GoldCurrencyState.Address, gold.Currency * 100000000000);
        }
Exemplo n.º 9
0
        public static MakeInitialStateResult MakeInitialState()
        {
            var goldCurrencyState = new GoldCurrencyState(new Currency("NCG", 2, minter: null));
            var ranking           = new RankingState1();

            for (var i = 0; i < RankingState1.RankingMapCapacity; i++)
            {
                ranking.RankingMap[RankingState1.Derive(i)] = new HashSet <Address>().ToImmutableHashSet();
            }

            var sheets             = TableSheetsImporter.ImportSheets();
            var weeklyArenaAddress = WeeklyArenaState.DeriveAddress(0);
            var initialState       = new Tests.Action.State()
                                     .SetState(GoldCurrencyState.Address, goldCurrencyState.Serialize())
                                     .SetState(
                Addresses.GoldDistribution,
                GoldDistributionTest.Fixture.Select(v => v.Serialize()).Serialize()
                )
                                     .SetState(
                Addresses.GameConfig,
                new GameConfigState(sheets[nameof(GameConfigSheet)]).Serialize()
                )
                                     .SetState(Addresses.Ranking, ranking.Serialize())
                                     .SetState(weeklyArenaAddress, new WeeklyArenaState(0).Serialize());

            foreach (var(key, value) in sheets)
            {
                initialState = initialState.SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }

            var tableSheets       = new TableSheets(sheets);
            var rankingMapAddress = new PrivateKey().ToAddress();

            var agentAddress = new PrivateKey().ToAddress();
            var agentState   = new AgentState(agentAddress);

            var avatarAddress = new PrivateKey().ToAddress();
            var avatarState   = new AvatarState(
                avatarAddress,
                agentAddress,
                0,
                tableSheets.GetAvatarSheets(),
                new GameConfigState(),
                rankingMapAddress)
            {
                worldInformation = new WorldInformation(
                    0,
                    tableSheets.WorldSheet,
                    GameConfig.RequireClearedStageLevel.ActionsInShop),
            };

            agentState.avatarAddresses[0] = avatarAddress;

            var initCurrencyGold   = goldCurrencyState.Currency * 100000000000;
            var agentCurrencyGold  = goldCurrencyState.Currency * 1000;
            var remainCurrencyGold = initCurrencyGold - agentCurrencyGold;

            initialState = initialState
                           .SetState(GoldCurrencyState.Address, goldCurrencyState.Serialize())
                           .SetState(agentAddress, agentState.Serialize())
                           .SetState(avatarAddress, avatarState.Serialize())
                           .SetState(Addresses.Shop, new ShopState().Serialize())
                           .MintAsset(GoldCurrencyState.Address, initCurrencyGold)
                           .TransferAsset(Addresses.GoldCurrency, agentAddress, agentCurrencyGold);

            var action = new CreateTestbed
            {
                weeklyArenaAddress = weeklyArenaAddress,
            };
            var nextState = action.Execute(new ActionContext()
            {
                BlockIndex     = 0,
                PreviousStates = initialState,
                Random         = new TestRandom(),
                Rehearsal      = false,
            });

            return(new MakeInitialStateResult(
                       nextState,
                       action,
                       agentState,
                       avatarState,
                       goldCurrencyState,
                       rankingMapAddress,
                       tableSheets,
                       remainCurrencyGold,
                       agentCurrencyGold));
        }
Exemplo n.º 10
0
        public void Execute()
        {
            var tableSheets  = TableSheets.FromTableSheetsState(_tableSheetsState);
            var itemId       = tableSheets.WeeklyArenaRewardSheet.Values.First().Reward.ItemId;
            var privateKey   = new PrivateKey();
            var agentAddress = privateKey.PublicKey.ToAddress();
            var agent        = new AgentState(agentAddress);

            var avatarAddress = agentAddress.Derive("avatar");
            var avatarState   = new AvatarState(avatarAddress, agentAddress, 0, tableSheets, new GameConfigState())
            {
                level = 10,
            };

            avatarState.worldInformation.ClearStage(
                1,
                GameConfig.RequireClearedStageLevel.ActionsInRankingBoard,
                1,
                tableSheets.WorldSheet,
                tableSheets.WorldUnlockSheet
                );
            agent.avatarAddresses.Add(0, avatarAddress);

            Assert.False(avatarState.inventory.HasItem(itemId));

            var avatarAddress2 = agentAddress.Derive("avatar2");
            var avatarState2   = new AvatarState(avatarAddress2, agentAddress, 0, tableSheets, new GameConfigState());

            avatarState2.worldInformation.ClearStage(
                1,
                GameConfig.RequireClearedStageLevel.ActionsInRankingBoard,
                1,
                tableSheets.WorldSheet,
                tableSheets.WorldUnlockSheet
                );
            agent.avatarAddresses.Add(1, avatarAddress);

            var weekly = new WeeklyArenaState(0);

            weekly.Set(avatarState, tableSheets.CharacterSheet);
            weekly[avatarAddress].Activate();
            weekly.Set(avatarState2, tableSheets.CharacterSheet);
            weekly[avatarAddress2].Activate();

            var state = new State(ImmutableDictionary <Address, IValue> .Empty
                                  .Add(_tableSheetsState.address, _tableSheetsState.Serialize())
                                  .Add(weekly.address, weekly.Serialize())
                                  .Add(agentAddress, agent.Serialize())
                                  .Add(avatarAddress, avatarState.Serialize())
                                  .Add(avatarAddress2, avatarState2.Serialize()));

            var action = new RankingBattle
            {
                AvatarAddress      = avatarAddress,
                EnemyAddress       = avatarAddress2,
                WeeklyArenaAddress = weekly.address,
                costumeIds         = new List <int>(),
                equipmentIds       = new List <Guid>(),
                consumableIds      = new List <Guid>(),
            };

            Assert.Null(action.Result);

            var nextState = action.Execute(new ActionContext()
            {
                PreviousStates = state,
                Signer         = agentAddress,
                Random         = new ItemEnhancementTest.TestRandom(),
                Rehearsal      = false,
            });

            var newState = nextState.GetAvatarState(avatarAddress);

            var newWeeklyState = nextState.GetWeeklyArenaState(0);

            Assert.True(newState.inventory.HasItem(itemId));
            Assert.NotNull(action.Result);
            Assert.Contains(typeof(GetReward), action.Result.Select(e => e.GetType()));
            Assert.Equal(BattleLog.Result.Win, action.Result.result);
            Assert.True(newWeeklyState[avatarAddress].Score > weekly[avatarAddress].Score);
        }
Exemplo n.º 11
0
        public override IAccountStateDelta Execute(IActionContext context)
        {
            var sellData       = TestbedHelper.LoadData <TestbedSell>("TestbedSell");
            var addedItemInfos = sellData.Items
                                 .Select(item => new TestbedHelper.AddedItemInfo(
                                             context.Random.GenerateRandomGuid(),
                                             context.Random.GenerateRandomGuid()))
                                 .ToList();

            var agentAddress = _privateKey.PublicKey.ToAddress();
            var states       = context.PreviousStates;

            var avatarAddress = agentAddress.Derive(
                string.Format(
                    CultureInfo.InvariantCulture,
                    CreateAvatar.DeriveFormat,
                    _slotIndex
                    )
                );
            var inventoryAddress        = avatarAddress.Derive(LegacyInventoryKey);
            var worldInformationAddress = avatarAddress.Derive(LegacyWorldInformationKey);
            var questListAddress        = avatarAddress.Derive(LegacyQuestListKey);
            var orderReceiptAddress     = OrderDigestListState.DeriveAddress(avatarAddress);

            var weeklyArenaData        = TestbedHelper.LoadData <TestbedWeeklyArena>("TestbedWeeklyArena");
            var weeklyArenaAvatarNames = weeklyArenaData.Avatars
                                         .Distinct()
                                         .Select(avatar => avatar.Name)
                                         .ToArray();
            var weeklyArenaAgentAndAvatarAddresses = weeklyArenaAvatarNames
                                                     .Select(name => _privateKey.ToAddress().Derive(name))
                                                     .Select(e => (agentAddress: e, avatarAddress: e.Derive(
                                                                       string.Format(
                                                                           CultureInfo.InvariantCulture,
                                                                           CreateAvatar.DeriveFormat,
                                                                           0
                                                                           )
                                                                       )))
                                                     .ToArray();

            if (context.Rehearsal)
            {
                states = states.SetState(agentAddress, MarkChanged);
                for (var i = 0; i < AvatarState.CombinationSlotCapacity; i++)
                {
                    var slotAddress = avatarAddress.Derive(
                        string.Format(CultureInfo.InvariantCulture,
                                      CombinationSlotState.DeriveFormat, i));
                    states = states.SetState(slotAddress, MarkChanged);
                }

                states = states.SetState(avatarAddress, MarkChanged)
                         .SetState(Addresses.Ranking, MarkChanged)
                         .SetState(worldInformationAddress, MarkChanged)
                         .SetState(questListAddress, MarkChanged)
                         .SetState(inventoryAddress, MarkChanged);

                for (var i = 0; i < sellData.Items.Length; i++)
                {
                    var itemAddress  = Addresses.GetItemAddress(addedItemInfos[i].TradableId);
                    var orderAddress = Order.DeriveAddress(addedItemInfos[i].OrderId);
                    var shopAddress  = ShardedShopStateV2.DeriveAddress(
                        sellData.Items[i].ItemSubType,
                        addedItemInfos[i].OrderId);

                    states = states.SetState(avatarAddress, MarkChanged)
                             .SetState(inventoryAddress, MarkChanged)
                             .MarkBalanceChanged(GoldCurrencyMock, agentAddress,
                                                 GoldCurrencyState.Address)
                             .SetState(orderReceiptAddress, MarkChanged)
                             .SetState(itemAddress, MarkChanged)
                             .SetState(orderAddress, MarkChanged)
                             .SetState(shopAddress, MarkChanged);
                }

                states = states.SetState(weeklyArenaAddress, MarkChanged);
                foreach (var tuple in weeklyArenaAgentAndAvatarAddresses)
                {
                    states = states
                             .SetState(tuple.agentAddress, MarkChanged)
                             .SetState(tuple.avatarAddress, MarkChanged);
                }

                return(states);
            }

            // Create Agent and avatar
            var existingAgentState = states.GetAgentState(agentAddress);
            var agentState         = existingAgentState ?? new AgentState(agentAddress);
            var avatarState        = states.GetAvatarState(avatarAddress);

            if (!(avatarState is null))
            {
                throw new InvalidAddressException(
                          $"Aborted as there is already an avatar at {avatarAddress}.");
            }

            if (agentState.avatarAddresses.ContainsKey(_slotIndex))
            {
                throw new AvatarIndexAlreadyUsedException(
                          $"Aborted as the signer already has an avatar at index #{_slotIndex}.");
            }

            agentState.avatarAddresses.Add(_slotIndex, avatarAddress);

            var rankingState      = context.PreviousStates.GetRankingState();
            var rankingMapAddress = rankingState.UpdateRankingMap(avatarAddress);

            avatarState = TestbedHelper.CreateAvatarState(sellData.Avatar.Name,
                                                          agentAddress,
                                                          avatarAddress,
                                                          context.BlockIndex,
                                                          context.PreviousStates.GetAvatarSheets(),
                                                          context.PreviousStates.GetSheet <WorldSheet>(),
                                                          context.PreviousStates.GetGameConfigState(),
                                                          rankingMapAddress);

            // Add item
            var costumeItemSheet    = context.PreviousStates.GetSheet <CostumeItemSheet>();
            var equipmentItemSheet  = context.PreviousStates.GetSheet <EquipmentItemSheet>();
            var optionSheet         = context.PreviousStates.GetSheet <EquipmentItemOptionSheet>();
            var skillSheet          = context.PreviousStates.GetSheet <SkillSheet>();
            var materialItemSheet   = context.PreviousStates.GetSheet <MaterialItemSheet>();
            var consumableItemSheet = context.PreviousStates.GetSheet <ConsumableItemSheet>();

            for (var i = 0; i < sellData.Items.Length; i++)
            {
                TestbedHelper.AddItem(costumeItemSheet,
                                      equipmentItemSheet,
                                      optionSheet,
                                      skillSheet,
                                      materialItemSheet,
                                      consumableItemSheet,
                                      context.Random,
                                      sellData.Items[i], addedItemInfos[i], avatarState);
            }

            avatarState.Customize(0, 0, 0, 0);

            foreach (var address in avatarState.combinationSlotAddresses)
            {
                var slotState =
                    new CombinationSlotState(address,
                                             GameConfig.RequireClearedStageLevel.CombinationEquipmentAction);
                states = states.SetState(address, slotState.Serialize());
            }

            avatarState.UpdateQuestRewards(materialItemSheet);
            states = states.SetState(agentAddress, agentState.Serialize())
                     .SetState(Addresses.Ranking, rankingState.Serialize())
                     .SetState(inventoryAddress, avatarState.inventory.Serialize())
                     .SetState(worldInformationAddress, avatarState.worldInformation.Serialize())
                     .SetState(questListAddress, avatarState.questList.Serialize())
                     .SetState(avatarAddress, avatarState.SerializeV2());
            // ~Create Agent and avatar && ~Add item

            // for sell
            var costumeStatSheet = states.GetSheet <CostumeStatSheet>();

            for (var i = 0; i < sellData.Items.Length; i++)
            {
                var itemAddress  = Addresses.GetItemAddress(addedItemInfos[i].TradableId);
                var orderAddress = Order.DeriveAddress(addedItemInfos[i].OrderId);
                var shopAddress  = ShardedShopStateV2.DeriveAddress(
                    sellData.Items[i].ItemSubType,
                    addedItemInfos[i].OrderId);

                var balance =
                    context.PreviousStates.GetBalance(agentAddress, states.GetGoldCurrency());
                var price = new FungibleAssetValue(balance.Currency, sellData.Items[i].Price, 0);
                var order = OrderFactory.Create(agentAddress, avatarAddress,
                                                addedItemInfos[i].OrderId,
                                                price,
                                                addedItemInfos[i].TradableId,
                                                context.BlockIndex,
                                                sellData.Items[i].ItemSubType,
                                                sellData.Items[i].Count);

                Orders.Add(order);
                order.Validate(avatarState, sellData.Items[i].Count);
                var tradableItem = order.Sell(avatarState);

                var shardedShopState =
                    states.TryGetState(shopAddress, out Dictionary serializedState)
                        ? new ShardedShopStateV2(serializedState)
                        : new ShardedShopStateV2(shopAddress);
                var orderDigest = order.Digest(avatarState, costumeStatSheet);
                shardedShopState.Add(orderDigest, context.BlockIndex);
                var orderReceiptList =
                    states.TryGetState(orderReceiptAddress, out Dictionary receiptDict)
                        ? new OrderDigestListState(receiptDict)
                        : new OrderDigestListState(orderReceiptAddress);
                orderReceiptList.Add(orderDigest);

                states = states.SetState(orderReceiptAddress, orderReceiptList.Serialize())
                         .SetState(inventoryAddress, avatarState.inventory.Serialize())
                         .SetState(avatarAddress, avatarState.SerializeV2())
                         .SetState(itemAddress, tradableItem.Serialize())
                         .SetState(orderAddress, order.Serialize())
                         .SetState(shopAddress, shardedShopState.Serialize());
            }

            result.SellerAgentAddress  = agentAddress;
            result.SellerAvatarAddress = avatarAddress;
            result.ItemInfos           = new List <ItemInfos>();
            for (var i = 0; i < sellData.Items.Length; i++)
            {
                result.ItemInfos.Add(new ItemInfos(
                                         addedItemInfos[i].OrderId,
                                         addedItemInfos[i].TradableId,
                                         sellData.Items[i].ItemSubType,
                                         sellData.Items[i].Price,
                                         sellData.Items[i].Count));
            }

            // NOTE: Update WeeklyArenaState
            var gameConfigState  = states.GetGameConfigState();
            var weeklyArenaState = states.GetWeeklyArenaState(weeklyArenaAddress);
            var characterSheet   = states.GetSheet <CharacterSheet>();

            // loop
            for (var i = 0; i < weeklyArenaAvatarNames.Length; i++)
            {
                var name  = weeklyArenaAvatarNames[i];
                var tuple = weeklyArenaAgentAndAvatarAddresses[i];
                rankingMapAddress = rankingState.UpdateRankingMap(tuple.avatarAddress);
                avatarState       = new AvatarState(
                    tuple.avatarAddress,
                    tuple.agentAddress,
                    context.BlockIndex,
                    states.GetAvatarSheets(),
                    gameConfigState,
                    rankingMapAddress,
                    name);
                weeklyArenaState.SetV2(avatarState, characterSheet, costumeStatSheet);
                states = states
                         .SetState(tuple.avatarAddress, new AgentState(tuple.agentAddress).Serialize())
                         .SetState(tuple.avatarAddress, avatarState.Serialize());
            }
            // ~loop
            states = states.SetState(weeklyArenaAddress, weeklyArenaState.Serialize());
            // ~Update WeeklyArenaState

            return(states);
        }