2023-05-15 04:08:30 +00:00
|
|
|
import type { Actions, RequestEvent } from '../$types';
|
2022-10-26 01:42:10 +00:00
|
|
|
import { BOARD_GAME_ATLAS_CLIENT_ID } from '$env/static/private';
|
2022-12-29 22:10:28 +00:00
|
|
|
import { error } from '@sveltejs/kit';
|
2023-05-15 04:08:30 +00:00
|
|
|
import { superValidate } from 'sveltekit-superforms/server';
|
|
|
|
|
import type { GameType, SearchQuery } from '$lib/types';
|
|
|
|
|
import { mapAPIGameToBoredGame } from '$lib/util/gameMapper';
|
|
|
|
|
import { search_schema } from '$lib/zodValidation';
|
2022-12-25 18:52:36 +00:00
|
|
|
|
2023-05-15 04:08:30 +00:00
|
|
|
async function searchForGames(urlQueryParams) {
|
2022-12-25 18:52:36 +00:00
|
|
|
try {
|
|
|
|
|
const url = `https://api.boardgameatlas.com/api/search${
|
|
|
|
|
urlQueryParams ? `?${urlQueryParams}` : ''
|
|
|
|
|
}`;
|
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
method: 'get',
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
console.log('Status not 200', response.status);
|
|
|
|
|
throw error(response.status);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 04:08:30 +00:00
|
|
|
const games: GameType[] = [];
|
|
|
|
|
let totalCount = 0;
|
|
|
|
|
if (response.ok) {
|
2022-12-25 18:52:36 +00:00
|
|
|
const gameResponse = await response.json();
|
|
|
|
|
const gameList = gameResponse?.games;
|
2023-05-15 04:08:30 +00:00
|
|
|
totalCount = gameResponse?.count;
|
2022-12-25 18:52:36 +00:00
|
|
|
console.log('totalCount', totalCount);
|
|
|
|
|
gameList.forEach((game) => {
|
2023-01-24 07:20:16 +00:00
|
|
|
if (game?.min_players && game?.max_players) {
|
|
|
|
|
game.players = `${game.min_players}-${game.max_players}`;
|
|
|
|
|
game.playtime = `${game.min_playtime}-${game.max_playtime}`;
|
|
|
|
|
}
|
2022-12-25 18:52:36 +00:00
|
|
|
games.push(mapAPIGameToBoredGame(game));
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-05-15 04:08:30 +00:00
|
|
|
return {
|
|
|
|
|
totalCount,
|
|
|
|
|
games
|
|
|
|
|
};
|
2022-12-25 18:52:36 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
console.log(`Error searching board games ${e}`);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
totalCount: 0,
|
2023-05-15 04:08:30 +00:00
|
|
|
games: []
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const load = async ({ fetch, url }) => {
|
|
|
|
|
const defaults = {
|
|
|
|
|
limit: 10,
|
|
|
|
|
skip: 0
|
|
|
|
|
};
|
|
|
|
|
const searchParams = Object.fromEntries(url?.searchParams);
|
|
|
|
|
searchParams.limit = searchParams.limit || `${defaults.limit}`;
|
|
|
|
|
searchParams.skip = searchParams.skip || `${defaults.skip}`;
|
|
|
|
|
const form = await superValidate(searchParams, search_schema);
|
|
|
|
|
|
|
|
|
|
const queryParams: SearchQuery = {
|
|
|
|
|
order_by: 'rank',
|
|
|
|
|
ascending: false,
|
|
|
|
|
limit: form.data?.limit,
|
|
|
|
|
skip: form.data?.skip,
|
|
|
|
|
client_id: BOARD_GAME_ATLAS_CLIENT_ID,
|
|
|
|
|
fuzzy_match: true,
|
|
|
|
|
name: form.data?.q,
|
|
|
|
|
fields:
|
|
|
|
|
'id,name,min_age,min_players,max_players,thumb_url,min_playtime,max_playtime,min_age,description'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (form.data?.minAge) {
|
|
|
|
|
if (form.data?.exactMinAge) {
|
|
|
|
|
queryParams.min_age = form.data?.minAge;
|
|
|
|
|
} else {
|
|
|
|
|
queryParams.gt_min_age = form.data?.minAge === 1 ? 0 : form.data?.minAge - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (form.data?.minPlayers) {
|
|
|
|
|
if (form.data?.exactMinPlayers) {
|
|
|
|
|
queryParams.min_players = form.data?.minPlayers;
|
|
|
|
|
} else {
|
|
|
|
|
queryParams.gt_min_players = form.data?.minPlayers === 1 ? 0 : form.data?.minPlayers - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (form.data?.maxPlayers) {
|
|
|
|
|
if (form.data?.exactMaxPlayers) {
|
|
|
|
|
queryParams.max_players = form.data?.maxPlayers;
|
|
|
|
|
} else {
|
|
|
|
|
queryParams.lt_max_players = form.data?.maxPlayers + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newQueryParams: Record<string, string> = {};
|
|
|
|
|
for (const key in queryParams) {
|
|
|
|
|
newQueryParams[key] = `${queryParams[key as keyof SearchQuery]}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const urlQueryParams = new URLSearchParams(newQueryParams);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
form,
|
|
|
|
|
searchData: await searchForGames(urlQueryParams)
|
2022-12-25 18:52:36 +00:00
|
|
|
};
|
|
|
|
|
};
|
2022-11-01 00:31:36 +00:00
|
|
|
|
2022-09-29 22:22:01 +00:00
|
|
|
export const actions: Actions = {
|
2022-12-27 07:15:41 +00:00
|
|
|
random: async ({ request }: RequestEvent): Promise<any> => {
|
2022-12-25 18:52:36 +00:00
|
|
|
const queryParams: SearchQuery = {
|
|
|
|
|
order_by: 'rank',
|
|
|
|
|
ascending: false,
|
|
|
|
|
client_id: BOARD_GAME_ATLAS_CLIENT_ID,
|
2023-01-08 19:46:22 +00:00
|
|
|
random: true,
|
|
|
|
|
fields:
|
|
|
|
|
'id,name,min_age,min_players,max_players,thumb_url,min_playtime,max_playtime,min_age,description'
|
2022-12-25 18:52:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const newQueryParams: Record<string, string> = {};
|
|
|
|
|
for (const key in queryParams) {
|
|
|
|
|
newQueryParams[key] = `${queryParams[key as keyof SearchQuery]}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const urlQueryParams = new URLSearchParams(newQueryParams);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const url = `https://api.boardgameatlas.com/api/search${
|
|
|
|
|
urlQueryParams ? `?${urlQueryParams}` : ''
|
|
|
|
|
}`;
|
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
method: 'get',
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// console.log('board game response', response);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
console.log('Status not 200', response.status);
|
|
|
|
|
throw error(response.status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
const gameResponse = await response.json();
|
|
|
|
|
// console.log('gameResponse', gameResponse);
|
|
|
|
|
const gameList = gameResponse?.games;
|
|
|
|
|
const totalCount = gameResponse?.count;
|
|
|
|
|
console.log('totalCount', totalCount);
|
|
|
|
|
const games: GameType[] = [];
|
|
|
|
|
gameList.forEach((game) => {
|
2023-01-08 08:35:40 +00:00
|
|
|
game.players = `${game.min_players}-${game.max_players}`;
|
|
|
|
|
game.playtime = `${game.min_playtime}-${game.max_playtime}`;
|
2022-12-25 18:52:36 +00:00
|
|
|
games.push(mapAPIGameToBoredGame(game));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// console.log('returning from search', games)
|
|
|
|
|
|
|
|
|
|
return {
|
2022-12-27 07:15:41 +00:00
|
|
|
games
|
2022-12-25 18:52:36 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(`Error searching board games ${e}`);
|
|
|
|
|
}
|
|
|
|
|
return {
|
2022-12-27 07:15:41 +00:00
|
|
|
games: []
|
2022-12-25 18:52:36 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|