Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .claude/settings.local.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@
"Bash(npm show:*)",
"Bash(git:*)",
"Bash(cp:*)",
"Bash(/home/mozyonee/Projects/widgetable/apps/server/src/storage/storage.config.ts:*)"
"Bash(/home/mozyonee/Projects/widgetable/apps/server/src/storage/storage.config.ts:*)",
"mcp__github__create_branch",
"mcp__github__update_pull_request"
],
"deny": [],
"ask": []
Expand Down
25 changes: 13 additions & 12 deletions apps/server/src/pets/pets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
MAX_EXPEDITION_SLOTS_RATIO,
PET_NEED_KEYS,
PET_THRESHOLDS,
PET_UPDATE_INTERVAL,
PetNeeds,
PetType,
PetUpdate,
} from '@widgetable/types';
Expand Down Expand Up @@ -145,13 +147,18 @@ export class PetsService extends BaseService {

if (pet.isEgg) return pet;

const lastUpdatedAt = pet.needsUpdatedAt ?? pet.updatedAt ?? new Date();
const { needs: updatedNeeds, changed } = calculateDecayedNeeds(pet.needs, lastUpdatedAt, new Date());
const now = new Date();
const lastUpdatedAt = pet.needsUpdatedAt ?? pet.updatedAt ?? now;
const plainNeeds = Object.fromEntries(PET_NEED_KEYS.map((key) => [key, pet.needs[key]])) as PetNeeds;
const { needs: updatedNeeds, changed, intervals } = calculateDecayedNeeds(plainNeeds, lastUpdatedAt, now);

if (!changed) return pet;

// Snap to the last completed interval boundary so the remainder carries over to the next run
const snappedUpdatedAt = new Date(lastUpdatedAt.getTime() + intervals * PET_UPDATE_INTERVAL);

const updatedPet = await this.petModel
.findByIdAndUpdate(pet._id, { needs: updatedNeeds, needsUpdatedAt: new Date() }, { new: true })
.findByIdAndUpdate(pet._id, { needs: updatedNeeds, needsUpdatedAt: snappedUpdatedAt }, { new: true })
.populate('parents', this.PARENT_FIELDS);

return updatedPet || pet;
Expand All @@ -163,16 +170,10 @@ export class PetsService extends BaseService {
if (pet.isEgg) throw new UnprocessableEntityException();
if (pet.isOnExpedition) throw new ConflictException();

if (pet.needs) {
const urgentNeeds: string[] = [];
if (pet.needs.hunger < PET_THRESHOLDS.URGENT) urgentNeeds.push('hunger');
if (pet.needs.thirst < PET_THRESHOLDS.URGENT) urgentNeeds.push('thirst');
if (pet.needs.hygiene < PET_THRESHOLDS.URGENT) urgentNeeds.push('hygiene');
if (pet.needs.energy < PET_THRESHOLDS.URGENT) urgentNeeds.push('energy');
if (pet.needs.toilet < PET_THRESHOLDS.URGENT) urgentNeeds.push('toilet');

if (urgentNeeds.length > 0) throw new BadRequestException();
if (pet.needs && PET_NEED_KEYS.some((key) => pet.needs[key] < PET_THRESHOLDS.URGENT)) {
throw new BadRequestException();
}

// Only count pets still away, not returned but unclaimed
const now = new Date();
const allPets = await this.getPetsForUser(userId);
Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/pet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ export const calculateDecayedNeeds = (
needs: PetNeeds,
lastUpdatedAt: Date,
now: Date,
): { needs: PetNeeds; changed: boolean } => {
): { needs: PetNeeds; changed: boolean; intervals: number } => {
const timeDiff = now.getTime() - lastUpdatedAt.getTime();
const intervals = Math.floor(timeDiff / PET_UPDATE_INTERVAL);

if (intervals <= 0) return { needs, changed: false };
if (intervals <= 0) return { needs, changed: false, intervals: 0 };

const updated = { ...needs };
for (const key of PET_NEED_KEYS) {
const decrease = intervals * PET_NEEDS_CONFIG[key].decayRate * (PET_UPDATE_INTERVAL / DECAY_TIME_UNIT_MS);
updated[key] = Math.max(0, Math.min(100, needs[key] - decrease));
}

return { needs: updated, changed: true };
return { needs: updated, changed: true, intervals };
};

const BASE_EXP = 50;
Expand Down