public static async Task <(ListOf_DBResult, Group)> CreateGroup(string SessionToken, Group group) { using (var db = new HolocronContext()) { User dataUser = await db.Users.FirstOrDefaultAsync(x => x.SessionToken == SessionToken); if (await db.Groups.CountAsync(x => x.Name == group.Name) == 0) { if (dataUser != null && group.ConnectionId != "" && group.Name != "") { group.Permissions.Add(new Permission() { User = dataUser, PermissionGroup = "Admin" }); db.Groups.Add(group); dataUser.CurrentAdventure = group; await db.SaveChangesAsync(); return(ListOf_DBResult.Success, group); } else { return(ListOf_DBResult.FormatError, null); } } else { return(ListOf_DBResult.Duplicate, null); } } }
public static async Task <ListOf_DBResult> CreateCharacter(string SessionToken, Character character) { using (var db = new HolocronContext()) { User dataUser = await db.Users.Where(x => x.SessionToken == SessionToken).Include("Characters").FirstOrDefaultAsync(); if (dataUser != null) { dataUser.Characters.Add(character); await db.SaveChangesAsync(); return(ListOf_DBResult.Success); } return(ListOf_DBResult.FormatError); } }
public static async Task <(ListOf_DBResult, Group)> JoinGroup(string SessionToken, Group dataGroup) { using (var db = new HolocronContext()) { if (dataGroup.ConnectionId != "" && dataGroup.Name != "") { User dataUser = await db.Users.FirstOrDefaultAsync(x => x.SessionToken == SessionToken); Group group = await db.Groups.Where(x => x.Name == dataGroup.Name) .Include(g => g.Permissions) .ThenInclude(x => x.User) .Include(g => g.GroupCharacters) .Include(g => g.Inventory) .Include(g => g.Ships) .FirstOrDefaultAsync(); if (dataUser != null && group != null && group.ConnectionId == dataGroup.ConnectionId) { if (!group.Permissions.Any(x => x.User.Id == dataUser.Id)) { group.Permissions.Add(new Permission() { User = dataUser, PermissionGroup = "Player" }); dataUser.CurrentAdventure = group; await db.SaveChangesAsync(); return(ListOf_DBResult.Success, group); } else { return(ListOf_DBResult.Duplicate, null); } } else { return(ListOf_DBResult.NotFound, null); } } else { return(ListOf_DBResult.FormatError, null); } } }
public static async Task <ListOf_DBResult> LoginGroup(string SessionToken, int id) { using (var db = new HolocronContext()) { User dataUser = await _FetchUser(db, SessionToken); Group group = await _FetchGroup(db, dataUser.CurrentAdventureId); if (dataUser != null && group != null && group.Permissions.Any(x => x.User.Id == dataUser.Id)) { dataUser.CurrentAdventure = group; await db.SaveChangesAsync(); return(ListOf_DBResult.Success); } else { return(ListOf_DBResult.NotFound); } } }
public static async Task <(ListOf_DBResult, User)> CreateUser(User user) { using (var db = new HolocronContext()) { if (await db.Users.CountAsync(x => x.Name == user.Name) == 0) { Guid g = Guid.NewGuid(); user.SessionToken = g.ToString(); await db.Users.AddAsync(user); await db.SaveChangesAsync(); return(ListOf_DBResult.Success, user); } else { return(ListOf_DBResult.Duplicate, null); } } }