Skip to content

Commit

Permalink
send selected stance with combat data
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisinajar committed Jul 30, 2024
1 parent 5ff28a7 commit 6cdf622
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/components/combat/attack-hero.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mutation AttackHero($id: ID!, $attackType: AttackType) {
attackHero(id: $id, attackType: $attackType) {
mutation AttackHero($id: ID!, $attackType: AttackType, $stance: HeroStance) {
attackHero(id: $id, attackType: $attackType, stance: $stance) {
account {
id
nextAllowedAction
Expand Down
13 changes: 13 additions & 0 deletions src/components/combat/combat-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import {
useAttackHeroMutation,
EnchantmentType,
InventoryItem,
HeroStance,
Hero,
} from "src/generated/graphql";

import { itemDisplayName } from "src/helpers";

import { StanceSelector } from "./stance-selector";

type CombatDisplayProps = {
autoBattle: boolean;
canAutoBattle: boolean;
Expand All @@ -45,6 +49,7 @@ type CombatDisplayProps = {
onError?: (e: any) => void;
onAutoBattle?: (monsterId: string, attackType: AttackType) => void;
fightMutationRef?: React.MutableRefObject<(attackType: AttackType) => void>;
hero: Hero;
};

export function CombatDisplay(props: CombatDisplayProps): JSX.Element | null {
Expand All @@ -57,6 +62,7 @@ export function CombatDisplay(props: CombatDisplayProps): JSX.Element | null {
onAutoBattle,
fightMutationRef,
duel,
hero,
} = props;
const [killedByAnother, setKilledByAnother] = useState<boolean>(false);
const [fightMutation, { data: fightData, loading: fightLoading }] =
Expand All @@ -79,6 +85,10 @@ export function CombatDisplay(props: CombatDisplayProps): JSX.Element | null {
null;
const theme = useTheme();

const [desiredStance, setDesiredStance] = useState<HeroStance>(
hero.activeStance || HeroStance.Normal,
);

const showAutoBattle = !autoBattle && canAutoBattle;

useEffect(() => {
Expand Down Expand Up @@ -110,6 +120,7 @@ export function CombatDisplay(props: CombatDisplayProps): JSX.Element | null {
variables: {
id: monsterId,
attackType,
stance: desiredStance,
},
});
if (data.data?.attackHero.victory) {
Expand All @@ -122,6 +133,7 @@ export function CombatDisplay(props: CombatDisplayProps): JSX.Element | null {
variables: {
monster: monsterId,
attackType,
stance: desiredStance,
},
});
if (data.data?.fight.victory) {
Expand Down Expand Up @@ -162,6 +174,7 @@ export function CombatDisplay(props: CombatDisplayProps): JSX.Element | null {

return (
<React.Fragment>
<StanceSelector onChange={setDesiredStance} />
<Grid
id="combat-display"
sx={{
Expand Down
4 changes: 2 additions & 2 deletions src/components/combat/fight.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mutation Fight($monster: ID!, $attackType: AttackType) {
fight(monster: $monster, attackType: $attackType) {
mutation Fight($monster: ID!, $attackType: AttackType, $stance: HeroStance) {
fight(monster: $monster, attackType: $attackType, stance: $stance) {
victory
experience
gold
Expand Down
20 changes: 11 additions & 9 deletions src/components/combat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import {
MonsterInstance,
AttackType,
PublicHero,
HeroStance,
} from "src/generated/graphql";

import { useHero } from "src/hooks/use-hero";
import { useDelay } from "src/hooks/use-delay";
import { useLocation } from "src/hooks/use-location";

import { CombatDisplay } from "./combat-display";
import { StanceSelector } from "./stance-selector";
import { itemAllowsAutoBattle, itemImprovesAutoBattle } from "src/helpers";

type PartialMonsterInstance = Pick<MonsterInstance, "monster" | "id">;
Expand All @@ -41,7 +41,7 @@ declare global {
}
}

export function Combat(): JSX.Element {
export function Combat(): JSX.Element | null {
const [autoBattleAttackType, setAutoBattleAttackType] = useState<AttackType>(
AttackType.Melee,
);
Expand Down Expand Up @@ -241,11 +241,13 @@ export function Combat(): JSX.Element {
duelPlayer = "";
}

if (hero) {
const showAll = hero.levelCap > 100;
if (!showAll) {
challenges = challenges.slice(0, hero.level);
}
if (!hero) {
return null;
}

const showAll = hero.levelCap > 100;
if (!showAll) {
challenges = challenges.slice(0, hero.level);
}

playerList = playerList.filter((p) => p.combat.health > 0);
Expand Down Expand Up @@ -419,9 +421,9 @@ export function Combat(): JSX.Element {
)}
{fightingMonster && (
<React.Fragment>
<StanceSelector />
<CombatDisplay
key={`${fightingMonster.id}-${currentFightId}`}
hero={hero}
fight={fightingMonster}
onError={() => {
refetch();
Expand All @@ -437,9 +439,9 @@ export function Combat(): JSX.Element {
)}
{activeDuelPlayer && (
<React.Fragment>
<StanceSelector />
<CombatDisplay
key={`${activeDuelPlayer.id}-${currentFightId}`}
hero={hero}
fight={{
id: activeDuelPlayer.id,
monster: activeDuelPlayer,
Expand Down
17 changes: 12 additions & 5 deletions src/components/combat/stance-selector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useState } from "react";

import Button from "@mui/material/Button";
import Grid from "@mui/material/Grid";
Expand Down Expand Up @@ -33,8 +33,13 @@ const stanceDescriptions: { [x in HeroStance]: string } = {
[HeroStance.Focus]: "???",
};

export function StanceSelector(): JSX.Element | null {
export function StanceSelector(props: {
onChange?: (stance: HeroStance) => void;
}): JSX.Element | null {
const hero = useHero();
const [selectedStance, setSelectedStance] = useState(
hero?.activeStance || HeroStance.Normal,
);

if (!hero) {
return null;
Expand All @@ -46,6 +51,10 @@ export function StanceSelector(): JSX.Element | null {

function handleChangeStance(stance: HeroStance) {
console.log("Setting stance to", stance, hero);
setSelectedStance(stance);
if (props.onChange) {
props.onChange(stance);
}
}

// do not show selector if there aren't enough stances
Expand All @@ -66,9 +75,7 @@ export function StanceSelector(): JSX.Element | null {
<Grid item lg={1} md={2} sm={3} xs={6} key={stance.name}>
<Tooltip title={stance.tooltip}>
<Button
variant={
hero.activeStance === stance.name ? "outlined" : "text"
}
variant={selectedStance === stance.name ? "outlined" : "text"}
sx={{ fontSize: "1rem", padding: 2 }}
size="large"
id={`set-stance-${stance.name.toLowerCase()}`}
Expand Down

0 comments on commit 6cdf622

Please sign in to comment.