Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue #386: Add a button so users can reset their AI Profile #387

Merged
merged 21 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/src/app/profile/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ import UserRequestSystem from "@/layout/UserRequestSystem";
import type { Gender } from "@/validators/register";
import type { BaseServerResponse } from "@/server/api/trpc";
import type { Bloodline, Village } from "@/drizzle/schema";
import {
AiRule,
ConditionDistanceHigherThan,
ActionMoveTowardsOpponent,
} from "@/validators/ai";

export default function EditProfile() {
// State
Expand Down Expand Up @@ -293,6 +298,17 @@ const BattleSettingsEdit: React.FC<{ userId: string }> = ({ userId }) => {
},
});

const { mutate: updateAiProfile, isPending } =
api.ai.updateAiProfile.useMutation({
onSuccess: async (data) => {
showMutationToast(data);
if (data.success) {
await utils.profile.getAi.invalidate();
await utils.profile.getPublicUser.invalidate();
}
},
});

// Update highest preferences
const { mutate: updatePreferences } = api.profile.updatePreferences.useMutation({
onSuccess: async (data) => {
Expand Down Expand Up @@ -445,6 +461,37 @@ const BattleSettingsEdit: React.FC<{ userId: string }> = ({ userId }) => {
}
/>
<Label htmlFor="battle-description">Show battle descriptions</Label>
< br/>
< br/>
<Confirm
title="Reset AI Profile"
button={
<Button
variant="destructive"
size="sm"
disabled={!profile?.aiProfileId || isPending}
>
{isPending ? <Loader size={5} /> : "Reset AI Profile"}
</Button>
}
onAccept={() => {
if (!profile?.aiProfileId) return;
const defaultAiProfilePayload = {
id: profile.aiProfileId,
rules: [
AiRule.parse({
conditions: [ConditionDistanceHigherThan.parse({ value: 2 })],
action: ActionMoveTowardsOpponent.parse({}),
}),
],
includeDefaultRules: true,
};
updateAiProfile(defaultAiProfilePayload);
}}
>
This will reset your AI profile to default settings. This action cannot be undone. Are you sure you want to continue?
</Confirm>

</TabsContent>
<TabsContent value="aiprofile">
<AiProfileEdit userData={profile} hideTitle />
Expand Down
9 changes: 9 additions & 0 deletions app/src/server/api/routers/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ export const profileRouter = createTRPCRouter({
where: and(eq(userData.userId, input.userId), eq(userData.isAi, true)),
with: { jutsus: { with: { jutsu: true } }, items: { with: { item: true } } },
});
// Filter off entries that do not exist
if (user) {
user.jutsus = user.jutsus.filter((j) => j.jutsu);
user.items = user.items.filter((i) => i.item);
}
// Return user
return user ?? null;
}),
// Create new AI
Expand Down Expand Up @@ -970,6 +976,9 @@ export const profileRouter = createTRPCRouter({
if (!requester || !canSeeIps(requester.role)) {
user.lastIp = "hidden";
}
// Filter off entries that do not exist
user.jutsus = user.jutsus.filter((j) => j.jutsu);
user.items = user.items.filter((i) => i.item);
// If no avatarLight version, create one
if (!user.avatarLight && user.avatar) {
const thumbnail = await createThumbnail(user.avatar);
Expand Down
Loading