feat: add friends' results display for today's game
All checks were successful
Build Docker Image / build (push) Successful in 1m12s
All checks were successful
Build Docker Image / build (push) Successful in 1m12s
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { db } from '$lib/server/db';
|
||||
import { config } from '$lib/server/db/schema';
|
||||
import { getDailyModeCharacters, getOrCreateTodayCharacter, getYesterdayCharacter, getTodayCharacterWinsCount } from '$lib/server/daily-character';
|
||||
import { like } from 'drizzle-orm';
|
||||
import { characterHistory, config, friendship, user, userCharacterHistory } from '$lib/server/db/schema';
|
||||
import { getDailyModeCharacters, getOrCreateTodayCharacter, getYesterdayCharacter, getTodayCharacterWinsCount, getDateKey } from '$lib/server/daily-character';
|
||||
import { and, eq, inArray, like, or } from 'drizzle-orm';
|
||||
|
||||
export async function load() {
|
||||
export async function load(event) {
|
||||
const characters = await getDailyModeCharacters();
|
||||
const dailyCharacter = await getOrCreateTodayCharacter(characters);
|
||||
|
||||
@@ -17,6 +17,60 @@ export async function load() {
|
||||
// Load the win count for today
|
||||
const winCount = await getTodayCharacterWinsCount(dailyCharacter.id);
|
||||
|
||||
let friendsTodayResults: Array<{ userId: string; name: string; image: string | null; tryCount: number }> = [];
|
||||
|
||||
if (event.locals.user) {
|
||||
const currentUserId = event.locals.user.id;
|
||||
|
||||
const acceptedFriendships = await db
|
||||
.select({
|
||||
requesterId: friendship.requesterId,
|
||||
addresseeId: friendship.addresseeId
|
||||
})
|
||||
.from(friendship)
|
||||
.where(
|
||||
and(
|
||||
eq(friendship.status, 'accepted'),
|
||||
or(eq(friendship.requesterId, currentUserId), eq(friendship.addresseeId, currentUserId))
|
||||
)
|
||||
);
|
||||
|
||||
const friendIds = acceptedFriendships.map((relation) =>
|
||||
relation.requesterId === currentUserId ? relation.addresseeId : relation.requesterId
|
||||
);
|
||||
|
||||
if (friendIds.length > 0) {
|
||||
const todayDate = getDateKey(new Date());
|
||||
|
||||
const [todayHistoryEntry] = await db
|
||||
.select({ id: characterHistory.id })
|
||||
.from(characterHistory)
|
||||
.where(eq(characterHistory.date, todayDate))
|
||||
.limit(1);
|
||||
|
||||
const todayCharacterHistoryId = todayHistoryEntry?.id;
|
||||
|
||||
if (todayCharacterHistoryId) {
|
||||
friendsTodayResults = await db
|
||||
.select({
|
||||
userId: user.id,
|
||||
name: user.name,
|
||||
image: user.image,
|
||||
tryCount: userCharacterHistory.tryCount
|
||||
})
|
||||
.from(userCharacterHistory)
|
||||
.innerJoin(user, eq(userCharacterHistory.userId, user.id))
|
||||
.where(
|
||||
and(
|
||||
eq(userCharacterHistory.characterHistoryId, todayCharacterHistoryId),
|
||||
inArray(userCharacterHistory.userId, friendIds)
|
||||
)
|
||||
)
|
||||
.orderBy(userCharacterHistory.tryCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load column visibility config
|
||||
const columnConfig = await db
|
||||
.select()
|
||||
@@ -37,6 +91,7 @@ export async function load() {
|
||||
dailyCharacter,
|
||||
yesterdayCharacter,
|
||||
columnVisibility,
|
||||
winCount
|
||||
winCount,
|
||||
friendsTodayResults
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user