public ActionResult <UserStep> Create(UserStep userStep) { userStep.Id = _context.UserSteps.Any() ? _context.UserSteps.Max(p => p.Id) + 1 : 1; _context.UserSteps.Add(userStep); _context.SaveChanges(); return(CreatedAtAction(nameof(GetById), new { id = userStep.Id }, userStep)); }
public async Task GetCards(Guid gameId, string userId) { using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var game = await _unitOfWork.Games.GetById(gameId); var cardsForRemove = new List <Card>(); var userInGame = await _unitOfWork.UserInGames.GetByUserIdAndGameId(userId, gameId); var botInGames = await _unitOfWork.BotInGames.GetAllByGameId(game.Id); var deck = await _unitOfWork.Cards.GetAllByGameId(game.Id); var bots = botInGames.Select(x => x.Bot).ToList(); var stepUser = new UserStep(); var stepBots = new List <BotStep>(); DealCard(game, deck, cardsForRemove, userId, stepUser, userInGame, bots, stepBots, botInGames); await _unitOfWork.UserSteps.Create(stepUser); await _unitOfWork.UserInGames.Update(userInGame); await _unitOfWork.BotSteps.CreateRange(stepBots); await _unitOfWork.BotInGames.UpdateRange(botInGames); await _unitOfWork.Cards.DeleteRange(cardsForRemove); transactionScope.Complete(); } }
public static IStepBuilder <TData, UserStepBody> UserStep <TData>(this IStepOutcomeBuilder <TData> builder, string userPrompt, Expression <Func <TData, string> > assigner, Action <IStepBuilder <TData, UserStepBody> > stepSetup = null) { var newStep = new UserStep(); newStep.Principal = assigner; newStep.UserPrompt = userPrompt; builder.WorkflowBuilder.AddStep(newStep); var stepBuilder = new StepBuilder <TData, UserStepBody>(builder.WorkflowBuilder, newStep); if (stepSetup != null) { stepSetup.Invoke(stepBuilder); } newStep.Name = newStep.Name ?? typeof(UserStep).Name; builder.Outcome.NextStep = newStep.Id; return(stepBuilder); }
private void DealTwoCards(Game game, List <Card> deck, List <Card> cardsForRemove, string userId, List <UserStep> stepUsers, UserInGame userInGame, List <Bot> bots, List <BotStep> stepBots, List <BotInGame> botInGames) { var startCards = 2; for (int i = 0; i < startCards; i++) { var currentCard = deck.FirstOrDefault(); var stepUser = new UserStep { GameId = game.Id, UserId = userId, Suit = currentCard.Suit, Rank = currentCard.Rank }; stepUsers.Add(stepUser); userInGame.CountPoint += _scoreHelper.GetValueCard(currentCard.Rank, userInGame.CountPoint); cardsForRemove.Add(currentCard); deck.Remove(currentCard); bots.ForEach(x => { currentCard = deck.FirstOrDefault(); stepBots.Add(new BotStep { GameId = game.Id, BotId = x.Id, Suit = currentCard.Suit, Rank = currentCard.Rank }); var botInGame = botInGames.FirstOrDefault(b => b.BotId == x.Id); botInGame.CountPoint += _scoreHelper.GetValueCard(currentCard.Rank, botInGame.CountPoint); cardsForRemove.Add(currentCard); deck.Remove(currentCard); }); } }
private static bool LoadProcessSteps(UserProcess process, List <StepDTO> steps, Dictionary <string, Process> processesByName, List <string> errors) { var stepsById = new Dictionary <string, Step>(); var stepsWithInputs = new List <Tuple <StepDTO, Step, IReadOnlyList <Parameter> > >(); var stepsWithOutputs = new List <Tuple <StepDTO, ReturningStep, IReadOnlyList <Parameter> > >(); int initialErrorCount = errors.Count; foreach (var stepData in steps) { if (stepsById.ContainsKey(stepData.ID)) { errors.Add($"Process \"{process.Name}\" has multiple steps with ID {stepData.ID}"); continue; } switch (stepData.Type) { case "start": { var step = new StartStep(stepData.ID); if (process.FirstStep == null) { process.FirstStep = step; } else { errors.Add($"Process \"{process.Name}\" has multiple start steps"); } stepsWithOutputs.Add(new Tuple <StepDTO, ReturningStep, IReadOnlyList <Parameter> >(stepData, step, process.Inputs)); stepsById.Add(step.ID, step); break; } case "stop": { var step = new StopStep(stepData.ID, stepData.PathName); stepsWithInputs.Add(new Tuple <StepDTO, Step, IReadOnlyList <Parameter> >(stepData, step, process.Outputs)); stepsById.Add(step.ID, step); break; } case "process": { if (!processesByName.TryGetValue(stepData.InnerProcess, out Process innerProcess)) { errors.Add($"Unrecognised process \"{stepData.InnerProcess}\" on step {stepData.ID} in process \"{process.Name}\""); break; } var step = new UserStep(stepData.ID, innerProcess); stepsWithInputs.Add(new Tuple <StepDTO, Step, IReadOnlyList <Parameter> >(stepData, step, innerProcess.Inputs)); stepsWithOutputs.Add(new Tuple <StepDTO, ReturningStep, IReadOnlyList <Parameter> >(stepData, step, innerProcess.Outputs)); stepsById.Add(step.ID, step); break; } default: errors.Add($"Invalid type \"{stepData.Type}\" on step {stepData.ID} in process \"{process.Name}\""); break; } } var variablesByName = process.Variables.ToDictionary(v => v.Name); foreach (var stepInfo in stepsWithInputs) { var stepData = stepInfo.Item1; var step = stepInfo.Item2; var parameters = stepInfo.Item3; if (stepData.Inputs != null) { MapParameters(stepData.Inputs, step, parameters, variablesByName, process, true, errors); } } foreach (var stepInfo in stepsWithOutputs) { var stepData = stepInfo.Item1; var step = stepInfo.Item2; var parameters = stepInfo.Item3; if (stepData.Outputs != null) { MapParameters(stepData.Outputs, step, parameters, variablesByName, process, false, errors); } if (stepData.ReturnPath != null) { if (stepsById.TryGetValue(stepData.ReturnPath, out Step destStep)) { step.DefaultReturnPath = destStep; } else { errors.Add($"Step {step.ID} in process \"{process.Name}\" tries to connect to non-existent step \"{stepData.ReturnPath}\""); } } else if (stepData.ReturnPaths != null && step is UserStep userStep) { var expectedReturnPaths = userStep.ChildProcess.ReturnPaths; var mappedPaths = new HashSet <string>(); foreach (var returnPath in stepData.ReturnPaths) { if (!expectedReturnPaths.Contains(returnPath.Key)) { errors.Add($"Step {step.ID} in process \"{process.Name}\" tries to map unexpected return path \"{returnPath.Key}\""); } else if (stepsById.TryGetValue(returnPath.Value, out Step destStep)) { if (mappedPaths.Contains(returnPath.Key)) { errors.Add($"Step {step.ID} in process \"{process.Name}\" tries to map the \"{returnPath.Key}\" return path multiple times"); continue; } userStep.ReturnPaths[returnPath.Key] = destStep; mappedPaths.Add(returnPath.Key); } else { errors.Add($"Step {step.ID} tries to connect to non-existent step \"{returnPath.Value}\" in process \"{process.Name}\""); } } foreach (var path in expectedReturnPaths) { if (!mappedPaths.Contains(path)) { errors.Add($"Step {step.ID} in process \"{process.Name}\" fails to map the \"{path}\" return path"); } } } } return(errors.Count == initialErrorCount); }
private void DealCard(Game game, List <Card> deck, List <Card> cardsForRemove, string userId, UserStep stepUser, UserInGame userInGame, List <Bot> bots, List <BotStep> stepBots, List <BotInGame> botInGames) { var currentCard = deck.FirstOrDefault(); stepUser.GameId = game.Id; stepUser.UserId = userId; stepUser.Suit = currentCard.Suit; stepUser.Rank = currentCard.Rank; userInGame.CountPoint += _scoreHelper.GetValueCard(currentCard.Rank, userInGame.CountPoint); cardsForRemove.Add(currentCard); deck.Remove(currentCard); bots.ForEach(x => { currentCard = deck.FirstOrDefault(); var pointBot = botInGames.FirstOrDefault(p => p.BotId == x.Id); if (IsNeedCard(pointBot)) { var stepBot = new BotStep { BotId = x.Id, GameId = game.Id, Rank = currentCard.Rank, Suit = currentCard.Suit }; stepBots.Add(stepBot); pointBot.CountPoint += _scoreHelper.GetValueCard(currentCard.Rank, pointBot.CountPoint); cardsForRemove.Add(currentCard); deck.Remove(currentCard); } }); }