public async Task <Unit> Handle(NpcScriptCommand command, CancellationToken cancellationToken) { var commandId = command.CommandId; var input = command.Message; var scriptId = command.ScriptId; var playerId = command.PlayerId; var player = await _playerDomainService.Get(playerId); if (player == null) { return(Unit.Value); } var npcId = command.NpcId; var npc = await _npcDomainService.Get(npcId); if (npc == null) { return(Unit.Value); } await DoScript(player, npc, scriptId, commandId, input); return(Unit.Value); }
public async Task NpcAction(NpcCommandAction commandAction) { var result = await DoCommand(async() => { var playerId = _account.PlayerId; var npcId = commandAction.NpcId; var commandId = commandAction.Action.CommandId; var commandName = commandAction.Action.Name; var input = commandAction.Action.Message; var scriptId = commandAction.Action.ScriptId; if (scriptId > 0) { var command = new NpcScriptCommand(playerId, commandAction.NpcId, commandAction.Action.ScriptId, commandAction.Action.CommandId, commandAction.Action.Message); await _bus.SendCommand(command); } else { NpcActionEnum actionEnum; if (Enum.TryParse(commandName, out actionEnum)) { switch (actionEnum) { case NpcActionEnum.切磋: await _bus.SendCommand(new FightWithNpcCommand(playerId, commandAction.NpcId)); break; case NpcActionEnum.杀死: await _bus.SendCommand(new KillNpcCommand(playerId, commandAction.NpcId)); break; case NpcActionEnum.给予: await _bus.SendCommand(new GiveToNpcCommand(playerId, commandAction.NpcId)); break; } } } }); }
public async Task <Unit> Handle(NpcScriptCommand command, CancellationToken cancellationToken) { var commandId = command.CommandId; var input = command.Message; var scriptId = command.ScriptId; var playerId = command.PlayerId; var player = await _playerDomainService.Get(playerId); if (player == null) { return(Unit.Value); } var npcId = command.NpcId; var npc = await _npcDomainService.Get(npcId); if (npc == null) { return(Unit.Value); } await DoScript(player, npc, scriptId, commandId, input); /* * * var npcScripts = (await _npcScriptDomainService.GetAll()).Where(x => x.NpcId == npc.Id); * var scriptIds = npcScripts.Select(x => x.ScriptId).ToList(); * if (!scriptIds.Contains(scriptId)) * { * //await _bus.RaiseEvent(new DomainNotification($"脚本不存在!")); * return Unit.Value; * } * * var script = await _scriptDomainService.Get(scriptId); * if (script == null) * { * //await _bus.RaiseEvent(new DomainNotification($"脚本不存在!")); * return Unit.Value; * } * ScriptCommandEntity scriptCommand; * var scriptCommands = await _scriptCommandDomainService.Query(x => x.ScriptId == scriptId); * if (commandId == 0) * { * scriptCommand = scriptCommands.FirstOrDefault(x => x.IsEntry); * } * else * { * scriptCommand = scriptCommands.FirstOrDefault(x => x.Id == commandId); * } * * if (scriptCommand == null) * { * return Unit.Value; * } * * if (!scriptCommand.IsEntry) * { * var key = string.Format(RedisKey.CommandIds, player.Id, npc.Id, scriptId); * var commandIds = await _redisDb.StringGet<List<int>>(key); * if (commandIds == null || !commandIds.Contains(scriptCommand.Id)) * { * return Unit.Value; * } * } * * var checkIf = true;//判断if条件是否为true * * var caseIfStr = scriptCommand.CaseIf; * * * if (!string.IsNullOrEmpty(caseIfStr)) * { * List<CaseIf> caseIfs = new List<CaseIf>(); * try * { * caseIfs = JsonConvert.DeserializeObject<List<CaseIf>>(caseIfStr); * } * catch (Exception ex) * { * _logger.LogError($"Convert CaseIf:{ex}"); * } * * foreach (var caseIf in caseIfs) * { * checkIf = await CheckIf(player, caseIf.Condition, caseIf.Attrs, input); * if (!checkIf) * { * break; * } * } * } * * * if (checkIf) * { * //执行then分支 * var caseThenStr = scriptCommand.CaseThen; * * * if (!string.IsNullOrEmpty(caseThenStr)) * { * List<CaseThen> caseThens = new List<CaseThen>(); * try * { * caseThens = JsonConvert.DeserializeObject<List<CaseThen>>(caseThenStr); * } * catch (Exception ex) * { * _logger.LogError($"Convert CaseThen:{ex}"); * } * * * foreach (var caseThen in caseThens) * { * await DoCommand(player, npc, scriptId, caseThen.Command, caseThen.Attrs, input); * } * } * } * else * { * //执行else分支 * var caseElseStr = scriptCommand.CaseElse; * if (!string.IsNullOrEmpty(caseElseStr)) * { * List<CaseElse> caseElses = new List<CaseElse>(); * try * { * caseElses = JsonConvert.DeserializeObject<List<CaseElse>>(caseElseStr); * } * catch (Exception ex) * { * _logger.LogError($"Convert CaseElse:{ex}"); * } * * foreach (var caseElse in caseElses) * { * await DoCommand(player, npc, scriptId, caseElse.Command, caseElse.Attrs, input); * } * } * } * */ return(Unit.Value); }