Skip to content

Commit

Permalink
Update JSDocs.
Browse files Browse the repository at this point in the history
Include more docs for the MapFlowService.
  • Loading branch information
PhlexPlexico committed Jun 17, 2023
1 parent dc3a025 commit ed518b9
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 34 deletions.
7 changes: 4 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ const options = {
openapi: "3.0.0", // Specification (optional, defaults to swagger: '2.0')
info: {
title: "G5API", // Title (required)
version: "2.0.0", // Version (required)
},
version: "2.0.0" // Version (required)
}
},
// Path to the API docs
apis: [
"./dist/src/routes/**/*.js",
"./dist/src/services/**/*.js",
"./dist/src/routes/*.js"
],
]
};
const swaggerSpec = swaggerJSDoc(options);

Expand Down
2 changes: 1 addition & 1 deletion jsdoc.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"dictionaries": ["jsdoc","closure"]
},
"source": {
"include": ["src"],
"include": ["dist/src"],
"includePattern": ".+\\.js(doc|x)?$",
"excludePattern": "(^|\\/|\\\\)_"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"startprod": "pm2 start ./prodrun.json --name \"G5API\"",
"restartprod": "pm2 restart G5API",
"stopprod": "pm2 stop G5API; pm2 delete G5API",
"doc": "jsdoc -c jsdoc.conf.json",
"doc": "tsc && jsdoc -c jsdoc.conf.json",
"docclean": "rm -rf ./docs",
"migrate-create-dev": "MYSQL_FLAGS=\"-CONNECT_WITH_DB\" db-migrate --env development --config config/development.json db:create get5dev",
"migrate-create-prod": "MYSQL_FLAGS=\"-CONNECT_WITH_DB\" db-migrate --env production --config config/production.json db:create get5",
Expand Down
2 changes: 2 additions & 0 deletions src/routes/v2/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const basicRateLimit = rateLimit({
* /v2:
* post:
* description: Retrieves all logged calls from the game server and operates on them as needed, based on the event.
* Please see [events and forwards](http://splewis.github.io/get5/latest/events.html#tag/All-Events/paths/Get_OnEvent/post)
* From the get5 documentation to see what data is required for each event. Typings can also be found in the repository.
* produces:
* - application/json
* tags:
Expand Down
123 changes: 94 additions & 29 deletions src/services/mapflowservices.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/** Service class for all map flow related logic during a live game.
* @module routes/v2
* @requires express
* @requires db
*/
import { db } from "./db.js";

/**
Expand All @@ -6,6 +11,10 @@ import { db } from "./db.js";
*/
import GlobalEmitter from "../utility/emitter.js";

/**
* @const
* Utility library to check API key validity.
*/
import Utils from "../utility/utils.js";
import { Get5_OnGoingLive } from "../types/map_flow/Get5_OnGoingLive.js";
import { Response } from "express";
Expand All @@ -18,7 +27,17 @@ import SeriesFlowService from "./seriesflowservices.js";
import { Get5_OnRoundStart } from "../types/map_flow/Get5_OnRoundStart.js";
import { Get5_Player } from "../types/Get5_Player.js";

/**
* @class
* Map flow service class for live games.
*/
class MapFlowService {
/**
* Updates the database and emits mapStatUpdate when the map has gone live.
* @param {string} apiKey The API key set by the API and given to the server.
* @param {Get5_OnGoingLive} event The OnGoingLive event provided from the game server.
* @param {Response} res The express response object to send status responses to the game server.
*/
static async OnGoingLive(
apiKey: string,
event: Get5_OnGoingLive,
Expand Down Expand Up @@ -91,6 +110,13 @@ class MapFlowService {
return;
}
}

/**
* Updates the database and emits playerStatsUpdate when a player has died.
* @param {string} apiKey The API key set by the API and given to the server.
* @param {Get5_OnPlayerDeath} event The Get5_OnPlayerDeath event provided from the game server.
* @param {Response} res The express response object to send status responses to the game server.
*/
static async OnPlayerDeath(
apiKey: string,
event: Get5_OnPlayerDeath,
Expand Down Expand Up @@ -297,6 +323,12 @@ class MapFlowService {
}
}

/**
* Updates the database and emits bombEvent when a bomb has been planted or defused.
* @param {string} apiKey The API key set by the API and given to the server.
* @param {Get5_OnBombEvent} event The Get5_OnBombEvent event provided from the game server.
* @param {Response} res The express response object to send status responses to the game server.
*/
static async OnBombEvent(
apiKey: string,
event: Get5_OnBombEvent,
Expand Down Expand Up @@ -360,6 +392,12 @@ class MapFlowService {
}
}

/**
* Updates the database and emits playerStatsUpdate when a round has ended.
* @param {string} apiKey The API key set by the API and given to the server.
* @param {Get5_OnRoundEnd} event The Get5_OnRoundEnd event provided from the game server.
* @param {Response} res The express response object to send status responses to the game server.
*/
static async OnRoundEnd(
apiKey: string,
event: Get5_OnRoundEnd,
Expand Down Expand Up @@ -389,7 +427,10 @@ class MapFlowService {
]);
sqlString =
"SELECT * FROM player_stats WHERE match_id = ? AND map_id = ?";
playerStats = await db.query(sqlString, [event.matchid, mapStatInfo[0].id]);
playerStats = await db.query(sqlString, [
event.matchid,
mapStatInfo[0].id
]);
for (let player of event.team1.players) {
singlePlayerStat = playerStats.filter(
(dbPlayer) => dbPlayer.steam_id == player.steamid
Expand Down Expand Up @@ -421,7 +462,19 @@ class MapFlowService {
}
}

private static async updatePlayerStats(event: Get5_OnRoundEnd, mapId: number, player: Get5_Player, playerId: number | null) {
/**
* Private helper function to update player stats based on a team.
* @param {Get5_OnRoundEnd} event The Get5_OnRoundEnd event.
* @param {number} mapId The map ID from the database.
* @param {Get5_Player} player The Get5_Player structure.
* @param {number} playerId The player ID from the database.
*/
private static async updatePlayerStats(
event: Get5_OnRoundEnd,
mapId: number,
player: Get5_Player,
playerId: number | null
) {
let insUpdStatement: object;
let sqlString: string;
insUpdStatement = {
Expand Down Expand Up @@ -475,6 +528,12 @@ class MapFlowService {
}
}

/**
* Updates the database and emits playerStatsUpdate when a round has been restored and the match has started again.
* @param {string} apiKey The API key set by the API and given to the server.
* @param {Get5_OnRoundStart} event The Get5_OnRoundStart event provided from the game server.
* @param {Response} res The express response object to send status responses to the game server.
*/
static async OnRoundStart(
apiKey: string,
event: Get5_OnRoundStart,
Expand All @@ -491,38 +550,44 @@ class MapFlowService {
message:
"Match already finalized or and invalid API key has been given."
});
// Check if round was backed up and nuke the additional player stats and bomb plants.
if (SeriesFlowService.wasRoundRestored) {
sqlString =
"SELECT id FROM map_stats WHERE match_id = ? AND map_number = ?";
mapStatInfo = await db.query(sqlString, [
event.matchid,
event.map_number
]);
}
// Check if round was backed up and nuke the additional player stats and bomb plants.
if (SeriesFlowService.wasRoundRestored) {
sqlString =
"SELECT id FROM map_stats WHERE match_id = ? AND map_number = ?";
mapStatInfo = await db.query(sqlString, [
event.matchid,
event.map_number
]);

sqlString =
"DELETE FROM match_bomb_plants WHERE round_number > ? AND match_id = ? AND map_id = ?";
await db.query(sqlString, [
event.round_number,
event.matchid,
mapStatInfo[0].id
]);
sqlString =
"DELETE FROM match_bomb_plants WHERE round_number > ? AND match_id = ? AND map_id = ?";
await db.query(sqlString, [
event.round_number,
event.matchid,
mapStatInfo[0].id
]);

sqlString =
"DELETE FROM player_stat_extras WHERE match_id = ? AND map_id = ? AND round_number > ?";
await db.query(sqlString, [
event.matchid,
mapStatInfo[0].id,
event.round_number
]);
SeriesFlowService.wasRoundRestored = false;
}
GlobalEmitter.emit("playerStatsUpdate");
res.status(200).send({ message: "Success" });
return;
sqlString =
"DELETE FROM player_stat_extras WHERE match_id = ? AND map_id = ? AND round_number > ?";
await db.query(sqlString, [
event.matchid,
mapStatInfo[0].id,
event.round_number
]);
SeriesFlowService.wasRoundRestored = false;
}
GlobalEmitter.emit("playerStatsUpdate");
res.status(200).send({ message: "Success" });
return;
}

/**
* Updates the database and emits matchUpdate when a match has been paused or unpaused.
* @param {string} apiKey The API key set by the API and given to the server.
* @param {Get5_OnMatchPausedUnpaused} event The Get5_OnMatchPausedUnpaused event provided from the game server.
* @param {Response} res The express response object to send status responses to the game server.
*/
static async OnMatchPausedUnPaused(
apiKey: string,
event: Get5_OnMatchPausedUnpaused,
Expand Down

0 comments on commit ed518b9

Please sign in to comment.