Repository navigation
Expand file tree
/
Copy pathAPI.js
More file actions
293 lines (272 loc) · 9 KB
/
Copy pathAPI.js
File metadata and controls
293 lines (272 loc) · 9 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/** Summary: Crea una instància del jugador a un punt aleatori del mapa a partir d'un nom escollit pel jugador, utilitzant una crida de l'API.
* @param {*} name: nom que el jugador desitja.
*/
function spawnPlayer(name) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/spawn/b89f987e/'+name);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/** Summary: Elimina el jugador actual a partir d'una crida de l'API.
* @param {*} playerToken: ID del jugador.
* @param {*} playerCode: Codi de seguretat del jugador.
*/
function removePlayer(playerToken,playerCode) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/remove/b89f987e/'+playerToken+'/'+playerCode);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/** Summary: Reviu al jugador actual, movent-lo a una posicio aleatoria, tot utilitzant l'API.
* @param {*} playerToken: ID del jugador.
*/
function respawnPlayer(playerToken) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/respawn/b89f987e/'+playerToken);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/** Summary: Retorna els jugadors i els objectes que es troben al costat del jugador, juntament amb tota la seva informacio disponible desde l'API.
* @param {*} playerToken: ID del jugador.
*/
function getNearPlayers(playerToken) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/playersobjects/b89f987e/'+playerToken);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/** Summary: Retorna la posicio de tots els enemics i objectes que hi ha el mapa a partir de la informacio disponible a l'API.
* @param {*} playerToken: ID del jugador.
*/
function getMapInfo(playerToken) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/map/b89f987e/'+playerToken);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/** Summary: Permet obtenir la informacio disponible a l'API del jugador actiu.
* @param {*} playerToken: ID del jugador.
*/
function getPlayerInfo(playerToken) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/player/b89f987e/'+playerToken);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/** Summary: Mou el jugador i actualitza la seva posició a l'API.
* @param {*} playerToken: ID del jugador.
* @param {*} direction: Direccio en la que es vol moure el jugador. Pot ser N, S, E i O.
*/
function movePlayer(playerToken,direction) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/move/b89f987e/'+playerToken+'/'+direction);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/** Summary: Ataca a un enemic que es troba davant del jugador.
* @param {*} playerToken: ID del jugador.
* @param {*} direction: Direccio en la que vol atacar el jugador. Pot ser N, S, E i O.
*/
function attack(playerToken,direction) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/attack/b89f987e/'+playerToken+'/'+direction);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/** Summary: Penja a l'API un nou objecte.
* @param {*} playerToken: ID del jugador.
* @param {*} nom: Nom de l'objecte.
* @param {*} url: URL de la imatge de l'objecte.
* @param {*} atac: Atac de l'objecte a crear.
* @param {*} defensa: Defense de l'objecte a crear.
*/
function craft(playerToken,nom,url,attack,defense) {
postData('http://battlearena.danielamo.info/api/craft/b89f987e/'+playerToken, { name: 'Llana', image: 'https://minecraft.gamepedia.com/File:White_Wool_JE2_BE2.png', attack: 10, defense: 10 })
.then(data => {
console.log('Hola');
});
}
/** Summary: Funcio asyncrona que permet penjar informacio a l'API a partir d'un fetch.
* @param {*} url: URL a on es vol penjar la informacio.
* @param {*} data: Dades que es volen penjar.
*/
async function postData(url = '', data = {}) {
// Opciones por defecto estan marcadas con un *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'no-cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
//return response.json(); // parses JSON response into native JavaScript objects
}
/** Summary: Recull un objecte que es trobi al terra i deixa anar el vell si en tenia un equipat.
* @param {*} playerToken: ID del jugador.
* @param {*} objectToken: ID de l'objecte a recollir.
*/
function pickup(playerToken,objectToken) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://battlearena.danielamo.info/api/pickup/b89f987e/'+playerToken+'/'+objectToken);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}