Skip to content

Commit

Permalink
fix(sanity): resolves issues in getting specific game achievements an…
Browse files Browse the repository at this point in the history
…d settings

also adds an enum for game settings by key
  • Loading branch information
KenEucker committed Dec 28, 2023
1 parent b19f29c commit 4a9e53b
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 16 deletions.
7 changes: 4 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 @@ -188,7 +188,7 @@ const runTests = async (out = false) => {
await get10PlayersAsync("BikeTag", biketagDefaultInstance, out)
}

if (false) {
if (bikeTagImgurInstance) {
console.log(pretty("Imgur BikeTag Client Instantiated"), imgurInstanceOpts)
await getGameAsync("Imgur", bikeTagImgurInstance, out)
// await getTag1Async("Imgur", bikeTagImgurInstance, out)
Expand All @@ -207,7 +207,8 @@ const runTests = async (out = false) => {
await getGameAsync("Sanity", bikeTagSanityInstance, out)
// await getAllGamesAsync("Sanity", bikeTagSanityInstance, out)
await get10PlayersAsync("Sanity", bikeTagSanityInstance, out)
await get1PlayerAsync("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
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

0 comments on commit 4a9e53b

Please sign in to comment.