-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathbundles.tsx
More file actions
684 lines (643 loc) · 24 KB
/
bundles.tsx
File metadata and controls
684 lines (643 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
import Head from "next/head";
import achievements from "@/data/achievements.json";
import bundlesData from "@/data/bundles.json";
import {
Bundle,
BundleItem,
BundleItemWithLocation,
BundleItemWithOptions,
BundleWithStatus,
BundleWithStatusAndOptions,
CommunityCenter,
CommunityCenterRoomName,
Randomizer,
isRandomizer,
} from "@/types/bundles";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { PlayerType, usePlayers } from "@/contexts/players-context";
import { usePreferences } from "@/contexts/preferences-context";
import { AchievementCard } from "@/components/cards/achievement-card";
import { BundleItemCard } from "@/components/cards/bundle-item-card";
import { UnblurDialog } from "@/components/dialogs/unblur-dialog";
import BundleSheet from "@/components/sheets/bundle-sheet";
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
AccordionTriggerNoToggle,
} from "@/components/ui/accordion";
import { Progress } from "@/components/ui/progress";
import { useMediaQuery } from "@react-hook/media-query";
import { IconClock, IconCloud, IconSettings } from '@tabler/icons-react';
import clsx from "clsx";
import { useEffect, useState } from "react";
import { isNumber } from "util";
import { FilterButton, FilterSearch } from '@/components/filter-btn';
import { Command, CommandInput } from '@/components/ui/command';
export const ItemQualityToString = {
"0": "Normal",
"1": "Silver",
"2": "Gold",
"3": "Iridium",
};
type BundleAccordionProps = {
bundleWithStatus: BundleWithStatus;
children: JSX.Element | JSX.Element[];
alternateOptions?: Bundle[];
onChangeBundle?: (bundle: Bundle, bundleWithStatus: BundleWithStatus) => void;
};
type AccordionSectionProps = {
title: string;
children: JSX.Element | JSX.Element[];
completedCount?: number;
};
const CommunityCenterRooms: CommunityCenterRoomName[] = [
"Pantry",
"Crafts Room",
"Fish Tank",
"Boiler Room",
"Vault",
"Bulletin Board",
"Abandoned Joja Mart",
];
function AccordionSection(props: AccordionSectionProps): JSX.Element {
const { activePlayer } = usePlayers();
let progressIndicator =
activePlayer &&
typeof props.completedCount === "number" &&
Array.isArray(props.children) &&
props.completedCount < props.children.length ? (
<Progress
value={props.completedCount}
max={props.children.length}
className="w-32"
/>
) : (
``
);
return (
<Accordion type="single" collapsible defaultValue="item-1" asChild>
<section className="space-y-3">
<AccordionItem value="item-1">
<AccordionTrigger
className="ml-1 pt-0 text-xl font-semibold text-gray-900 dark:text-white"
pullRight={progressIndicator}
>
{props.title}
</AccordionTrigger>
<AccordionContent asChild>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
{props.children}
</div>
</AccordionContent>
</AccordionItem>
</section>
</Accordion>
);
}
function BundleAccordion(props: BundleAccordionProps): JSX.Element {
const isDesktop = useMediaQuery("only screen and (min-width: 768px)");
const { bundle, bundleStatus } = props.bundleWithStatus;
const totalItems = bundle.items.length;
const requiredItems =
bundle.itemsRequired === -1 ? totalItems : bundle.itemsRequired;
const completedItems = bundleStatus.filter(Boolean).length;
const remainingCount = requiredItems - completedItems;
const bundleCompleted = completedItems >= requiredItems;
const bundleName = props.bundleWithStatus.bundle.localizedName;
const [selectedBundleName, setSelectedBundleName] = useState(
props.bundleWithStatus.bundle.name,
);
return (
<Accordion type="single" collapsible defaultValue="item-1" asChild>
<section
className={clsx(
"relative h-min select-none justify-between space-y-3 rounded-lg border px-5 pt-4 text-neutral-950 shadow-sm hover:cursor-pointer dark:text-neutral-50",
bundleCompleted
? "border-green-900 bg-green-500/20 hover:bg-green-500/30 dark:bg-green-500/10 hover:dark:bg-green-500/20"
: "border-neutral-200 dark:border-neutral-800",
)}
>
<AccordionItem value="item-1" className="border-none">
<AccordionTriggerNoToggle
className={`ml-1 pt-0 text-xl font-semibold text-gray-900 dark:text-white ${isDesktop ? "flex-row" : "flex-col items-start"}`}
>
<div>
<div className="flex items-center gap-3">
<span>{bundleName} Bundle</span>
{props.alternateOptions &&
props.alternateOptions.length > 0 && (
<DropdownMenu>
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<DropdownMenuTrigger asChild>
<IconSettings
size={16}
className="relative top-0.5 text-neutral-500 dark:text-neutral-400"
/>
</DropdownMenuTrigger>
</TooltipTrigger>
<TooltipContent>
<p>Change Bundle</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
<DropdownMenuContent className="w-56">
<DropdownMenuLabel>Remix Bundles</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuRadioGroup
value={selectedBundleName}
onValueChange={(newBundleName) => {
setSelectedBundleName(newBundleName);
const selectedBundle = props.alternateOptions?.find(
(bundle) => bundle.name === newBundleName,
);
if (props.onChangeBundle && selectedBundle) {
props.onChangeBundle(
selectedBundle,
props.bundleWithStatus,
);
}
}}
>
{props.alternateOptions.map((newBundle) => (
<DropdownMenuRadioItem
key={newBundle.name}
value={newBundle.name}
>
{newBundle.localizedName}
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
)}
</div>
</div>
{!bundleCompleted && (
<div className={`flex items-center ${isDesktop ? "" : "pt-2"}`}>
<Progress
value={completedItems}
max={requiredItems}
className="w-32"
/>
</div>
)}
</AccordionTriggerNoToggle>
<AccordionContent asChild>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
{props.children}
</div>
</AccordionContent>
</AccordionItem>
</section>
</Accordion>
);
}
function BundleCompleted(bundleWithStatus: BundleWithStatus): boolean {
if (!bundleWithStatus.bundleStatus) {
return false;
}
if (bundleWithStatus.bundle.itemsRequired === -1) {
// Gold bundles are encoded in the save file as requiring -1 items
return (
bundleWithStatus.bundleStatus.reduce((acc, cur) => {
return cur ? acc + 1 : acc;
}, 0) >= bundleWithStatus.bundle.items.length
);
}
return (
bundleWithStatus.bundleStatus.reduce((acc, cur) => {
return cur ? acc + 1 : acc;
}, 0) >= bundleWithStatus.bundle.itemsRequired
);
}
function ResolveBundleRandomizer(bundleRandomizer: Randomizer): Bundle[] {
let selectionCount = bundleRandomizer.selectionCount;
let relevantBundles = bundleRandomizer.options.slice(
0,
selectionCount,
) as Bundle[];
let resolvedBundles: Bundle[] = [];
relevantBundles.forEach((bundle) => {
resolvedBundles.push(ResolveItemRandomizers(bundle));
});
return resolvedBundles;
}
function ResolveItemRandomizers(bundle: Bundle): Bundle {
let finishedBundle = {
...bundle,
};
if (isRandomizer(bundle.items)) {
let selectionCount = bundle.items.selectionCount;
let relevantItems = bundle.items.options.slice(
0,
selectionCount,
) as BundleItem[];
finishedBundle.items = relevantItems;
} else {
let items: BundleItem[] = [];
bundle.items.forEach((item) => {
if (isRandomizer(item)) {
let selectionCount = item.selectionCount;
let relevantItems = item.options.slice(
0,
selectionCount,
) as BundleItem[];
items = items.concat(relevantItems);
} else {
items.push(item);
}
});
finishedBundle.items = items;
}
return finishedBundle;
}
function AttachRandomizerData(
allBundlesWithStatuses: BundleWithStatus[],
): BundleWithStatus[] {
// Find and attach alternate bundle options
CommunityCenterRooms.forEach((roomName) => {
let roomBundleSpecification = (bundlesData as CommunityCenter)[roomName];
roomBundleSpecification.forEach((bundleSpecification) => {
if (isRandomizer(bundleSpecification)) {
let optionNames = bundleSpecification.options.map(
(bundle) => (bundle as Bundle).name,
);
let currentlySelectedBundles = allBundlesWithStatuses.filter(
(bundleWithStatus) =>
optionNames.includes(bundleWithStatus.bundle.name),
);
currentlySelectedBundles.forEach((bundleWithStatus) => {
let options = bundleSpecification.options.map((bundle) => {
let resolvedBundle = ResolveItemRandomizers(bundle as Bundle);
return {
...resolvedBundle,
localizedName: resolvedBundle.name,
};
});
(bundleWithStatus as BundleWithStatusAndOptions)["options"] = options;
});
}
});
});
// Find and attach alternate item options to Bundles
allBundlesWithStatuses.forEach((bundleWithStatus) => {
if (
!bundleWithStatus ||
!bundleWithStatus.bundle ||
!bundleWithStatus.bundle.items ||
!Array.isArray(bundleWithStatus.bundle.items)
) {
return;
}
let bundle = bundleWithStatus.bundle;
if (!bundle.areaName) {
return;
}
let items = bundle.items as BundleItem[];
// Find bundle spec
const bundleRoomSpec = bundlesData[bundle.areaName];
const possibleBundles: Bundle[] = [];
bundleRoomSpec.forEach((bundleSpec) => {
if (isRandomizer(bundleSpec)) {
possibleBundles.push(...(bundleSpec.options as Bundle[]));
} else {
possibleBundles.push(bundleSpec as Bundle);
}
});
const bundleSpec = possibleBundles.find((b) => b.name === bundle.name);
if (!bundleSpec) {
return;
}
let index = -1;
// Iterate over items in spec to find randomizers
bundleSpec.items.forEach((itemSpec) => {
index = index + 1;
if (isRandomizer(itemSpec)) {
let itemRandomizer = itemSpec;
let itemOptions = itemRandomizer.options as BundleItem[];
let selectionCount = itemRandomizer.selectionCount;
let currentIndex = index;
while (currentIndex < index + selectionCount) {
// Attach (randomizer options - used options) to relevant items
let alternateOptions = itemOptions.filter((newItem) => {
return !items
.map((item) => {
return item.itemID;
})
.includes(newItem.itemID);
});
(bundle.items[currentIndex] as BundleItemWithOptions).options =
alternateOptions;
currentIndex = currentIndex + 1;
}
index = currentIndex - 1;
}
});
});
return allBundlesWithStatuses;
}
function GenerateFreshBundles(): BundleWithStatus[] {
let allBundlesWithStatuses: BundleWithStatus[] = [];
CommunityCenterRooms.forEach((roomName) => {
let roomBundleSpecification = bundlesData[roomName];
let resolvedBundles: Bundle[] = [];
roomBundleSpecification.forEach((bundleSpecification) => {
if (isRandomizer(bundleSpecification)) {
resolvedBundles.push(...ResolveBundleRandomizer(bundleSpecification));
} else {
resolvedBundles.push(
ResolveItemRandomizers(bundleSpecification as Bundle),
);
}
});
let roomBundles = resolvedBundles.map((bundle) => {
let bundleStatus: boolean[] = [];
bundle.items.forEach(() => {
bundleStatus.push(false);
});
bundle.areaName = roomName;
bundle.localizedName = bundle.name;
return {
bundle,
bundleStatus,
};
});
allBundlesWithStatuses = allBundlesWithStatuses.concat(roomBundles);
});
return allBundlesWithStatuses;
}
export default function Bundles() {
// unblur dialog
const [showPrompt, setPromptOpen] = useState(false);
const { show, toggleShow } = usePreferences();
const [_filter, setFilter] = useState("all");
let [open, setIsOpen] = useState(false);
let [object, setObject] = useState<BundleItemWithLocation | null>(null);
let [bundles, setBundles] = useState<BundleWithStatus[]>([]);
const { activePlayer, patchPlayer } = usePlayers();
function GetActiveBundles(
activePlayer: PlayerType | undefined,
): BundleWithStatus[] {
let activeBundles: BundleWithStatus[] = [];
if (activePlayer) {
if (
Array.isArray(activePlayer.bundles) &&
activePlayer.bundles.length > 0
) {
activeBundles = activePlayer.bundles;
} else {
activeBundles = GenerateFreshBundles();
patchPlayer({ bundles: activeBundles });
}
} else {
activeBundles = GenerateFreshBundles();
}
AttachRandomizerData(activeBundles);
return activeBundles;
}
async function SwapBundle(
newBundle: Bundle,
oldBundleWithStatus: BundleWithStatus,
) {
let index = bundles.findIndex(
(b) => b.bundle.name === oldBundleWithStatus.bundle.name,
);
// Allow swapping bundles out even if not logged in
// It won't persist, but it's easy to redo
if (!activePlayer) {
let newBundles = [...bundles];
newBundle.areaName = oldBundleWithStatus.bundle.areaName;
let newBundleWithStatus = {
bundle: newBundle,
bundleStatus: new Array(newBundle.items.length).fill(false),
};
newBundles[index] = newBundleWithStatus;
setBundles(AttachRandomizerData(newBundles));
} else {
newBundle.areaName = oldBundleWithStatus.bundle.areaName;
let patch = {
bundles: {
[index]: {
bundle: newBundle,
bundleStatus: new Array(newBundle.items.length).fill(false),
options: null,
},
},
};
// See note in bundlesheet.tsx
// @ts-ignore
await patchPlayer(patch);
}
}
useEffect(() => {
setBundles(GetActiveBundles(activePlayer));
}, [activePlayer]);
const getAchievementProgress = (name: string) => {
if (bundles.length < 1) {
// Guard for this function being called prior to bundles being loaded
return { completed: false, additionalDescription: "" };
}
let completed = false;
let additionalDescription = "";
let completedCount = 0;
if (name === "Local Legend") {
completedCount = bundles.reduce((acc, curBundelRet) => {
if (BundleCompleted(curBundelRet)) return acc + 1;
return acc;
}, 0);
completed = completedCount >= 30;
if (!completed) {
additionalDescription = ` - ${30 - completedCount} bundles left`;
}
}
if (activePlayer?.general?.jojaMembership?.isMember) {
if (name === "Joja Co. Member Of The Year") {
completedCount =
activePlayer?.general?.jojaMembership?.developmentProjects?.length;
completed = completedCount >= 5;
if (!completed) {
additionalDescription = ` - ${5 - completedCount} projects left`;
}
}
}
return { completed, additionalDescription, completedCount };
};
return (
<>
<Head>
<title>stardew.app | Bundles</title>
<meta name="title" content="Stardew Valley Bundles | stardew.app" />
<meta
name="description"
content="Track and manage items needed for bundles in Stardew Valley's Community Center. Keep tabs on the items you've collected and monitor your progress towards completing the bundles. Discover what items are still needed to fulfill each bundle requirement and restore the Community Center to its former glory."
/>
<meta
name="og:description"
content="Track and manage items needed for bundles in Stardew Valley's Community Center. Keep tabs on the items you've collected and monitor your progress towards completing the bundles. Discover what items are still needed to fulfill each bundle requirement and restore the Community Center to its former glory."
/>
<meta
name="twitter:description"
content="Track and manage items needed for bundles in Stardew Valley's Community Center. Keep tabs on the items you've collected and monitor your progress towards completing the bundles. Discover what items are still needed to fulfill each bundle requirement and restore the Community Center to its former glory."
/>
<meta
name="keywords"
content="stardew valley bundle tracker, stardew valley community center bundles, stardew valley bundle items, stardew valley bundle progress, stardew valley community center restoration, stardew valley gameplay tracker, stardew valley, stardew, bundle tracker, stardew valley, stardew, stardew checkup, stardew bundles, stardew 100% completion, stardew perfection tracker, stardew, valley"
/>
</Head>
<main
className={`flex min-h-screen justify-center border-neutral-200 px-5 pb-8 pt-2 dark:border-neutral-800 md:border-l md:px-8`}
>
<div className="mx-auto mt-4 w-full space-y-4">
<h1 className="ml-1 text-2xl font-semibold text-gray-900 dark:text-white">
Bundle Tracker
</h1>
<AccordionSection title="Achievements" key="Achievements">
{Object.values(achievements)
.filter((a) => a.description.includes("Community"))
.map((achievement) => {
const { completed, additionalDescription } =
getAchievementProgress(achievement.name);
return (
<AchievementCard
key={achievement.id}
achievement={achievement}
completed={completed}
additionalDescription={additionalDescription}
/>
);
})}
</AccordionSection>
{/* Filters */}
<div className="grid grid-cols-1 justify-between gap-2 lg:flex">
<div className="grid grid-cols-2 gap-2 sm:flex sm:gap-3">
<FilterButton
target={"0"}
_filter={_filter}
title={`Incomplete (${31 - (getAchievementProgress("Local Legend")?.["completedCount"] ?? 0)})`}
setFilter={setFilter}
/>
<FilterButton
target={"2"}
_filter={_filter}
title={`Completed (${(getAchievementProgress("Local Legend")?.["completedCount"] ?? 0)})`}
setFilter={setFilter}
/>
</div>
</div>
{CommunityCenterRooms.map((roomName: CommunityCenterRoomName) => {
let roomBundles: BundleWithStatus[] = [];
let completedCount = 0;
if (activePlayer && Array.isArray(activePlayer.bundles)) {
roomBundles = activePlayer.bundles.filter((bundleWithStatus) => {
if (bundleWithStatus?.bundle) {
return bundleWithStatus.bundle.areaName === roomName;
} else {
return false;
}
}).filter((f) => {
if (_filter === "0") {
return f.bundleStatus.slice(0, f.bundle.items.length).includes(false);
} else if (_filter === "2") {
return !f.bundleStatus.slice(0, f.bundle.items.length).includes(false);
} else return true; // all
});
completedCount = roomBundles.reduce((acc, curBundelRet) => {
if (BundleCompleted(curBundelRet)) return acc + 1;
return acc;
}, 0);
} else {
roomBundles = bundles.filter(
(bundleWithStatus) =>
bundleWithStatus.bundle.areaName === roomName,
);
}
return (
<AccordionSection
key={roomName}
title={roomName}
completedCount={completedCount}
>
{roomBundles.map((bundleWithStatus: BundleWithStatus) => {
return (
<BundleAccordion
key={bundleWithStatus.bundle.localizedName}
bundleWithStatus={bundleWithStatus}
alternateOptions={(
bundleWithStatus as BundleWithStatusAndOptions
)?.options?.filter((newBundle) => {
return !bundles
.map((bundleWithStatus) => {
return bundleWithStatus.bundle.name;
})
.includes(newBundle.name);
})}
onChangeBundle={SwapBundle}
>
{bundleWithStatus.bundle.items.map ? (
bundleWithStatus.bundle.items.map(
(item, index: number) => {
if (isRandomizer(item)) {
// Guard clause for type coercion
return <></>;
}
const BundleItemWithLocation: BundleItemWithLocation =
{
...item,
index: index,
bundleID: bundleWithStatus.bundle.name,
};
return (
<BundleItemCard
key={item.itemID + "-" + index}
item={BundleItemWithLocation}
setIsOpen={setIsOpen}
completed={bundleWithStatus.bundleStatus[index]}
setObject={setObject}
show={show}
setPromptOpen={setPromptOpen}
/>
);
},
)
) : (
<>error</>
)}
</BundleAccordion>
);
})}
</AccordionSection>
);
})}
<BundleSheet
open={open}
setIsOpen={setIsOpen}
bundleItemWithLocation={object}
/>
<UnblurDialog
open={showPrompt}
setOpen={setPromptOpen}
toggleShow={toggleShow}
/>
</div>
</main>
</>
);
}