Skip to content

Commit

Permalink
add route hasBuildTeamToolsInstalled
Browse files Browse the repository at this point in the history
  • Loading branch information
MineFact committed Dec 15, 2023
1 parent 5e6ad14 commit 00df992
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ const network = new Network(plotsystemDatabase, networkDatabase);
Joi,
network
);
(await import("./routes/teams/GET_TeamHasBuildTeamToolsInstalled.js")).initRoutes(
router,
Joi,
network
);
(await import("./routes/teams/GET_TeamHeadID.js")).initRoutes(
router,
Joi,
Expand Down Expand Up @@ -150,6 +155,11 @@ router.use(helmet());
);

// Init PUT Routes for the API
(await import("./routes/teams/PUT_TeamHasBuildTeamToolsInstalled.js")).initRoutes(
router,
Joi,
network
);
(await import("./routes/plotsystem/teams/PUT_Plots.js")).initRoutes(
router,
Joi,
Expand Down
30 changes: 30 additions & 0 deletions src/routes/teams/GET_TeamHasBuildTeamToolsInstalled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Router } from "express";
import Network from "../../struct/core/network.js";

export async function initRoutes(app: Router, joi: any, network: Network) {

app.get('/api/teams/:key/hasBuildTeamToolsInstalled', async function (req, res) {

// Validate that the Key is a valid API Key or Build Team ID or Build Team Tag or BuildTeam Server ID
const type = await network.validateKey(req, res)
if(type == null)
return;

const buildTeam = await network.getBuildTeam(req.params.key, type);

if(buildTeam == null) {
res.status(400).send({ error: 'Build Team not found' });
return;
}

const value = await buildTeam.getBuildTeamInfo("hasBuildTeamToolsInstalled");

if(value == null) {
res.status(400).send({ error: 'HasBuildTeamToolsInstalled variable for this Build Team not found' });
return;
}

res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({hasBuildTeamToolsInstalled : value}))
})
}
62 changes: 62 additions & 0 deletions src/routes/teams/PUT_TeamHasBuildTeamToolsInstalled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Router } from "express";
import Network, { BuildTeamIdentifier } from "../../struct/core/network.js";

export async function initRoutes(app: Router, joi: any, network: Network) {

app.put('/api/teams/:apikey/hasBuildTeamToolsInstalled', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);

if(buildTeam == null) {
res.status(400).send({ error: 'Build Team not found' });
return;
}

// Validate the parameters with joi
const schema = joi.object({
hasBuildTeamToolsInstalled: joi.boolean().required()
});

const validation = schema.validate(req.body);

// If the validation failed, return an error
if(validation.error != null){
res.status(400).send({success: false, error: validation.error.details[0].message});
return;
}

// Get the current warp
const warps = await buildTeam.getWarps();
const warp = warps.find((warp: any) => warp.ID == req.body.ID);

// If the warp was not found, return an error
if(warp == null){
res.status(400).send({success: false, error: 'Warp not found in this team.'});
return;
}

// Get the parameters from the request
let hasBuildTeamToolsInstalled = req.body.hasBuildTeamToolsInstalled; // The new value of the variable hasBuildTeamToolsInstalled

// Update the variable
const promise = buildTeam.setHasBuildTeamToolsInstalled(hasBuildTeamToolsInstalled);


// Wait for the promise to resolve
promise.then((success) => {
// If the variable was not updated, return an error
if(!success){
res.status(400).send({success: false, error: 'An error occurred while updating the variable hasBuildTeamToolsInstalled'});
return;
}

// Return the success message to the client
res.setHeader('Content-Type', 'application/json');
res.send({success: true})
})
})
}
41 changes: 28 additions & 13 deletions src/struct/core/buildteam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export default class BuildTeam {
return this.buildTeamInfo[key];
}

/** Updates the variable hasBuildTeamToolsInstalled for the Build Team */
async setHasBuildTeamToolsInstalled(hasBuildTeamToolsInstalled: boolean){
return await this.updateHasBuildTeamToolsInstalledInDatabase(hasBuildTeamToolsInstalled);
}

/** Creates a new warp for the build team.
*
* @param key The key of the warp
Expand Down Expand Up @@ -474,7 +479,7 @@ export default class BuildTeam {
/* DATABASE GET REQUESTS */
/* =================================================== */

async getBuildTeamIDFromDatabase(){
private async getBuildTeamIDFromDatabase(){
const SQL = "SELECT ID as btid FROM BuildTeams WHERE APIKey = ?";
const result = await this.nwDatabase.query(SQL, [this.apiKey]);

Expand All @@ -484,7 +489,7 @@ export default class BuildTeam {
return result[0].btid;
}

async getPSBuildTeamIDFromDatabase(){
private async getPSBuildTeamIDFromDatabase(){
const SQL = "SELECT plotsystem_buildteams.id FROM plotsystem_buildteams, plotsystem_api_keys WHERE plotsystem_api_keys.api_key = ? AND plotsystem_api_keys.id = plotsystem_buildteams.api_key_id";
const result = await this.psDatabase.query(SQL, [this.apiKey]);

Expand All @@ -494,32 +499,32 @@ export default class BuildTeam {
return result[0].id;
}

async getPSCountriesFromDatabase(){
private async getPSCountriesFromDatabase(){
const SQL = "SELECT a.* FROM plotsystem_countries as a, plotsystem_buildteam_has_countries as b WHERE buildteam_id = ? AND a.id = b.country_id";
return await this.psDatabase.query(SQL, [this.psBuildTeamID]);
}

async getPSCitiesFromDatabase(country_id: number){
private async getPSCitiesFromDatabase(country_id: number){
const SQL = "SELECT * FROM plotsystem_city_projects WHERE country_id = ?";
return await this.psDatabase.query(SQL, [country_id]);
}

async getPSServersFromDatabase(country_id: number){
private async getPSServersFromDatabase(country_id: number){
const SQL = "SELECT * FROM plotsystem_servers WHERE id = (SELECT server_id FROM plotsystem_countries WHERE id = ?)";
return await this.psDatabase.query(SQL, [country_id]);
}

async getPSFTPConfigurationFromDatabase(server_id: number){
private async getPSFTPConfigurationFromDatabase(server_id: number){
const SQL = "SELECT * FROM plotsystem_ftp_configurations as a WHERE a.id = (SELECT ftp_configuration_id FROM plotsystem_servers as b WHERE b.id = ?)";
return await this.psDatabase.query(SQL, [server_id]);
}

async getPSCityPlotsFromDatabase(city_id: number){
private async getPSCityPlotsFromDatabase(city_id: number){
const SQL = "SELECT * FROM plotsystem_plots WHERE city_project_id = ?";
return await this.psDatabase.query(SQL, [city_id]);
}

async getPSCityReviewsFromDatabase(city_id: number){
private async getPSCityReviewsFromDatabase(city_id: number){
const SQL = "SELECT a.* FROM plotsystem_reviews as a, plotsystem_plots as b WHERE a.id = b.review_id";
return await this.psDatabase.query(SQL, [city_id]);
}
Expand All @@ -530,7 +535,7 @@ export default class BuildTeam {
/* DATABASE POST REQUEST */
/* =================================================== */

async createPSPlotInDatabase(city_project_id: number, difficulty_id: number, mc_coordinates: [number, number, number], outline: any, create_player: string, version: string){
private async createPSPlotInDatabase(city_project_id: number, difficulty_id: number, mc_coordinates: [number, number, number], outline: any, create_player: string, version: string){
const SQL = "INSERT INTO plotsystem_plots (city_project_id, difficulty_id, mc_coordinates, outline, create_player, version) VALUES (?, ?, ?, ?, ?, ?)";

const result = await this.psDatabase.query(SQL, [city_project_id, difficulty_id, mc_coordinates, outline, create_player, version]);
Expand All @@ -541,7 +546,7 @@ export default class BuildTeam {
return false;
}

async createWarpInDatabase(ID: string, buildTeamID: string, name: string, countryCode: string, subRegion: string, city: string, worldName: string, lat: number, lon: number, height: number, yaw: number, pitch: number, isHighlight: boolean) {
private async createWarpInDatabase(ID: string, buildTeamID: string, name: string, countryCode: string, subRegion: string, city: string, worldName: string, lat: number, lon: number, height: number, yaw: number, pitch: number, isHighlight: boolean) {
const SQL = "INSERT INTO BuildTeamWarps (ID, BuildTeam, Name, CountryCode, SubRegion, City, WorldName, Latitude, Longitude, Height, Yaw, Pitch, IsHighlight) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

const result = await this.nwDatabase.query(SQL, [ID, buildTeamID, name, countryCode, subRegion, city, worldName, lat, lon, height, yaw, pitch, isHighlight]);
Expand All @@ -557,7 +562,7 @@ export default class BuildTeam {
/* =================================================== */

// Updates the plot with the given plot id. If one of the parameters is null, the value in the database is not updated.
async updatePSPlotInDatabase(plot_id: number, city_project_id: number, difficulty_id: number, review_id: number, owner_uuid: string, member_uuids: string[], status: any, mc_coordinates: [number, number, number], outline: any, score: string, last_activity: string, pasted: string, type: string, version: string){
private async updatePSPlotInDatabase(plot_id: number, city_project_id: number, difficulty_id: number, review_id: number, owner_uuid: string, member_uuids: string[], status: any, mc_coordinates: [number, number, number], outline: any, score: string, last_activity: string, pasted: string, type: string, version: string){
const SQL = "UPDATE plotsystem_plots SET city_project_id = ?, difficulty_id = ?, review_id = ?, owner_uuid = ?, member_uuids = ?, status = ?, mc_coordinates = ?, outline = ?, score = ?, last_activity = ?, pasted = ?, type = ?, version = ? WHERE id = ?";

const result = await this.psDatabase.query(SQL, [city_project_id, difficulty_id, review_id, owner_uuid, member_uuids, status, mc_coordinates, outline, score, last_activity, pasted, type, version, plot_id]);
Expand All @@ -569,7 +574,7 @@ export default class BuildTeam {
}

// Updates an existing warp in the database
async updateWarpInDatabase(ID: string, buildTeamID: string, name: string, countryCode: string, subRegion: string, city: string, worldName: string, lat: number, lon: number, height: number, yaw: number, pitch: number, isHighlight: boolean) {
private async updateWarpInDatabase(ID: string, buildTeamID: string, name: string, countryCode: string, subRegion: string, city: string, worldName: string, lat: number, lon: number, height: number, yaw: number, pitch: number, isHighlight: boolean) {
const SQL = "UPDATE BuildTeamWarps SET ID = ?, BuildTeam = ?, Name = ?, CountryCode = ?, SubRegion = ?, City = ?, WorldName = ?, Latitude = ?, Longitude = ?, Height = ?, Yaw = ?, Pitch = ?, IsHighlight = ? WHERE ID = ? AND BuildTeam = ?";

const result = await this.nwDatabase.query(SQL, [ID, buildTeamID, name, countryCode, subRegion, city, worldName, lat, lon, height, yaw, pitch, isHighlight, ID, buildTeamID]);
Expand All @@ -580,13 +585,23 @@ export default class BuildTeam {
return false;
}

private async updateHasBuildTeamToolsInstalledInDatabase(hasBuildTeamToolsInstalled: boolean) {
const SQL = "UPDATE BuildTeams SET hasBuildTeamToolsInstalled = ? WHERE ID = ?";

const result = await this.nwDatabase.query(SQL, [hasBuildTeamToolsInstalled, this.buildTeamID]);

if(result.affectedRows == 1)
return true;
else
return false;
}

/* =================================================== */
/* DATABASE DELETE REQUEST */
/* =================================================== */

// Deletes a warp from the database
async deleteWarpInDatabase(key: string) {
private async deleteWarpInDatabase(key: string) {
const SQL = "DELETE FROM BuildTeamWarps WHERE (Name = ? OR ID = ?) AND BuildTeam = ?";

const result = await this.nwDatabase.query(SQL, [key, key, this.buildTeamID]);
Expand Down
82 changes: 82 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@
}
}
},
"/api/teams": {
"get": {
"tags": [
"Build Teams"
],
"summary": "Get all Information about all Build Teams",
"description": "Returns all general information about all Build Teams of BuildTheEarth.",
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/teams/%KEY%": {
"get": {
"tags": [
Expand Down Expand Up @@ -349,6 +363,74 @@
}
}
},
"/api/teams/%KEY%/hasBuildTeamToolsInstalled": {
"get": {
"tags": [
"Build Teams"
],
"summary": "Check if Build Team has BuildTeamTools Installed",
"description": "Returns a boolean if the BuildTeam has BuildTeamTools Installed https://www.spigotmc.org/resources/buildteamtools.101854/.",
"parameters": [
{
"name": "KEY",
"description": "The API Key, Build Team ID, Build Team Tag, or Build Team Server of the Build Team.",
"in": "path",
"schema": {
"type": "string"
},
"required": true
}
],
"responses": {
"200": {
"description": "Success"
}
}
},
"put": {
"tags": [
"Build Teams"
],
"summary": "Updates the variable hasBuildTeamToolsInstalled",
"description": "This route is used by the BuildTeamTools plugin to tell that its installed on the build team server.",
"parameters": [
{
"name": "api_key",
"in": "path",
"schema": {
"type": "string"
},
"required": true
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"hasBuildTeamToolsInstalled": {
"type": "boolean",
"description": "The variable that gets changed.",
"required": true
}
},
"example": [
{
"hasBuildTeamToolsInstalled": true
}
]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/teams/%KEY%/headid": {
"get": {
"tags": [
Expand Down

0 comments on commit 00df992

Please sign in to comment.