Exemplo n.º 1
0
        public static AvatarState CreateAvatarState(string name,
                                                    Address agentAddress,
                                                    Address avatarAddress,
                                                    long blockIndex,
                                                    AvatarSheets avatarSheets,
                                                    WorldSheet worldSheet,
                                                    GameConfigState gameConfigState,
                                                    Address rankingMapAddress)
        {
            var avatarState = new AvatarState(
                avatarAddress,
                agentAddress,
                blockIndex,
                avatarSheets,
                gameConfigState,
                rankingMapAddress,
                name != string.Empty ? name : "testId"
                )
            {
                worldInformation = new WorldInformation(
                    0,
                    worldSheet,
                    GameConfig.RequireClearedStageLevel.ActionsInShop),
            };

            return(avatarState);
        }
Exemplo n.º 2
0
        private static void DrawPlayer(GameConfigState gameConfigState, PlayerState playerState, char[,] view)
        {
            int playerX = playerState.Position;
            int playerY = gameConfigState.Height - 1;

            view[playerX, playerY] = 'P';
        }
Exemplo n.º 3
0
        public static Block <PolymorphicAction <ActionBase> > MineGenesisBlock(
            IDictionary <string, string> tableSheets,
            GoldDistribution[] goldDistributions,
            PendingActivationState[] pendingActivationStates,
            AdminState adminState,
            AuthorizedMinersState authorizedMinersState = null,
            IImmutableSet <Address> activatedAccounts   = null,
            bool isActivateAdminAddress  = false,
            IEnumerable <string> credits = null,
            int maximumTransactions      = 100,
            PrivateKey privateKey        = null
            )
        {
            if (!tableSheets.TryGetValue(nameof(GameConfigSheet), out var csv))
            {
                throw new KeyNotFoundException(nameof(GameConfigSheet));
            }
            var gameConfigState     = new GameConfigState(csv);
            var redeemCodeListSheet = new RedeemCodeListSheet();

            redeemCodeListSheet.Set(tableSheets[nameof(RedeemCodeListSheet)]);

            if (privateKey is null)
            {
                privateKey = new PrivateKey();
            }

            var ncg = new Currency("NCG", 2, privateKey.ToAddress());

            activatedAccounts = activatedAccounts ?? ImmutableHashSet <Address> .Empty;
            var initialStatesAction = new InitializeStates
                                      (
                rankingState: new RankingState(),
                shopState: new ShopState(),
                tableSheets: (Dictionary <string, string>)tableSheets,
                gameConfigState: gameConfigState,
                redeemCodeState: new RedeemCodeState(redeemCodeListSheet),
                adminAddressState: adminState,
                activatedAccountsState: new ActivatedAccountsState(
                    isActivateAdminAddress
                    ? activatedAccounts.Add(adminState.AdminAddress)
                    : activatedAccounts),
                goldCurrencyState: new GoldCurrencyState(ncg),
                goldDistributions: goldDistributions,
                pendingActivationStates: pendingActivationStates,
                authorizedMinersState: authorizedMinersState,
                creditsState: credits is null ? null : new CreditsState(credits)
                                      );
            var actions = new PolymorphicAction <ActionBase>[]
            {
                initialStatesAction,
            };
            var blockAction = new BlockPolicySource(Log.Logger).GetPolicy(5000000, maximumTransactions).BlockAction;

            return
                (BlockChain <PolymorphicAction <ActionBase> > .MakeGenesisBlock(
                     actions,
                     privateKey : privateKey,
                     blockAction : blockAction));
        }
Exemplo n.º 4
0
        public void Execute()
        {
            var agentState = new AgentState(_agentAddress);

            agentState.avatarAddresses[0] = _avatarAddress;

            var gameConfigState = new GameConfigState();
            var avatarState     = new AvatarState(
                _avatarAddress,
                _agentAddress,
                1,
                _tableSheets,
                gameConfigState
                );
            var row = _tableSheets.ConsumableItemRecipeSheet.Values.First();

            foreach (var materialInfo in row.Materials)
            {
                var materialRow = _tableSheets.MaterialItemSheet[materialInfo.Id];
                var material    = ItemFactory.CreateItem(materialRow);
                avatarState.inventory.AddItem(material, materialInfo.Count);
            }

            const int requiredStage = GameConfig.RequireClearedStageLevel.CombinationConsumableAction;

            for (var i = 1; i < requiredStage + 1; i++)
            {
                avatarState.worldInformation.ClearStage(1, i, 0, _tableSheets.WorldSheet, _tableSheets.WorldUnlockSheet);
            }

            var initialState = new State()
                               .SetState(_agentAddress, agentState.Serialize())
                               .SetState(_avatarAddress, avatarState.Serialize())
                               .SetState(_slotAddress, new CombinationSlotState(_slotAddress, requiredStage).Serialize())
                               .SetState(TableSheetsState.Address, _tableSheetsState.Serialize());

            var action = new CombinationConsumable()
            {
                AvatarAddress = _avatarAddress,
                recipeId      = row.Id,
                slotIndex     = 0,
            };

            var nextState = action.Execute(new ActionContext()
            {
                PreviousStates = initialState,
                Signer         = _agentAddress,
                BlockIndex     = 1,
                Random         = _random,
            });

            var slotState = nextState.GetCombinationSlotState(_avatarAddress, 0);

            Assert.NotNull(slotState.Result);

            var consumable = (Consumable)slotState.Result.itemUsable;

            Assert.NotNull(consumable);
        }
Exemplo n.º 5
0
        public void Execute(bool backward)
        {
            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.GetAvatarSheets(),
                gameConfigState,
                default
                );

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

            var initialState = new State()
                               .SetState(_agentAddress, agentState.Serialize())
                               .SetState(RedeemCodeState.Address, prevRedeemCodesState.Serialize())
                               .SetState(GoldCurrencyState.Address, goldState.Serialize())
                               .MintAsset(GoldCurrencyState.Address, goldState.Currency * 100000000);

            if (backward)
            {
                initialState = initialState.SetState(_avatarAddress, avatarState.Serialize());
            }
            else
            {
                initialState = initialState
                               .SetState(_avatarAddress.Derive(LegacyInventoryKey), avatarState.inventory.Serialize())
                               .SetState(_avatarAddress.Derive(LegacyWorldInformationKey), avatarState.worldInformation.Serialize())
                               .SetState(_avatarAddress.Derive(LegacyQuestListKey), avatarState.questList.Serialize())
                               .SetState(_avatarAddress, avatarState.SerializeV2());
            }

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

            var redeemCode = new RedeemCode(
                ByteUtil.Hex(privateKey.ByteArray),
                _avatarAddress
                );

            IAccountStateDelta nextState = redeemCode.Execute(new ActionContext()
            {
                BlockIndex     = 1,
                Miner          = default,
Exemplo n.º 6
0
        public void Execute()
        {
            var privateKey   = new PrivateKey();
            var agentAddress = privateKey.PublicKey.ToAddress();
            var agent        = new AgentState(agentAddress);

            var avatarAddress   = agentAddress.Derive("avatar");
            var gameConfigState = new GameConfigState(_sheets[nameof(GameConfigSheet)]);
            var avatarState     = new AvatarState(
                avatarAddress,
                agentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                gameConfigState,
                default
                )
            {
                actionPoint = 0,
            };

            agent.avatarAddresses.Add(0, avatarAddress);

            var apStone =
                ItemFactory.CreateItem(
                    _tableSheets.MaterialItemSheet.Values.First(r => r.ItemSubType == ItemSubType.ApStone),
                    new TestRandom());

            avatarState.inventory.AddItem2(apStone);

            Assert.Equal(0, avatarState.actionPoint);

            var state = new State()
                        .SetState(Addresses.GameConfig, gameConfigState.Serialize())
                        .SetState(agentAddress, agent.Serialize())
                        .SetState(avatarAddress, avatarState.Serialize());

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

            var action = new ChargeActionPoint0()
            {
                avatarAddress = avatarAddress,
            };

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

            var nextAvatarState = nextState.GetAvatarState(avatarAddress);

            Assert.Equal(gameConfigState.ActionPointMax, nextAvatarState.actionPoint);
        }
Exemplo n.º 7
0
        public HackAndSlash10Test()
        {
            _sheets      = TableSheetsImporter.ImportSheets();
            _tableSheets = new TableSheets(_sheets);

            var privateKey = new PrivateKey();

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

            _avatarAddress = _agentAddress.Derive("avatar");
            var gameConfigState = new GameConfigState(_sheets[nameof(GameConfigSheet)]);

            _rankingMapAddress = _avatarAddress.Derive("ranking_map");
            _avatarState       = new AvatarState(
                _avatarAddress,
                _agentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                gameConfigState,
                _rankingMapAddress
                )
            {
                level = 100,
            };
            _inventoryAddress        = _avatarAddress.Derive(LegacyInventoryKey);
            _worldInformationAddress = _avatarAddress.Derive(LegacyWorldInformationKey);
            _questListAddress        = _avatarAddress.Derive(LegacyQuestListKey);
            agentState.avatarAddresses.Add(0, _avatarAddress);

            _weeklyArenaState = new WeeklyArenaState(0);

            _initialState = new State()
                            .SetState(_weeklyArenaState.address, _weeklyArenaState.Serialize())
                            .SetState(_agentAddress, agentState.SerializeV2())
                            .SetState(_avatarAddress, _avatarState.SerializeV2())
                            .SetState(_inventoryAddress, _avatarState.inventory.Serialize())
                            .SetState(_worldInformationAddress, _avatarState.worldInformation.Serialize())
                            .SetState(_questListAddress, _avatarState.questList.Serialize())
                            .SetState(_rankingMapAddress, new RankingMapState(_rankingMapAddress).Serialize())
                            .SetState(gameConfigState.address, gameConfigState.Serialize());

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

            foreach (var address in _avatarState.combinationSlotAddresses)
            {
                var slotState = new CombinationSlotState(
                    address,
                    GameConfig.RequireClearedStageLevel.CombinationEquipmentAction);
                _initialState = _initialState.SetState(address, slotState.Serialize());
            }
        }
        public CombinationEquipment6Test(ITestOutputHelper outputHelper)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .WriteTo.TestOutput(outputHelper)
                         .CreateLogger();

            _agentAddress  = default;
            _avatarAddress = _agentAddress.Derive("avatar");
            var slotAddress = _avatarAddress.Derive(
                string.Format(
                    CultureInfo.InvariantCulture,
                    CombinationSlotState.DeriveFormat,
                    0
                    )
                );
            var 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
                );
            var gold = new GoldCurrencyState(new Currency("NCG", 2, minter: null));

            _initialState = new State()
                            .SetState(_agentAddress, agentState.Serialize())
                            .SetState(_avatarAddress, _avatarState.Serialize())
                            .SetState(
                slotAddress,
                new CombinationSlotState(
                    slotAddress,
                    GameConfig.RequireClearedStageLevel.CombinationEquipmentAction
                    ).Serialize())
                            .SetState(GoldCurrencyState.Address, gold.Serialize())
                            .MintAsset(_agentAddress, gold.Currency * 300);

            foreach (var(key, value) in sheets)
            {
                _initialState =
                    _initialState.SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }
        }
Exemplo n.º 9
0
        public static int CalculateHourglassCount(GameConfigState state, long diff)
        {
            if (diff <= 0)
            {
                return(0);
            }

            var cost = Math.Ceiling((decimal)diff / state.HourglassPerBlock);

            return(Math.Max(1, (int)cost));
        }
Exemplo n.º 10
0
        public void CalculateHourglassCost([Values(1, 2, 3)] int diff)
        {
            var state = new GameConfigState();
            var row   = new GameConfigSheet.Row();

            row.Set(new List <string>
            {
                "hourglass_per_block", "3"
            });
            state.Update(row);
            Assert.AreEqual(1, RapidCombination.CalculateHourglassCount(state, diff));
        }
Exemplo n.º 11
0
        public CombinationConsumableTest()
        {
            _agentAddress  = new PrivateKey().ToAddress();
            _avatarAddress = _agentAddress.Derive("avatar");
            var slotAddress = _avatarAddress.Derive(
                string.Format(
                    CultureInfo.InvariantCulture,
                    CombinationSlotState.DeriveFormat,
                    0
                    )
                );
            var sheets = TableSheetsImporter.ImportSheets();

            _random      = new TestRandom();
            _tableSheets = new TableSheets(sheets);

            var agentState = new AgentState(_agentAddress);

            agentState.avatarAddresses[0] = _avatarAddress;

            var gameConfigState = new GameConfigState();

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

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

            _initialState = new State()
                            .SetState(_agentAddress, agentState.Serialize())
                            .SetState(_avatarAddress, avatarState.Serialize())
                            .SetState(
                slotAddress,
                new CombinationSlotState(
                    slotAddress,
                    GameConfig.RequireClearedStageLevel.CombinationConsumableAction).Serialize())
                            .SetState(GameConfigState.Address, gold.Serialize());

            foreach (var(key, value) in sheets)
            {
                _initialState =
                    _initialState.SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }
        }
Exemplo n.º 12
0
 protected override void LoadPlainValueInternal(IImmutableDictionary <string, IValue> plainValue)
 {
     RankingState           = new RankingState((Bencodex.Types.Dictionary)plainValue["ranking_state"]);
     ShopState              = new ShopState((Bencodex.Types.Dictionary)plainValue["shop_state"]);
     TableSheetsState       = new TableSheetsState((Bencodex.Types.Dictionary)plainValue["table_sheets_state"]);
     GameConfigState        = new GameConfigState((Bencodex.Types.Dictionary)plainValue["game_config_state"]);
     RedeemCodeState        = new RedeemCodeState((Bencodex.Types.Dictionary)plainValue["redeem_code_state"]);
     AdminAddressState      = new AdminState((Bencodex.Types.Dictionary)plainValue["admin_address_state"]);
     ActivatedAccountsState = new ActivatedAccountsState(
         (Bencodex.Types.Dictionary)plainValue["activated_accounts_state"]
         );
     GoldCurrencyState = new GoldCurrencyState(
         (Bencodex.Types.Dictionary)plainValue["gold_currency_state"]
         );
 }
Exemplo n.º 13
0
        public void ExecuteCreateNextWeeklyArenaState()
        {
            var weekly          = new WeeklyArenaState(0);
            var gameConfigState = new GameConfigState();

            gameConfigState.Set(_tableSheets.GameConfigSheet);
            var state = _baseState
                        .SetState(weekly.address, weekly.Serialize())
                        .SetState(gameConfigState.address, gameConfigState.Serialize());
            var action = new RewardGold();

            var nextState = action.Execute(new ActionContext()
            {
                PreviousStates = state,
                Miner          = default,
Exemplo n.º 14
0
        public MimisbrunnrBattle6Test()
        {
            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");
            var gameConfigState = new GameConfigState(sheets[nameof(GameConfigSheet)]);

            _rankingMapAddress = _avatarAddress.Derive("ranking_map");
            var avatarState = new AvatarState(
                _avatarAddress,
                _agentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                gameConfigState,
                _rankingMapAddress
                )
            {
                level = 400,
            };

            agentState.avatarAddresses.Add(0, _avatarAddress);

            _initialState = new State()
                            .SetState(_agentAddress, agentState.Serialize())
                            .SetState(_avatarAddress, avatarState.Serialize())
                            .SetState(_avatarAddress.Derive(LegacyInventoryKey), avatarState.inventory.Serialize())
                            .SetState(_avatarAddress.Derive(LegacyWorldInformationKey), avatarState.worldInformation.Serialize())
                            .SetState(_avatarAddress.Derive(LegacyQuestListKey), avatarState.questList.Serialize())
                            .SetState(_rankingMapAddress, new RankingMapState(_rankingMapAddress).Serialize())
                            .SetState(gameConfigState.address, gameConfigState.Serialize());

            foreach (var(key, value) in sheets)
            {
                _initialState = _initialState
                                .SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }
        }
Exemplo n.º 15
0
        public DailyReward5Test(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 gameConfigState = new GameConfigState();

            gameConfigState.Set(tableSheets.GameConfigSheet);
            _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(),
                gameConfigState,
                rankingMapAddress)
            {
                actionPoint = 0,
            };

            agentState.avatarAddresses[0] = _avatarAddress;

            _initialState = _initialState
                            .SetState(Addresses.GameConfig, gameConfigState.Serialize())
                            .SetState(_agentAddress, agentState.Serialize())
                            .SetState(_avatarAddress, avatarState.Serialize());
        }
Exemplo n.º 16
0
        public override IAccountStateDelta Execute(IActionContext context)
        {
            IActionContext ctx          = context;
            var            states       = ctx.PreviousStates;
            var            sheetAddress = Addresses.TableSheet.Derive(TableName);

            if (ctx.Rehearsal)
            {
                return(states
                       .SetState(sheetAddress, MarkChanged)
                       .SetState(GameConfigState.Address, MarkChanged));
            }

            var addressesHex = GetSignerAndOtherAddressesHex(context);

            CheckPermission(context);

            var sheets = states.GetState(sheetAddress);
            var value  = sheets is null ? string.Empty : sheets.ToDotnetString();

            Log.Verbose(
                "{AddressesHex}{TableName} was patched\n" +
                "before:\n" +
                "{Value}\n" +
                "after:\n" +
                "{TableCsv}",
                addressesHex,
                TableName,
                value,
                TableCsv
                );

            states = states.SetState(sheetAddress, TableCsv.Serialize());

            if (TableName == nameof(GameConfigSheet))
            {
                var gameConfigState = new GameConfigState(TableCsv);
                states = states.SetState(GameConfigState.Address, gameConfigState.Serialize());
            }

            return(states);
        }
Exemplo n.º 17
0
        public override IAccountStateDelta Execute(IActionContext context)
        {
            IActionContext ctx              = context;
            var            states           = ctx.PreviousStates;
            var            weeklyArenaState = new WeeklyArenaState(0);

            if (ctx.Rehearsal)
            {
                states = states.SetState(RankingState.Address, MarkChanged);
                states = states.SetState(ShopState.Address, MarkChanged);
                states = states.SetState(TableSheetsState.Address, MarkChanged);
                states = states.SetState(weeklyArenaState.address, MarkChanged);
                states = states.SetState(GameConfigState.Address, MarkChanged);
                states = states.SetState(RedeemCodeState.Address, MarkChanged);
                states = states.SetState(AdminState.Address, MarkChanged);
                states = states.SetState(ActivatedAccountsState.Address, MarkChanged);
                states = states.SetState(GoldCurrencyState.Address, MarkChanged);
                states = states.SetState(Addresses.GoldDistribution, MarkChanged);
                return(states);
            }

            if (ctx.BlockIndex != 0)
            {
                return(states);
            }

            states = states
                     .SetState(weeklyArenaState.address, weeklyArenaState.Serialize())
                     .SetState(RankingState.Address, RankingState.Serialize())
                     .SetState(ShopState.Address, ShopState.Serialize())
                     .SetState(TableSheetsState.Address, TableSheetsState.Serialize())
                     .SetState(GameConfigState.Address, GameConfigState.Serialize())
                     .SetState(RedeemCodeState.Address, RedeemCodeState.Serialize())
                     .SetState(AdminState.Address, AdminAddressState.Serialize())
                     .SetState(ActivatedAccountsState.Address, ActivatedAccountsState.Serialize())
                     .SetState(GoldCurrencyState.Address, GoldCurrencyState.Serialize())
                     .SetState(Addresses.GoldDistribution, GoldDistributions.Select(v => v.Serialize()).Serialize());

            states = states.MintAsset(GoldCurrencyState.Address, GoldCurrencyState.Currency, 1000000000);
            return(states);
        }
Exemplo n.º 18
0
        public void WeeklyArenaRankingBoard(bool resetCount, bool updateNext)
        {
            var weekly = new WeeklyArenaState(0);

            weekly.Set(_avatarState, _tableSheets.CharacterSheet);
            weekly[_avatarState.address].Update(weekly[_avatarState.address], BattleLog.Result.Lose);
            var gameConfigState = new GameConfigState();

            gameConfigState.Set(_tableSheets.GameConfigSheet);
            var state = _baseState
                        .SetState(weekly.address, weekly.Serialize())
                        .SetState(gameConfigState.address, gameConfigState.Serialize());
            var blockIndex = 0;

            if (resetCount)
            {
                blockIndex = gameConfigState.DailyArenaInterval;
            }

            if (updateNext)
            {
                weekly[_avatarState.address].Activate();
                blockIndex = gameConfigState.WeeklyArenaInterval;
                // Avoid NRE in test case.
                var nextWeekly = new WeeklyArenaState(1);
                state = state
                        .SetState(weekly.address, weekly.Serialize())
                        .SetState(nextWeekly.address, nextWeekly.Serialize());
            }

            Assert.False(weekly.Ended);
            Assert.Equal(4, weekly[_avatarState.address].DailyChallengeCount);

            var action = new RewardGold();

            var ctx = new ActionContext()
            {
                BlockIndex     = blockIndex,
                PreviousStates = _baseState,
                Miner          = default,
Exemplo n.º 19
0
        public CombinationConsumable4Test()
        {
            _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.º 20
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.º 21
0
        public InitializeStates(
            RankingState rankingState,
            ShopState shopState,
            Dictionary <string, string> tableSheets,
            GameConfigState gameConfigState,
            RedeemCodeState redeemCodeState,
            AdminState adminAddressState,
            ActivatedAccountsState activatedAccountsState,
            GoldCurrencyState goldCurrencyState,
            GoldDistribution[] goldDistributions,
            PendingActivationState[] pendingActivationStates,
            AuthorizedMinersState authorizedMinersState = null,
            CreditsState creditsState = null)
        {
            Ranking           = (Bencodex.Types.Dictionary)rankingState.Serialize();
            Shop              = (Bencodex.Types.Dictionary)shopState.Serialize();
            TableSheets       = tableSheets;
            GameConfig        = (Bencodex.Types.Dictionary)gameConfigState.Serialize();
            RedeemCode        = (Bencodex.Types.Dictionary)redeemCodeState.Serialize();
            AdminAddress      = (Bencodex.Types.Dictionary)adminAddressState.Serialize();
            ActivatedAccounts = (Bencodex.Types.Dictionary)activatedAccountsState.Serialize();
            GoldCurrency      = (Bencodex.Types.Dictionary)goldCurrencyState.Serialize();
            GoldDistributions = new Bencodex.Types.List(
                goldDistributions.Select(d => d.Serialize()).Cast <Bencodex.Types.IValue>()
                );
            PendingActivations = new Bencodex.Types.List(pendingActivationStates.Select(p => p.Serialize()));

            if (!(authorizedMinersState is null))
            {
                AuthorizedMiners = (Bencodex.Types.Dictionary)authorizedMinersState.Serialize();
            }

            if (!(creditsState is null))
            {
                Credits = (Bencodex.Types.Dictionary)creditsState.Serialize();
            }
        }
Exemplo n.º 22
0
        public void Execute()
        {
            var gameConfigState     = new GameConfigState(_sheets[nameof(GameConfigSheet)]);
            var redeemCodeListSheet = new RedeemCodeListSheet();

            redeemCodeListSheet.Set(_sheets[nameof(RedeemCodeListSheet)]);
            var goldDistributionCsvPath = GoldDistributionTest.CreateFixtureCsvFile();
            var goldDistributions       = GoldDistribution.LoadInDescendingEndBlockOrder(goldDistributionCsvPath);
            var minterKey  = new PrivateKey();
            var ncg        = new Currency("NCG", 2, minterKey.ToAddress());
            var nonce      = new byte[] { 0x00, 0x01, 0x02, 0x03 };
            var privateKey = new PrivateKey();

            (ActivationKey activationKey, PendingActivationState pendingActivation) =
                ActivationKey.Create(privateKey, nonce);

            var action = new InitializeStates(
                rankingState: new RankingState(),
                shopState: new ShopState(),
                tableSheets: _sheets,
                gameConfigState: gameConfigState,
                redeemCodeState: new RedeemCodeState(redeemCodeListSheet),
                adminAddressState: new AdminState(
                    new Address("F9A15F870701268Bd7bBeA6502eB15F4997f32f9"),
                    1500000
                    ),
                activatedAccountsState: new ActivatedAccountsState(),
                goldCurrencyState: new GoldCurrencyState(ncg),
                goldDistributions: goldDistributions,
                pendingActivationStates: new[] { pendingActivation }
                );

            var genesisState = action.Execute(new ActionContext()
            {
                BlockIndex     = 0,
                Miner          = default,
Exemplo n.º 23
0
        public override IAccountStateDelta Execute(IActionContext context)
        {
            IActionContext ctx          = context;
            var            states       = ctx.PreviousStates;
            var            sheetAddress = Addresses.TableSheet.Derive(TableName);

            if (ctx.Rehearsal)
            {
                return(states
                       .SetState(sheetAddress, MarkChanged)
                       .SetState(GameConfigState.Address, MarkChanged));
            }

            CheckPermission(context);

            var sheets = states.GetState(sheetAddress);
            var value  = sheets is null ? string.Empty : sheets.ToDotnetString();

            Log.Debug($"[{ctx.BlockIndex}] {TableName} was patched by {ctx.Signer.ToHex()}\n" +
                      "before:\n" +
                      value +
                      "\n" +
                      "after:\n" +
                      TableCsv
                      );

            states = states.SetState(sheetAddress, TableCsv.Serialize());

            if (TableName == nameof(GameConfigSheet))
            {
                var gameConfigState = new GameConfigState(TableCsv);
                states = states.SetState(GameConfigState.Address, gameConfigState.Serialize());
            }

            return(states);
        }
Exemplo n.º 24
0
        public override IAccountStateDelta Execute(IActionContext context)
        {
            IActionContext ctx    = context;
            var            states = ctx.PreviousStates;

            if (ctx.Rehearsal)
            {
                return(states
                       .SetState(TableSheetsState.Address, MarkChanged)
                       .SetState(GameConfigState.Address, MarkChanged));
            }

            CheckPermission(context);

            var tableSheetsState = TableSheetsState.FromActionContext(ctx);

            Log.Debug($"[{ctx.BlockIndex}] {TableName} was patched by {ctx.Signer.ToHex()}\n" +
                      "before:\n" +
                      (tableSheetsState.TableSheets.TryGetValue(TableName, out string value) ? value : string.Empty) +
                      "\n" +
                      "after:\n" +
                      TableCsv
                      );

            TableSheetsState nextState = tableSheetsState.UpdateTableSheet(TableName, TableCsv);

            states = states.SetState(TableSheetsState.Address, nextState.Serialize());

            if (TableName == nameof(GameConfigSheet))
            {
                var gameConfigState = new GameConfigState(TableCsv);
                states = states.SetState(GameConfigState.Address, gameConfigState.Serialize());
            }

            return(states);
        }
Exemplo n.º 25
0
        public ChargeActionPointTest()
        {
            _sheets      = TableSheetsImporter.ImportSheets();
            _tableSheets = new TableSheets(_sheets);

            var privateKey = new PrivateKey();

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

            _avatarAddress = _agentAddress.Derive("avatar");
            var gameConfigState = new GameConfigState(_sheets[nameof(GameConfigSheet)]);
            var avatarState     = new AvatarState(
                _avatarAddress,
                _agentAddress,
                0,
                _tableSheets.GetAvatarSheets(),
                gameConfigState,
                default
                )
            {
                actionPoint = 0,
            };

            agent.avatarAddresses.Add(0, _avatarAddress);

            _initialState = new State()
                            .SetState(Addresses.GameConfig, gameConfigState.Serialize())
                            .SetState(_agentAddress, agent.Serialize())
                            .SetState(_avatarAddress, avatarState.Serialize());

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

            var sheets = TableSheetsImporter.ImportSheets();

            _tableSheets = new TableSheets(sheets);

            var gold            = new GoldCurrencyState(new Currency("NCG", 2, minter: null));
            var gameConfigState = new GameConfigState(sheets[nameof(GameConfigSheet)]);

            _agentAddress  = new PrivateKey().ToAddress();
            _avatarAddress = _agentAddress.Derive("avatar");
            _slot0Address  = _avatarAddress.Derive(
                string.Format(
                    CultureInfo.InvariantCulture,
                    CombinationSlotState.DeriveFormat,
                    0
                    )
                );
            var slot0State = new CombinationSlotState(
                _slot0Address,
                GameConfig.RequireClearedStageLevel.CombinationEquipmentAction);

            var agentState = new AgentState(_agentAddress);

            agentState.avatarAddresses[0] = _avatarAddress;

            var avatarState = new AvatarState(
                _avatarAddress,
                _agentAddress,
                1,
                _tableSheets.GetAvatarSheets(),
                gameConfigState,
                default
                )
            {
                worldInformation = new WorldInformation(
                    0,
                    _tableSheets.WorldSheet,
                    GameConfig.RequireClearedStageLevel.CombinationEquipmentAction),
            };

            _inventoryAddress        = _avatarAddress.Derive(LegacyInventoryKey);
            _worldInformationAddress = _avatarAddress.Derive(LegacyWorldInformationKey);
            _questListAddress        = _avatarAddress.Derive(LegacyQuestListKey);

            _initialState = new Tests.Action.State()
                            .SetState(GoldCurrencyState.Address, gold.Serialize())
                            .SetState(gameConfigState.address, gameConfigState.Serialize())
                            .SetState(_agentAddress, agentState.Serialize())
                            .SetState(_avatarAddress, avatarState.SerializeV2())
                            .SetState(_inventoryAddress, avatarState.inventory.Serialize())
                            .SetState(_worldInformationAddress, avatarState.worldInformation.Serialize())
                            .SetState(_questListAddress, avatarState.questList.Serialize())
                            .SetState(_slot0Address, slot0State.Serialize());

            foreach (var(key, value) in sheets)
            {
                _initialState = _initialState
                                .SetState(Addresses.TableSheet.Derive(key), value.Serialize());
            }
        }
        //TODO : This should be covered in test.
        private void NotifyRefillActionPoint(long newTipIndex)
        {
            List <Tuple <Guid, ProtectedPrivateKey> > tuples =
                StandaloneContext.KeyStore.List().ToList();

            if (!tuples.Any())
            {
                return;
            }

            IEnumerable <Address> playerAddresses = tuples.Select(tuple => tuple.Item2.Address);
            var chain = StandaloneContext.BlockChain;

            List <IValue> states = playerAddresses
                                   .Select(addr => chain.GetState(addr))
                                   .Where(value => !(value is null))
                                   .ToList();

            if (!states.Any())
            {
                return;
            }

            var agentStates =
                states.Select(state => new AgentState((Bencodex.Types.Dictionary)state));
            var avatarStates = agentStates.SelectMany(agentState =>
                                                      agentState.avatarAddresses.Values.Select(address =>
                                                                                               new AvatarState((Bencodex.Types.Dictionary)chain.GetState(address))));
            var gameConfigState =
                new GameConfigState((Bencodex.Types.Dictionary)chain.GetState(Addresses.GameConfig));

            bool IsDailyRewardRefilled(long dailyRewardReceivedIndex)
            {
                return(newTipIndex >= dailyRewardReceivedIndex + gameConfigState.DailyRewardInterval);
            }

            bool NeedsRefillNotification(AvatarState avatarState)
            {
                if (NotificationRecords.TryGetValue(avatarState.address, out long record))
                {
                    return(avatarState.dailyRewardReceivedIndex != record &&
                           IsDailyRewardRefilled(avatarState.dailyRewardReceivedIndex));
                }

                return(IsDailyRewardRefilled(avatarState.dailyRewardReceivedIndex));
            }

            var avatarStatesToSendNotification = avatarStates
                                                 .Where(NeedsRefillNotification)
                                                 .ToList();

            if (avatarStatesToSendNotification.Any())
            {
                var notification = new Notification(NotificationEnum.Refill);
                StandaloneContext.NotificationSubject.OnNext(notification);
            }

            foreach (var avatarState in avatarStatesToSendNotification)
            {
                Log.Debug(
                    "Record notification for {AvatarAddress}",
                    avatarState.address.ToHex());
                NotificationRecords[avatarState.address] = avatarState.dailyRewardReceivedIndex;
            }
        }
Exemplo n.º 28
0
 public static void OnNext(GameConfigState state)
 {
     GameConfigState.OnNext(state);
 }
Exemplo n.º 29
0
 public void SetGameConfigState(GameConfigState state)
 {
     GameConfigState = state;
     GameConfigStateSubject.OnNext(state);
 }