/// <summary> /// Adds the given user to the given roleplay. /// </summary> /// <param name="db">The database where the roleplays are stored.</param> /// <param name="context">The context of the user.</param> /// <param name="roleplay">The roleplay to add the user to.</param> /// <param name="newUser">The user to add to the roleplay.</param> /// <returns>An execution result which may or may not have succeeded.</returns> public async Task <ExecuteResult> AddUserToRoleplayAsync ( [NotNull] GlobalInfoContext db, [NotNull] SocketCommandContext context, [NotNull] Roleplay roleplay, [NotNull] IUser newUser ) { var isCurrentUser = context.Message.Author.Id == newUser.Id; if (roleplay.HasJoined(newUser)) { var errorMessage = isCurrentUser ? "You're already in that roleplay." : "The user is aleady in that roleplay."; return(ExecuteResult.FromError(CommandError.Unsuccessful, errorMessage)); } if (roleplay.IsKicked(newUser)) { var errorMessage = isCurrentUser ? "You've been kicked from that roleplay, and can't rejoin unless invited." : "The user has been kicked from that roleplay, and can't rejoin unless invited."; return(ExecuteResult.FromError(CommandError.UnmetPrecondition, errorMessage)); } // Check the invite list for nonpublic roleplays. if (!roleplay.IsPublic && !roleplay.IsInvited(newUser)) { var errorMessage = isCurrentUser ? "You haven't been invited to that roleplay." : "The user hasn't been invited to that roleplay."; return(ExecuteResult.FromError(CommandError.UnmetPrecondition, errorMessage)); } var participantEntry = roleplay.ParticipatingUsers.FirstOrDefault(p => p.User.DiscordID == (long)newUser.Id); if (participantEntry is null) { var user = await db.GetOrRegisterUserAsync(newUser); participantEntry = new RoleplayParticipant(roleplay, user, ParticipantStatus.Joined); roleplay.ParticipatingUsers.Add(participantEntry); } else { participantEntry.Status = ParticipantStatus.Joined; } await db.SaveChangesAsync(); return(ExecuteResult.FromSuccess()); }
public async Task <ModifyEntityResult> InviteUserAsync ( [NotNull] Roleplay roleplay, [NotNull] IUser invitedUser ) { if (roleplay.IsPublic && !roleplay.IsKicked(invitedUser)) { return(ModifyEntityResult.FromError("The roleplay is not set to private.")); } if (roleplay.InvitedUsers.Any(p => p.User.DiscordID == (long)invitedUser.Id)) { return(ModifyEntityResult.FromError("The user has already been invited to that roleplay.")); } // Remove the invited user from the kick list, if they're on it var participantEntry = roleplay.ParticipatingUsers.FirstOrDefault(p => p.User.DiscordID == (long)invitedUser.Id); if (participantEntry is null) { var getUserResult = await _users.GetOrRegisterUserAsync(invitedUser); if (!getUserResult.IsSuccess) { return(ModifyEntityResult.FromError(getUserResult)); } var user = getUserResult.Entity; // Ensure the user is attached, so we don't create any conflicts. _database.Attach(user); participantEntry = new RoleplayParticipant(roleplay, user) { Status = ParticipantStatus.Invited }; roleplay.ParticipatingUsers.Add(participantEntry); } else { participantEntry.Status = ParticipantStatus.Invited; } await _database.SaveChangesAsync(); return(ModifyEntityResult.FromSuccess()); }
/// <summary> /// Invites the user to the given roleplay. /// </summary> /// <param name="db">The database where the roleplays are stored.</param> /// <param name="roleplay">The roleplay to invite the user to.</param> /// <param name="invitedUser">The user to invite.</param> /// <returns>An execution result which may or may not have succeeded.</returns> public async Task <ExecuteResult> InviteUserAsync ( [NotNull] GlobalInfoContext db, [NotNull] Roleplay roleplay, [NotNull] IUser invitedUser ) { if (roleplay.IsPublic) { return(ExecuteResult.FromError(CommandError.UnmetPrecondition, "The roleplay is not set to private.")); } if (roleplay.InvitedUsers.Any(p => p.User.DiscordID == (long)invitedUser.Id)) { return(ExecuteResult.FromError(CommandError.Unsuccessful, "The user has already been invited to that roleplay.")); } // Remove the invited user from the kick list, if they're on it var participantEntry = roleplay.ParticipatingUsers.FirstOrDefault(p => p.User.DiscordID == (long)invitedUser.Id); if (participantEntry is null) { var user = await db.GetOrRegisterUserAsync(invitedUser); participantEntry = new RoleplayParticipant(roleplay, user, ParticipantStatus.Invited); roleplay.ParticipatingUsers.Add(participantEntry); } else { participantEntry.Status = ParticipantStatus.Invited; } await db.SaveChangesAsync(); return(ExecuteResult.FromSuccess()); }
public async Task <CreateEntityResult <RoleplayParticipant> > AddUserToRoleplayAsync ( [NotNull] ICommandContext context, [NotNull] Roleplay roleplay, [NotNull] IUser newUser ) { var isCurrentUser = context.Message.Author.Id == newUser.Id; if (roleplay.HasJoined(newUser)) { var errorMessage = isCurrentUser ? "You're already in that roleplay." : "The user is already in that roleplay."; return(CreateEntityResult <RoleplayParticipant> .FromError(errorMessage)); } if (roleplay.IsKicked(newUser)) { var errorMessage = isCurrentUser ? "You've been kicked from that roleplay, and can't rejoin unless invited." : "The user has been kicked from that roleplay, and can't rejoin unless invited."; return(CreateEntityResult <RoleplayParticipant> .FromError(errorMessage)); } // Check the invite list for nonpublic roleplays. if (!roleplay.IsPublic && !roleplay.IsInvited(newUser)) { var errorMessage = isCurrentUser ? "You haven't been invited to that roleplay." : "The user hasn't been invited to that roleplay."; return(CreateEntityResult <RoleplayParticipant> .FromError(errorMessage)); } var participantEntry = roleplay.ParticipatingUsers.FirstOrDefault(p => p.User.DiscordID == (long)newUser.Id); if (participantEntry is null) { var getUserResult = await _users.GetOrRegisterUserAsync(newUser); if (!getUserResult.IsSuccess) { return(CreateEntityResult <RoleplayParticipant> .FromError(getUserResult)); } var user = getUserResult.Entity; // Ensure the user is attached, so we don't create any conflicts. _database.Attach(user); participantEntry = new RoleplayParticipant(roleplay, user) { Status = ParticipantStatus.Joined }; roleplay.ParticipatingUsers.Add(participantEntry); } else { participantEntry.Status = ParticipantStatus.Joined; } await _database.SaveChangesAsync(); return(CreateEntityResult <RoleplayParticipant> .FromSuccess(participantEntry)); }
public async Task <CreateEntityResult <Roleplay> > CreateRoleplayAsync ( [NotNull] ICommandContext context, [NotNull] string roleplayName, [NotNull] string roleplaySummary, bool isNSFW, bool isPublic ) { var getOwnerResult = await _users.GetOrRegisterUserAsync(context.User); if (!getOwnerResult.IsSuccess) { return(CreateEntityResult <Roleplay> .FromError(getOwnerResult)); } // Ensure the user is attached, so we don't create any conflicts. var owner = getOwnerResult.Entity; _database.Attach(owner); // Use a dummy name, since we'll be setting it useng the service. var roleplay = new Roleplay((long)context.Guild.Id, owner, string.Empty); var ownerParticipant = new RoleplayParticipant(roleplay, owner) { Status = ParticipantStatus.Joined }; roleplay.ParticipatingUsers.Add(ownerParticipant); var setNameResult = await SetRoleplayNameAsync(context, roleplay, roleplayName); if (!setNameResult.IsSuccess) { return(CreateEntityResult <Roleplay> .FromError(setNameResult)); } var setSummaryResult = await SetRoleplaySummaryAsync(roleplay, roleplaySummary); if (!setSummaryResult.IsSuccess) { return(CreateEntityResult <Roleplay> .FromError(setSummaryResult)); } var setIsNSFWResult = await SetRoleplayIsNSFWAsync(roleplay, isNSFW); if (!setIsNSFWResult.IsSuccess) { return(CreateEntityResult <Roleplay> .FromError(setIsNSFWResult)); } var setIsPublicResult = await SetRoleplayIsPublicAsync(roleplay, isPublic); if (!setIsPublicResult.IsSuccess) { return(CreateEntityResult <Roleplay> .FromError(setIsPublicResult)); } _database.Roleplays.Update(roleplay); await _database.SaveChangesAsync(); var roleplayResult = await GetUserRoleplayByNameAsync(context, context.Message.Author, roleplayName); if (!roleplayResult.IsSuccess) { return(CreateEntityResult <Roleplay> .FromError(roleplayResult)); } return(CreateEntityResult <Roleplay> .FromSuccess(roleplayResult.Entity)); }