Skip to content

Commit

Permalink
Added TypeScript, restructured, added services and types, moved cache…
Browse files Browse the repository at this point in the history
… logs to its service
  • Loading branch information
Eghizio committed May 12, 2020
1 parent c4d5a28 commit 963d491
Show file tree
Hide file tree
Showing 16 changed files with 906 additions and 127 deletions.
654 changes: 624 additions & 30 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "ogameapi",
"version": "1.0.0",
"description": "Ogame API explorer and analytic tool",
"main": "server.js",
"main": "server.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
"start": "ts-node-dev --respawn server.ts",
"build": "tsc"
},
"keywords": [
"ogame",
Expand All @@ -16,10 +17,16 @@
"author": "Eghizio",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.6",
"@types/node": "^13.13.5",
"@types/xml2js": "^0.4.5",
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"nodemon": "^1.19.3",
"ts-node": "^8.10.1",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.8.3",
"xml2js": "^0.4.22"
}
}
6 changes: 3 additions & 3 deletions server.js → server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require("express");
const bodyParser = require("body-parser");
import express from "express";
import bodyParser from "body-parser";


const app = express();
Expand All @@ -20,7 +20,7 @@ const ogameAPI = {
app.set("ogameAPI", ogameAPI);
app.set("json spaces", 2);

app.use("/api", require("./api"));
app.use("/api", require("./src/api"));

const PORT = process.env.PORT || 2137;
app.listen(PORT, () =>
Expand Down
15 changes: 8 additions & 7 deletions api/alliances.js → src/api/alliances.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// "/api/alliances"
const express = require("express");
const axios = require("axios");
const { Parser } = require("xml2js");
import { Alliances } from "../types/api";
import express from "express";
import axios from "axios";
import { Parser } from "xml2js";


const router = express.Router();
Expand All @@ -13,14 +14,14 @@ router.get("/", (req, res) => {
.then(response => response.data)
.then(xml =>
XML_Parser.parseStringPromise(xml)
.catch(err => console.log("Error parsing XML ", err))
.catch((err: Error) => console.log("Error parsing XML ", err))
.then(parsedXML => parsedXML))
.then(json => {
const orderedJSON = {
const orderedJSON: Alliances = {
serverID: json.alliances.$.serverId,
timestamp: json.alliances.$.timestamp,
alliances: [
...json.alliances.alliance.map(a =>
...json.alliances.alliance.map((a: any) =>
({
id: a.$.id,
name: a.$.name,
Expand All @@ -29,7 +30,7 @@ router.get("/", (req, res) => {
foundDate: a.$.foundDate,
...(a.$.logo && {logo: a.$.logo}),
...(a.$.open && { open: true }),
...(a.player && {players: a.player.map(p => p.$)})
...(a.player && {players: a.player.map((p: any) => p.$)})
})
)
]
Expand Down
13 changes: 7 additions & 6 deletions api/highscore.js → src/api/highscore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// "/api/highscore"
const express = require("express");
const axios = require("axios");
const { Parser } = require("xml2js");
import express from "express";
import axios from "axios";
import { Parser } from "xml2js";
import { Highscore } from "../types/api";


const router = express.Router();
Expand Down Expand Up @@ -40,11 +41,11 @@ router.get("/players", (req, res) => {
.then(parsedXML => parsedXML))
.then(json => {
const orderedJSON = {
serverId: json.highscore.$.serverId,
serverID: json.highscore.$.serverId,
timestamp: json.highscore.$.timestamp,
category: json.highscore.$.category,
type: json.highscore.$.type,
players: [...json.highscore.player.map(({$}) => $)]
players: [...json.highscore.player.map((a: any) => a.$)]
};

return orderedJSON;
Expand All @@ -71,7 +72,7 @@ router.get("/alliances", (req, res) => {
timestamp: json.highscore.$.timestamp,
category: json.highscore.$.category,
type: json.highscore.$.type,
alliances: [...json.highscore.alliance.map(({$}) => $)]
alliances: [...json.highscore.alliance.map((a: any) => a.$)]
};

return orderedJSON;
Expand Down
2 changes: 1 addition & 1 deletion api/index.js → src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// "/api/highscore"
const express = require("express");
import express from "express";


const router = express.Router();
Expand Down
15 changes: 8 additions & 7 deletions api/localization.js → src/api/localization.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// "/api/localization"
const express = require("express");
const axios = require("axios");
const { Parser } = require("xml2js");
import { Localization } from "../types/api";
import express from "express";
import axios from "axios";
import { Parser } from "xml2js";


const router = express.Router();
Expand All @@ -16,7 +17,7 @@ router.get("/", (req, res) => {
.catch(err => console.log("Error parsing XML ", err))
.then(parsedXML => parsedXML))
.then(json => {
const orderedJSON = {
const orderedJSON: Localization = {
serverID: json.localization.$.serverId,
timestamp: json.localization.$.timestamp,
buildings: [],
Expand All @@ -25,14 +26,14 @@ router.get("/", (req, res) => {
defense: [],
officers: [],
missions: [
...json.localization.missions[0].name.map(m =>
...json.localization.missions[0].name.map((m: any) =>
({ name: m._, id: m.$.id })
)
]
};

json.localization.techs[0].name.map(t => ({ name: t._, id: t.$.id }))
.forEach(t => {
json.localization.techs[0].name.map((t: any) => ({ name: t._, id: t.$.id }))
.forEach((t: any) => {
//a lot of ifs, cause why the fuck not
if(t.id <100)
orderedJSON.buildings.push(t);
Expand Down
13 changes: 7 additions & 6 deletions api/playerData.js → src/api/playerData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// "/api/playerData"
const express = require("express");
const axios = require("axios");
const { Parser } = require("xml2js");
import { PlayerData } from "../types/api";
import express from "express";
import axios from "axios";
import { Parser } from "xml2js";


const router = express.Router();
Expand All @@ -21,21 +22,21 @@ router.get("/", (req, res) => {
.catch(err => console.log("Error parsing XML ", err))
.then(parsedXML => parsedXML))
.then(json => {
const orderedJSON = {
const orderedJSON: PlayerData = {
serverID: json.playerData.$.serverId,
timestamp: json.playerData.$.timestamp,
player: {
id: json.playerData.$.id,
name: json.playerData.$.name,
...(json.playerData.positions[0] && {
highscore: [
...json.playerData.positions[0].position.map(h =>
...json.playerData.positions[0].position.map((h: any) =>
({ type: h.$.type, rank: h._, points: h.$.score, ships: h.$.ships })
)
]
}),
planets: [
...json.playerData.planets[0].planet.map(p =>
...json.playerData.planets[0].planet.map((p: any) =>
({
name: p.$.name, coords: p.$.coords, id: p.$.id,
...(p.moon && {
Expand Down
26 changes: 11 additions & 15 deletions api/players.js → src/api/players.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
// "/api/players"
const express = require("express");
const axios = require("axios");
const { Parser } = require("xml2js");
const Cache = require("../utils/Cache");
import { Players } from "../types/api";
import express from "express";
import axios from "axios";
import { Parser } from "xml2js";
import CacheService from "../services/CacheService";


const router = express.Router();
const cache = new Cache();
const cache = new CacheService();

router.get("/", (req, res) => {
// Need to move logs to cache, prolly will swap to use node-cache behind the Cache class
// Need to create services and extract the logic
console.log(`Calling "${req.baseUrl}"...`);
// console.log(`Calling "${req.baseUrl}"...`); // need to npm morgan for logs

if(cache.has(req.baseUrl)){
console.log(`Retrieving "${req.baseUrl}" from cache...`);
if(cache.has(req.baseUrl))
return res.json(cache.get(req.baseUrl));
}

console.log(`No cache item found for ${req.baseUrl}`);

const XML_Parser = new Parser();

Expand All @@ -28,10 +24,10 @@ router.get("/", (req, res) => {
.catch(err => console.log("Error parsing XML ", err))
.then(parsedXML => parsedXML))
.then(json => {
const orderedJSON = {
const orderedJSON: Players = {
serverID: json.players.$.serverId,
timestamp: json.players.$.timestamp,
players: [...json.players.player.map(({$}) => $)]
players: [...json.players.player.map((p: any) => p.$)]
};

return orderedJSON;
Expand Down
6 changes: 3 additions & 3 deletions api/serverData.js → src/api/serverData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// "/api/serverData"
const express = require("express");
const axios = require("axios");
const { Parser } = require("xml2js");
import express from "express";
import axios from "axios";
import { Parser } from "xml2js";


const router = express.Router();
Expand Down
11 changes: 6 additions & 5 deletions api/universe.js → src/api/universe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// "/api/universe"
const express = require("express");
const axios = require("axios");
const { Parser } = require("xml2js");
import { Universe } from "../types/api";
import express from "express";
import axios from "axios";
import { Parser } from "xml2js";


const router = express.Router();
Expand All @@ -16,11 +17,11 @@ router.get("/", (req, res) => {
.catch(err => console.log("Error parsing XML ", err))
.then(parsedXML => parsedXML))
.then(json => {
const orderedJSON = {
const orderedJSON: Universe = {
serverID: json.universe.$.serverId,
timestamp: json.universe.$.timestamp,
planets: [
...json.universe.planet.map(p =>
...json.universe.planet.map((p: any) =>
({
name: p.$.name, player: p.$.player, coords: p.$.coords, id: p.$.id,
...(p.moon && {moon: { name: p.moon[0].$.name, size: p.moon[0].$.size, id: p.moon[0].$.id }})
Expand Down
11 changes: 6 additions & 5 deletions api/universes.js → src/api/universes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// "/api/universes"
const express = require("express");
const axios = require("axios");
const { Parser } = require("xml2js");
import { Universes } from "../types/api";
import express from "express";
import axios from "axios";
import { Parser } from "xml2js";


const router = express.Router();
Expand All @@ -16,10 +17,10 @@ router.get("/", (req, res) => {
.catch(err => console.log("Error parsing XML ", err))
.then(parsedXML => parsedXML))
.then(json => {
const orderedJSON = {
const orderedJSON: Universes = {
serverID: json.universes.$.serverId,
timestamp: json.universes.$.timestamp,
universes: [...json.universes.universe.map(({$}) => $)]
universes: [...json.universes.universe.map((u: any) => u.$)]
};

return orderedJSON;
Expand Down
46 changes: 46 additions & 0 deletions src/services/CacheService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Simple permanent in memory cache
class CacheService{
cache: Map<string, any>

constructor(){
this.cache = new Map();
}

set(key: string, value: any){
console.log(`Setting "${key}" to cache...`);
this.cache.set(key, value);
return value;
}

get(key: string){
console.log(`Retrieving "${key}" from cache...`);
return this.cache.get(key);
}

delete(key: string){
console.log(`Deleting "${key}" from cache...`);
return this.cache.delete(key);
}

clearCache(){
console.log("Clearing cache...")
this.cache.clear();
}

has(key: string){
const result = this.cache.has(key);
if(result) console.log(`Found cache item for "${key}"...`);
else console.log(`No cache item found for "${key}"...`);
return result;
}

keys(){
return this.cache.keys();
}

values(){
return this.cache.values();
}
}

export default CacheService;
Loading

0 comments on commit 963d491

Please sign in to comment.