improved fetch in gameServer

This commit is contained in:
LuckyLaszlo
2023-01-15 02:00:42 +01:00
parent 4318b8a707
commit a2b2168884
3 changed files with 60 additions and 29 deletions

View File

@@ -279,7 +279,8 @@ export class GameSession {
gc.scoreRight = 0; gc.scoreRight = 0;
} }
} }
await fetch(c.addressBackEnd + "/game/gameserver/updategame",
fetch(`${c.addressBackEnd}/game/gameserver/updategame`,
{ {
method: "POST", method: "POST",
headers: { headers: {
@@ -290,6 +291,14 @@ export class GameSession {
playerOneUsernameResult: gc.scoreLeft, playerOneUsernameResult: gc.scoreLeft,
playerTwoUsernameResult: gc.scoreRight, playerTwoUsernameResult: gc.scoreRight,
}) })
})
.then((response) => {
if (!response.ok) {
throw new Error("HTTP " + response.status);
}
})
.catch((error) => {
console.log("catch /game/gameserver/updategame: ", error);
}); });
setTimeout(this.destroy, 15000, this); setTimeout(this.destroy, 15000, this);

View File

@@ -89,7 +89,8 @@ async function clientAnnounceListener(this: WebSocket, data: string)
if (announce.privateMatch) { if (announce.privateMatch) {
body.playerTwoUsername = announce.playerTwoUsername; body.playerTwoUsername = announce.playerTwoUsername;
} }
const response = await fetch(c.addressBackEnd + "/game/gameserver/validate",
fetch(`${c.addressBackEnd}/game/gameserver/validate`,
{ {
method: "POST", method: "POST",
headers: { headers: {
@@ -97,17 +98,17 @@ async function clientAnnounceListener(this: WebSocket, data: string)
}, },
body: JSON.stringify(body) body: JSON.stringify(body)
}) })
.catch(error => console.log("ERROR : " + error)); .then((response) => {
if (!response || !response.ok) if (!response.ok) {
{ throw new Error("HTTP " + response.status);
let errMessage = "validate token error";
if (response) {
errMessage = (await response.json()).message;
} }
this.send(JSON.stringify( new ev.EventError(errMessage) )); })
.catch((error) => {
console.log("catch /game/gameserver/validate: ", error);
this.send(JSON.stringify( new ev.EventError("validate token error") ));
clientTerminate(clientsMap.get(this.id)); clientTerminate(clientsMap.get(this.id));
return; });
}
player.matchOptions = announce.matchOptions; player.matchOptions = announce.matchOptions;
player.token = announce.token; player.token = announce.token;
player.username = announce.username; player.username = announce.username;
@@ -232,14 +233,26 @@ function privateMatchmaking(player: ClientPlayer)
if (player.socket.OPEN) { if (player.socket.OPEN) {
player.socket.send(JSON.stringify( new ev.EventMatchAbort() )); player.socket.send(JSON.stringify( new ev.EventMatchAbort() ));
} }
const response = await fetch(c.addressBackEnd + "/game/gameserver/destroysession",{
fetch(`${c.addressBackEnd}/game/gameserver/destroysession`,
{
method: "POST", method: "POST",
headers : {"Content-Type": "application/json"}, headers: {
body : JSON.stringify({ "Content-Type": "application/json",
},
body: JSON.stringify({
token : player.token token : player.token
}) })
}) })
.catch(error => console.log("ERROR : " + error)); .then((response) => {
if (!response.ok) {
throw new Error("HTTP " + response.status);
}
})
.catch((error) => {
console.log("catch /game/gameserver/destroysession: ", error);
});
clientTerminate(player); clientTerminate(player);
} }
}, 60000); }, 60000);
@@ -315,24 +328,29 @@ async function playerReadyConfirmationListener(this: WebSocket, data: string)
playerOneUsernameResult : 0, playerOneUsernameResult : 0,
playerTwoUsernameResult : 0 playerTwoUsernameResult : 0
}; };
const response = await fetch(c.addressBackEnd + "/game/gameserver/creategame",
fetch(`${c.addressBackEnd}/game/gameserver/creategame`,
{ {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(body) body: JSON.stringify(body)
}); })
if (!response.ok) .then((response) => {
{ if (!response.ok) {
throw new Error("HTTP " + response.status);
}
})
.catch((error) => {
console.log("catch /game/gameserver/creategame: ", error);
gameSessionsMap.delete(gameSession.id); gameSessionsMap.delete(gameSession.id);
gameSession.playersMap.forEach((client) => { gameSession.playersMap.forEach((client) => {
client.socket.send(JSON.stringify( new ev.EventMatchAbort() )); client.socket.send(JSON.stringify( new ev.EventMatchAbort() ));
client.gameSession = null; client.gameSession = null;
clientTerminate(client); clientTerminate(client);
}); });
return; });
}
gameSession.playersMap.forEach( (client) => { gameSession.playersMap.forEach( (client) => {
client.socket.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchStart) )); client.socket.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchStart) ));
@@ -445,18 +463,22 @@ export async function clientTerminate(client: Client)
if (client.role === en.ClientRole.player) if (client.role === en.ClientRole.player)
{ {
const player = client as ClientPlayer; const player = client as ClientPlayer;
console.log("/resetuserstatus " + player.username); fetch(`${c.addressBackEnd}/game/gameserver/resetuserstatus`,
const response = await fetch(c.addressBackEnd + "/game/gameserver/resetuserstatus",
{ {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({username: player.username}) body: JSON.stringify({username: player.username})
})
.then((response) => {
if (!response.ok) {
throw new Error("HTTP " + response.status);
}
})
.catch((error) => {
console.log("catch /game/gameserver/resetuserstatus: ", error);
}); });
if (!response.ok) {
console.log("/resetuserstatus " + player.username + " failed");
}
} }
} }

View File

@@ -4,7 +4,7 @@ export async function fetchAvatar(username: string)
return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/avatar?username=${username}`) return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/avatar?username=${username}`)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw new Error("Avatar not retrieved"); throw new Error("HTTP " + response.status);
} }
return response.blob(); return response.blob();
}) })
@@ -22,7 +22,7 @@ export async function fetchUser()
return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user`) return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user`)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw new Error("User not retrieved"); throw new Error("HTTP " + response.status);
} }
return response.json(); return response.json();
}) })
@@ -40,7 +40,7 @@ export async function fetchAllUsers()
return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/all`) return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/all`)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw new Error("All Users not retrieved"); throw new Error("HTTP " + response.status);
} }
return response.json(); return response.json();
}) })