Skip to content

Commit

Permalink
changed subRegion and city to address
Browse files Browse the repository at this point in the history
  • Loading branch information
MineFact committed Jan 25, 2024
1 parent 3c4dba2 commit 098a998
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/routes/teams/GET_WarpGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {

app.get('/api/teams/warpgroups', async function (req, res) {

let value = await network.getWarpGroups();
let value = await network.getBuildTeamWarpGroups();

if(value == null) {
res.status(400).send({ error: 'Warp Groups not found' });
Expand Down
8 changes: 1 addition & 7 deletions src/routes/teams/GET_Warps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/teams/warps', async function (req, res) {

const country = req.query.country; // (Optional) Only get warps from this country
const subRegion = req.query.subRegion; // (Optional) Only get warps from this subRegion
const city = req.query.city; // (Optional) Only get warps from this city

let value = await network.getWarps();
let value = await network.getBuildTeamWarps();

if(country != null)
value = value.filter((warp: any) => warp.CountryCode == country);
if(subRegion != null)
value = value.filter((warp: any) => warp.SubRegion == subRegion);
if(city != null)
value = value.filter((warp: any) => warp.City == city);


if(value == null) {
Expand Down
48 changes: 27 additions & 21 deletions src/routes/teams/POST_Warp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from "express";
import Network, { BuildTeamIdentifier } from "../../struct/core/network.js";
import Network, { AddressType, BuildTeamIdentifier } from "../../struct/core/network.js";

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

Expand All @@ -23,8 +23,8 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
warpgroup: joi.string().optional(),
countryCode: joi.string().required(),
countryCodeType: joi.string().required().valid('cca2', 'cca3', 'ccn3', 'cioc'),
subRegion: joi.string().required(),
city: joi.string().required(),
address: joi.string().optional(),
addressType: joi.string().optional(),

worldName: joi.string().required(),
lat: joi.number().required(),
Expand All @@ -46,26 +46,25 @@ export async function initRoutes(app: Router, joi: any, network: Network) {


// Get the parameters from the request
const id = req.body.id; // The id of the warp.
const warpGroupID = req.body.warpGroupID // The id of the warp group.
const name = req.body.name; // The name of the warp.
const countryCode = req.body.countryCode; // Country Code that matches the countryCodeType.
const countryCodeType = req.body.countryCodeType; // Country Code Type like cca2, cca3, ccn3, or cioc.
const subRegion = req.body.subRegion; // Name of the the subregion like state or province.
const city = req.body.city; // Name of the city.

const worldName = req.body.worldName; // The name of the world the warp is in.
const lat = req.body.lat; // The latitude of the warp.
const lon = req.body.lon; // The longitude of the warp.
const y = req.body.y; // The y coordinate of the warp.
const yaw = req.body.yaw; // The yaw of the warp.
const pitch = req.body.pitch; // The pitch of the warp.

const isHighlight = req.body.isHighlight; // Whether the warp is a highlight or not.

const id = req.body.id; // The id of the warp.
const warpGroupID = req.body.warpGroupID // The id of the warp group.
const name = req.body.name; // The name of the warp.
const countryCode = req.body.countryCode; // Country Code that matches the countryCodeType.
const countryCodeType = req.body.countryCodeType; // Country Code Type like cca2, cca3, ccn3, or cioc.
const address = req.body.address; // The address of the warp.
const addressType: AddressType = convertStringToEnum(req.body.addressType); // The type of address. (STREET, CITY, STATE, COUNTRY)

const worldName = req.body.worldName; // The name of the world the warp is in.
const lat = req.body.lat; // The latitude of the warp.
const lon = req.body.lon; // The longitude of the warp.
const y = req.body.y; // The y coordinate of the warp.
const yaw = req.body.yaw; // The yaw of the warp.
const pitch = req.body.pitch; // The pitch of the warp.

const isHighlight = req.body.isHighlight; // Whether the warp is a highlight or not.

// Create a new warp
const promise = buildTeam.createWarp(id, warpGroupID, name, countryCode, countryCodeType, subRegion, city, worldName, lat, lon, y, yaw, pitch, isHighlight);
const promise = buildTeam.createWarp(id, warpGroupID, name, countryCode, countryCodeType, address, addressType, worldName, lat, lon, y, yaw, pitch, isHighlight);


// Wait for the promise to resolve
Expand All @@ -81,4 +80,11 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
res.send({success: true})
})
})

function convertStringToEnum(input: string): AddressType {
if (input in AddressType) {
return AddressType[input as keyof typeof AddressType];
}
return AddressType.CITY;
}
}
14 changes: 5 additions & 9 deletions src/routes/teams/PUT_Warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
name: joi.string().optional(),
countryCode: joi.string().optional(),
countryCodeType: joi.string().optional().valid('cca2', 'cca3', 'ccn3', 'cioc'),
subRegion: joi.string().optional(),
city: joi.string().optional(),
address: joi.string().optional(),

worldName: joi.string().optional(),
lat: joi.number().optional(),
Expand Down Expand Up @@ -61,8 +60,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
let name = warp.Name; // The name of the warp.
let countryCode = warp.CountryCode; // Country Code that matches the countryCodeType.
let countryCodeType = "cca3"; // Country Code Type like cca2, cca3, ccn3, or cioc.
let subRegion = warp.SubRegion; // Name of the the subregion like state or province.
let city = warp.City; // Name of the city.
let address = warp.Address; // The address of the warp.

let worldName = warp.WorldName; // The name of the world the warp is in.
let lat = warp.Latitude; // The latitude of the warp.
Expand All @@ -85,10 +83,8 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
countryCode = req.body.countryCode;
if(req.body.countryCodeType != null)
countryCodeType = req.body.countryCodeType;
if(req.body.subRegion != null)
subRegion = req.body.subRegion;
if(req.body.city != null)
city = req.body.city;
if(req.body.address != null)
address = req.body.address;
if(req.body.worldName != null)
worldName = req.body.worldName;
if(req.body.lat != null)
Expand All @@ -106,7 +102,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {


// Update the warp
const promise = buildTeam.updateWarp(id, warpGroupID, name, countryCode, countryCodeType, subRegion, city, worldName, lat, lon, y, yaw, pitch, isHighlight);
const promise = buildTeam.updateWarp(id, warpGroupID, name, countryCode, countryCodeType, address, worldName, lat, lon, y, yaw, pitch, isHighlight);


// Wait for the promise to resolve
Expand Down
42 changes: 24 additions & 18 deletions src/struct/core/buildteam.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DatabaseHandler from "../database.js";
import Network from "./network.js";
import Network, { AddressType } from "./network.js";
import { v4 as uuidv4 } from 'uuid';
import fs from 'fs/promises';
import path from 'path';
Expand Down Expand Up @@ -251,8 +251,7 @@ export default class BuildTeam {
* @param name The name of the warp
* @param countryCode Country Code that matches the countryCodeType
* @param countryCodeType Country Code Type like cca2, cca3, ccn3, or cioc
* @param subRegion Name of the the subregion like state or province.
* @param city Name of the city
* @param address The address of the warp
* @param worldName The name of the world the warp is in
* @param lat The latitude of the warp
* @param lon The longitude of the warp
Expand All @@ -263,11 +262,19 @@ export default class BuildTeam {
*
* @returns Returns true if the warp was created successfully, otherwise false.
**/
async createWarp(id: string|null, warpGroupID: string, name: string, countryCode: string, countryCodeType: string, subRegion: string, city: string, worldName: string, lat: number, lon: number, y: number, yaw: number, pitch: number, isHighlight: boolean) {
async createWarp(id: string|null, warpGroupID: string|null, name: string, countryCode: string, countryCodeType: string, address: string|null, addressType: AddressType, worldName: string, lat: number, lon: number, y: number, yaw: number, pitch: number, isHighlight: boolean) {
// Generate a new uuid if the id is null
if(id == null)
if(id == null || id == undefined)
id = uuidv4();

// Get the address of the warp if it is null
if(address == null || address == undefined)
address = await this.network.getAddressFromCoordinates(lat, lon, addressType);

// If warpGroupID is undefined, set it to null
if(warpGroupID == undefined)
warpGroupID = null;

// Validate that the build team id is loaded
if(this.buildTeamID == null)
await this.loadBuildTeamData();
Expand Down Expand Up @@ -302,7 +309,7 @@ export default class BuildTeam {
return false;
}

return await this.createWarpInDatabase(id, this.buildTeamID, warpGroupID, name, finalCountryCode, subRegion, city, worldName, lat, lon, y, yaw, pitch, isHighlight);
return await this.createWarpInDatabase(id, this.buildTeamID, warpGroupID, name, finalCountryCode, address, worldName, lat, lon, y, yaw, pitch, isHighlight);
}


Expand All @@ -313,8 +320,7 @@ export default class BuildTeam {
* @param name The new name of the warp
* @param countryCode The new Country Code that matches the countryCodeType
* @param countryCodeType Country Code Type like cca2, cca3, ccn3, or cioc
* @param subRegion The new name of the the subregion like state or province.
* @param city The new name of the city
* @param address The new address of the warp
* @param worldName The name of the world the warp is in
* @param lat The new latitude of the warp
* @param lon The new longitude of the warp
Expand All @@ -325,7 +331,7 @@ export default class BuildTeam {
*
* @returns Returns true if the warp was created successfully, otherwise false.
**/
async updateWarp(ID: string, warpGroupID: string, name: string, countryCode: string, countryCodeType: string, subRegion: string, city: string, worldName: string, lat: number, lon: number, y: number, yaw: number, pitch: number, isHighlight: boolean) {
async updateWarp(ID: string, warpGroupID: string, name: string, countryCode: string, countryCodeType: string, address: string, worldName: string, lat: number, lon: number, y: number, yaw: number, pitch: number, isHighlight: boolean) {
// Validate that the build team id is loaded
if(this.buildTeamID == null)
await this.loadBuildTeamData();
Expand Down Expand Up @@ -365,7 +371,7 @@ export default class BuildTeam {
return false;
}

return await this.updateWarpInDatabase(ID, this.buildTeamID, warpGroupID, name, finalCountryCode, subRegion, city, worldName, lat, lon, y, yaw, pitch, isHighlight);
return await this.updateWarpInDatabase(ID, this.buildTeamID, warpGroupID, name, finalCountryCode, address, worldName, lat, lon, y, yaw, pitch, isHighlight);
}


Expand All @@ -392,7 +398,7 @@ export default class BuildTeam {
if(this.buildTeamID == null)
return [];

const result = await this.network.getWarps();
const result = await this.network.getBuildTeamWarps();

if(result == null)
return [];
Expand Down Expand Up @@ -488,7 +494,7 @@ export default class BuildTeam {
if(this.buildTeamID == null)
return [];

const result = await this.network.getWarpGroups();
const result = await this.network.getBuildTeamWarpGroups();

if(result == null)
return [];
Expand Down Expand Up @@ -788,10 +794,10 @@ export default class BuildTeam {
return false;
}

private async createWarpInDatabase(ID: string, buildTeamID: string, warpGroupID: 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, WarpGroup, Name, CountryCode, SubRegion, City, WorldName, Latitude, Longitude, Height, Yaw, Pitch, IsHighlight) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
private async createWarpInDatabase(ID: string, buildTeamID: string, warpGroupID: string|null, name: string, countryCode: string, address: string, worldName: string, lat: number, lon: number, height: number, yaw: number, pitch: number, isHighlight: boolean) {
const SQL = "INSERT INTO BuildTeamWarps (ID, BuildTeam, WarpGroup, Name, CountryCode, Address, WorldName, Latitude, Longitude, Height, Yaw, Pitch, IsHighlight) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

const result = await this.nwDatabase.query(SQL, [ID, buildTeamID, warpGroupID, name, countryCode, subRegion, city, worldName, lat, lon, height, yaw, pitch, isHighlight]);
const result = await this.nwDatabase.query(SQL, [ID, buildTeamID, warpGroupID, name, countryCode, address, worldName, lat, lon, height, yaw, pitch, isHighlight]);

if(result.affectedRows == 1)
return true;
Expand Down Expand Up @@ -827,10 +833,10 @@ export default class BuildTeam {
}

// Updates an existing warp in the database
private async updateWarpInDatabase(ID: string, buildTeamID: string, warpGroupID: 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 = ?, WarpGroup = ?, Name = ?, CountryCode = ?, SubRegion = ?, City = ?, WorldName = ?, Latitude = ?, Longitude = ?, Height = ?, Yaw = ?, Pitch = ?, IsHighlight = ? WHERE ID = ? AND BuildTeam = ?";
private async updateWarpInDatabase(ID: string, buildTeamID: string, warpGroupID: string, name: string, countryCode: string, address: string, worldName: string, lat: number, lon: number, height: number, yaw: number, pitch: number, isHighlight: boolean) {
const SQL = "UPDATE BuildTeamWarps SET ID = ?, BuildTeam = ?, WarpGroup = ?, Name = ?, CountryCode = ?, Address = ?, WorldName = ?, Latitude = ?, Longitude = ?, Height = ?, Yaw = ?, Pitch = ?, IsHighlight = ? WHERE ID = ? AND BuildTeam = ?";

const result = await this.nwDatabase.query(SQL, [ID, buildTeamID, warpGroupID, name, countryCode, subRegion, city, worldName, lat, lon, height, yaw, pitch, isHighlight, ID, buildTeamID]);
const result = await this.nwDatabase.query(SQL, [ID, buildTeamID, warpGroupID, name, countryCode, address, worldName, lat, lon, height, yaw, pitch, isHighlight, ID, buildTeamID]);

if(result.affectedRows == 1)
return true;
Expand Down
45 changes: 39 additions & 6 deletions src/struct/core/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export enum BuildTeamIdentifier {
Server = "Server"
}

export enum AddressType {
STREET = "17",
CITY = "10",
STATE = "5",
COUNTRY = "3",
}

export default class Network {
private static readonly API_KEY_UPDATE_INTERVAL: number = 10; // 10 minutes

Expand Down Expand Up @@ -353,21 +360,47 @@ export default class Network {
return buildTeamRegionsCopy;
}

async getBuildTeamWarps(){
if(this.buildTeamWarps == null)
await this.loadBuildTeamWarps();

if(this.buildTeamWarps == null)
return null;

return this.buildTeamWarps;
}

async getBuildTeamWarpGroups(){
if(this.buildTeamWarpGroups == null)
await this.loadBuildTeamWarpGroups();

if(this.buildTeamWarpGroups == null)
return null;

/** Returns a list of all warps. If no warps are found, an empty list is returned.*/
async getWarps(){
return await this.getWarpsFromDatabase();
return this.buildTeamWarpGroups;
}

/** Returns a list of all warp groups. If no warps are found, an empty list is returned.*/
async getWarpGroups(){
return await this.getWarpGroupsFromDatabase();
async getAddressFromCoordinates(lat: number, lon: number, addressType: AddressType): Promise<string>{
// Convert the addressType to a zoom level
let zoomLevel = addressType.valueOf();

// Get the address from the OpenStreetMap API
const url = `https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${lat}&lon=${lon}&zoom=${zoomLevel}`;

const response = await fetch(url);
const json = await response.json();

// Return the address
return json.display_name;
}








// Validate values

// Validate an API key that looks like this "fffb262b-0324-499a-94a6-eebf845e6123"
Expand Down
Loading

0 comments on commit 098a998

Please sign in to comment.