Skip to content

Commit

Permalink
better admin
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed May 22, 2024
1 parent bad4ecf commit 10ae517
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 382 deletions.
4 changes: 0 additions & 4 deletions src/config/config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ mod config {
}
}

// fn update_game_config(self: @ContractState, game_config: GameConfig) {
// self.assert_caller_is_owner();
// self.s().save_game_config(game_config);
// }

fn update_drug_config(self: @ContractState, drug_config: DrugConfig) {
self.assert_caller_is_owner();
Expand Down
22 changes: 22 additions & 0 deletions src/config/ryo.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use starknet::info::get_block_timestamp;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use rollyourown::{models::{season::{Season, SeasonImpl, SeasonTrait}}};

const TWO_MIN: u16 = 120;
//
const TWENTY_MIN: u32 = 1200;
const ONE_HOUR: u32 = 3600;
const ONE_DAY: u32 = 86_400;
const ONE_WEEK: u32 = 604_800;

#[derive(Model, Copy, Drop, Serde)]
struct RyoConfig {
#[key]
Expand All @@ -23,6 +30,21 @@ struct RyoConfig {

#[generate_trait]
impl RyoConfigImpl of RyoConfigTrait {
fn build_initial_ryo_config() -> RyoConfig {
RyoConfig {
key: 0,
initialized: true,
paused: false,
season_version: 1,
season_duration: TWENTY_MIN, // ONE_WEEK
season_time_limit: TWO_MIN, // ONE_HOUR
paper_fee: 1000, // in ether
paper_reward_launderer: 100, // in ether
treasury_fee_pct: 5,
treasury_balance: 0,
}
}

fn build_season(self: RyoConfig, season_version: u16) -> Season {
Season {
version: season_version,
Expand Down
72 changes: 19 additions & 53 deletions src/systems/ryo.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use starknet::ContractAddress;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use rollyourown::{config::ryo::{RyoConfig}};

#[starknet::interface]
trait IRyo<T> {
//
Expand All @@ -11,9 +13,8 @@ trait IRyo<T> {
laundromat_address: ContractAddress
);
fn set_paused(self: @T, paused: bool);
fn set_paper_fee(self: @T, fee: u16);
fn set_treasury_fee_pct(self: @T, fee_pct: u8);
fn set_season_duration(self: @T, duration_sec: u32);
fn update_ryo_config(self: @T, ryo_config: RyoConfig);


//
fn paper(self: @T) -> ContractAddress;
Expand All @@ -35,20 +36,12 @@ mod ryo {

use rollyourown::{
interfaces::paper::{IPaperDispatcher, IPaperDispatcherTrait}, constants::{ETHER},
config::{ryo::{RyoConfig}, ryo_address::{RyoAddress},}, models::{season::Season,},
utils::random::{RandomImpl}, helpers::season_manager::{SeasonManager, SeasonManagerTrait},
config::{ryo::{RyoConfig, RyoConfigImpl}, ryo_address::{RyoAddress},},
models::{season::Season,}, utils::random::{RandomImpl},
helpers::season_manager::{SeasonManager, SeasonManagerTrait},
library::store::{IStoreLibraryDispatcher, IStoreDispatcherTrait},
};

const THREE_MIN: u16 = 120;
const TEN_MIN: u16 = 600;
const TWENTY_MIN: u16 = 1200;
//
const THIRTY_MIN: u32 = 1800;
const ONE_HOUR: u32 = 3600;
const TWO_HOUR: u32 = 7200;
const ONE_DAY: u32 = 86_400;
const ONE_WEEK: u32 = 604_800;

#[abi(embed_v0)]
impl RyoExternalImpl of super::IRyo<ContractState> {
Expand All @@ -64,30 +57,17 @@ mod ryo {

assert(ryo_config.initialized == false, 'Already initialized');

// consume first world uuid = 0
let _ = self.world().uuid();

// RyoConfig
ryo_config.initialized = true;
ryo_config.paused = false;

ryo_config.season_version = 1;
ryo_config.season_duration = ONE_HOUR; // ONE_WEEK
ryo_config.season_time_limit = TEN_MIN; // ONE_HOUR

ryo_config.paper_fee = 1000; // in ether
ryo_config.treasury_fee_pct = 5;
ryo_config.paper_reward_launderer = 100; // in ether

// initial config
ryo_config = RyoConfigImpl::build_initial_ryo_config();
// save
self.s().save_ryo_config(ryo_config);

// RyoAddresses
let mut ryo_addresses = self.s().ryo_addresses();

ryo_addresses.paper = paper_address;
ryo_addresses.treasury = treasury_address;
ryo_addresses.laundromat = laundromat_address;
ryo_addresses.treasury = treasury_address; // could be removed
ryo_addresses.laundromat = laundromat_address; // could be removed

// save
self.s().save_ryo_addresses(ryo_addresses);
Expand All @@ -99,7 +79,7 @@ mod ryo {
}

//
// TODO: test it
//
//

fn set_paused(self: @ContractState, paused: bool) {
Expand All @@ -111,31 +91,17 @@ mod ryo {
self.s().save_ryo_config(ryo_config);
}

fn set_paper_fee(self: @ContractState, fee: u16) {
self.assert_caller_is_owner();

let mut ryo_config = self.s().ryo_config();

ryo_config.paper_fee = fee;
self.s().save_ryo_config(ryo_config);
}


fn set_treasury_fee_pct(self: @ContractState, fee_pct: u8) {
fn update_ryo_config(self: @ContractState, ryo_config: RyoConfig) {
self.assert_caller_is_owner();

let mut ryo_config = self.s().ryo_config();
let mut new_ryo_config = self.s().ryo_config();

ryo_config.treasury_fee_pct = fee_pct;
self.s().save_ryo_config(ryo_config);
}

fn set_season_duration(self: @ContractState, duration_sec: u32) {
self.assert_caller_is_owner();

let mut ryo_config = self.s().ryo_config();
new_ryo_config.season_duration = ryo_config.season_duration;
new_ryo_config.season_time_limit = ryo_config.season_time_limit;
new_ryo_config.paper_fee = ryo_config.paper_fee;
new_ryo_config.paper_reward_launderer = ryo_config.paper_reward_launderer;
new_ryo_config.treasury_fee_pct = ryo_config.treasury_fee_pct;

ryo_config.season_duration = duration_sec;
self.s().save_ryo_config(ryo_config);
}

Expand Down
2 changes: 1 addition & 1 deletion web/src/components/pages/admin/DrugTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { shortString } from "starknet";
import { editComponents } from "./tables";

const columns = [
{ key: "id" },
// { key: "id" , title: "id", dataType: DataType.Number },
{ key: "icon", width: 80 },
{ key: "drugs_mode", title: "drugs_mode", dataType: DataType.String },
{ key: "drug", title: "drug", dataType: DataType.String },
Expand Down
21 changes: 11 additions & 10 deletions web/src/components/pages/admin/EncounterTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { useDojoContext, useSystems } from "@/dojo/hooks";
import { observer } from "mobx-react-lite";

import { Table, useTable } from "ka-table";
import { ActionType, DataType, SortingMode } from "ka-table/enums";
import { ActionType, DataType, FilteringMode, SortingMode } from "ka-table/enums";
import { useState } from "react";
import { editComponents } from "./tables";

const columns = [
// { key: "image", width: 100 },
{ key: "encounters_mode", title: "mode", dataType: DataType.String },
{ key: "encounter", title: "encounter", dataType: DataType.String },
{ key: "encounters_mode", title: "encounters_mode", dataType: DataType.String },

{ key: "health_base", title: "health_base", dataType: DataType.Number },
{ key: "health_step", title: "health_step", dataType: DataType.Number },
{ key: "attack_base", title: "attack_base", dataType: DataType.Number },
{ key: "attack_step", title: "attack_step", dataType: DataType.Number },
{ key: "defense_base", title: "defense_base", dataType: DataType.Number },
{ key: "defense_step", title: "defense_step", dataType: DataType.Number },
{ key: "speed_base", title: "speed_base", dataType: DataType.Number },
{ key: "speed_step", title: "speed_step", dataType: DataType.Number },
{ key: "health_base", title: "HP base", dataType: DataType.Number },
{ key: "health_step", title: "HP step", dataType: DataType.Number },
{ key: "attack_base", title: "ATK base", dataType: DataType.Number },
{ key: "attack_step", title: "ATK step", dataType: DataType.Number },
{ key: "defense_base", title: "DEF base", dataType: DataType.Number },
{ key: "defense_step", title: "DEF step", dataType: DataType.Number },
{ key: "speed_base", title: "SPD base", dataType: DataType.Number },
{ key: "speed_step", title: "SPD step", dataType: DataType.Number },

{ key: "editColumn", width: 80 },
];
Expand Down Expand Up @@ -70,6 +70,7 @@ export const EncounterTable = observer(() => {
data={data}
rowKeyField={"id"}
sortingMode={SortingMode.Single}
filteringMode={FilteringMode.HeaderFilter}
childComponents={editComponents}
/>
);
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/pages/admin/HustlerItemBaseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useDojoContext } from "@/dojo/hooks";
import { observer } from "mobx-react-lite";

import { Table } from "ka-table";
import { DataType, EditingMode } from "ka-table/enums";
import { DataType, EditingMode, FilteringMode } from "ka-table/enums";
import { editComponents } from "./tables";

const columns = [
Expand All @@ -26,8 +26,8 @@ export const HustlerItemBaseTable = observer(() => {
data={config?.items || []}
rowKeyField={"name"}
editingMode={EditingMode.None}
filteringMode={FilteringMode.HeaderFilter}
childComponents={editComponents}
/>
);
});

3 changes: 2 additions & 1 deletion web/src/components/pages/admin/HustlerItemTiersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useDojoContext } from "@/dojo/hooks";
import { observer } from "mobx-react-lite";

import { Table } from "ka-table";
import { DataType, EditingMode, SortingMode } from "ka-table/enums";
import { DataType, EditingMode, FilteringMode, SortingMode } from "ka-table/enums";

const columns = [
{ key: "slot", title: "slot", dataType: DataType.String },
Expand All @@ -25,6 +25,7 @@ export const HustlerItemTiersTable = observer(() => {
rowKeyField={"id"}
editingMode={EditingMode.None}
sortingMode={SortingMode.Single}
filteringMode={FilteringMode.HeaderFilter}
/>
);
});
47 changes: 27 additions & 20 deletions web/src/components/pages/home/Leaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Arrow, InfosIcon, PaperIcon, Skull } from "../../icons";
import { SeasonDetailsModal } from "./SeasonDetailsModal";
import { Game } from "@/generated/graphql";
import { shortString } from "starknet";
import { Config, ConfigStoreClass } from "@/dojo/stores/config";

const renderer = ({
days,
Expand Down Expand Up @@ -54,24 +55,30 @@ const renderer = ({
}
};

export const Leaderboard = observer(({ nameEntry, ...props }: { nameEntry?: boolean } & StyleProps & ListProps) => {
export const Leaderboard = observer(({ config }: { config: Config }) => {
const { router, gameId } = useRouterContext();

const { uiStore } = useDojoContext();
const { account } = useAccount();
const { config } = useConfigStore();

const [currentVersion, setCurrentVersion] = useState(config?.ryo.season_version || 0);
const [selectedVersion, setSelectedVersion] = useState(config?.ryo.season_version || 0);
const [selectedVersion, setSelectedVersion] = useState(config?.ryo.season_version || 0);

const { season }= useSeasonByVersion(selectedVersion)
const { season } = useSeasonByVersion(selectedVersion);

const {
registeredGames,
isFetching: isFetchingRegisteredGames,
refetch: refetchRegisteredGames,
} = useRegisteredGamesBySeason(selectedVersion);

useEffect(() => {
if (!config) return;

setCurrentVersion(config?.ryo.season_version || 0);
refetchRegisteredGames();
}, [config]);

const onPrev = async () => {
if (selectedVersion > 1) {
setSelectedVersion(selectedVersion - 1);
Expand All @@ -88,15 +95,20 @@ export const Leaderboard = observer(({ nameEntry, ...props }: { nameEntry?: bool
router.push(`/season/${version}`);
};

if (!registeredGames || !season ) {
if (!registeredGames || !season) {
return <></>;
}

return (
<VStack w="full" h="100%">
<VStack my="15px" w="full">
<HStack w="full" justifyContent="space-between">
<Arrow direction="left" cursor="pointer" opacity={selectedVersion > 1 ? "1" : "0.25"} onClick={onPrev}></Arrow>
<Arrow
direction="left"
cursor="pointer"
opacity={selectedVersion > 1 ? "1" : "0.25"}
onClick={onPrev}
></Arrow>
<HStack textStyle="subheading" fontSize="12px" w="full" justifyContent="center" position="relative">
<Text cursor="pointer" onClick={() => onDetails(selectedVersion)}>
SEASON {selectedVersion} REWARDS
Expand All @@ -115,10 +127,7 @@ export const Leaderboard = observer(({ nameEntry, ...props }: { nameEntry?: bool
</HStack>
{selectedVersion === currentVersion && (
<HStack>
<Countdown
date={new Date(season.next_version_timestamp * 1_000)}
renderer={renderer}
></Countdown>
<Countdown date={new Date(season.next_version_timestamp * 1_000)} renderer={renderer}></Countdown>
<Box cursor="pointer" onClick={() => uiStore.openSeasonDetails()}>
<InfosIcon />
</Box>
Expand All @@ -144,7 +153,9 @@ export const Leaderboard = observer(({ nameEntry, ...props }: { nameEntry?: bool
const isOwn = game.player_id === account?.address;
const color = isOwn ? colors.yellow["400"].toString() : colors.neon["200"].toString();
const avatarColor = isOwn ? "yellow" : "green";
const displayName = game.player_name ? `${shortString.decodeShortString( game.player_name)}${isOwn ? " (you)" : ""}` : "Anonymous";
const displayName = game.player_name
? `${shortString.decodeShortString(game.player_name)}${isOwn ? " (you)" : ""}`
: "Anonymous";

return (
<ListItem color={color} key={game.game_id} cursor="pointer">
Expand All @@ -167,7 +178,7 @@ export const Leaderboard = observer(({ nameEntry, ...props }: { nameEntry?: bool
{/* {game.health === 0 ? (
<Skull color={avatarColor} hasCrown={index === 0} />
) : ( */}
<HustlerIcon hustler={game.hustler_id as Hustlers} color={color} />
<HustlerIcon hustler={game.hustler_id as Hustlers} color={color} />
{/* )} */}
</Box>

Expand All @@ -194,19 +205,15 @@ export const Leaderboard = observer(({ nameEntry, ...props }: { nameEntry?: bool
{"."}
</Text>

{
game.claimable > 0 && (
<Text flexShrink={0} fontSize={["12px", "16px"]}>
<PaperIcon /> {game.claimable} ...
{game.claimable > 0 && (
<Text flexShrink={0} fontSize={["12px", "16px"]}>
<PaperIcon /> {game.claimable} ...
</Text>
)
}
)}

<Text flexShrink={0} fontSize={["12px", "16px"]}>
{formatCash(game.final_score)}
</Text>


</HStack>
</ListItem>
);
Expand Down
Loading

0 comments on commit 10ae517

Please sign in to comment.