Skip to content

Commit

Permalink
Game mechanics now works perfectly
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-hanzal committed Jan 30, 2025
1 parent 2583f67 commit 5747b36
Show file tree
Hide file tree
Showing 18 changed files with 268 additions and 535 deletions.
4 changes: 2 additions & 2 deletions apps/pico/src/@routes/$locale/apps/derivean/map/$mapId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export const Route = createFileRoute("/$locale/apps/derivean/map/$mapId")({
transports.some(({ mark }) => mark) ?
{
stroke: "#23BC43",
strokeWidth: 5,
strokeWidth: 8,
}
: {
stroke: "#b1b1b7",
Expand Down Expand Up @@ -663,7 +663,7 @@ export const Route = createFileRoute("/$locale/apps/derivean/map/$mapId")({
transports.length > 0 ?
{
stroke: "#23BC43",
strokeWidth: 5,
strokeWidth: 8,
}
: {
stroke: "#b1b1b7",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createFileRoute, useLoaderData } from "@tanstack/react-router";
import { zodValidator } from "@tanstack/zod-adapter";
import { BackIcon, LinkTo, withList } from "@use-pico/client";
import { withList } from "@use-pico/client";
import { z } from "zod";
import { InventoryPanel } from "~/app/derivean/game/GameMap2/Inventory/InventoryPanel";

Expand All @@ -18,11 +18,14 @@ export const Route = createFileRoute(
};
},
async loader({
context: { queryClient, kysely },
context: { queryClient, kysely, session },
deps: { fulltext },
params: { mapId, buildingId },
}) {
const user = await session();

return {
user,
inventory: await queryClient.ensureQueryData({
queryKey: [
"GameMap",
Expand Down Expand Up @@ -96,31 +99,14 @@ export const Route = createFileRoute(
from: "/$locale/apps/derivean/map/$mapId/building/$buildingId",
});
const { fulltext } = Route.useSearch();
const { mapId, locale } = Route.useParams();
const navigate = Route.useNavigate();
const { inventory } = Route.useLoaderData();
const { user, inventory } = Route.useLoaderData();

return (
<InventoryPanel
building={building}
userId={user.id}
inventory={inventory}
textSubTitle={
<>
<LinkTo
icon={BackIcon}
to={"/$locale/apps/derivean/map/$mapId/building/$buildingId/view"}
params={{ locale, mapId, buildingId: building.id }}
/>
<LinkTo
to={
"/$locale/apps/derivean/map/$mapId/building/$buildingId/inventory"
}
params={{ locale, mapId, buildingId: building.id }}
search={{ zoomToId: building.id }}
>
{building.name}
</LinkTo>
</>
}
fulltextProps={{
value: fulltext,
onFulltext(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,6 @@ export const Route = createFileRoute(
)
.as("supply");
},
(eb) => {
return eb
.selectFrom("Demand as d")
.innerJoin("Building as b", "b.id", "d.supplierId")
.innerJoin("Blueprint as bp", "bp.id", "b.blueprintId")
.select((eb) => {
return Kysely.jsonObject({
id: eb.ref("b.id"),
name: eb.ref("bp.name"),
}).as("supplier");
})
.whereRef("d.resourceId", "=", "br.resourceId")
.whereRef("d.buildingId", "=", "bg.id")
.as("supplier");
},
(eb) => {
return eb
.selectFrom("Inventory as i")
Expand Down Expand Up @@ -118,14 +103,6 @@ export const Route = createFileRoute(
name: z.string().min(1),
}),
).nullish(),
supplier: withJsonSchema(
z
.object({
id: z.string().min(1),
name: z.string().min(1),
})
.nullish(),
).nullish(),
passive: withBoolSchema(),
}),
});
Expand Down
30 changes: 18 additions & 12 deletions apps/pico/src/app/derivean/db/kysely.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,24 @@ export const { kysely, bootstrap } = withDatabase<Database>({
.ifNotExists()
.addColumn("id", $id, (col) => col.primaryKey())

.addColumn("userId", $id, (col) => col.notNull())
.addForeignKeyConstraint(
"[Supply] userId",
["userId"],
"User",
["id"],
(c) => c.onDelete("cascade").onUpdate("cascade"),
)

.addColumn("mapId", $id, (col) => col.notNull())
.addForeignKeyConstraint(
"[Supply] mapId",
["mapId"],
"Map",
["id"],
(c) => c.onDelete("cascade").onUpdate("cascade"),
)

.addColumn("buildingId", $id, (col) => col.notNull())
.addForeignKeyConstraint(
"[Supply] buildingId",
Expand Down Expand Up @@ -1169,18 +1187,6 @@ export const { kysely, bootstrap } = withDatabase<Database>({
(c) => c.onDelete("cascade").onUpdate("cascade"),
)

/**
* If supplier is NULL, there is nobody to supply the resource.
*/
.addColumn("supplierId", $id)
.addForeignKeyConstraint(
"[Resource_Queue] supplierId",
["supplierId"],
"Building",
["id"],
(c) => c.onDelete("cascade").onUpdate("cascade"),
)

.addColumn("resourceId", $id, (col) => col.notNull())
.addForeignKeyConstraint(
"[Demand] resourceId",
Expand Down
22 changes: 12 additions & 10 deletions apps/pico/src/app/derivean/db/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
IdentitySchema,
withBoolSchema,
withSourceSchema,
type FilterSchema,
type ShapeSchema,
IdentitySchema,
withSourceSchema,
type FilterSchema,
type ShapeSchema,
withBoolSchema,
} from "@use-pico/common";
import { z } from "zod";

Expand Down Expand Up @@ -593,9 +593,6 @@ export const withDemandSchema = <
buildingId:
// varchar(36) / not nullable
z.string().min(1),
supplierId:
// varchar(36) / nullable
z.string().nullish(),
resourceId:
// varchar(36) / not nullable
z.string().min(1),
Expand All @@ -617,7 +614,6 @@ export const withDemandSchema = <
"userId",
"mapId",
"buildingId",
"supplierId",
"resourceId",
"amount",
"priority",
Expand Down Expand Up @@ -1027,6 +1023,12 @@ export const withSupplySchema = <
return withSourceSchema({
entity: IdentitySchema.merge(
z.object({
userId:
// varchar(36) / not nullable
z.string().min(1),
mapId:
// varchar(36) / not nullable
z.string().min(1),
buildingId:
// varchar(36) / not nullable
z.string().min(1),
Expand All @@ -1037,7 +1039,7 @@ export const withSupplySchema = <
),
shape,
filter,
sort: ["id", "buildingId", "resourceId"],
sort: ["id", "userId", "mapId", "buildingId", "resourceId"],
});
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useParams } from "@tanstack/react-router";
import { Icon, LinkTo, Progress } from "@use-pico/client";
import { Icon, Progress } from "@use-pico/client";
import { toHumanNumber, tvc } from "@use-pico/common";
import type { FC } from "react";
import type { RequirementPanel } from "~/app/derivean/game/GameMap2/Construction/Requirement/RequirementPanel";
import { ArrowRightIcon } from "~/app/derivean/icon/ArrowRightIcon";
import { DemandIcon } from "~/app/derivean/icon/DemandIcon";
import { PackageIcon } from "~/app/derivean/icon/PackageIcon";

Expand All @@ -14,9 +12,6 @@ export namespace Item {
}

export const Item: FC<Item.Props> = ({ requirement }) => {
const { locale, mapId } = useParams({
from: "/$locale/apps/derivean/map/$mapId",
});
const available = requirement.available || 0;

return (
Expand All @@ -31,7 +26,7 @@ export const Item: FC<Item.Props> = ({ requirement }) => {
"p-2",
"cursor-default",
"hover:bg-slate-100",
requirement.supplier ?
requirement.supply ?
[
"bg-purple-50",
"border-purple-400",
Expand Down Expand Up @@ -61,32 +56,15 @@ export const Item: FC<Item.Props> = ({ requirement }) => {
"flex-row",
"gap-2",
"items-center",
requirement.supplier ? ["text-purple-600"] : ["text-red-600"],
requirement.supply ? ["text-purple-600"] : ["text-red-600"],
requirement.transport || available >= requirement.amount ?
["text-green-600"]
: undefined,
])}
>
{requirement.supplier || available >= requirement.amount ?
{requirement.supply || available >= requirement.amount ?
<Icon icon={DemandIcon} />
: <Icon icon={PackageIcon} />}
{requirement.supplier ?
<div className={"flex flex-row gap-2 items-center"}>
<LinkTo
to={
"/$locale/apps/derivean/map/$mapId/building/$buildingId/view"
}
params={{
locale,
mapId,
buildingId: requirement.supplier.id,
}}
>
{requirement.supplier.name}
</LinkTo>
<Icon icon={ArrowRightIcon} />
</div>
: null}
<div className={"font-bold"}>{requirement.name}</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ export namespace RequirementPanel {

export interface Supply {
id: string;
buildingId: string;
name: string;
}

export interface Supplier {
id: string;
name: string;
}

export interface Requirement {
Expand All @@ -29,7 +22,6 @@ export namespace RequirementPanel {
amount: number;
transport: number;
supply?: Supply | null;
supplier?: Supplier | null;
available?: number | null;
passive: boolean;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Fulltext, Tx } from "@use-pico/client";
import { useParams } from "@tanstack/react-router";
import { BackIcon, Fulltext, LinkTo, Tx } from "@use-pico/client";
import { tvc } from "@use-pico/common";
import type { FC } from "react";
import { Item } from "~/app/derivean/game/GameMap2/Inventory/Item";
import { Panel } from "~/app/derivean/game/GameMap2/Panel";
import { InventoryIcon } from "~/app/derivean/icon/InventoryIcon";

export namespace InventoryPanel {
export interface Building {
id: string;
name: string;
}

export interface Inventory {
id: string;
buildingId: string;
Expand All @@ -17,20 +23,46 @@ export namespace InventoryPanel {
}

export interface Props extends Panel.PropsEx {
building: Building;
userId: string;
inventory: Inventory[];
fulltextProps: Fulltext.Props;
}
}

export const InventoryPanel: FC<InventoryPanel.Props> = ({
building,
userId,
inventory,
fulltextProps,
...props
}) => {
const { locale, mapId } = useParams({
from: "/$locale/apps/derivean/map/$mapId",
});

return (
<Panel
icon={InventoryIcon}
textTitle={<Tx label={"Inventory (label)"} />}
textSubTitle={
<>
<LinkTo
icon={BackIcon}
to={"/$locale/apps/derivean/map/$mapId/building/$buildingId/view"}
params={{ locale, mapId, buildingId: building.id }}
/>
<LinkTo
to={
"/$locale/apps/derivean/map/$mapId/building/$buildingId/inventory"
}
params={{ locale, mapId, buildingId: building.id }}
search={{ zoomToId: building.id }}
>
{building.name}
</LinkTo>
</>
}
{...props}
>
<Fulltext {...fulltextProps} />
Expand All @@ -46,6 +78,8 @@ export const InventoryPanel: FC<InventoryPanel.Props> = ({
return (
<Item
key={item.id}
mapId={mapId}
userId={userId}
inventory={item}
/>
);
Expand Down
Loading

0 comments on commit 5747b36

Please sign in to comment.