public IAccountStateDelta WeeklyArenaRankingBoard(IActionContext ctx, IAccountStateDelta states) { var index = Math.Max((int)ctx.BlockIndex / GameConfig.WeeklyArenaInterval, 0); var weekly = states.GetWeeklyArenaState(index); var nextIndex = index + 1; var nextWeekly = states.GetWeeklyArenaState(nextIndex); if (nextWeekly is null) { nextWeekly = new WeeklyArenaState(nextIndex); states = states.SetState(nextWeekly.address, nextWeekly.Serialize()); } if (ctx.BlockIndex % GameConfig.WeeklyArenaInterval == 0 && index > 0) { var prevWeekly = states.GetWeeklyArenaState(index - 1); if (!prevWeekly.Ended) { prevWeekly.End(); weekly.Update(prevWeekly, ctx.BlockIndex); states = states.SetState(prevWeekly.address, prevWeekly.Serialize()); states = states.SetState(weekly.address, weekly.Serialize()); } } else if (ctx.BlockIndex - weekly.ResetIndex >= GameConfig.DailyArenaInterval) { weekly.ResetCount(ctx.BlockIndex); states = states.SetState(weekly.address, weekly.Serialize()); } return(states); }
public void Execute() { var previousWeeklyState = _initialState.GetWeeklyArenaState(0); var previousAvatar1State = _initialState.GetAvatarState(_avatar1Address); previousAvatar1State.level = 10; var previousState = _initialState.SetState( _avatar1Address, previousAvatar1State.Serialize()); var itemIds = _tableSheets.WeeklyArenaRewardSheet.Values .Select(r => r.Reward.ItemId) .ToList(); Assert.All(itemIds, id => Assert.False(previousAvatar1State.inventory.HasItem(id))); var action = new RankingBattle4 { AvatarAddress = _avatar1Address, EnemyAddress = _avatar2Address, WeeklyArenaAddress = _weeklyArenaAddress, costumeIds = new List <int>(), equipmentIds = new List <Guid>(), consumableIds = new List <Guid>(), }; Assert.Null(action.Result); var nextState = action.Execute(new ActionContext() { PreviousStates = previousState, Signer = _agent1Address, Random = new TestRandom(), Rehearsal = false, }); var nextAvatar1State = nextState.GetAvatarState(_avatar1Address); var nextWeeklyState = nextState.GetWeeklyArenaState(0); Assert.Contains(nextAvatar1State.inventory.Materials, i => itemIds.Contains(i.Id)); Assert.NotNull(action.Result); Assert.Contains(typeof(GetReward), action.Result.Select(e => e.GetType())); Assert.Equal(BattleLog.Result.Win, action.Result.result); Assert.True(nextWeeklyState[_avatar1Address].Score > previousWeeklyState[_avatar1Address].Score); }
public IAccountStateDelta WeeklyArenaRankingBoard2(IActionContext ctx, IAccountStateDelta states) { var gameConfigState = states.GetGameConfigState(); var index = Math.Max((int)ctx.BlockIndex / gameConfigState.WeeklyArenaInterval, 0); var weeklyAddress = WeeklyArenaState.DeriveAddress(index); var rawWeekly = (Dictionary)states.GetState(weeklyAddress); var nextIndex = index + 1; var nextWeekly = states.GetWeeklyArenaState(nextIndex); if (nextWeekly is null) { nextWeekly = new WeeklyArenaState(nextIndex); states = states.SetState(nextWeekly.address, nextWeekly.Serialize()); } var resetIndex = rawWeekly["resetIndex"].ToLong(); // Beginning block of a new weekly arena. if (ctx.BlockIndex % gameConfigState.WeeklyArenaInterval == 0 && index > 0) { var prevWeeklyAddress = WeeklyArenaState.DeriveAddress(index - 1); var rawPrevWeekly = (Dictionary)states.GetState(prevWeeklyAddress); if (!rawPrevWeekly["ended"].ToBoolean()) { rawPrevWeekly = rawPrevWeekly.SetItem("ended", true.Serialize()); var weekly = new WeeklyArenaState(rawWeekly); var prevWeekly = new WeeklyArenaState(rawPrevWeekly); weekly.Update(prevWeekly, ctx.BlockIndex); states = states.SetState(prevWeeklyAddress, rawPrevWeekly); states = states.SetState(weeklyAddress, weekly.Serialize()); } } else if (ctx.BlockIndex - resetIndex >= gameConfigState.DailyArenaInterval) { var weekly = new WeeklyArenaState(rawWeekly); weekly.ResetCount(ctx.BlockIndex); states = states.SetState(weeklyAddress, weekly.Serialize()); } return(states); }
public void Execute(bool isNew, bool avatarBackward, bool enemyBackward) { var previousWeeklyState = _initialState.GetWeeklyArenaState(0); var previousAvatar1State = _initialState.GetAvatarState(_avatar1Address); previousAvatar1State.level = 10; var prevScore = previousWeeklyState[_avatar1Address].Score; if (isNew) { previousWeeklyState.Remove(_avatar1Address); } var previousState = _initialState.SetState( _avatar1Address, previousAvatar1State.Serialize()); var itemIds = _tableSheets.WeeklyArenaRewardSheet.Values .Select(r => r.Reward.ItemId) .ToList(); Assert.All(itemIds, id => Assert.False(previousAvatar1State.inventory.HasItem(id))); var row = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.ATK); var costume = (Costume)ItemFactory.CreateItem( _tableSheets.ItemSheet[row.CostumeId], new TestRandom()); costume.equipped = true; previousAvatar1State.inventory.AddItem(costume); var row2 = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.DEF); var enemyCostume = (Costume)ItemFactory.CreateItem( _tableSheets.ItemSheet[row2.CostumeId], new TestRandom()); enemyCostume.equipped = true; var enemyAvatarState = _initialState.GetAvatarState(_avatar2Address); enemyAvatarState.inventory.AddItem(enemyCostume); if (avatarBackward) { previousState = previousState.SetState(_avatar1Address, previousAvatar1State.Serialize()); } else { previousState = previousState .SetState(_avatar1Address.Derive(LegacyInventoryKey), previousAvatar1State.inventory.Serialize()) .SetState(_avatar1Address.Derive(LegacyWorldInformationKey), previousAvatar1State.worldInformation.Serialize()) .SetState(_avatar1Address.Derive(LegacyQuestListKey), previousAvatar1State.questList.Serialize()) .SetState(_avatar1Address, previousAvatar1State.SerializeV2()); } if (enemyBackward) { previousState = previousState.SetState(_avatar2Address, enemyAvatarState.Serialize()); } else { previousState = previousState .SetState(_avatar2Address.Derive(LegacyInventoryKey), enemyAvatarState.inventory.Serialize()) .SetState(_avatar2Address.Derive(LegacyWorldInformationKey), enemyAvatarState.worldInformation.Serialize()) .SetState(_avatar2Address.Derive(LegacyQuestListKey), enemyAvatarState.questList.Serialize()) .SetState(_avatar2Address, enemyAvatarState.SerializeV2()); } var action = new RankingBattle { avatarAddress = _avatar1Address, enemyAddress = _avatar2Address, weeklyArenaAddress = _weeklyArenaAddress, costumeIds = new List <Guid> { costume.ItemId }, equipmentIds = new List <Guid>(), consumableIds = new List <Guid>(), }; Assert.Null(action.Result); var nextState = action.Execute(new ActionContext() { PreviousStates = previousState, Signer = _agent1Address, Random = new TestRandom(), Rehearsal = false, }); var nextAvatar1State = nextState.GetAvatarStateV2(_avatar1Address); var nextWeeklyState = nextState.GetWeeklyArenaState(0); Assert.Contains(nextAvatar1State.inventory.Materials, i => itemIds.Contains(i.Id)); Assert.NotNull(action.Result); Assert.Contains(typeof(GetReward), action.Result.Select(e => e.GetType())); Assert.Equal(BattleLog.Result.Win, action.Result.result); Assert.True(nextWeeklyState[_avatar1Address].Score > prevScore); }
public void Execute() { var previousWeeklyState = _initialState.GetWeeklyArenaState(ArenaIndex); var previousAvatar1State = _initialState.GetAvatarState(_avatar1Address); previousAvatar1State.level = 10; var prevScore = previousWeeklyState[_avatar1Address].Score; var previousState = _initialState.SetState( _avatar1Address, previousAvatar1State.Serialize()); var itemIds = _tableSheets.WeeklyArenaRewardSheet.Values .Select(r => r.Reward.ItemId) .ToList(); Assert.All(itemIds, id => Assert.False(previousAvatar1State.inventory.HasItem(id))); var row = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.ATK); var costume = (Costume)ItemFactory.CreateItem( _tableSheets.ItemSheet[row.CostumeId], new TestRandom()); costume.equipped = true; previousAvatar1State.inventory.AddItem(costume); var row2 = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.DEF); var enemyCostume = (Costume)ItemFactory.CreateItem( _tableSheets.ItemSheet[row2.CostumeId], new TestRandom()); enemyCostume.equipped = true; var enemyAvatarState = _initialState.GetAvatarState(_avatar2Address); enemyAvatarState.inventory.AddItem(enemyCostume); Address worldInformationAddress = _avatar1Address.Derive(LegacyWorldInformationKey); var weeklyArenaAddress = WeeklyArenaState.DeriveAddress(RankingBattle.UpdateTargetWeeklyArenaIndex); previousState = previousState .SetState( _avatar1Address.Derive(LegacyInventoryKey), previousAvatar1State.inventory.Serialize()) .SetState( worldInformationAddress, previousAvatar1State.worldInformation.Serialize()) .SetState( _avatar1Address.Derive(LegacyQuestListKey), previousAvatar1State.questList.Serialize()) .SetState(_avatar1Address, previousAvatar1State.SerializeV2()) .SetState( _avatar2Address.Derive(LegacyInventoryKey), enemyAvatarState.inventory.Serialize()) .SetState( _avatar2Address.Derive(LegacyWorldInformationKey), enemyAvatarState.worldInformation.Serialize()) .SetState( _avatar2Address.Derive(LegacyQuestListKey), enemyAvatarState.questList.Serialize()) .SetState(_avatar2Address, enemyAvatarState.SerializeV2()) .SetState( weeklyArenaAddress, new WeeklyArenaState(RankingBattle.UpdateTargetWeeklyArenaIndex).Serialize()); var action = new RankingBattle { avatarAddress = _avatar1Address, enemyAddress = _avatar2Address, weeklyArenaAddress = weeklyArenaAddress, costumeIds = new List <Guid> { costume.ItemId }, equipmentIds = new List <Guid>(), }; var nextState = action.Execute(new ActionContext { PreviousStates = previousState, Signer = _agent1Address, Random = new TestRandom(), Rehearsal = false, BlockIndex = RankingBattle.UpdateTargetBlockIndex, }); Assert.True(nextState.TryGetState(weeklyArenaAddress.Derive(_avatar1Address.ToByteArray()), out Dictionary rawArenaInfo)); Assert.True(nextState.TryGetState(weeklyArenaAddress.Derive(_avatar2Address.ToByteArray()), out Dictionary rawEnemyInfo)); Assert.True(nextState.TryGetState(weeklyArenaAddress.Derive("address_list"), out List rawAddressList)); var nextWeekly = nextState.GetWeeklyArenaState(weeklyArenaAddress); Assert.Empty(nextWeekly.Map); var nextAvatar1State = nextState.GetAvatarStateV2(_avatar1Address); var nextArenaInfo = new ArenaInfo(rawArenaInfo); var addressList = rawAddressList.ToList(StateExtensions.ToAddress); Assert.Contains(_avatar1Address, addressList); Assert.Contains(_avatar2Address, addressList); Assert.Contains(nextAvatar1State.inventory.Materials, i => itemIds.Contains(i.Id)); Assert.NotNull(action.ArenaInfo); Assert.NotNull(action.EnemyArenaInfo); Assert.True(nextArenaInfo.Score > prevScore); // Check simulation result equal. var player = new Player( previousAvatar1State, _tableSheets.CharacterSheet, _tableSheets.CharacterLevelSheet, _tableSheets.EquipmentItemSetEffectSheet); var simulator = new RankingSimulator( new TestRandom(), player, action.EnemyPlayerDigest, new List <Guid>(), _tableSheets.GetRankingSimulatorSheets(), RankingBattle.StageId, action.ArenaInfo, action.EnemyArenaInfo, _tableSheets.CostumeStatSheet); simulator.Simulate(); Assert.Equal(nextArenaInfo.Score, simulator.Log.score); Assert.Equal(previousAvatar1State.SerializeV2(), nextAvatar1State.SerializeV2()); Assert.Equal(previousAvatar1State.worldInformation.Serialize(), nextAvatar1State.worldInformation.Serialize()); }
public IAccountStateDelta PrepareNextArena(IActionContext ctx, IAccountStateDelta states) { var gameConfigState = states.GetGameConfigState(); var index = Math.Max((int)ctx.BlockIndex / gameConfigState.WeeklyArenaInterval, 0); var weeklyAddress = WeeklyArenaState.DeriveAddress(index); var rawWeekly = (Dictionary)states.GetState(weeklyAddress); var nextIndex = index + 1; var nextWeekly = states.GetWeeklyArenaState(nextIndex); if (nextWeekly is null) { nextWeekly = new WeeklyArenaState(nextIndex); states = states.SetState(nextWeekly.address, nextWeekly.Serialize()); } // Beginning block of a new weekly arena. if (ctx.BlockIndex % gameConfigState.WeeklyArenaInterval == 0 && index > 0) { var prevWeeklyAddress = WeeklyArenaState.DeriveAddress(index - 1); var rawPrevWeekly = (Dictionary)states.GetState(prevWeeklyAddress); if (!rawPrevWeekly["ended"].ToBoolean()) { rawPrevWeekly = rawPrevWeekly.SetItem("ended", true.Serialize()); var weekly = new WeeklyArenaState(rawWeekly); var prevWeekly = new WeeklyArenaState(rawPrevWeekly); var listAddress = weekly.address.Derive("address_list"); // Set ArenaInfo, address list for new RankingBattle. var addressList = states.TryGetState(listAddress, out List rawList) ? rawList.ToList(StateExtensions.ToAddress) : new List <Address>(); if (ctx.BlockIndex >= RankingBattle.UpdateTargetBlockIndex) { weekly.ResetIndex = ctx.BlockIndex; // Copy Map to address list. if (ctx.BlockIndex == RankingBattle.UpdateTargetBlockIndex) { foreach (var kv in prevWeekly.Map) { var address = kv.Key; var lazyInfo = kv.Value; var info = new ArenaInfo(lazyInfo.State); states = states.SetState( weeklyAddress.Derive(address.ToByteArray()), info.Serialize()); if (!addressList.Contains(address)) { addressList.Add(address); } } } else { // Copy addresses from prev weekly address list. var prevListAddress = prevWeekly.address.Derive("address_list"); if (states.TryGetState(prevListAddress, out List prevRawList)) { var prevList = prevRawList.ToList(StateExtensions.ToAddress); foreach (var address in prevList.Where(address => !addressList.Contains(address))) { addressList.Add(address); } } // Copy ArenaInfo from prev ArenaInfo. foreach (var address in addressList) { if (states.TryGetState( prevWeekly.address.Derive(address.ToByteArray()), out Dictionary rawInfo)) { var prevInfo = new ArenaInfo(rawInfo); var info = new ArenaInfo(prevInfo); states = states.SetState( weeklyAddress.Derive(address.ToByteArray()), info.Serialize()); } } } // Set address list. states = states.SetState(listAddress, addressList.Aggregate(List.Empty, (current, address) => current.Add(address.Serialize()))); } // Run legacy Update. else { weekly.Update(prevWeekly, ctx.BlockIndex); } states = states.SetState(prevWeeklyAddress, rawPrevWeekly); states = states.SetState(weeklyAddress, weekly.Serialize()); } } return(states); }
public void Execute() { var previousWeeklyState = _initialState.GetWeeklyArenaState(0); var previousAvatar1State = _initialState.GetAvatarState(_avatar1Address); previousAvatar1State.level = 10; var previousState = _initialState.SetState( _avatar1Address, previousAvatar1State.Serialize()); var itemIds = _tableSheets.WeeklyArenaRewardSheet.Values .Select(r => r.Reward.ItemId) .ToList(); Assert.All(itemIds, id => Assert.False(previousAvatar1State.inventory.HasItem(id))); var row = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.ATK); var costume = (Costume)ItemFactory.CreateItem( _tableSheets.ItemSheet[row.CostumeId], new ItemEnhancementTest.TestRandom()); costume.equipped = true; var avatarState = _initialState.GetAvatarState(_avatar1Address); avatarState.inventory.AddItem(costume); var row2 = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.DEF); var enemyCostume = (Costume)ItemFactory.CreateItem( _tableSheets.ItemSheet[row2.CostumeId], new ItemEnhancementTest.TestRandom()); enemyCostume.equipped = true; var enemyAvatarState = _initialState.GetAvatarState(_avatar2Address); enemyAvatarState.inventory.AddItem(enemyCostume); _initialState = _initialState .SetState(_avatar1Address, avatarState.Serialize()) .SetState(_avatar2Address, enemyAvatarState.Serialize()); var action = new RankingBattle2 { AvatarAddress = _avatar1Address, EnemyAddress = _avatar2Address, WeeklyArenaAddress = _weeklyArenaAddress, costumeIds = new List <int> { costume.Id }, equipmentIds = new List <Guid>(), consumableIds = new List <Guid>(), }; Assert.Null(action.Result); var nextState = action.Execute(new ActionContext() { PreviousStates = previousState, Signer = _agent1Address, Random = new ItemEnhancementTest.TestRandom(), Rehearsal = false, }); var nextAvatar1State = nextState.GetAvatarState(_avatar1Address); var nextWeeklyState = nextState.GetWeeklyArenaState(0); Assert.Contains(nextAvatar1State.inventory.Materials, i => itemIds.Contains(i.Id)); Assert.NotNull(action.Result); Assert.Contains(typeof(GetReward), action.Result.Select(e => e.GetType())); Assert.Equal(BattleLog.Result.Win, action.Result.result); Assert.True(nextWeeklyState[_avatar1Address].Score > previousWeeklyState[_avatar1Address].Score); }
public void Execute(bool isNew, bool avatarBackward, bool enemyBackward) { var previousWeeklyState = _initialState.GetWeeklyArenaState(0); var previousAvatar1State = _initialState.GetAvatarState(_avatar1Address); previousAvatar1State.level = 10; var prevScore = previousWeeklyState[_avatar1Address].Score; if (isNew) { previousWeeklyState.Remove(_avatar1Address); } var previousState = _initialState.SetState( _avatar1Address, previousAvatar1State.Serialize()); var itemIds = _tableSheets.WeeklyArenaRewardSheet.Values .Select(r => r.Reward.ItemId) .ToList(); Assert.All(itemIds, id => Assert.False(previousAvatar1State.inventory.HasItem(id))); var row = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.ATK); var costume = (Costume)ItemFactory.CreateItem( _tableSheets.ItemSheet[row.CostumeId], new TestRandom()); costume.equipped = true; previousAvatar1State.inventory.AddItem(costume); var row2 = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.DEF); var enemyCostume = (Costume)ItemFactory.CreateItem( _tableSheets.ItemSheet[row2.CostumeId], new TestRandom()); enemyCostume.equipped = true; var enemyAvatarState = _initialState.GetAvatarState(_avatar2Address); enemyAvatarState.inventory.AddItem(enemyCostume); Address worldInformationAddress = _avatar1Address.Derive(LegacyWorldInformationKey); if (avatarBackward) { previousState = previousState.SetState(_avatar1Address, previousAvatar1State.Serialize()); } else { previousState = previousState .SetState( _avatar1Address.Derive(LegacyInventoryKey), previousAvatar1State.inventory.Serialize()) .SetState( worldInformationAddress, previousAvatar1State.worldInformation.Serialize()) .SetState( _avatar1Address.Derive(LegacyQuestListKey), previousAvatar1State.questList.Serialize()) .SetState(_avatar1Address, previousAvatar1State.SerializeV2()); } if (enemyBackward) { previousState = previousState.SetState(_avatar2Address, enemyAvatarState.Serialize()); } else { previousState = previousState .SetState( _avatar2Address.Derive(LegacyInventoryKey), enemyAvatarState.inventory.Serialize()) .SetState( _avatar2Address.Derive(LegacyWorldInformationKey), enemyAvatarState.worldInformation.Serialize()) .SetState( _avatar2Address.Derive(LegacyQuestListKey), enemyAvatarState.questList.Serialize()) .SetState(_avatar2Address, enemyAvatarState.SerializeV2()); } var action = new RankingBattle10 { avatarAddress = _avatar1Address, enemyAddress = _avatar2Address, weeklyArenaAddress = _weeklyArenaAddress, costumeIds = new List <Guid> { costume.ItemId }, equipmentIds = new List <Guid>(), }; var nextState = action.Execute(new ActionContext { PreviousStates = previousState, Signer = _agent1Address, Random = new TestRandom(), Rehearsal = false, }); var nextAvatar1State = nextState.GetAvatarStateV2(_avatar1Address); var nextWeeklyState = nextState.GetWeeklyArenaState(0); var nextArenaInfo = nextWeeklyState[_avatar1Address]; Assert.Contains(nextAvatar1State.inventory.Materials, i => itemIds.Contains(i.Id)); Assert.NotNull(action.ArenaInfo); Assert.NotNull(action.EnemyArenaInfo); Assert.True(nextArenaInfo.Score > prevScore); // Check simulation result equal. var player = new Player( previousAvatar1State, _tableSheets.CharacterSheet, _tableSheets.CharacterLevelSheet, _tableSheets.EquipmentItemSetEffectSheet); var simulator = new RankingSimulator( new TestRandom(), player, action.EnemyPlayerDigest, new List <Guid>(), _tableSheets.GetRankingSimulatorSheets(), RankingBattle10.StageId, action.ArenaInfo, action.EnemyArenaInfo, _tableSheets.CostumeStatSheet); simulator.Simulate(); Assert.Equal(nextArenaInfo.Score, simulator.Log.score); Assert.Equal(previousAvatar1State.SerializeV2(), nextAvatar1State.SerializeV2()); Assert.Equal(previousAvatar1State.worldInformation.Serialize(), nextAvatar1State.worldInformation.Serialize()); }