Skip to content

Commit 9f4d2c0

Browse files
committed
lint fix
1 parent 0f38fe1 commit 9f4d2c0

File tree

6 files changed

+254
-261
lines changed

6 files changed

+254
-261
lines changed

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export * as ui from './ui'
3333
export * as queue from './queue'
3434
export { sceneParentEntity } from './config'
3535
export * as utilities from './utilities'
36-
export * as progress from './progress'
36+
export * as progress from './progress'

src/progress/challenge.ts

+66-72
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,74 @@
1-
import { signedFetch } from "~system/SignedFetch"
2-
import { IChallenge } from "./types"
3-
import { GAME_SERVER } from "../config"
4-
import { getSDK } from "../sdk"
1+
import { signedFetch } from '~system/SignedFetch'
2+
import { IChallenge } from './types'
3+
import { GAME_SERVER } from '../config'
4+
import { getSDK } from '../sdk'
55

6+
export async function getActiveChallenges() {
7+
const { config } = getSDK()
68

7-
export async function getActiveChallenges(){
8-
const {
9-
config,
10-
} = getSDK()
9+
// {{host}}/api/missions/in_progress
10+
const url = `${GAME_SERVER}/api/missions/in_progress`
11+
try {
12+
const allChallengeRes = await signedFetch({
13+
url: url,
14+
init: {
15+
method: 'GET',
16+
headers: {}
17+
}
18+
})
19+
const allActiveChallenges = (await JSON.parse(allChallengeRes.body).data.challenges) as IChallenge[]
20+
const gameChallenges: IChallenge[] = []
21+
for (let i = 0; i < allActiveChallenges.length; i++) {
22+
if (allActiveChallenges[i].game_id === config.gameId && allActiveChallenges[i].active) {
23+
gameChallenges.push(allActiveChallenges[i])
24+
}
25+
}
1126

27+
// check for completed challenge.
1228
// {{host}}/api/missions/in_progress
13-
let url = `${GAME_SERVER}/api/missions/in_progress`
14-
try {
15-
const allChallengeRes = (await signedFetch({
16-
url: url,
17-
init: {
18-
method: "GET",
19-
headers: {}
20-
}
21-
}))
22-
let allActiveChallenges = await JSON.parse(allChallengeRes.body).data.challenges as IChallenge[]
23-
let gameChallenges: IChallenge[] = []
24-
for(let i = 0; i < allActiveChallenges.length; i++){
25-
if(allActiveChallenges[i].game_id === config.gameId && allActiveChallenges[i].active){
26-
gameChallenges.push(allActiveChallenges[i])
27-
}
28-
}
29-
30-
// check for completed challenge.
31-
// {{host}}/api/missions/in_progress
32-
let completedChallengesurl = `${GAME_SERVER}/api/games/${config.gameId}/challenges/completed`
33-
const completedChallengeRes = (await signedFetch({
34-
url: completedChallengesurl,
35-
init: {
36-
method: "GET",
37-
headers: {}
38-
}
39-
}))
40-
let completedChallenges = await JSON.parse(completedChallengeRes.body).data as IChallenge[]
41-
let validGameChallenges: IChallenge[] = []
42-
for(let i = 0; i < gameChallenges.length; i++){
43-
let isFound = false
44-
for(let j = 0; j < completedChallenges.length; j++){
45-
if(completedChallenges[j].id === gameChallenges[i].id) {
46-
isFound = true
47-
break
48-
}
49-
}
50-
if(!isFound) validGameChallenges.push(gameChallenges[i])
29+
const completedChallengesurl = `${GAME_SERVER}/api/games/${config.gameId}/challenges/completed`
30+
const completedChallengeRes = await signedFetch({
31+
url: completedChallengesurl,
32+
init: {
33+
method: 'GET',
34+
headers: {}
35+
}
36+
})
37+
const completedChallenges = (await JSON.parse(completedChallengeRes.body).data) as IChallenge[]
38+
const validGameChallenges: IChallenge[] = []
39+
for (let i = 0; i < gameChallenges.length; i++) {
40+
let isFound = false
41+
for (let j = 0; j < completedChallenges.length; j++) {
42+
if (completedChallenges[j].id === gameChallenges[i].id) {
43+
isFound = true
44+
break
5145
}
52-
53-
return validGameChallenges
54-
}
55-
catch (e) {
56-
console.log('getActiveChallenges. error:', e)
57-
return undefined
46+
}
47+
if (!isFound) validGameChallenges.push(gameChallenges[i])
5848
}
49+
50+
return validGameChallenges
51+
} catch (e) {
52+
console.log('getActiveChallenges. error:', e)
53+
return undefined
54+
}
5955
}
6056

61-
export async function completeChallenge(challengeId: string){
62-
63-
//{{host}}/api/challenges/:id
64-
let url = `${GAME_SERVER}/api/challenges/${challengeId}`
65-
try {
66-
const response = (await signedFetch({
67-
url: url,
68-
init: {
69-
method: "POST",
70-
headers: {}
71-
}
72-
}))
73-
74-
getActiveChallenges()
75-
}
76-
catch (e) {
77-
console.log('completeChallenge. error:', e)
78-
return undefined
79-
}
80-
}
57+
export async function completeChallenge(challengeId: string) {
58+
//{{host}}/api/challenges/:id
59+
const url = `${GAME_SERVER}/api/challenges/${challengeId}`
60+
try {
61+
const response = await signedFetch({
62+
url: url,
63+
init: {
64+
method: 'POST',
65+
headers: {}
66+
}
67+
})
68+
69+
getActiveChallenges()
70+
} catch (e) {
71+
console.log('completeChallenge. error:', e)
72+
return undefined
73+
}
74+
}

src/progress/index.ts

+41-38
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,51 @@
1-
import { getSDK } from "../sdk";
2-
import { completeChallenge, getActiveChallenges } from "./challenge";
3-
import { getProgress, upsertProgress } from "./progress";
4-
import { isScoreMetCondition } from "./scoreCheck";
5-
import { IChallenge, IScore } from "./types";
1+
import { getSDK } from '../sdk'
2+
import { completeChallenge, getActiveChallenges } from './challenge'
3+
import { getProgress, upsertProgress } from './progress'
4+
import { isScoreMetCondition } from './scoreCheck'
5+
import { IChallenge, IScore } from './types'
66

77
// ideally on PROD we only have 1 active challenge for 1 game.
88
let activeChallenges: IChallenge[] | undefined
99

10-
async function init(){
11-
try{
12-
activeChallenges = await getActiveChallenges()
13-
console.log('init. active challenges:', activeChallenges)
14-
}
15-
catch (e) {
16-
console.log('progress init error', e)
17-
}
10+
async function init() {
11+
try {
12+
activeChallenges = await getActiveChallenges()
13+
console.log('init. active challenges:', activeChallenges)
14+
} catch (e) {
15+
console.log('progress init error', e)
16+
}
1817
}
1918

2019
// check score with all active challenges.
21-
function checkIfChallengeComplete(score: IScore){
22-
if(!activeChallenges) {
23-
console.log('checkScoreWithActiveChallenges. active challenges undefined. check progress initialization. return.')
24-
return
25-
}
26-
27-
if(activeChallenges.length === 0) {
28-
console.log('checkScoreWithActiveChallenges. no active challenge. return.')
29-
return
30-
}
31-
32-
let isChallengeConditionMet = false
33-
34-
for(let i = 0; i < activeChallenges.length; i++){
35-
let challengeData = activeChallenges[i].data
36-
let challengeId = activeChallenges[i].id
37-
isChallengeConditionMet = isScoreMetCondition(score, challengeData)
38-
39-
if(isChallengeConditionMet) {
40-
console.log('checkScoreWithActiveChallenges. score met challenge condition. ID:', challengeId, 'data:', challengeData)
41-
42-
completeChallenge(challengeId)
43-
}
20+
function checkIfChallengeComplete(score: IScore) {
21+
if (!activeChallenges) {
22+
console.log('checkScoreWithActiveChallenges. active challenges undefined. check progress initialization. return.')
23+
return
24+
}
25+
26+
if (activeChallenges.length === 0) {
27+
console.log('checkScoreWithActiveChallenges. no active challenge. return.')
28+
return
29+
}
30+
31+
let isChallengeConditionMet = false
32+
33+
for (let i = 0; i < activeChallenges.length; i++) {
34+
const challengeData = activeChallenges[i].data
35+
const challengeId = activeChallenges[i].id
36+
isChallengeConditionMet = isScoreMetCondition(score, challengeData)
37+
38+
if (isChallengeConditionMet) {
39+
console.log(
40+
'checkScoreWithActiveChallenges. score met challenge condition. ID:',
41+
challengeId,
42+
'data:',
43+
challengeData
44+
)
45+
46+
completeChallenge(challengeId)
4447
}
48+
}
4549
}
4650

47-
48-
export {init, checkIfChallengeComplete, upsertProgress, getProgress, IScore}
51+
export { init, checkIfChallengeComplete, upsertProgress, getProgress, IScore }

src/progress/progress.ts

+49-56
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,55 @@
1-
import { IScore } from "./types"
2-
import { signedFetch } from "~system/SignedFetch"
3-
import { GAME_SERVER } from "../config"
4-
import { getSDK } from "../sdk"
5-
import { checkIfChallengeComplete } from "."
1+
import { IScore } from './types'
2+
import { signedFetch } from '~system/SignedFetch'
3+
import { GAME_SERVER } from '../config'
4+
import { getSDK } from '../sdk'
5+
import { checkIfChallengeComplete } from '.'
66

7-
export async function upsertProgress(score: IScore){
8-
const {
9-
config,
10-
players
11-
} = getSDK()
12-
13-
// {{host}}/api/games/:id/progress?sort=level&limit=10&direction=DESC
14-
let url = `${GAME_SERVER}/api/games/${config.gameId}/progress?sort=level&limit=10&direction=DESC`
15-
console.log('upsert progress url:', url)
16-
try {
17-
const response = (await signedFetch({
18-
url: url,
19-
init: {
20-
method: "POST",
21-
body: JSON.stringify({
22-
user_name: players.getPlayer()?.name,
23-
...score
24-
}),
25-
headers: {}
26-
}
27-
}))
28-
let progress = await JSON.parse(response.body)
29-
console.log('upsert progress response:', progress)
7+
export async function upsertProgress(score: IScore) {
8+
const { config, players } = getSDK()
309

31-
checkIfChallengeComplete(score)
32-
}
33-
catch(e){
34-
console.log('error upsert progress', e)
35-
}
10+
// {{host}}/api/games/:id/progress?sort=level&limit=10&direction=DESC
11+
const url = `${GAME_SERVER}/api/games/${config.gameId}/progress?sort=level&limit=10&direction=DESC`
12+
console.log('upsert progress url:', url)
13+
try {
14+
const response = await signedFetch({
15+
url: url,
16+
init: {
17+
method: 'POST',
18+
body: JSON.stringify({
19+
user_name: players.getPlayer()?.name,
20+
...score
21+
}),
22+
headers: {}
23+
}
24+
})
25+
const progress = await JSON.parse(response.body)
26+
console.log('upsert progress response:', progress)
27+
28+
checkIfChallengeComplete(score)
29+
} catch (e) {
30+
console.log('error upsert progress', e)
31+
}
3632
}
3733

38-
export async function getProgress(){
39-
const {
40-
config
41-
} = getSDK()
34+
export async function getProgress() {
35+
const { config } = getSDK()
36+
37+
// {{host}}/api/games/:id/progress?sort=level&limit=10&direction=DESC
38+
const url = `${GAME_SERVER}/api/games/${config.gameId}/progress?sort=level&limit=10&direction=DESC`
39+
console.log('get progress url:', url)
40+
try {
41+
const response = await signedFetch({
42+
url: url,
43+
init: {
44+
method: 'GET',
45+
headers: {}
46+
}
47+
})
48+
const progress = await JSON.parse(response.body).data
49+
console.log('get progress response:', progress)
4250

43-
// {{host}}/api/games/:id/progress?sort=level&limit=10&direction=DESC
44-
let url = `${GAME_SERVER}/api/games/${config.gameId}/progress?sort=level&limit=10&direction=DESC`
45-
console.log('get progress url:', url)
46-
try {
47-
const response = (await signedFetch({
48-
url: url,
49-
init: {
50-
method: "GET",
51-
headers: {}
52-
}
53-
}))
54-
let progress = await JSON.parse(response.body).data
55-
console.log('get progress response:', progress)
56-
57-
return progress
58-
}
59-
catch(e){
60-
console.log('error get progress', e)
61-
}
51+
return progress
52+
} catch (e) {
53+
console.log('error get progress', e)
54+
}
6255
}

0 commit comments

Comments
 (0)