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

Adjustment to clans/hideouts #406

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions app/drizzle/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ export const ElementNames = [
"Lava",
"Explosion",
"Light",
"Boil",
"None",
] as const;
export type ElementName = (typeof ElementNames)[number];
Expand Down Expand Up @@ -903,6 +904,8 @@ export const IMG_ELEMENT_DUST =
"https://utfs.io/f/Hzww9EQvYURJchNmlmSnxBpQqGNDcTHbLmYz8uXAl3oa54ti";
export const IMG_ELEMENT_LIGHTNING =
"https://utfs.io/f/Hzww9EQvYURJ4DIVIclYIif5CL8BKvMsOh2ZnmS7yHt0jTD3";
export const IMG_ELEMENT_BOIL =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add to assets/elements/ folder for future recoverability :)

"https://i.imgur.com/63KgdPB.png";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


export const IMG_BASIC_HEAL =
"https://utfs.io/f/Hzww9EQvYURJnlXNSKmojJ0EqeDCvBrNmZaXVdY97gSpOWiA";
Expand Down
28 changes: 19 additions & 9 deletions app/src/layout/Clan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,17 @@ export const ClanInfo: React.FC<ClanInfoProps> = (props) => {
});
const onDeposit = toBankForm.handleSubmit((data) => toBank({ ...data, clanId }));

const { mutate: instantJoinAndLead } = api.clan.instantJoinAndLead.useMutation({
onSuccess: async (data) => {
showMutationToast(data);
if (data.success) {
await utils.profile.getUser.invalidate();
await utils.clan.get.invalidate();
router.push("/clanhall");
}
},
});

// Rename Form
const renameForm = useForm<ClanRenameSchema>({
resolver: zodResolver(clanRenameSchema),
Expand Down Expand Up @@ -1146,21 +1157,20 @@ export const ClanInfo: React.FC<ClanInfoProps> = (props) => {
Resign as Leader
</Button>
)}
{inClan && !isLeader && (
{canEditClans(userData.role) && (
<Confirm
title="Challenge Leader"
proceed_label="Submit"
title="Instantly Join & Take Leadership"
proceed_label="Confirm"
button={
<Button id={`challenge-leader`} className="w-full my-2">
<Button id={`instant-join-lead`} className="w-full my-2">
<Swords className="mr-2 h-5 w-5" />
Challenge Leader
Take Leadership
</Button>
}
onAccept={() => fight({ clanId, villageId: userData.villageId ?? "" })}
onAccept={() => instantJoinAndLead({ clanId })}
>
Confirm that you wish to challenge the current leader. Note that
challenges are carried out as AI vs AI, and if you lose you will be kicked
out of the clan!
You have the permission to instantly join this clan and take leadership.
Are you sure you want to proceed?
</Confirm>
)}
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/src/layout/ElementImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
IMG_ELEMENT_SCORCH,
IMG_ELEMENT_DUST,
IMG_ELEMENT_LIGHTNING,
IMG_ELEMENT_BOIL,
} from "@/drizzle/constants";

import { GeneralTypes, StatTypes, PoolTypes, ElementNames } from "@/drizzle/constants";
Expand Down Expand Up @@ -83,6 +84,8 @@ export const getElementImg = (element: ElementName): string => {
return IMG_ELEMENT_DUST;
case "Lightning":
return IMG_ELEMENT_LIGHTNING;
case "Boil":
return IMG_ELEMENT_BOIL;
}
};

Expand Down
29 changes: 29 additions & 0 deletions app/src/server/api/routers/clan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,35 @@ export const clanRouter = createTRPCRouter({
}
return errorResponse(`Failed to initiate ${groupLabel} battle`);
}),
instantJoinAndLead: protectedProcedure
.input(z.object({ clanId: z.string() }))
.output(baseServerResponse)
.mutation(async ({ ctx, input }) => {
const [user, fetchedClan] = await Promise.all([
fetchUser(ctx.drizzle, ctx.userId),
fetchClan(ctx.drizzle, input.clanId),
]);
if (!fetchedClan) return errorResponse("Faction not found");
if (!user) return errorResponse("User not found");
if (!canEditClans(user.role)) return errorResponse("Permission denied");
if (user.clanId) return errorResponse("Already in a faction");

await ctx.drizzle.transaction(async (tx) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid using transactions on the database; they work with planetscale, but I've consistently experienced when we introduce transactions we also start seeing deadlocks popping up. As for this specific case, I think simply putting the two transactions into a Promise.all is fine :)

await tx
.update(userData)
.set({ clanId: fetchedClan.id, villageId: fetchedClan.village?.id })
.where(eq(userData.userId, user.userId));
await tx
.update(clan)
.set({ leaderId: user.userId })
.where(eq(clan.id, fetchedClan.id));
});

return {
success: true,
message: `You have instantly joined and taken leadership of ${fetchedClan.name}`,
};
}),
});

/**
Expand Down