public override IAccountStateDelta Execute(IActionContext context) { IActionContext ctx = context; var states = ctx.PreviousStates; if (ctx.Rehearsal) { states = states.SetState(RankingMapAddress, MarkChanged); states = states.SetState(avatarAddress, MarkChanged); states = states.SetState(WeeklyArenaAddress, MarkChanged); return(states.SetState(ctx.Signer, MarkChanged)); } CheckObsolete(BlockChain.Policy.BlockPolicySource.V100080ObsoleteIndex, context); var addressesHex = GetSignerAndOtherAddressesHex(context, avatarAddress); var sw = new Stopwatch(); sw.Start(); var started = DateTimeOffset.UtcNow; Log.Verbose("{AddressesHex}HAS exec started", addressesHex); if (!states.TryGetAvatarState(ctx.Signer, avatarAddress, out AvatarState avatarState)) { throw new FailedLoadStateException($"{addressesHex}Aborted as the avatar state of the signer was failed to load."); } sw.Stop(); Log.Verbose("{AddressesHex}HAS Get AgentAvatarStates: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); if (avatarState.RankingMapAddress != RankingMapAddress) { throw new InvalidAddressException($"{addressesHex}Invalid ranking map address"); } // worldId와 stageId가 유효한지 확인합니다. var worldSheet = states.GetSheet <WorldSheet>(); if (!worldSheet.TryGetValue(worldId, out var worldRow, false)) { throw new SheetRowNotFoundException(addressesHex, nameof(WorldSheet), worldId); } if (stageId < worldRow.StageBegin || stageId > worldRow.StageEnd) { throw new SheetRowColumnException( $"{addressesHex}{worldId} world is not contains {worldRow.Id} stage: " + $"{worldRow.StageBegin}-{worldRow.StageEnd}"); } var stageSheet = states.GetSheet <StageSheet>(); if (!stageSheet.TryGetValue(stageId, out var stageRow)) { throw new SheetRowNotFoundException(addressesHex, nameof(StageSheet), stageId); } var worldInformation = avatarState.worldInformation; if (!worldInformation.TryGetWorld(worldId, out var world)) { // NOTE: Add new World from WorldSheet worldInformation.AddAndUnlockNewWorld(worldRow, ctx.BlockIndex, worldSheet); } if (!world.IsUnlocked) { throw new InvalidWorldException($"{addressesHex}{worldId} is locked."); } if (world.StageBegin != worldRow.StageBegin || world.StageEnd != worldRow.StageEnd) { worldInformation.UpdateWorld(worldRow); } if (world.IsStageCleared && stageId > world.StageClearedId + 1 || !world.IsStageCleared && stageId != world.StageBegin) { throw new InvalidStageException( $"{addressesHex}Aborted as the stage ({worldId}/{stageId}) is not cleared; " + $"cleared stage: {world.StageClearedId}" ); } avatarState.ValidateEquipments(equipments, context.BlockIndex); avatarState.ValidateConsumable(foods, context.BlockIndex); avatarState.ValidateCostume(new HashSet <int>(costumes)); var costumeStatSheet = states.GetSheet <CostumeStatSheet>(); sw.Restart(); if (avatarState.actionPoint < stageRow.CostAP) { throw new NotEnoughActionPointException( $"{addressesHex}Aborted due to insufficient action point: " + $"{avatarState.actionPoint} < {stageRow.CostAP}" ); } avatarState.actionPoint -= stageRow.CostAP; avatarState.EquipCostumes(new HashSet <int>(costumes)); avatarState.EquipEquipments(equipments); sw.Stop(); Log.Verbose("{AddressesHex}HAS Unequip items: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); var characterSheet = states.GetSheet <CharacterSheet>(); var simulator = new StageSimulator( ctx.Random, avatarState, foods, worldId, stageId, states.GetStageSimulatorSheets(), costumeStatSheet ); sw.Stop(); Log.Verbose("{AddressesHex}HAS Initialize Simulator: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); simulator.Simulate2(); sw.Stop(); Log.Verbose("{AddressesHex}HAS Simulator.Simulate(): {Elapsed}", addressesHex, sw.Elapsed); Log.Verbose( "{AddressesHex}Execute HackAndSlash({AvatarAddress}); worldId: {WorldId}, stageId: {StageId}, result: {Result}, " + "clearWave: {ClearWave}, totalWave: {TotalWave}", addressesHex, avatarAddress, worldId, stageId, simulator.Log.result, simulator.Log.clearedWaveNumber, simulator.Log.waveCount ); sw.Restart(); if (simulator.Log.IsClear) { var worldUnlockSheet = states.GetSheet <WorldUnlockSheet>(); simulator.Player.worldInformation.ClearStage( worldId, stageId, ctx.BlockIndex, worldSheet, worldUnlockSheet ); } sw.Stop(); Log.Verbose("{AddressesHex}HAS ClearStage: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); avatarState.Update(simulator); var materialSheet = states.GetSheet <MaterialItemSheet>(); avatarState.UpdateQuestRewards2(materialSheet); //Avoid InvalidBlockStateRootHashException to 50000 index. if (avatarState.questList.Any(q => q.Complete && !q.IsPaidInAction)) { var prevIds = avatarState.questList.completedQuestIds; avatarState.UpdateQuestRewards(materialSheet); avatarState.questList.completedQuestIds = prevIds; } avatarState.updatedAt = ctx.BlockIndex; states = states.SetState(avatarAddress, avatarState.Serialize()); sw.Stop(); Log.Verbose("{AddressesHex}HAS Set AvatarState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); if (states.TryGetState(RankingMapAddress, out Dictionary d) && simulator.Log.IsClear) { var ranking = new RankingMapState(d); ranking.Update(avatarState); sw.Stop(); Log.Verbose("{AddressesHex}HAS Update RankingState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); var serialized = ranking.Serialize(); sw.Stop(); Log.Verbose("{AddressesHex}HAS Serialize RankingState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); states = states.SetState(RankingMapAddress, serialized); } sw.Stop(); Log.Verbose("{AddressesHex}HAS Set RankingState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); if (simulator.Log.stageId >= GameConfig.RequireClearedStageLevel.ActionsInRankingBoard && simulator.Log.IsClear && states.TryGetState(WeeklyArenaAddress, out Dictionary weeklyDict)) { var weekly = new WeeklyArenaState(weeklyDict); if (!weekly.Ended) { if (weekly.ContainsKey(avatarAddress)) { var info = weekly[avatarAddress]; info.UpdateV2(avatarState, characterSheet, costumeStatSheet); weekly.Update(info); } else { weekly.SetV2(avatarState, characterSheet, costumeStatSheet); } sw.Stop(); Log.Verbose("{AddressesHex}HAS Update WeeklyArenaState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); var weeklySerialized = weekly.Serialize(); sw.Stop(); Log.Verbose("{AddressesHex}HAS Serialize RankingState: {Elapsed}", addressesHex, sw.Elapsed); states = states.SetState(weekly.address, weeklySerialized); } } Result = simulator.Log; var ended = DateTimeOffset.UtcNow; Log.Verbose("{AddressesHex}HAS Total Executed Time: {Elapsed}", addressesHex, ended - started); return(states); }
public override IAccountStateDelta Execute(IActionContext context) { IActionContext ctx = context; var states = ctx.PreviousStates; if (ctx.Rehearsal) { states = states.SetState(RankingMapAddress, MarkChanged); states = states.SetState(avatarAddress, MarkChanged); return(states.SetState(WeeklyArenaAddress, MarkChanged)); } CheckObsolete(BlockChain.Policy.BlockPolicySource.V100080ObsoleteIndex, context); var addressesHex = GetSignerAndOtherAddressesHex(context, avatarAddress); var sw = new Stopwatch(); sw.Start(); var started = DateTimeOffset.UtcNow; Log.Verbose("{AddressesHex}Mimisbrunnr exec started", addressesHex); if (!states.TryGetAvatarState(ctx.Signer, avatarAddress, out AvatarState avatarState)) { throw new FailedLoadStateException($"{addressesHex}Aborted as the avatar state of the signer was failed to load."); } sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Get AgentAvatarStates: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); if (avatarState.RankingMapAddress != RankingMapAddress) { throw new InvalidAddressException($"{addressesHex}Invalid ranking map address"); } var worldSheet = states.GetSheet <WorldSheet>(); var worldUnlockSheet = states.GetSheet <WorldUnlockSheet>(); if (!worldSheet.TryGetValue(worldId, out var worldRow, false)) { throw new SheetRowNotFoundException(nameof(WorldSheet), worldId); } if (stageId < worldRow.StageBegin || stageId > worldRow.StageEnd) { throw new SheetRowColumnException( $"{addressesHex}{worldId} world is not contains {worldRow.Id} stage: " + $"{worldRow.StageBegin}-{worldRow.StageEnd}"); } var stageSheet = states.GetSheet <StageSheet>(); if (!stageSheet.TryGetValue(stageId, out var stageRow)) { throw new SheetRowNotFoundException(addressesHex, nameof(StageSheet), stageId); } var worldInformation = avatarState.worldInformation; if (!worldInformation.TryGetWorld(worldId, out var world)) { // NOTE: Add new World from WorldSheet worldInformation.AddAndUnlockMimisbrunnrWorld(worldRow, ctx.BlockIndex, worldSheet, worldUnlockSheet); if (!worldInformation.TryGetWorld(worldId, out world)) { // Do nothing. } } if (!world.IsUnlocked) { var worldUnlockSheetRow = worldUnlockSheet.OrderedList.FirstOrDefault(row => row.WorldIdToUnlock == worldId); if (!(worldUnlockSheetRow is null) && worldInformation.IsWorldUnlocked(worldUnlockSheetRow.WorldId) && worldInformation.IsStageCleared(worldUnlockSheetRow.StageId)) { worldInformation.UnlockWorld(worldId, ctx.BlockIndex, worldSheet); if (!worldInformation.TryGetWorld(worldId, out world)) { // Do nothing. } } } if (!world.IsUnlocked) { throw new InvalidWorldException($"{addressesHex}{worldId} is locked."); } if (world.StageBegin != worldRow.StageBegin || world.StageEnd != worldRow.StageEnd) { worldInformation.UpdateWorld(worldRow); } if (world.IsStageCleared && stageId > world.StageClearedId + 1 || !world.IsStageCleared && stageId != world.StageBegin) { throw new InvalidStageException( $"{addressesHex}Aborted as the stage ({worldId}/{stageId}) is not cleared; " + $"cleared stage: {world.StageClearedId}" ); } sw.Restart(); var mimisbrunnrSheet = states.GetSheet <MimisbrunnrSheet>(); if (!mimisbrunnrSheet.TryGetValue(stageId, out var mimisbrunnrSheetRow)) { throw new SheetRowNotFoundException(addressesHex, "MimisbrunnrSheet", stageId); } foreach (var equipmentId in equipments) { if (avatarState.inventory.TryGetNonFungibleItem(equipmentId, out ItemUsable itemUsable)) { var elementalType = ((Equipment)itemUsable).ElementalType; if (!mimisbrunnrSheetRow.ElementalTypes.Exists(x => x == elementalType)) { throw new InvalidElementalException( $"{addressesHex}ElementalType of {equipmentId} does not match."); } } } sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Check Equipments ElementalType: {Elapsed}", addressesHex, sw.Elapsed); avatarState.ValidateEquipments(equipments, context.BlockIndex); avatarState.ValidateConsumable(foods, context.BlockIndex); avatarState.ValidateCostume(costumes); sw.Restart(); if (avatarState.actionPoint < stageRow.CostAP) { throw new NotEnoughActionPointException( $"{addressesHex}Aborted due to insufficient action point: " + $"{avatarState.actionPoint} < {stageRow.CostAP}" ); } avatarState.actionPoint -= stageRow.CostAP; var equippableItem = new List <Guid>(); equippableItem.AddRange(costumes); equippableItem.AddRange(equipments); avatarState.EquipItems(equippableItem); sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Unequip items: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); var costumeStatSheet = states.GetSheet <CostumeStatSheet>(); var simulator = new StageSimulator( ctx.Random, avatarState, foods, worldId, stageId, states.GetStageSimulatorSheets(), costumeStatSheet ); sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Initialize Simulator: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); simulator.Simulate2(); sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Simulator.Simulate(): {Elapsed}", addressesHex, sw.Elapsed); Log.Verbose( "{AddressesHex}Execute Mimisbrunnr({AvatarAddress}); worldId: {WorldId}, stageId: {StageId}, result: {Result}, " + "clearWave: {ClearWave}, totalWave: {TotalWave}", addressesHex, avatarAddress, worldId, stageId, simulator.Log.result, simulator.Log.clearedWaveNumber, simulator.Log.waveCount ); sw.Restart(); if (simulator.Log.IsClear) { simulator.Player.worldInformation.ClearStage( worldId, stageId, ctx.BlockIndex, worldSheet, worldUnlockSheet ); } sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr ClearStage: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); avatarState.Update(simulator); var materialSheet = states.GetSheet <MaterialItemSheet>(); avatarState.UpdateQuestRewards2(materialSheet); avatarState.updatedAt = ctx.BlockIndex; avatarState.mailBox.CleanUp2(); states = states.SetState(avatarAddress, avatarState.Serialize()); sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Set AvatarState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); if (states.TryGetState(RankingMapAddress, out Dictionary d) && simulator.Log.IsClear) { var ranking = new RankingMapState(d); ranking.Update(avatarState); sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Update RankingState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); var serialized = ranking.Serialize(); sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Serialize RankingState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); states = states.SetState(RankingMapAddress, serialized); } sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Set RankingState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); if (simulator.Log.stageId >= GameConfig.RequireClearedStageLevel.ActionsInRankingBoard && simulator.Log.IsClear && states.TryGetState(WeeklyArenaAddress, out Dictionary weeklyDict)) { var weekly = new WeeklyArenaState(weeklyDict); if (!weekly.Ended) { var characterSheet = states.GetSheet <CharacterSheet>(); if (weekly.ContainsKey(avatarAddress)) { var info = weekly[avatarAddress]; info.UpdateV2(avatarState, characterSheet, costumeStatSheet); weekly.Update(info); } else { weekly.SetV2(avatarState, characterSheet, costumeStatSheet); } sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Update WeeklyArenaState: {Elapsed}", addressesHex, sw.Elapsed); sw.Restart(); var weeklySerialized = weekly.Serialize(); sw.Stop(); Log.Verbose("{AddressesHex}Mimisbrunnr Serialize RankingState: {Elapsed}", addressesHex, sw.Elapsed); states = states.SetState(weekly.address, weeklySerialized); } } Result = simulator.Log; var ended = DateTimeOffset.UtcNow; Log.Verbose("{AddressesHex}Mimisbrunnr Total Executed Time: {Elapsed}", addressesHex, ended - started); return(states); }