Skip to content

Commit 65d9934

Browse files
committed
Add used by section to Used by
1 parent de2a103 commit 65d9934

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/commands/misc/material.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { AutocompleteInteraction, CommandInteraction, Message, MessageEmbed } fr
22
import config from "../../data/config.json"
33
import client from "../../main"
44
import Command from "../../utils/Command"
5-
import { CommandSource, Material, SendMessage } from "../../utils/Types"
6-
import { Bookmarkable, Colors, findFuzzyBestCandidates, getLinkToGuide, paginator, sendMessage, simplePaginator, urlify } from "../../utils/Utils"
5+
import { Character, CommandSource, Material, SendMessage, Weapon } from "../../utils/Types"
6+
import { Bookmarkable, Colors, findFuzzyBestCandidates, getLinkToGuide, joinMulti, paginator, sendMessage, simplePaginator, urlify } from "../../utils/Utils"
77

88
export default class MaterialCommand extends Command {
99
constructor(name: string) {
@@ -142,6 +142,55 @@ Note: this command supports fuzzy search.`,
142142
if (material.sources)
143143
embed.addField("Sources", material.sources.join("\n"))
144144

145+
const charAscension: Character[] = []
146+
const charTalents: Character[] = []
147+
148+
for (const c of data.getCharacters()) {
149+
if (c.ascensionCosts && data.isInCosts(c.ascensionCosts, material.name))
150+
charAscension.push(c)
151+
152+
if (c.skills) {
153+
const talents = c.skills.flatMap(s => [...(s.talents ?? []), s.ult])
154+
155+
if (talents.some(x => x?.costs && data.isInCosts(x.costs, material.name)))
156+
charTalents.push(c)
157+
}
158+
}
159+
160+
const weaponAscension: Weapon[] = []
161+
for (const w of Object.values(data.weapons)) {
162+
if (w.ascensionCosts && data.isInCosts(w.ascensionCosts, material.name))
163+
weaponAscension.push(w)
164+
}
165+
166+
const usedByDesc = []
167+
168+
const overlap = charTalents.filter(x => charAscension.some(y => x.name == y.name))
169+
const uniqueTalents = charTalents.filter(x => !charAscension.some(y => x.name == y.name))
170+
const uniqueAscension = charAscension.filter(x => !charTalents.some(y => x.name == y.name))
171+
172+
if (overlap.length > 0)
173+
usedByDesc.push(`Used by ${joinMulti(overlap.map(x => data.emoji(x.name)).filter((v, i, a) => a.indexOf(v) == i))} character talents and ascensions.`)
174+
175+
if (uniqueTalents.length > 0)
176+
usedByDesc.push(`Used by ${joinMulti(uniqueTalents.map(x => data.emoji(x.name)))} character talents.`)
177+
178+
if (uniqueAscension.length > 0)
179+
usedByDesc.push(`Used by ${joinMulti(uniqueAscension.map(x => data.emoji(x.name)))} character ascensions.`)
180+
181+
const sortedWeapon = weaponAscension
182+
.sort((a, b) => (a.stars && b.stars && b.stars - a.stars) || (a.weaponType && b.weaponType && a.weaponType.localeCompare(b.weaponType)) || a.name.localeCompare(b.name))
183+
if (sortedWeapon.length > 0 && sortedWeapon.length < 7)
184+
usedByDesc.push(`Used by ${joinMulti(sortedWeapon.map(x => data.emoji(x.name)))} weapon ascensions.`)
185+
else if (sortedWeapon.length > 0 && sortedWeapon.filter(x => x.stars >= 4).length < 7)
186+
usedByDesc.push(`Used by ${joinMulti([...sortedWeapon.filter(x => x.stars >= 4).map(x => data.emoji(x.name)), "more low star"])} weapon ascensions.`)
187+
else if (sortedWeapon.length > 0)
188+
usedByDesc.push(`Used by ${joinMulti([...sortedWeapon.slice(0, 7).map(x => data.emoji(x.name)), "more"])} weapon ascensions.`)
189+
190+
191+
if (usedByDesc.length > 0)
192+
embed.addField("Used by", usedByDesc.join("\n"))
193+
145194
if (material.icon)
146195
embed.setThumbnail(`${data.baseURL}${material.icon}`)
147196

src/utils/DataManager.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,14 @@ export default class DataManager {
311311

312312
return items.map(i => `**${i.count}**x *${this.emoji(i.name, true)}*`).join("\n")
313313
}
314+
315+
isInCosts(template: CostTemplate | Cost[], name: string): boolean {
316+
const costs = Array.isArray(template) ? template : this.getCostsFromTemplate(template)
317+
318+
for (const c of costs)
319+
if (c.items.some(i => i.name == name))
320+
return true
321+
322+
return false
323+
}
314324
}

src/utils/Utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ export function truncate(text: string, maxLength = 50): string {
100100
return `${words.slice(0, end).join(" ")}${end < words.length ? "..." : ""}`
101101
}
102102

103+
export function joinMulti(input: string[]): string {
104+
if (input.length <= 1) return input[0]
105+
106+
const last = input[input.length - 1]
107+
return `${input.slice(0, -1).join(", ")} and ${last}`
108+
}
109+
110+
103111
// Get time information
104112
const offsets: {[server in Server]: number} = {
105113
Asia: +8,

0 commit comments

Comments
 (0)