Skip to content

Commit

Permalink
feat: add weapon filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tractorcow committed Feb 29, 2024
1 parent 8f34218 commit b32abca
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/components/TeamGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { useState } from 'react'
import { Character } from 'genshin-db'
import { filterNamed, findTeamType, healers } from '../lib/queries'
import teamTypes from '../lib/teamTypes'
import { Gender } from '../types/teams'
import { Gender, WeaponType, WeaponMap } from '../types/teams'
import { OptionBuilder } from '../lib/builders/OptionBuilder'
import classnames from 'classnames'

Expand All @@ -26,13 +26,19 @@ const TeamGenerator = ({
const [teamType, setTeamType] = useState('')
const [gender, setGender] = useState<Gender | undefined>(undefined)
const [includeHealer, setIncludeHealer] = useState(false)
const [weapon, setWeapon] = useState<WeaponType | undefined>(undefined)
const selectedType = findTeamType(teamType, teamTypes)

const healerTooltip = `Note: Healers list includes ${healers.join(', ')}`

// Trigger option builder
const generateTeam = () => {
const builder = new OptionBuilder(selectedType, gender, includeHealer)
const builder = new OptionBuilder(
selectedType,
gender,
includeHealer,
weapon
)
const team = builder.build(characters)
if (!team) {
alert("Sorry, I couldn't find enough members to make that kind of team")
Expand Down Expand Up @@ -112,6 +118,22 @@ const TeamGenerator = ({
)}
</div>

<div className="mb-4">
<label className="block text-gray-700 text-sm font-bold mb-2">
Weapon Type
</label>
<select
className="w-full p-2"
value={weapon}
onChange={(e) => setWeapon(e.target.value as WeaponType)}
>
<option value="">Any weapon</option>
{WeaponMap.map((type) => (
<option value={type.type}>{type.label}</option>
))}
</select>
</div>

<div className="mb-4">
<span className="text-gray-700 text-sm font-bold">Genders</span>
<div className="mt-2">
Expand Down
11 changes: 9 additions & 2 deletions src/lib/builders/OptionBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Builder, Filter } from '../../types/selector'
import { Character } from 'genshin-db'
import { CompleteTeam, Gender, TeamType } from '../../types/teams'
import { CompleteTeam, Gender, TeamType, WeaponType } from '../../types/teams'
import { RandomBuilder } from './RandomBuilder'
import { GenderFilter } from '../filters/GenderFilter'
import { TeamTypeFilter } from '../filters/TeamTypeFilter'
import { TeamTypeBuilder } from './TeamTypeBuilder'
import { HealingBuilder } from './HealingBuilder'
import { WeaponFilter } from '../filters/WeaponFilter'

/**
* Generate team based on selected options
Expand All @@ -14,15 +15,18 @@ export class OptionBuilder implements Builder {
selectedType: TeamType | undefined
gender: Gender | undefined
includesHealer: boolean
weapon: WeaponType | undefined

constructor(
selectedType: TeamType | undefined,
gender: Gender | undefined,
includeHeader: boolean
includeHeader: boolean,
weapon: WeaponType | undefined
) {
this.selectedType = selectedType
this.gender = gender
this.includesHealer = includeHeader
this.weapon = weapon
}

/**
Expand All @@ -36,6 +40,9 @@ export class OptionBuilder implements Builder {
if (this.selectedType) {
filters.push(new TeamTypeFilter(this.selectedType))
}
if (this.weapon) {
filters.push(new WeaponFilter(this.weapon))
}
return filters
}

Expand Down
20 changes: 20 additions & 0 deletions src/lib/filters/WeaponFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Filter } from '../../types/selector'
import { Character } from 'genshin-db'
import { WeaponType } from '../../types/teams'

/**
* Filters characters by a specific weapon
*/
export class WeaponFilter implements Filter {
weapon: WeaponType

constructor(weapon: WeaponType) {
this.weapon = weapon
}

filter(characters: Array<Character>): Array<Character> {
return characters.filter(
(character) => character.weaponType === this.weapon
)
}
}
8 changes: 8 additions & 0 deletions src/types/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export enum Gender {
Female = 'Female',
}

export const WeaponMap: Array<{ type: WeaponType; label: string }> = [
{ type: 'WEAPON_BOW', label: 'Bow' },
{ type: 'WEAPON_CATALYST', label: 'Catalyst' },
{ type: 'WEAPON_CLAYMORE', label: 'Claymore' },
{ type: 'WEAPON_POLE', label: 'Polearm' },
{ type: 'WEAPON_SWORD_ONE_HAND', label: 'Sword' },
]

// Restriction rules for a team member. Can be empty if slot is open to all.
export type TeamMemberType = {
// For specific named character
Expand Down

0 comments on commit b32abca

Please sign in to comment.