Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sanity): resolves issues in getting specific game achievements and settings #221

Merged
merged 2 commits into from
Dec 28, 2023
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
8 changes: 5 additions & 3 deletions examples/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const queueTagAsync = async (pre, client, out = false, opts = {}) => {
const get10TagsAsync = async (pre, client, out = false, opts = {}) => {
opts.limit = opts.limit ? opts.limit : 10
const tags = await client.getTags(undefined, opts).catch(console.error)
log(`${pre} :: successfully retrieved tags data`, tags, out)
log(`${pre} :: successfully retrieved 10 tags data`, tags, out)
console.log(tags[0])

return tags
Expand Down Expand Up @@ -130,7 +130,7 @@ const getAllGamesAsync = async (pre, client, out = false, opts = {}) => {

const get1PlayerAsync = async (pre, client, out = false, opts = {}) => {
opts.limit = opts.limit ? opts.limit : 10
const testPlayerData = await client.getPlayer('player-test', opts).catch(console.error)
const testPlayerData = await client.getPlayer('Ken', opts).catch(console.error)
log(`${pre} :: success fully retrieved player data`, testPlayerData, out)

return testPlayerData
Expand Down Expand Up @@ -189,6 +189,7 @@ const runTests = async (out = false) => {
}

if (false) {
// if (bikeTagImgurInstance) {
console.log(pretty("Imgur BikeTag Client Instantiated"), imgurInstanceOpts)
await getGameAsync("Imgur", bikeTagImgurInstance, out)
// await getTag1Async("Imgur", bikeTagImgurInstance, out)
Expand All @@ -206,8 +207,9 @@ const runTests = async (out = false) => {
// await get10TagsAsync("Sanity", bikeTagSanityInstance, out)
await getGameAsync("Sanity", bikeTagSanityInstance, out)
// await getAllGamesAsync("Sanity", bikeTagSanityInstance, out)
await get10PlayersAsync("Sanity", bikeTagSanityInstance, out)
// await get10PlayersAsync("Sanity", bikeTagSanityInstance, out)
await get1PlayerAsync("Sanity", bikeTagSanityInstance, out)
// await get10AchievementsAsync("Sanity", bikeTagSanityInstance, out)
// await get10AmbassadorsAsync("Sanity", bikeTagSanityInstance, out)
// await get10SettingsAsync("Sanity", bikeTagSanityInstance, out)
// await get10AchievementsAsync("Sanity", bikeTagSanityInstance, out)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "biketag",
"version": "3.2.3",
"version": "3.2.4",
"description": "The Javascript client API for BikeTag Games",
"main": "./biketag.node.js",
"browser": "./biketag.js",
Expand Down
36 changes: 34 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ export class BikeTagClient extends EventEmitter {
options.slug = options.slug ?? options.game?.toLowerCase() ?? undefined
break

case DataTypes.achievement:
options.game = options.game ?? options.slug ?? this.biketagConfig?.game
break

case DataTypes.setting:
options.game = options.game ?? options.slug ?? this.biketagConfig?.game
break

case DataTypes.player:
options.game = options.game ? options.game : this.biketagConfig?.game

Expand Down Expand Up @@ -1383,10 +1391,22 @@ export class BikeTagClient extends EventEmitter {
opts,
DataTypes.setting
)
const clientMethod = api.getAchievement
let clientMethod = api.getAchievement

/// If the client adapter implements a direct way to retrieve a single setting
if (clientMethod) {
switch (options.source) {
case AvailableApis.sanity:
clientMethod = clientMethod.bind({
getGame: this.getPassthroughApiMethod(
api.getGame,
client,
DataTypes.game
),
})
break
}

return clientMethod(client, options).catch((e) => {
return {
status: HttpStatusCode.InternalServerError,
Expand Down Expand Up @@ -1426,9 +1446,21 @@ export class BikeTagClient extends EventEmitter {
DataTypes.achievement,
'getAchievements'
)
const clientMethod = api.getAchievements
let clientMethod = api.getAchievements

if (clientMethod) {
switch (options.source) {
case AvailableApis.sanity:
clientMethod = clientMethod.bind({
getGame: this.getPassthroughApiMethod(
api.getGame,
client,
DataTypes.game
),
})
break
}

return clientMethod(client, options).catch((e) => {
return Promise.resolve({
status: HttpStatusCode.InternalServerError,
Expand Down
1 change: 1 addition & 0 deletions src/common/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Tag, Game, Player, Ambassador, Setting, Achievement } from './schema'

/// TODO: make an enum and put into the enums
export const cacheKeys = {
sanityUrlText: `sanity::`,
imageHashText: `hash::`,
Expand Down
4 changes: 4 additions & 0 deletions src/common/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export enum DataTypes {
achievement,
}

export enum GameSettingsKeys {
achievementsEnabled = 'achievements::enabled',
}

export enum Errors {
NotImplemented = 'method not implemented for adapter: ',
}
Expand Down
37 changes: 31 additions & 6 deletions src/sanity/getAchievements.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { SanityClient } from '@sanity/client'
import { BikeTagApiResponse } from '../common/types'
import { AvailableApis, HttpStatusCode, DataTypes } from '../common/enums'
import {
constructPlayerFromSanityObject,
AvailableApis,
HttpStatusCode,
DataTypes,
GameSettingsKeys,
} from '../common/enums'
import {
constructAchievementFromSanityObject,
constructSanityDocumentQuery,
constructSanityFieldsQuery,
} from './helpers'
Expand All @@ -22,18 +27,38 @@ export async function getAchievements(
const fieldsFilter = payload.fields?.length ? payload.fields : []
const query = constructSanityDocumentQuery(
DataTypes[DataTypes.achievement],
payload.game,
// payload.game, // don't include the game here because we are not specifically assigning achievements to a game
undefined,
payload.player,
payload.slugs,
undefined,
fields
)

return client.fetch(query, {}).then((achievementsData) => {
const achievements = achievementsData.map((achievement: any) =>
constructPlayerFromSanityObject(achievement, fieldsFilter)
return client.fetch(query, {}).then(async (achievementsData) => {
let achievements = achievementsData.map((achievement: any) =>
constructAchievementFromSanityObject(achievement, fieldsFilter)
)

if (payload.game?.length) {
const game = (await this.getGame(payload.game)).data
if (game) {
const achievementsEnabled =
game.settings[GameSettingsKeys.achievementsEnabled]

if (!achievementsEnabled || achievementsEnabled === 'false') {
achievements = []
} else if (achievementsEnabled && achievementsEnabled !== 'true') {
const enabledAchievements = achievementsEnabled.split(',')
achievements = achievements.filter((a) =>
enabledAchievements.includes(a.key)
)
} else {
// first do nothing
}
}
}

const response = {
data: sortAchievements(achievements, payload.sort, payload.limit),
status: HttpStatusCode.Found,
Expand Down
8 changes: 7 additions & 1 deletion src/sanity/getPlayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ export async function getPlayers(
)

return client.fetch(query, {}).then((players) => {
const playersData = players.map((player: any) =>
let playersData = players.map((player: any) =>
constructPlayerFromSanityObject(player, fieldsFilter)
)

if (payload.slugs?.length) {
playersData = playersData.filter((p) => payload.slugs?.includes(p.slug))
} else if (payload.names?.length) {
playersData = playersData.filter((p) => payload.names?.includes(p.name))
}

const response = {
data: sortPlayers(playersData, payload.sort, payload.limit),
status: HttpStatusCode.Found,
Expand Down
4 changes: 2 additions & 2 deletions src/sanity/getSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SanityClient } from '@sanity/client'
import { BikeTagApiResponse } from '../common/types'
import { AvailableApis, HttpStatusCode, DataTypes } from '../common/enums'
import {
constructPlayerFromSanityObject,
constructSettingFromSanityObject,
constructSanityDocumentQuery,
constructSanityFieldsQuery,
} from './helpers'
Expand All @@ -27,7 +27,7 @@ export async function getSettings(

return client.fetch(query, {}).then((settingsData) => {
const settings = settingsData.map((setting: any) =>
constructPlayerFromSanityObject(setting, fieldsFilter)
constructSettingFromSanityObject(setting, fieldsFilter)
)

const response = {
Expand Down
34 changes: 34 additions & 0 deletions src/sanity/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import {
playerDataObjectFields,
playerDataArrayFields,
createPlayerObject,
createAchievementObject,
ambassadorDataReferenceFields,
createAmbassadorObject,
settingDataFields,
ambassadorDataFields,
gameDataAssetFields,
playerDataAssetFields,
createSettingObject,
} from '../common/data'
import { DataTypes } from '../common/enums'

Expand Down Expand Up @@ -393,6 +395,38 @@ export function constructAmbassadorFromSanityObject(
return createAmbassadorObject(ambassadorData)
}

export function constructSettingFromSanityObject(
data: any,
fields: string[] = []
): any {
const settingData = fields.length
? fields.reduce((o: any, f: any) => {
o[f] = data[f]
return o
}, {})
: data

settingData.slug = settingData.slug?.current ?? settingData.slug

return createSettingObject(settingData)
}

export function constructAchievementFromSanityObject(
data: any,
fields: string[] = []
): any {
const achievementData = fields.length
? fields.reduce((o: any, f: any) => {
o[f] = data[f]
return o
}, {})
: data

achievementData.slug = achievementData.slug?.current ?? achievementData.slug

return createAchievementObject(achievementData)
}

export function constructSanityDocumentQuery(
docType: string,
game?: string,
Expand Down