-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.ts
152 lines (140 loc) · 4.86 KB
/
pack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import * as coda from "@codahq/packs-sdk";
import * as schemas from "./schemas";
import * as helpers from "./helpers";
import { Champion, Match } from "./types";
export const pack = coda.newPack();
pack.setVersion("6.0");
pack.setSystemAuthentication({
type: coda.AuthenticationType.CustomHeaderToken,
headerName: "X-Riot-Token",
});
pack.addNetworkDomain("riotgames.com");
pack.addNetworkDomain("leagueoflegends.com");
const RegionParameter = coda.makeParameter({
type: coda.ParameterType.String,
name: "Region",
description: "The region the summoner is associated with.",
suggestedValue: "NA",
});
const SummonerNameParameter = coda.makeParameter({
type: coda.ParameterType.String,
name: "Summoner Name",
description: "The summoner name of the League of Legends account.",
});
pack.addFormula({
name: "Summoner",
description: "Gets account information about a League of Legends summoner.",
parameters: [SummonerNameParameter, RegionParameter],
resultType: coda.ValueType.Object,
schema: schemas.SummonerSchema,
execute: async function ([name, region]: [string, string], context: coda.ExecutionContext) {
return helpers.getSummonerByName(name, region, context.fetcher);
},
});
pack.addSyncTable({
name: "Champions",
schema: schemas.ChampionSchema,
identityName: "Champion",
formula: {
name: "SyncChampions",
description: "Sync all champion data.",
parameters: [],
execute: async function ([], context) {
let result = await helpers.getAllChampions(context.fetcher);
return { result };
},
},
});
pack.addSyncTable({
name: "ChampionMasteries",
schema: schemas.ChampionMasterySchema,
identityName: "ChampionMasteries",
formula: {
name: "SyncChampionMasteries",
description: "Sync champion mastery data for a summoner.",
parameters: [SummonerNameParameter, RegionParameter],
execute: async function ([name, region]: [string, string], context: coda.ExecutionContext) {
let summonerId: string = await helpers.getSummonerIdByName(name, region, context.fetcher);
let championMasteryResponse = await context.fetcher.fetch({
method: "GET",
url: `${helpers.riotApiUrl(region)}/champion-mastery/v4/champion-masteries/by-summoner/${summonerId}`,
});
let allChampionsById: Map<number, Champion> = new Map();
(await helpers.getAllChampions(context.fetcher)).forEach((champion: Champion) => {
allChampionsById.set(champion.key, champion);
});
let result = championMasteryResponse.body.map((mastery) => ({
championId: mastery.championId,
championLevel: mastery.championLevel,
championPoints: mastery.championPoints,
lastPlayTime: mastery.lastPlayTime / 1000,
championPointsSinceLastLevel: mastery.championPointsSinceLastLevel,
championPointsUntilNextLevel: mastery.championPointsUntilNextLevel,
chestGranted: mastery.chestGranted,
tokensEarned: mastery.tokensEarned,
summonerId: mastery.summonerId,
champion: allChampionsById.get(mastery.championId),
}));
return { result };
},
},
});
pack.addSyncTable({
name: "Matches",
schema: schemas.MatchSchema,
identityName: "Matches",
formula: {
name: "SyncMatches",
description: "Sync match history data for a summoner.",
parameters: [
SummonerNameParameter,
RegionParameter,
coda.makeParameter({
type: coda.ParameterType.Number,
name: "Count",
description: "Number of matches to return. Valid values: 0 to 100. Defaults to 20.",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.Number,
name: "Start",
description: "Start index. Defaults to 0.",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.Number,
name: "Start Time",
description:
"Epoch timestamp in seconds. Matches played before June 16th, 2021 won't be included if this is set.",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.Number,
name: "End Time",
description: "Epoch timestamp in seconds.",
optional: true,
}),
],
execute: async function (
[name, region, count, start, startTime, endTime]: [string, string, number, number, number, number],
context: coda.ExecutionContext
) {
const puuid: string = await helpers.getPuuidByName(name, region, context.fetcher);
const matchIds: string[] = await helpers.getMatchIdsByPuuid(
puuid,
region,
context.fetcher,
startTime,
endTime,
start,
count
);
const matches: Match[] = await Promise.all(
matchIds.map(
(matchId: string): Promise<Match> => helpers.getMatchByMatchId(matchId, region, context.fetcher, puuid)
)
);
return { result: matches };
},
},
});