Skip to content

Commit 0d796f0

Browse files
authored
Merge pull request #296 from KenEucker/develop
v3.5.0
2 parents 6816ec5 + aff67f8 commit 0d796f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+16469
-15726
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
},
88
"[vue]": {
99
"editor.defaultFormatter": "Vue.volar"
10+
},
11+
"[scss]": {
12+
"editor.defaultFormatter": "vscode.css-language-features"
1013
}
1114
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Below you will find some of the primary settings for testing all features of the
9797
# Used for internal authentication
9898
9999
HOST_KEY=anythingyouwantititobe
100-
ACCESS_TOKEN=BIKETAGACCESSTOKEN
100+
CLIENT_KEY=BIKETAGACCESSKEY
101101
# Used for uploading new BikeTag posts
102102
IMGUR_CLIENT_ID=IMGURCLIENTID
103103
IMGUR_CLIENT_SECRET=IMGURCLIENTSECRET
Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { Handler } from '@netlify/functions'
2-
import BikeTagClient from 'biketag'
3-
import { Achievement, Game, Player, Tag } from 'biketag/dist/common/schema'
1+
import BikeTagClient, { Achievement, Game, Player, Tag } from 'biketag'
42
import { getSupportedGames } from '../src/common'
5-
import { getBikeTagClientOpts } from './common'
3+
import { getBikeTagClientOpts, log } from './common'
64
import { HttpStatusCode } from './common/constants'
75
import { BackgroundProcessResults } from './common/types'
86

97
export const assignAchievements = async (): Promise<BackgroundProcessResults> => {
10-
if (process.env.SKIP_AUTOPOST_FUNCTION) {
8+
if (process.env.SKIP_ACHIEVEMENTS_FUNCTION === "true") {
119
return Promise.resolve({
1210
results: ['function skipped'],
1311
errors: false,
@@ -62,13 +60,13 @@ export const assignAchievements = async (): Promise<BackgroundProcessResults> =>
6260
if (players.length > 20) {
6361
/// Only award achievements if at least 20 players have logged in
6462
} else {
65-
console.log(`[${game.name}] does not have enough players to award achievements`)
63+
log(`[${game.name}] does not have enough players to award achievements`, { playersCount: players.length }, 'warn')
6664
}
6765
}
6866
}
6967
}
7068
} else {
71-
console.log('couldnt get games', gamesResponse)
69+
log('couldnt get games', gamesResponse, 'error')
7270
}
7371

7472
return {
@@ -77,24 +75,18 @@ export const assignAchievements = async (): Promise<BackgroundProcessResults> =>
7775
}
7876
}
7977

80-
const assignAchievementsHandler: Handler = async () => {
78+
export default async (req: Request) => {
8179
const { results, errors } = await assignAchievements()
8280

8381
if (results.length) {
84-
console.log('achievements assigning attempted', { results })
85-
return {
86-
statusCode: errors ? HttpStatusCode.BadRequest : HttpStatusCode.Ok,
87-
body: JSON.stringify(results),
88-
}
82+
log('achievements assigning attempted', { results }, 'info')
83+
return new Response(JSON.stringify(results), {
84+
status: errors ? HttpStatusCode.BadRequest : HttpStatusCode.Ok,
85+
})
8986
} else {
90-
console.log('nothing to report')
91-
return {
92-
statusCode: errors ? HttpStatusCode.BadRequest : HttpStatusCode.Ok,
93-
body: '',
94-
}
87+
log('nothing to report')
88+
return new Response('', {
89+
status: errors ? HttpStatusCode.BadRequest : HttpStatusCode.Ok,
90+
})
9591
}
96-
}
97-
98-
const handler = assignAchievementsHandler
99-
100-
export { handler }
92+
}

functions/achievements.mts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { BikeTagClient, Game } from 'biketag'
2+
import {
3+
acceptCorsHeaders,
4+
getBikeTagClientOpts,
5+
getPayloadOpts,
6+
HttpStatusCode,
7+
log,
8+
} from './common'
9+
10+
export default async (req: Request) => {
11+
const headers = acceptCorsHeaders()
12+
13+
log('[get-achievements] Incoming request', { method: req.method, url: req.url })
14+
15+
if (req.method === 'OPTIONS') {
16+
log('[get-achievements] OPTIONS preflight handled')
17+
return new Response(undefined, {
18+
status: HttpStatusCode.NoContent,
19+
headers,
20+
})
21+
}
22+
23+
try {
24+
const biketagOpts = getBikeTagClientOpts(req, true)
25+
log('[get-achievements] Parsed BikeTagClient options', biketagOpts)
26+
27+
const biketag = new BikeTagClient(biketagOpts)
28+
29+
const game = (await biketag.game(biketagOpts.game, {
30+
source: 'sanity',
31+
concise: true,
32+
})) as unknown as Game
33+
log('[get-achievements] Retrieved game', { name: game.name, awsRegion: game.awsRegion })
34+
35+
const biketagPayload = await getPayloadOpts(req, {
36+
imgur: { hash: game.mainhash },
37+
game: biketagOpts.game,
38+
})
39+
log('[get-achievements] Prepared biketag payload', biketagPayload)
40+
41+
const imageSource = game.awsRegion ? 'aws' : 'imgur'
42+
log('[get-achievements] Using image source', { imageSource })
43+
44+
const achievementsResponse = await biketag.getAchievements(biketagPayload, {
45+
source: imageSource,
46+
})
47+
log('[get-achievements] getAchievements response', {
48+
success: achievementsResponse.success,
49+
status: achievementsResponse.status,
50+
count: Array.isArray(achievementsResponse.data) ? achievementsResponse.data.length : 0,
51+
})
52+
53+
const { success, data } = achievementsResponse
54+
55+
return new Response(JSON.stringify(success ? data : achievementsResponse), {
56+
status: achievementsResponse.status,
57+
headers,
58+
})
59+
} catch (err: any) {
60+
log('[get-achievements] Unexpected error', err, 'error')
61+
return new Response(JSON.stringify({ error: 'Internal server error' }), {
62+
status: HttpStatusCode.InternalServerError,
63+
headers,
64+
})
65+
}
66+
}

functions/achievements.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

functions/ambassadors.mts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { BikeTagClient, Game } from 'biketag'
2+
import {
3+
acceptCorsHeaders,
4+
getBikeTagClientOpts,
5+
getPayloadOpts,
6+
HttpStatusCode,
7+
log,
8+
} from './common'
9+
10+
export default async (req: Request) => {
11+
const headers = acceptCorsHeaders()
12+
13+
log('[ambassadors] Incoming request', { method: req.method, url: req.url })
14+
15+
if (req.method === 'OPTIONS') {
16+
log('[ambassadors] OPTIONS preflight handled')
17+
return new Response(undefined, {
18+
status: HttpStatusCode.NoContent,
19+
headers,
20+
})
21+
}
22+
23+
try {
24+
const biketagOpts = getBikeTagClientOpts(req, true)
25+
log('[ambassadors] Parsed BikeTagClient options', biketagOpts)
26+
27+
const biketag = new BikeTagClient(biketagOpts)
28+
29+
const game = (await biketag.game(biketagOpts.game, {
30+
source: 'sanity',
31+
concise: true,
32+
})) as unknown as Game
33+
log('[ambassadors] Retrieved game', {
34+
name: game.name,
35+
id: game._id,
36+
region: game.awsRegion ?? 'imgur',
37+
})
38+
39+
const biketagPayload = await getPayloadOpts(req, {
40+
imgur: { hash: game.mainhash },
41+
game: biketagOpts.game,
42+
})
43+
log('[ambassadors] Prepared payload for getAmbassadors', biketagPayload)
44+
45+
const imageSource = game.awsRegion ? 'aws' : 'imgur'
46+
log('[ambassadors] Using image source', { imageSource })
47+
48+
const ambassadorsResponse = await biketag.getAmbassadors(biketagPayload, {
49+
source: imageSource,
50+
})
51+
log('[ambassadors] getAmbassadors response', {
52+
success: ambassadorsResponse.success,
53+
status: ambassadorsResponse.status,
54+
count: Array.isArray(ambassadorsResponse.data) ? ambassadorsResponse.data.length : 0,
55+
})
56+
57+
return new Response(
58+
JSON.stringify(ambassadorsResponse.success ? ambassadorsResponse.data : ambassadorsResponse),
59+
{
60+
status: ambassadorsResponse.status,
61+
headers,
62+
},
63+
)
64+
} catch (err: any) {
65+
log('[ambassadors] Unexpected error', err, 'error')
66+
return new Response(
67+
JSON.stringify({
68+
success: false,
69+
error: err.message ?? 'Unknown error',
70+
}),
71+
{
72+
status: HttpStatusCode.InternalServerError,
73+
headers,
74+
},
75+
)
76+
}
77+
}

functions/ambassadors.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)