-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue #336: Mirror #340
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1546,6 +1546,58 @@ export const summonPrevent = ( | |||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
/** Prevent target from being stunned */ | ||||||||||||||||||||||||||
/** Mirror negative effects from caster to target */ | ||||||||||||||||||||||||||
export const mirror = ( | ||||||||||||||||||||||||||
effect: UserEffect, | ||||||||||||||||||||||||||
usersEffects: UserEffect[], | ||||||||||||||||||||||||||
target: BattleUserState, | ||||||||||||||||||||||||||
) => { | ||||||||||||||||||||||||||
if (!effect.isNew && !effect.castThisRound) { | ||||||||||||||||||||||||||
// Find all negative effects on the caster | ||||||||||||||||||||||||||
const negativeEffects = usersEffects.filter( | ||||||||||||||||||||||||||
(e) => e.targetId === effect.creatorId && isNegativeUserEffect(e), | ||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Apply each negative effect to the target | ||||||||||||||||||||||||||
negativeEffects.forEach((negEffect) => { | ||||||||||||||||||||||||||
// Create a new effect with the same properties but different target | ||||||||||||||||||||||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access | ||||||||||||||||||||||||||
const mirroredEffect = { | ||||||||||||||||||||||||||
type: negEffect.type, | ||||||||||||||||||||||||||
id: nanoid(), | ||||||||||||||||||||||||||
targetId: effect.targetId, | ||||||||||||||||||||||||||
creatorId: effect.creatorId, | ||||||||||||||||||||||||||
isNew: true, | ||||||||||||||||||||||||||
castThisRound: true, | ||||||||||||||||||||||||||
createdRound: effect.createdRound, | ||||||||||||||||||||||||||
power: negEffect.power ?? 0, | ||||||||||||||||||||||||||
powerPerLevel: negEffect.powerPerLevel ?? 0, | ||||||||||||||||||||||||||
rounds: negEffect.rounds ?? 0, | ||||||||||||||||||||||||||
direction: negEffect.direction ?? "offence", | ||||||||||||||||||||||||||
calculation: negEffect.calculation ?? "static", | ||||||||||||||||||||||||||
description: negEffect.description ?? "", | ||||||||||||||||||||||||||
target: negEffect.target ?? "INHERIT", | ||||||||||||||||||||||||||
friendlyFire: negEffect.friendlyFire, | ||||||||||||||||||||||||||
staticAssetPath: negEffect.staticAssetPath ?? "", | ||||||||||||||||||||||||||
staticAnimation: negEffect.staticAnimation ?? "", | ||||||||||||||||||||||||||
appearAnimation: negEffect.appearAnimation ?? "", | ||||||||||||||||||||||||||
disappearAnimation: negEffect.disappearAnimation ?? "", | ||||||||||||||||||||||||||
timeTracker: negEffect.timeTracker, | ||||||||||||||||||||||||||
} as const; | ||||||||||||||||||||||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access | ||||||||||||||||||||||||||
const newEffect = structuredClone(mirroredEffect) as unknown as UserEffect; | ||||||||||||||||||||||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||||||||||||||||||||||||||
usersEffects.push(newEffect); | ||||||||||||||||||||||||||
Comment on lines
+1588
to
+1590
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Optimize memory usage and improve type safety. The code uses - const newEffect = structuredClone(mirroredEffect) as unknown as UserEffect;
- usersEffects.push(newEffect);
+ const newEffect = { ...mirroredEffect } as UserEffect;
+ try {
+ usersEffects.push(newEffect);
+ } catch (error) {
+ console.error('Failed to add mirrored effect:', error);
+ return {
+ txt: `Failed to mirror effects: ${error.message}`,
+ color: "red",
+ };
+ }
|
||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return getInfo( | ||||||||||||||||||||||||||
target, | ||||||||||||||||||||||||||
effect, | ||||||||||||||||||||||||||
`all negative effects on ${target.username} are mirrored to their opponent`, | ||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||
Comment on lines
+1594
to
+1598
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for edge cases. The function should handle potential edge cases:
return getInfo(
target,
effect,
- `all negative effects on ${target.username} are mirrored to their opponent`,
+ negativeEffects.length > 0
+ ? `all negative effects on ${target.username} are mirrored to their opponent`
+ : `no negative effects found to mirror`,
); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export const weakness = (effect: UserEffect, target: BattleUserState) => { | ||||||||||||||||||||||||||
const { power } = getPower(effect); | ||||||||||||||||||||||||||
const mainCheck = Math.random() < power / 100; | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add validation for effect.userId.
The function assumes
effect.userId
exists when filtering negative effects, but it's not validated. This could lead to runtime errors if the effect doesn't have a userId.