Beispiel #1
0
 public static void HandleDetatchedActor(this SUGARContext context, Actor actor)
 {
     if (actor != null && context.Entry(actor).State == EntityState.Detached)
     {
         context.Actors.Attach(actor);
     }
 }
Beispiel #2
0
 public static void HandleDetatchedEvaluationData(this SUGARContext context, EvaluationData evaluationData)
 {
     if (evaluationData != null && context.Entry(evaluationData).State == EntityState.Detached)
     {
         context.EvaluationData.Attach(evaluationData);
     }
 }
        public User Create(User user, SUGARContext context = null)
        {
            var didCreate = false;

            if (context == null)
            {
                context   = ContextFactory.Create();
                didCreate = true;
            }

            if (context.Users.Any(g => g.Name == user.Name))
            {
                throw new DuplicateRecordException($"A user with the name: \"{user.Name}\" already exists.");
            }

            context.Users.Add(user);

            if (didCreate)
            {
                context.SaveChanges();
                context.Dispose();
            }

            return(user);
        }
        private static Skill CreateSkill(SUGARContext context, int gameId, string key)
        {
            var skill = context.Skills.Add(new Skill
            {
                GameId              = gameId,
                ActorType           = ActorType.User,
                Description         = key,
                EvaluationCriterias = new List <EvaluationCriteria>
                {
                    new EvaluationCriteria
                    {
                        ComparisonType         = ComparisonType.GreaterOrEqual,
                        CriteriaQueryType      = CriteriaQueryType.Sum,
                        EvaluationDataCategory = EvaluationDataCategory.GameData,
                        EvaluationDataType     = EvaluationDataType.Long,
                        EvaluationDataKey      = key,
                        Scope = CriteriaScope.Actor,
                        Value = $"{100}"
                    }
                },
                Name  = key,
                Token = key
            }).Entity;

            context.SaveChanges();
            return(skill);
        }
        public ActorRole Create(ActorRole newRole, SUGARContext context = null)
        {
            newRole = _actorRoleDbController.Create(newRole, context);

            _logger.LogInformation($"{newRole?.Id}");

            return(newRole);
        }
Beispiel #6
0
 public IQueryable <EvaluationData> Query(SUGARContext context, int gameId, int actorId, string key, EvaluationDataType evaluationDataType, DateTime?start = null, DateTime?end = null)
 {
     return(context.GetCategoryData(_category)
            .FilterByGameId(gameId)
            .FilterByActorId(actorId)
            .FilterByKey(key)
            .FilterByDataType(evaluationDataType)
            .FilterByDateTimeRange(start, end));
 }
Beispiel #7
0
        public static void HandleDetatchedActor(this SUGARContext context, int actorId)
        {
            var actor = context.Actors.FirstOrDefault(a => a.Id == actorId);

            if (actor != null && context.Entry(actor).State == EntityState.Detached)
            {
                context.Actors.Attach(actor);
            }
        }
Beispiel #8
0
        public Game Create(Game newGame, int creatorId, SUGARContext context = null)
        {
            newGame = _gameDbController.Create(newGame, context);
            _actorRoleController.Create(ClaimScope.Game, creatorId, newGame.Id, context);

            _logger.LogInformation($"Created: Game: {newGame.Id}, for CreatorId: {creatorId}");

            return(newGame);
        }
Beispiel #9
0
        /// <summary>
        /// Immediately creates a new relationship between 2 actors
        /// </summary>
        /// <param name="newRelation">Relationship to create</param>
        /// <param name="context">Optional DbContext to perform opperations on. If ommitted a DbContext will be created.</param>
        public void CreateRelationship(ActorRelationship newRelation, SUGARContext context = null)
        {
            var didCreateContext = false;

            if (context == null)
            {
                context          = ContextFactory.Create();
                didCreateContext = true;
            }

            if (newRelation.AcceptorId == newRelation.RequestorId)
            {
                throw new InvalidRelationshipException("Two different users are needed to create a relationship.");
            }

            var hasConflicts = context.Relationships
                               .Any(r => AreRelated(r, newRelation));

            if (!hasConflicts)
            {
                hasConflicts = context.RelationshipRequests
                               .Any(r => AreRelated(r, newRelation));
            }

            if (hasConflicts)
            {
                throw new DuplicateRelationshipException("A relationship with these users already exists.");
            }

            var requestorExists = context.Actors.Any(u => u.Id == newRelation.RequestorId);

            if (!requestorExists)
            {
                throw new InvalidRelationshipException("The requesting user does not exist.");
            }

            var acceptorExists = context.Actors.Any(u => u.Id == newRelation.AcceptorId);

            if (!acceptorExists)
            {
                throw new InvalidRelationshipException("The targeted user does not exist.");
            }

            var relation = new ActorRelationship
            {
                RequestorId = newRelation.RequestorId,
                AcceptorId  = newRelation.AcceptorId
            };

            context.Relationships.Add(relation);

            if (didCreateContext)
            {
                context.SaveChanges();
                context.Dispose();
            }
        }
        private static User CreateUser(SUGARContext context, string name)
        {
            var user = context.Users.Add(new User
            {
                Name = name
            }).Entity;

            context.SaveChanges();
            return(user);
        }
        private static Game CreateGame(SUGARContext context, string name)
        {
            var game = context.Games.Add(new Game
            {
                Name = name
            }).Entity;

            context.SaveChanges();
            return(game);
        }
        private void CreateMembership(int requestor, int acceptor, SUGARContext context)
        {
            var relationship = new ActorRelationship
            {
                RequestorId = requestor,
                AcceptorId  = acceptor,
            };

            _relationshipController.CreateRequest(relationship, true, context);
        }
        public User Create(User newUser, SUGARContext context = null)
        {
            newUser = _userController.Create(newUser, context);

            _actoRoleController.Create(ClaimScope.User, newUser.Id, newUser.Id, context);

            _logger.LogInformation($"{newUser.Id}");

            return newUser;
        }
        private User CreateUser(string name, SUGARContext context = null)
        {
            var user = new User
            {
                Name = $"User_{name}",
            };

            _userController.Create(user, context);

            return(user);
        }
Beispiel #15
0
 public static void HandleDetatchedGame(this SUGARContext context, int gameId)
 {
     if (gameId != 0)
     {
         var game = context.Games.FirstOrDefault(a => a.Id == gameId);
         if (game != null && context.Entry(game).State == EntityState.Detached)
         {
             context.Games.Attach(game);
         }
     }
 }
        private Game CreateGame(string name, SUGARContext context)
        {
            var game = new Game
            {
                Name = $"Game_{name}",
            };

            //todo use actual user id instead of 0
            _gameController.Create(game, 1, context);

            return(game);
        }
        private static ActorRole CreateActorRole(SUGARContext context, Role role, Actor actor, int entityId)
        {
            var actorRole = context.ActorRoles.Add(new ActorRole
            {
                Role     = role,
                Actor    = actor,
                EntityId = entityId
            }).Entity;

            context.SaveChanges();
            return(actorRole);
        }
        private static AccountSource CreateAccountSource(SUGARContext context, string description, string token, bool requiresPass)
        {
            var accountSource = context.AccountSources.Add(new AccountSource
            {
                Description      = description,
                Token            = token,
                RequiresPassword = requiresPass
            }).Entity;

            context.SaveChanges();
            return(accountSource);
        }
        private static Role CreateRole(SUGARContext context, ClaimScope claimScope, bool defaultRole = true)
        {
            var role = context.Roles.Add(new Role
            {
                Name       = claimScope.ToString(),
                ClaimScope = claimScope,
                Default    = defaultRole
            }).Entity;

            context.SaveChanges();
            return(role);
        }
        public void Create(ClaimScope claimScope, int actorId, int entityId, SUGARContext context = null)
        {
            var role = _roleController.GetDefault(claimScope, context);

            _logger.LogInformation($"ClaimScope: {claimScope}, ActorId: {actorId}, EntityId: {entityId}");

            if (role != null)
            {
                Create(new ActorRole {
                    ActorId = actorId, RoleId = role.Id, EntityId = entityId
                }, context);
            }
        }
        private static Account CreateAccount(SUGARContext context, string name, string password, AccountSource source, User user)
        {
            var account = context.Accounts.Add(new Account
            {
                Name          = name,
                Password      = password,
                AccountSource = source,
                User          = user
            }).Entity;

            context.SaveChanges();
            return(account);
        }
        public static void EnsureSeeded(this SUGARContext context)
        {
            var roles = new Dictionary <ClaimScope, Role>();

            foreach (var claimScope in (ClaimScope[])Enum.GetValues(typeof(ClaimScope)))
            {
                var addedClaimScope = context.Roles.FirstOrDefault(r => r.Name == claimScope.ToString()) ?? CreateRole(context, claimScope);
                roles.Add(claimScope, addedClaimScope);
            }

            var adminUser = context.Users.FirstOrDefault(u => u.Name == "admin") ?? CreateUser(context, "admin");

            var adminAccountSource = context.AccountSources.FirstOrDefault(a => a.Token == "SUGAR") ?? CreateAccountSource(context, "SUGAR", "SUGAR", true);

            var adminAccount = context.Accounts.FirstOrDefault(a => a.Name == "admin") ?? CreateAccount(context, "admin", "$2a$12$SSIgQE0cQejeH0dM61JV/eScAiHwJo/I3Gg6xZFUc0gmwh0FnMFv.", adminAccountSource, adminUser);

            #region Actor Roles
            if (!context.ActorRoles.Any())
            {
                //global (admin)
                CreateActorRole(context, roles[ClaimScope.Global], adminUser, Platform.AllId);

                //global game control
                CreateActorRole(context, roles[ClaimScope.Game], adminUser, Platform.AllId);

                //global group control
                CreateActorRole(context, roles[ClaimScope.Group], adminUser, Platform.AllId);

                // admin user
                CreateActorRole(context, roles[ClaimScope.User], adminUser, adminUser.Id);

                //global user control
                CreateActorRole(context, roles[ClaimScope.User], adminUser, Platform.AllId);

                // admin account
                CreateActorRole(context, roles[ClaimScope.Account], adminUser, adminAccount.Id);

                //global account control
                CreateActorRole(context, roles[ClaimScope.Account], adminUser, Platform.AllId);

                //global role control
                CreateActorRole(context, roles[ClaimScope.Role], adminUser, Platform.AllId);
            }
            #endregion
        }
        private static Leaderboard CreateLeaderboard(SUGARContext context, int gameId, string key, ActorType type = ActorType.User)
        {
            var leaderboard = context.Leaderboards.Add(new Leaderboard
            {
                Token                  = key,
                GameId                 = gameId,
                Name                   = key,
                EvaluationDataKey      = key,
                EvaluationDataCategory = EvaluationDataCategory.GameData,
                ActorType              = type,
                EvaluationDataType     = EvaluationDataType.Long,
                CriteriaScope          = CriteriaScope.Actor,
                LeaderboardType        = LeaderboardType.Highest
            }).Entity;

            context.SaveChanges();
            return(leaderboard);
        }
        public Role GetDefault(ClaimScope scope, SUGARContext context = null)
        {
            var didCreate = false;

            if (context == null)
            {
                context   = ContextFactory.Create();
                didCreate = true;
            }

            var role = context.Roles.FirstOrDefault(r => r.ClaimScope == scope && r.Default);

            if (didCreate)
            {
                context.Dispose();
            }

            return(role);
        }
Beispiel #25
0
        public Game Create(Game game, SUGARContext context = null)
        {
            var didCreateContext = false;

            if (context == null)
            {
                context          = ContextFactory.Create();
                didCreateContext = true;
            }

            context.Games.Add(game);

            if (didCreateContext)
            {
                context.SaveChanges();
                context.Dispose();
            }

            return(game);
        }
Beispiel #26
0
        public ActorRole Create(ActorRole actorRole, SUGARContext context = null)
        {
            var didCreate = false;

            if (context == null)
            {
                context   = ContextFactory.Create();
                didCreate = true;
            }

            context.ActorRoles.Add(actorRole);

            if (didCreate)
            {
                context.SaveChanges();
                context.Dispose();
            }

            return(actorRole);
        }
Beispiel #27
0
        public Group Create(Group group, SUGARContext context = null)
        {
            var didCreateContext = false;

            if (context == null)
            {
                context          = ContextFactory.Create();
                didCreateContext = true;
            }

            context.Groups.Add(group);

            if (didCreateContext)
            {
                context.SaveChanges();
                context.Dispose();
            }

            return(group);
        }
Beispiel #28
0
 public static IQueryable <EvaluationData> GetCategoryData(this SUGARContext context, EvaluationDataCategory category)
 {
     return(context.EvaluationData.Where(data => data.Category == category));
 }
        /// <summary>
        /// Create a new relationship between actores
        /// </summary>
        /// <param name="newRelationship"></param>
        /// <param name="autoAccept">If the relationship is accepted immediately</param>
        /// <param name="context">Optional DbContext to perform opperations on. If ommitted a DbContext will be created.</param>
        public void CreateRequest(ActorRelationship newRelationship, bool autoAccept, SUGARContext context = null)
        {
            // HACK auto accept default to false when using SUGAR Unity 1.0.2 or prior, not the expected behaviour
            // autoAccept = true;
            // END HACK
            if (autoAccept)
            {
                _relationshipDbController.CreateRelationship(newRelationship, context);
                // Assign users claims to the group that they do not get by default
                AssignUserResourceClaims(newRelationship);
            }
            else
            {
                _relationshipDbController.CreateRelationshipRequest(newRelationship, context);
            }

            _logger.LogInformation($"{newRelationship?.RequestorId} -> {newRelationship?.AcceptorId}, Auto Accept: {autoAccept}");
        }
        public static void EnsureTestsSeeded(this SUGARContext context)
        {
            if (!context.Games.Any())
            {
                CreateGame(context, "Global");

                #region Game Client Tests
                CreateGame(context, "Game_CanGetGamesByName 1");
                CreateGame(context, "Game_CanGetGamesByName 2");
                CreateGame(context, "Game_CanGetGameById");
                #endregion

                #region GameData Client Tests
                CreateGame(context, "GameData_CanCreate");
                CreateGame(context, "GameData_CannotCreateWithoutActorId");
                CreateGame(context, "GameData_CannotCreateWithoutKey");
                CreateGame(context, "GameData_CannotCreateWithoutValue");
                CreateGame(context, "GameData_CannotCreateWithoutEvaluationDataType");
                CreateGame(context, "GameData_CannotCreateWithMismatchedData");
                CreateGame(context, "GameData_CanGetGameData");
                CreateGame(context, "GameData_CannotGetGameDataWithoutActorId");
                CreateGame(context, "GameData_CanGetGameDataWithoutGameId");
                CreateGame(context, "GameData_CanGetGameDataByMultipleKeys");
                CreateGame(context, "GameData_CanGetGameDataByLeaderboardType");
                #endregion

                #region Resource Client Tests
                CreateGame(context, "Resource_CanCreate");
                CreateGame(context, "Resource_CannotCreateWithoutActorId");
                CreateGame(context, "Resource_CanUpdateExisting");
                CreateGame(context, "Resource_CanGetResource");
                CreateGame(context, "Resource_CannotGetResourceWithoutActorId");
                CreateGame(context, "Resource_CanGetResourceByMultipleKeys");
                CreateGame(context, "Resource_CannotCreateWithoutQuantity");
                CreateGame(context, "Resource_CannotCreateWithoutKey");
                #endregion
            }

            #region Achievement Client Tests
            if (!context.Achievements.Any())
            {
                CreateAchievement(context, CreateGame(context, "Achievement_CanDisableNotifications").Id, "Achievement_CanDisableNotifications");
                CreateAchievement(context, CreateGame(context, "Achievement_CanGetNotifications").Id, "Achievement_CanGetNotifications");
                CreateAchievement(context, CreateGame(context, "Achievement_DontGetAlreadyRecievedNotifications").Id, "Achievement_DontGetAlreadyRecievedNotifications");
                CreateAchievement(context, 0, "Achievement_CanGetGlobalAchievementProgress");
                CreateAchievement(context, CreateGame(context, "Achievement_CanGetAchievementProgress").Id, "Achievement_CanGetAchievementProgress");
                CreateGame(context, "Achievement_CannotGetNotExistingAchievementProgress");
            }
            #endregion

            #region Leaderboard Client Tests
            if (!context.Leaderboards.Any())
            {
                var leaderboardCanGetLeaderboardsByGame = CreateGame(context, "Leaderboard_CanGetLeaderboardsByGame");
                CreateLeaderboard(context, leaderboardCanGetLeaderboardsByGame.Id, leaderboardCanGetLeaderboardsByGame.Name + "1");
                CreateLeaderboard(context, leaderboardCanGetLeaderboardsByGame.Id, leaderboardCanGetLeaderboardsByGame.Name + "2");
                CreateLeaderboard(context, leaderboardCanGetLeaderboardsByGame.Id, leaderboardCanGetLeaderboardsByGame.Name + "3");
                CreateGame(context, "Leaderboard_CannotGetLeaderboardWithEmptyToken");
                CreateLeaderboard(context, 0, "Leaderboard_CanGetGlobalLeaderboardStandings");
                var leaderboardCanGetLeaderboardStandings = CreateGame(context, "Leaderboard_CanGetLeaderboardStandings");
                CreateLeaderboard(context, leaderboardCanGetLeaderboardStandings.Id, leaderboardCanGetLeaderboardStandings.Name);
                CreateGame(context, "Leaderboard_CannotGetNotExistingLeaderboardStandings");
                var leaderboardCannotGetLeaderboardStandingsWithoutToken = CreateGame(context, "Leaderboard_CannotGetStandingsWithoutToken");
                CreateLeaderboard(context, leaderboardCannotGetLeaderboardStandingsWithoutToken.Id, leaderboardCannotGetLeaderboardStandingsWithoutToken.Name);
                var leaderboardCannotGetLeaderboardStandingsWithoutGameId = CreateGame(context, "Leaderboard_CannotGetStandingsWithoutGameId");
                CreateLeaderboard(context, leaderboardCannotGetLeaderboardStandingsWithoutGameId.Id, leaderboardCannotGetLeaderboardStandingsWithoutGameId.Name);
                var leaderboardCannotGetLeaderboardStandingsWithoutFilterType = CreateGame(context, "Leaderboard_CannotGetStandingsWithoutFilterType");
                CreateLeaderboard(context, leaderboardCannotGetLeaderboardStandingsWithoutFilterType.Id, leaderboardCannotGetLeaderboardStandingsWithoutFilterType.Name);
                var leaderboardCannotGetLeaderboardStandingsWithoutLimit = CreateGame(context, "Leaderboard_CannotGetStandingsWithoutLimit");
                CreateLeaderboard(context, leaderboardCannotGetLeaderboardStandingsWithoutLimit.Id, leaderboardCannotGetLeaderboardStandingsWithoutLimit.Name);
                var leaderboardCannotGetLeaderboardStandingsWithoutOffset = CreateGame(context, "Leaderboard_CannotGetStandingsWithoutOffset");
                CreateLeaderboard(context, leaderboardCannotGetLeaderboardStandingsWithoutOffset.Id, leaderboardCannotGetLeaderboardStandingsWithoutOffset.Name);
                var leaderboardCanGetMultipleLeaderboardStandingsForActor = CreateGame(context, "Leaderboard_CanGetMultipleLeaderboardStandingsForActor");
                CreateLeaderboard(context, leaderboardCanGetMultipleLeaderboardStandingsForActor.Id, leaderboardCanGetMultipleLeaderboardStandingsForActor.Name);
                var leaderboardCannotGetLeaderboardStandingsWithIncorrectActorType = CreateGame(context, "Leaderboard_CannotGetStandingsWithIncorrectActorType");
                CreateLeaderboard(context, leaderboardCannotGetLeaderboardStandingsWithIncorrectActorType.Id, leaderboardCannotGetLeaderboardStandingsWithIncorrectActorType.Name, ActorType.Group);
                var leaderboardCannotGetLeaderboardStandingsWithZeroPageLimit = CreateGame(context, "Leaderboard_CannotGetLeaderboardStandingsWithZeroPageLimit");
                CreateLeaderboard(context, leaderboardCannotGetLeaderboardStandingsWithZeroPageLimit.Id, leaderboardCannotGetLeaderboardStandingsWithZeroPageLimit.Name);
                var leaderboardCannotGetNearLeaderboardStandingsWithoutActorId = CreateGame(context, "Leaderboard_CannotGetNearLeaderboardStandingsWithoutActorId");
                CreateLeaderboard(context, leaderboardCannotGetNearLeaderboardStandingsWithoutActorId.Id, leaderboardCannotGetNearLeaderboardStandingsWithoutActorId.Name);
                var leaderboardCannotGetFriendsLeaderboardStandingsWithoutActorId = CreateGame(context, "Leaderboard_CannotGetFriendsLeaderboardStandingsWithoutActorId");
                CreateLeaderboard(context, leaderboardCannotGetFriendsLeaderboardStandingsWithoutActorId.Id, leaderboardCannotGetFriendsLeaderboardStandingsWithoutActorId.Name);
                var leaderboardCannotGetGroupMembersLeaderboardStandingsWithoutActorId = CreateGame(context, "Leaderboard_CannotGetGroupMemberWithoutActorId");
                CreateLeaderboard(context, leaderboardCannotGetGroupMembersLeaderboardStandingsWithoutActorId.Id, leaderboardCannotGetGroupMembersLeaderboardStandingsWithoutActorId.Name, ActorType.Group);
                var leaderboardCannotGetGroupMembersLeaderboardStandingsWithIncorrectActorType = CreateGame(context, "Leaderboard_CannotGetGroupMembersWithIncorrectActorType");
                CreateLeaderboard(context, leaderboardCannotGetGroupMembersLeaderboardStandingsWithIncorrectActorType.Id, leaderboardCannotGetGroupMembersLeaderboardStandingsWithIncorrectActorType.Name);
            }
            #endregion

            #region Skill Client Tests
            if (!context.Skills.Any())
            {
                CreateSkill(context, CreateGame(context, "Skill_CanDisableNotifications").Id, "Skill_CanDisableNotifications");
                CreateSkill(context, CreateGame(context, "Skill_CanGetNotifications").Id, "Skill_CanGetNotifications");
                CreateSkill(context, CreateGame(context, "Skill_DontGetAlreadyRecievedNotifications").Id,
                            "Skill_DontGetAlreadyRecievedNotifications");
                CreateSkill(context, 0, "Skill_CanGetGlobalSkillProgress");
                CreateSkill(context, CreateGame(context, "Skill_CanGetSkillProgress").Id, "Skill_CanGetSkillProgress");
                CreateGame(context, "Skill_CannotGetNotExistingSkillProgress");
            }

            #endregion
        }