-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathAboveApi.js
169 lines (149 loc) · 5.35 KB
/
AboveApi.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/** AboveApi - functions that call the abovevtt api endpoints */
class AboveApiConfig {
constructor(baseUrl) {
this.#baseUrl = baseUrl
}
/** @return {string} */
get baseUrl() { return this.#baseUrl; }
#baseUrl;
static get dev() {
return new AboveApiConfig("https://jiv5p31gj3.execute-api.eu-west-1.amazonaws.com");
}
static get prod() {
return new AboveApiConfig("https://services.abovevtt.net");
}
}
class AboveApi {
static #buildUrl(action, extraParams) {
const url = `${this.config.baseUrl}/services?action=${action}&campaign=${window.CAMPAIGN_SECRET}`;
const extraParamString = $.param(extraParams);
if (extraParamString) {
return `${url}&${extraParamString}`;
}
return url;
}
static get config() {
if (new URLSearchParams(window.location.search).has("dev")) {
return AboveApiConfig.dev;
} else if (typeof AVTT_ENVIRONMENT?.baseUrl === "string" && AVTT_ENVIRONMENT.baseUrl.length > 1) {
return new AboveApiConfig(AVTT_ENVIRONMENT.baseUrl);
} else {
return AboveApiConfig.prod;
}
};
static async fetchJson(action, extraParams) {
const url = this.#buildUrl(action, extraParams);
const request = await fetch(url);
const response = await request.json();
this.checkForErrors(response);
return response;
}
static checkForErrors(response) {
if (typeof response.message === "string" && response.message.toLowerCase().includes("error")) {
throw new Error(response.message);
}
}
static async getSceneList() {
if (!window.DM) return;
const response = await this.fetchJson("getSceneList");
if (response.Items && Array.isArray(response.Items)) {
console.log("AboveApi.getSceneList", response.Items);
return response.Items.map(item => item.data);
} else {
console.log("AboveApi.getSceneList returned an empty list");
return [];
}
}
static async getCurrentScene() {
const response = await this.fetchJson("getCurrentScene");
console.log("AboveApi.getCurrentScene", response);
if(!window.DM && response.playerscene.players){
if(response.playerscene[window.PLAYER_ID])
response.playerscene = response.playerscene[window.PLAYER_ID];
else
response.playerscene = response.playerscene.players;
}
window.splitPlayerScenes = response.playerscene
return response;
}
// Until we store more than {cloud:1} this isn't necessary
static async getCampaignData() {
const response = await this.fetchJson("getCampaignData");
console.log("AboveApi.getCampaignData", response);
if(response.Item && response.Item.data) {
return response.Item.data
}
return {};
}
// Until we store more than {cloud:1} this isn't necessary
static async setCampaignData() {
const config = {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({cloud:1})
}
const url = this.#buildUrl("setCampaignData")
const request = await fetch(url, config);
const response = await request.json();
console.log("AboveApi.setCampaignData", response);
return response;
}
static async getScene(sceneId) {
const response = await this.fetchJson("getScene", {scene: sceneId});
console.log(`AboveApi.getScene(${sceneId})`, response);
return response;
}
static async exportScenes() {
const response = await this.fetchJson("export_scenes");
console.log(`AboveApi.exportScenes`, response);
return response;
}
static async migrateScenes(gameId, scenes) {
console.debug(`AboveApi.migrateScenes gameId: ${gameId}`, scenes);
if (!Array.isArray(scenes)) {
throw new Error(`AboveApi.migrateScenes received the wrong data type: ${typeof scenes}`);
}
if (scenes.length === 0) {
throw new Error(`AboveApi.migrateScenes received an empty list of scenes`);
}
for(let i = 0; i<scenes.length; i++){
if(Array.isArray(scenes[i].tokens)){
let tokensObject = {}
for(let token in scenes[i].tokens){
let tokenId = scenes[i].tokens[token].id;
tokensObject[tokenId] = scenes[i].tokens[token];
}
scenes[i].tokens = tokensObject;
}
}
// never upload data urls
const sanitizedScenes = await normalize_scene_urls(scenes);
console.log(`AboveApi.migrateScenes about to upload`, sanitizedScenes);
if(JSON.stringify(sanitizedScenes).length > 4000000 && sanitizedScenes.length>1) {
let newScenes1 = sanitizedScenes.slice(0, parseInt(sanitizedScenes.length/2))
let newScenes2 = sanitizedScenes.slice(parseInt(sanitizedScenes.length/2), sanitizedScenes.length)
await AboveApi.migrateScenes(window.gameId, newScenes1);
await AboveApi.migrateScenes(window.gameId, newScenes2);
}
else{
const url = this.#buildUrl("migrate");
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(sanitizedScenes)
}
const request = await fetch(url, config);
console.log("AboveApi.migrateScenes request", request);
const response = await request.text();
console.log("AboveApi.migrateScenes response", response);
localStorage.setItem(`Migrated${gameId}`, "1");
}
return sanitizedScenes;
}
}