Skip to content

Commit

Permalink
Yaaay, priority works
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-hanzal committed Jan 31, 2025
1 parent a2f4fe0 commit edfd4fc
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ export const Route = createFileRoute(
"/$locale/apps/derivean/map/$mapId/building/$buildingId/demand",
)({
async loader({
context: { queryClient, kysely },
context: { queryClient, kysely, session },
params: { mapId, buildingId },
}) {
const user = await session();

return {
user,
demand: await queryClient.ensureQueryData({
queryKey: ["GameMap", mapId, "building", buildingId, "demand", "list"],
queryFn: async () => {
Expand All @@ -34,6 +37,7 @@ export const Route = createFileRoute(
"r.name",
"d.amount",
"i.limit",
"d.priority",
"i.amount as available",
(eb) => {
return eb
Expand All @@ -48,12 +52,14 @@ export const Route = createFileRoute(
])
.where("d.buildingId", "=", buildingId)
.where("i.type", "=", "storage")
.where("d.type", "=", "storage")
.orderBy("r.name", "asc"),
output: z.object({
id: z.string().min(1),
name: z.string().min(1),
amount: z.number(),
available: z.number(),
priority: z.number(),
limit: z.number(),
transport: z.number().nullish(),
}),
Expand All @@ -67,10 +73,11 @@ export const Route = createFileRoute(
const { building } = useLoaderData({
from: "/$locale/apps/derivean/map/$mapId/building/$buildingId",
});
const { demand } = Route.useLoaderData();
const { user, demand } = Route.useLoaderData();

return (
<DemandPanel
userId={user.id}
building={building}
demand={demand}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ export namespace DemandPanel {
name: string;
amount: number;
available: number;
priority: number;
limit: number;
transport?: number | null;
}

export interface Props extends Panel.PropsEx {
userId: string;
building: Building;
demand: Demand[];
}
}

export const DemandPanel: FC<DemandPanel.Props> = ({
userId,
building,
demand,
...props
Expand Down Expand Up @@ -63,6 +66,8 @@ export const DemandPanel: FC<DemandPanel.Props> = ({
return (
<Item
key={item.id}
mapId={mapId}
userId={userId}
demand={item}
/>
);
Expand Down
147 changes: 113 additions & 34 deletions apps/pico/src/app/derivean/game/GameMap2/Building/Demand/Item.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useMutation } from "@tanstack/react-query";
import {
ArrowDownIcon,
ArrowUpIcon,
Badge,
Button,
Icon,
TrashIcon,
Tx,
useInvalidator,
} from "@use-pico/client";
import { toHumanNumber, tvc } from "@use-pico/common";
Expand All @@ -15,11 +18,13 @@ import { PackageIcon } from "~/app/derivean/icon/PackageIcon";

export namespace Item {
export interface Props {
mapId: string;
userId: string;
demand: DemandPanel.Demand;
}
}

export const Item: FC<Item.Props> = ({ demand }) => {
export const Item: FC<Item.Props> = ({ mapId, userId, demand }) => {
const invalidator = useInvalidator([["GameMap"]]);
const deleteDemandMutation = useMutation({
async mutationFn({ demandId }: { demandId: string }) {
Expand All @@ -31,66 +36,140 @@ export const Item: FC<Item.Props> = ({ demand }) => {
await invalidator();
},
});
const priorityUpMutation = useMutation({
async mutationFn({ demandId }: { demandId: string }) {
return kysely.transaction().execute(async (tx) => {
const { priority } = await tx
.selectFrom("Demand as d")
.select((eb) => eb.fn.max("d.priority").as("priority"))
.where("d.mapId", "=", mapId)
.where("d.userId", "=", userId)
.where("d.id", "!=", demandId)
.executeTakeFirstOrThrow();

const limit = demand.limit - (demand.transport || 0) - demand.available;
return tx
.updateTable("Demand")
.set({ priority: priority + 1 })
.where("id", "=", demandId)
.execute();
});
},
async onSuccess() {
await invalidator();
},
});
const priorityDownMutation = useMutation({
async mutationFn({ demandId }: { demandId: string }) {
return kysely.transaction().execute(async (tx) => {
const { priority } = await tx
.selectFrom("Demand as d")
.select((eb) => eb.fn.min("d.priority").as("priority"))
.where("d.mapId", "=", mapId)
.where("d.userId", "=", userId)
.where("d.id", "!=", demandId)
.executeTakeFirstOrThrow();

return tx
.updateTable("Demand")
.set({ priority: priority - 1 })
.where("id", "=", demandId)
.execute();
});
},
async onSuccess() {
await invalidator();
},
});

return (
<div
className={tvc([
"flex",
"flex-row",
"flex-col",
"gap-2",
"items-center",
"justify-between",
"border",
"p-4",
"rounded-sm",
"border-slate-200",
"hover:border-slate-300",
"hover:bg-slate-100",
demand.transport ?
[
"text-green-600",
"bg-green-50",
"border-green-400",
"hover:bg-green-50",
"hover:border-green-400",
]
["text-green-600", "bg-green-50", "border-green-400"]
: undefined,
])}
>
<div className={"flex flex-row gap-2 items-center"}>
{(demand.transport || 0) > 0 ?
<Icon icon={DemandIcon} />
: <Icon icon={PackageIcon} />}
<div className={"font-bold"}>{demand.name}</div>
<div
className={"flex flex-row gap-2 items-center justify-between w-full"}
>
<div className={"flex flex-row gap-2 items-center"}>
{(demand.transport || 0) > 0 ?
<Icon icon={DemandIcon} />
: <Icon icon={PackageIcon} />}
<div className={"font-bold"}>{demand.name}</div>
</div>

<div className={"flex flex-row gap-2 items-center"}>
<Badge
css={{
base:
demand.transport ?
[
"bg-green-50",
"border-green-400",
"hover:bg-green-50",
"hover:border-green-400",
]
: undefined,
}}
>
x{toHumanNumber({ number: demand.amount })}
</Badge>

<Button
iconEnabled={TrashIcon}
loading={deleteDemandMutation.isPending}
onClick={() => {
deleteDemandMutation.mutate({ demandId: demand.id });
}}
variant={{ variant: "danger" }}
/>
</div>
</div>

<div className={"flex flex-row gap-2 items-center"}>
<div
className={"flex flex-row gap-2 items-center justify-between w-full"}
>
<Button
iconEnabled={ArrowUpIcon}
variant={{ variant: "subtle" }}
loading={
priorityUpMutation.isPending || priorityDownMutation.isPending
}
onClick={() => {
priorityUpMutation.mutate({ demandId: demand.id });
}}
>
<Tx label={"Priority up (label)"} />
</Button>
<Badge
css={{
base:
demand.transport ?
[
"bg-green-50",
"border-green-400",
"hover:bg-green-50",
"hover:border-green-400",
]
: undefined,
base: ["bg-purple-50", "border-purple-400", "text-purple-600"],
}}
>
x{toHumanNumber({ number: demand.amount })}
{demand.priority}
</Badge>

<Button
iconEnabled={TrashIcon}
loading={deleteDemandMutation.isPending}
iconEnabled={ArrowDownIcon}
variant={{ variant: "subtle" }}
loading={
priorityUpMutation.isPending || priorityDownMutation.isPending
}
onClick={() => {
deleteDemandMutation.mutate({ demandId: demand.id });
priorityDownMutation.mutate({ demandId: demand.id });
}}
variant={{ variant: "danger" }}
/>
>
<Tx label={"Priority down (label)"} />
</Button>
</div>
</div>
);
Expand Down
20 changes: 18 additions & 2 deletions apps/pico/src/translation/cs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@
282447aea716ad0adce959ef2fa721688cf8614506ecdfd8c1dcf052:
ref: No tags (label)
value: No tags (label)
29261126b2796afece1903e8358f353d0e5c6f36d96886a45ce223f6:
ref: Submit demand (submit)
value: Uložit požadavek
293f4ff52457449c36cd77f2ff4458fcdcd8db96ad515628be4a4d10:
ref: Blueprint conflict (hint)
value: Pokud bude vybraná budova postavena, "tuto" budovu nebude možné postavit.
2a006ba6e2e260f2c60c8c3b2ee3cc26547ea6720981066968928fda:
ref: Demand resource (title)
value: Zadání požadavku na zdroj
2cd153399b8229188aa110c4f15461c5d6db1bd65aa84d8c477a1c9e:
ref: Create requirement item (menu)
value: Nový požadavek zdroje
Expand Down Expand Up @@ -263,8 +269,9 @@
5160844f3bf988623380083cff7115f07339d7410f0a5a91a1c03670:
ref: Not enough space (inventory is full/all resources are already on the way)
(toast)
value: Tento zdroj nelze vyžádat, jelikož inventář je plný nebo zbytek zdrojů
je již na cestě.
value:
Tento zdroj nelze vyžádat, jelikož inventář je plný nebo zbytek zdrojů je
již na cestě.
519c07031df43b317f56b864c9c77eb2696cef28f7ce0c0f639a1c9e:
ref: Update inventory item - Success (label)
value: Inventář byl úspěšně aktualizován.
Expand Down Expand Up @@ -537,6 +544,9 @@ af8f35d688a6936914f62e095e58125cd5fa4407d8c0343bc7fdeb7d:
b0153746a23ed62e2d9a29b8f11b3fb87a6d4122e60e418cc90355ba:
ref: Region limit (label)
value: Limit výskytů
b0d055aab431a584fc9279434f1643671ff0c6fc5c8d3afc97e13ea9:
ref: Priority up (label)
value: Zvýšit prioritu
b1eab5f194131f445609e3ceccf07fac5adf5924b6cde6daed300ecb:
ref: Building name (label)
value: Název budovy
Expand All @@ -549,6 +559,9 @@ b28be2abf28e5f6d9b9c09d9a96a296aa88333245e6e4a362737349d:
b2a0340361b1fb6f22b3239c88fcccf8149215887dd826d2f97e3f73:
ref: Amount of resources transported per cycle (hint)
value: Amount of resources transported per cycle (hint)
b3ca777a84a5364e70e878ebfba825cb7fdc7db04b21f6b88fe89ae1:
ref: Priority down (label)
value: Snížit prioritu
b41b5465adbeb7a6c853df051f999969c8549996f53cef7b4fcdab1a:
ref: There are no available blueprints (label)
value: (Zatím) nejsou dostupné žádné stavby.
Expand Down Expand Up @@ -582,6 +595,9 @@ c19d73b27d07e0d75cdb22df7339033a3ea479d2123dc66d1eb286f9:
c2b41375c2627229c3694a6f150e0795bf3799a2dd2fb8a6f0ec23af:
ref: Logout (label)
value: Logout (label)
c39172960527bcdd1e4bdc385af302fec6c702e943ed09ca75072109:
ref: Demanded amount (label)
value: Požadované množství
c4c460295c1978851ece4a45639f30252dcc4ca8a301a86c6ed2041b:
ref: Region min width (label)
value: Minimální šířka
Expand Down
15 changes: 15 additions & 0 deletions apps/pico/src/translation/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@
282447aea716ad0adce959ef2fa721688cf8614506ecdfd8c1dcf052:
ref: No tags (label)
value: No tags (label)
29261126b2796afece1903e8358f353d0e5c6f36d96886a45ce223f6:
ref: Submit demand (submit)
value: Submit demand (submit)
293f4ff52457449c36cd77f2ff4458fcdcd8db96ad515628be4a4d10:
ref: Blueprint conflict (hint)
value: Blueprint conflict (hint)
2a006ba6e2e260f2c60c8c3b2ee3cc26547ea6720981066968928fda:
ref: Demand resource (title)
value: Demand resource (title)
2cd153399b8229188aa110c4f15461c5d6db1bd65aa84d8c477a1c9e:
ref: Create requirement item (menu)
value: Create requirement item (menu)
Expand Down Expand Up @@ -516,6 +522,9 @@ af8f35d688a6936914f62e095e58125cd5fa4407d8c0343bc7fdeb7d:
b0153746a23ed62e2d9a29b8f11b3fb87a6d4122e60e418cc90355ba:
ref: Region limit (label)
value: Region limit (label)
b0d055aab431a584fc9279434f1643671ff0c6fc5c8d3afc97e13ea9:
ref: Priority up (label)
value: Priority up (label)
b1eab5f194131f445609e3ceccf07fac5adf5924b6cde6daed300ecb:
ref: Building name (label)
value: Building name (label)
Expand All @@ -528,6 +537,9 @@ b28be2abf28e5f6d9b9c09d9a96a296aa88333245e6e4a362737349d:
b2a0340361b1fb6f22b3239c88fcccf8149215887dd826d2f97e3f73:
ref: Amount of resources transported per cycle (hint)
value: Amount of resources transported per cycle (hint)
b3ca777a84a5364e70e878ebfba825cb7fdc7db04b21f6b88fe89ae1:
ref: Priority down (label)
value: Priority down (label)
b41b5465adbeb7a6c853df051f999969c8549996f53cef7b4fcdab1a:
ref: There are no available blueprints (label)
value: There are no available blueprints (label)
Expand Down Expand Up @@ -561,6 +573,9 @@ c19d73b27d07e0d75cdb22df7339033a3ea479d2123dc66d1eb286f9:
c2b41375c2627229c3694a6f150e0795bf3799a2dd2fb8a6f0ec23af:
ref: Logout (label)
value: Logout (label)
c39172960527bcdd1e4bdc385af302fec6c702e943ed09ca75072109:
ref: Demanded amount (label)
value: Demanded amount (label)
c4c460295c1978851ece4a45639f30252dcc4ca8a301a86c6ed2041b:
ref: Region min width (label)
value: Region min width (label)
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/icon/ArrowDownIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ArrowDownIcon = "icon-[fluent--arrow-down-12-regular]";
1 change: 1 addition & 0 deletions packages/client/src/icon/ArrowUpIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ArrowUpIcon = "icon-[fluent--arrow-up-12-regular]";
2 changes: 2 additions & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export { Fulltext } from "./fulltext/Fulltext";
export { FulltextCss } from "./fulltext/FulltextCss";
export { useDebounce } from "./hook/useDebounce";
export { ActionMenuIcon } from "./icon/ActionMenuIcon";
export { ArrowDownIcon } from "./icon/ArrowDownIcon";
export { ArrowUpIcon } from "./icon/ArrowUpIcon";
export { BackIcon } from "./icon/BackIcon";
export { CheckIcon } from "./icon/CheckIcon";
export { CloseIcon } from "./icon/CloseIcon";
Expand Down

0 comments on commit edfd4fc

Please sign in to comment.