Esempio n. 1
0
            public async Task <Unit> Handle(EnqueueCommand request, CancellationToken cancellationToken)
            {
                var roster = await _gameContext.Rosters.GetOneAsync(x => x.UserId == request.UserId);

                var characters = await _gameContext.Characters.GetAsync(x => x.RosterId == roster.Id && !x.Deleted);

                if (characters.Count() != 3)
                {
                    throw new CannotPerformOperationException("Wrong amount of characters");
                }

                if (_battleService.IsUserInBattle(request.UserId))
                {
                    throw new CannotPerformOperationException("User is in battle");
                }

                var result = _queueService.Enqueue(new UserToEnqueueDto()
                {
                    UserId = request.UserId,
                    Mode   = request.Mode
                });

                if (!result)
                {
                    throw new CannotPerformOperationException("User is already in queue");
                }

                return(Unit.Value);
            }
Esempio n. 2
0
            public async Task <ActiveUserDto> Handle(GetActiveUserQuery request, CancellationToken cancellationToken)
            {
                var user = await _userManager.GetUserAsync(request.User);

                var statusInBattle = _battleService.IsUserInBattle(user.Id);
                var roster         = await _gameContext.Rosters.GetOneAsync(x => x.UserId == user.Id);

                IEnumerable <Character> characters;

                if (roster == null)
                {
                    roster = new Roster()
                    {
                        Id             = user.Id,
                        UserId         = user.Id,
                        Experience     = 0,
                        Seed           = user.Id.GetHashCode(),
                        TavernCapacity = 6,
                        BoughtPatrons  = new int[0]
                    };
                    _gameContext.Rosters.InsertOne(roster);
                    var userCharacter = new Character()
                    {
                        Id             = Guid.NewGuid().ToString(),
                        Deleted        = false,
                        IsKeyCharacter = true,
                        RosterId       = roster.Id,
                        Name           = user.ViewName,
                        ChosenTalents  = new int[0]
                    };
                    _gameContext.Characters.InsertOne(userCharacter);
                    characters = new Character[] { userCharacter };
                    await _gameContext.ApplyChangesAsync();
                }
                else
                {
                    characters = await _gameContext.Characters.GetAsync(x => x.RosterId == roster.Id && !x.Deleted);
                }

                var tavern = new List <CharacterForSaleDto>();

                for (int i = 1; i <= roster.TavernCapacity; i++)
                {
                    if (!roster.BoughtPatrons.Any(x => x == i))
                    {
                        tavern.Add(new CharacterForSaleDto()
                        {
                            Id = i
                        });
                    }
                }

                var talents = await _registryContext.TalentMap.GetAsync(x => true);

                return(new ActiveUserDto()
                {
                    Name = user.ViewName,
                    UniqueId = user.UserName,
                    Id = user.Id,
                    Email = user.Email,
                    State = statusInBattle ? UserState.Battle : UserState.Lobby,
                    Experience = roster.Experience,
                    Tavern = tavern,
                    Roster = characters.Select(x =>
                    {
                        return new CharacterDto()
                        {
                            Id = x.Id,
                            Name = x.Name,
                            IsKeyCharacter = x.IsKeyCharacter,
                            Talents = x.ChosenTalents.Select(k =>
                            {
                                var coordinates = TalentPositionHelper.GetCoordinatesFromPosition(k);
                                return new PointDto()
                                {
                                    X = coordinates.x,
                                    Y = coordinates.y
                                };
                            })
                        };
                    }),
                    TalentsMap = talents.Select(x =>
                    {
                        var position = TalentPositionHelper.GetCoordinatesFromPosition(x.Position);
                        return new TalentNodeDto()
                        {
                            X = position.x,
                            Y = position.y,
                            Name = x.Name,
                            Description = x.UniqueDescription,
                            Id = x.Id,
                            Speed = x.SpeedModifier,
                            Strength = x.StrengthModifier,
                            Willpower = x.WillpowerModifier,
                            Constitution = x.ConstitutionModifier,
                            Class = x.Class,
                            ClassPoints = x.ClassPoints,
                            Prerequisites = x.Prerequisites,
                            Exceptions = x.Exceptions,
                            SkillsAmount = x.SkillsAmount
                        };
                    })
                });
            }
Esempio n. 3
0
            public async Task <NewCharacterDto> Handle(HirePatronCommand request, CancellationToken cancellationToken)
            {
                if (_queueService.IsUserInQueue(request.UserId) != null || _battleService.IsUserInBattle(request.UserId))
                {
                    throw new CannotPerformOperationException("User cannot do that in queue or battle.");
                }

                var roster = await _gameContext.Rosters.GetOneAsync(x => x.UserId == request.UserId);

                var characters = await _gameContext.Characters.GetAsync(x => x.RosterId == roster.Id);

                if (characters.Count() < 3 && request.CharacterForReplace != null)
                {
                    throw new CannotPerformOperationException("Cannot fire character while roster is not full.");
                }

                if (characters.Count() >= 3 && request.CharacterForReplace == null)
                {
                    throw new CannotPerformOperationException("Need character to fire while roster is not full.");
                }

                if (request.CharacterForReplace != null && !characters.Any(x => x.Id == request.CharacterForReplace))
                {
                    throw new CannotPerformOperationException("Firing character is not yours.");
                }

                if (roster.BoughtPatrons.Contains(request.PatronId) || request.PatronId > roster.TavernCapacity || request.PatronId < 1)
                {
                    throw new CannotPerformOperationException("Patron has already been hired.");
                }

                var characterForReplace = characters.FirstOrDefault(x => x.Id == request.CharacterForReplace);

                if (request.CharacterForReplace != null && characterForReplace.IsKeyCharacter)
                {
                    throw new CannotPerformOperationException("Cannot fire key character");
                }

                _gameContext.Rosters.Update(
                    x => x.UserId == roster.UserId,
                    Builders <Roster> .Update.Set(x => x.BoughtPatrons, roster.BoughtPatrons.Append(request.PatronId)));
                var character = new Character()
                {
                    Id             = characterForReplace != null ? characterForReplace.Id : Guid.NewGuid().ToString(),
                    Deleted        = false,
                    IsKeyCharacter = false,
                    RosterId       = roster.Id,
                    Name           = request.Name.Trim(),
                    ChosenTalents  = new int[0]
                };

                if (request.CharacterForReplace != null)
                {
                    _gameContext.Characters.ReplaceOne(x => x.Id == request.CharacterForReplace, character, true);
                }
                else
                {
                    _gameContext.Characters.InsertOne(character);
                }

                await _gameContext.ApplyChangesAsync(cancellationToken);

                return(new NewCharacterDto()
                {
                    Id = character.Id
                });
            }