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'
5
5
6
+ export async function getActiveChallenges ( ) {
7
+ const { config } = getSDK ( )
6
8
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
+ }
11
26
27
+ // check for completed challenge.
12
28
// {{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
51
45
}
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 ] )
58
48
}
49
+
50
+ return validGameChallenges
51
+ } catch ( e ) {
52
+ console . log ( 'getActiveChallenges. error:' , e )
53
+ return undefined
54
+ }
59
55
}
60
56
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
+ }
0 commit comments