Skip to content

Commit

Permalink
show prices on travel
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Sep 17, 2023
1 parent c606c7e commit dfabb83
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 90 deletions.
10 changes: 5 additions & 5 deletions src/components/market.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl MarketImpl of MarketTrait {
if drug_id == 'Acid' {
PricingInfos {
min_price: 500 * SCALING_FACTOR,
max_price: 1500 * SCALING_FACTOR,
max_price: 1800 * SCALING_FACTOR,
min_qty: 400,
max_qty: 900,
}
Expand All @@ -75,15 +75,15 @@ impl MarketImpl of MarketTrait {
}
} else if drug_id == 'Heroin' {
PricingInfos {
min_price: 1000 * SCALING_FACTOR,
max_price: 3000 * SCALING_FACTOR,
min_price: 1200 * SCALING_FACTOR,
max_price: 4000 * SCALING_FACTOR,
min_qty: 300,
max_qty: 700,
}
} else if drug_id == 'Cocaine' {
PricingInfos {
min_price: 2000 * SCALING_FACTOR,
max_price: 6000 * SCALING_FACTOR,
min_price: 3000 * SCALING_FACTOR,
max_price: 8000 * SCALING_FACTOR,
min_qty: 250,
max_qty: 600,
}
Expand Down
2 changes: 1 addition & 1 deletion src/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const SCALING_FACTOR: u128 = 10_000;

const TRAVEL_RISK: u8 = 30; // 30% chance of travel encounter
const TRAVEL_RISK: u8 = 50; // 50% chance of travel encounter
const CAPTURE_RISK: u8 = 50; // 50% chance of capture

const ENCOUNTER_BIAS_GANGS: u128 = 60;
Expand Down
2 changes: 0 additions & 2 deletions web/src/components/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export const Map = ({
const isMobile = useBreakpointValue([true, false]);

useEffect(() => {
console.log({ highlight });
if (highlight !== undefined) {
console.log("got here");
const animation = isMobile
? { scale: 1.75, ...coordinate[highlight] }
: { scale: 1, x: 0, y: 0 };
Expand Down
12 changes: 4 additions & 8 deletions web/src/dojo/components/useGlobalScores.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PlayerEdge, Name, useGlobalScoresQuery } from "@/generated/graphql";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { shortString } from "starknet";
import { SCALING_FACTOR } from "..";

Expand Down Expand Up @@ -42,20 +42,16 @@ export class GlobalScores {
}

export const useGlobalScores = (offset?: number, limit?: number) => {
const [scores, setScores] = useState<Score[]>([]);
// Gets top 100
// TODO: paginate with cursor for more scores
const { data, isFetched, refetch } = useGlobalScoresQuery({
limit: limit || 100,
});

useEffect(() => {
const scores = GlobalScores.create(
data?.playerComponents?.edges as PlayerEdge[],
const scores: Score[] = useMemo(() => {
return (
GlobalScores.create(data?.playerComponents?.edges as PlayerEdge[]) || []
);
if (scores) {
setScores(scores);
}
}, [data]);

return {
Expand Down
68 changes: 68 additions & 0 deletions web/src/dojo/components/useMarkets.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Market, MarketEdge, useMarketPricesQuery } from "@/generated/graphql";
import { useEffect, useMemo, useState } from "react";
import { num } from "starknet";
import { REFETCH_INTERVAL, SCALING_FACTOR } from "..";
import { LocationPrices, DrugMarket } from "../types";

export class MarketPrices {
locationPrices: LocationPrices;

constructor(locationMarkets: LocationPrices) {
this.locationPrices = locationMarkets;
}

static create(edges: MarketEdge[]): LocationPrices | undefined {
if (!edges || edges.length === 0) return undefined;

const locationPrices: LocationPrices = new Map();

for (let edge of edges) {
const node = edge.node;
const locationId = num.toHexString(node?.location_id);
const drugId = num.toHexString(node?.drug_id);
const price =
Number(node?.cash) / Number(node?.quantity) / SCALING_FACTOR;

const drugMarket: DrugMarket = {
id: drugId,
price: price,
marketPool: node as Market,
};

if (locationPrices.has(locationId)) {
locationPrices.get(locationId)?.push(drugMarket);
} else {
locationPrices.set(locationId, [drugMarket]);
}
}

return locationPrices;
}
}

export interface MarketsInterface {
locationPrices?: LocationPrices;
}

export const useMarketPrices = ({
gameId,
}: {
gameId?: string;
}): MarketsInterface => {
const { data } = useMarketPricesQuery(
{ gameId: Number(gameId) },

{
enabled: !!gameId,
refetchInterval: REFETCH_INTERVAL,
},
);

const locationPrices = useMemo(() => {
return MarketPrices.create(data?.marketComponents?.edges as MarketEdge[]);
}, [data]);

return {
locationPrices,
};
};
21 changes: 8 additions & 13 deletions web/src/dojo/entities/useGameEntity.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Game, useGameEntityQuery } from "@/generated/graphql";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { ec, num } from "starknet";
import { REFETCH_INTERVAL } from "..";

Expand Down Expand Up @@ -50,8 +50,11 @@ export const useGameEntity = ({
}: {
gameId?: string;
}): GameInterface => {
const [game, setGame] = useState<GameEntity>();
const [key, setKey] = useState<string>("");
const key: string = useMemo(() => {
return num.toHex(
ec.starkCurve.poseidonHashMany([num.toBigInt(gameId || "")]),
);
}, [gameId]);

const { data, isFetched } = useGameEntityQuery(
{ id: key },
Expand All @@ -60,16 +63,8 @@ export const useGameEntity = ({
},
);

useEffect(() => {
if (gameId) {
const key_ = ec.starkCurve.poseidonHashMany([num.toBigInt(gameId)]);
setKey(num.toHex(key_));
}
}, [gameId]);

useEffect(() => {
const game_ = GameEntity.create(data as GameEntityData);
if (game_) setGame(game_);
const game = useMemo(() => {
return GameEntity.create(data as GameEntityData);
}, [data]);

return {
Expand Down
22 changes: 5 additions & 17 deletions web/src/dojo/entities/useLocationEntity.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import {
Name,
Market,
Risks,
useLocationEntitiesQuery,
Entity,
EntityEdge,
} from "@/generated/graphql";
import { useEffect, useState } from "react";
import { num, shortString } from "starknet";
import { useEffect, useMemo, useState } from "react";
import { num } from "starknet";
import { REFETCH_INTERVAL, SCALING_FACTOR } from "..";

export type DrugMarket = {
id: string; // id is hex encoded drug name
price: number;
marketPool: Market;
};
import { DrugMarket } from "../types";

export class LocationEntity {
id: string; // id is hex encoded location name
Expand Down Expand Up @@ -91,8 +84,6 @@ export const useLocationEntity = ({
gameId?: string;
locationId?: string;
}): LocationInterface => {
const [location, setLocation] = useState<LocationEntity>();

const { data, isFetched } = useLocationEntitiesQuery(
{
gameId: gameId || "",
Expand All @@ -104,11 +95,8 @@ export const useLocationEntity = ({
},
);

useEffect(() => {
const location_ = LocationEntity.create(
data?.entities?.edges as EntityEdge[],
);
if (location_) setLocation(location_);
const location = useMemo(() => {
return LocationEntity.create(data?.entities?.edges as EntityEdge[]);
}, [data]);

return {
Expand Down
8 changes: 3 additions & 5 deletions web/src/dojo/entities/usePlayerEntity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
usePlayerEntityQuery,
EntityEdge,
} from "@/generated/graphql";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { REFETCH_INTERVAL, SCALING_FACTOR } from "..";
import { PlayerStatus } from "../types";

Expand Down Expand Up @@ -80,7 +80,6 @@ export const usePlayerEntity = ({
gameId?: string;
address?: string;
}): PlayerInterface => {
const [player, setPlayer] = useState<PlayerEntity>();
// TODO: remove leading zeros in address, maybe implemented in torii
const { data, isFetched } = usePlayerEntityQuery(
{ gameId: gameId || "", playerId: address || "" },
Expand All @@ -90,9 +89,8 @@ export const usePlayerEntity = ({
},
);

useEffect(() => {
const player_ = PlayerEntity.create(data?.entities?.edges as EntityEdge[]);
if (player_) setPlayer(player_);
const player = useMemo(() => {
return PlayerEntity.create(data?.entities?.edges as EntityEdge[]);
}, [data]);

return {
Expand Down
31 changes: 0 additions & 31 deletions web/src/dojo/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,37 +156,6 @@ export const outcomes: OutcomeInfo[] = [
getMuggerResponses(Outcome.Escaped, isInitial),
color: "neon.200",
},

// {
// name: "Got Arrested",
// type: Outcome.Captured,
// status: PlayerStatus.BeingArrested,
// imageSrc: "/images/events/police_cruiser.gif",
// description: "You lost some health and drugs",
// getResponse: (isInitial: boolean) =>
// getCopResponses(Outcome.Captured, isInitial),
// color: "red",
// },
// {
// name: "Fought the Gang",
// type: Outcome.Fought,
// status: PlayerStatus.BeingMugged,
// imageSrc: "/images/events/fought.png",
// description: "You lost 20HP",
// getResponse: (isInitial: boolean) =>
// getMuggerResponses(Outcome.Fought, isInitial),
// color: "yellow.400",
// },
// {
// name: "Got Captured",
// type: Outcome.Captured,
// status: PlayerStatus.BeingMugged,
// imageSrc: "/images/sunset.png",
// description: "You some some health and cash",
// getResponse: (isInitial: boolean) =>
// getMuggerResponses(Outcome.Captured, isInitial),
// color: "red",
// },
];

function findBy<T>(array: T[], key: keyof T, value: any): T | undefined {
Expand Down
10 changes: 10 additions & 0 deletions web/src/dojo/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Market } from "@/generated/graphql";

export enum Location {
Queens,
Bronx,
Expand Down Expand Up @@ -59,3 +61,11 @@ export interface OutcomeInfo {
getResponse: (isInitial: boolean) => string;
color: string;
}

export type DrugMarket = {
id: string; // id is hex encoded drug name
price: number;
marketPool: Market;
};

export type LocationPrices = Map<string, DrugMarket[]>;
Loading

0 comments on commit dfabb83

Please sign in to comment.