diff --git a/src/api/nether/index.ts b/src/api/nether/index.ts new file mode 100644 index 0000000..255d123 --- /dev/null +++ b/src/api/nether/index.ts @@ -0,0 +1,75 @@ +import { Request, Response, Router } from "express"; +import * as request from "request"; +import { ReferenceRepo, RepoFileFetcher } from "../../github"; + +export class NetherRouter { + public router: Router; + private referenceRepo: ReferenceRepo; + + constructor(private repoFileFetcher: RepoFileFetcher) { + this.router = Router(); + this.referenceRepo = new ReferenceRepo(repoFileFetcher, { + owner: "PilzAdam", + repo: "nether", + path: "textures", + }); + + this.referenceRepo.fetch(); + this.init(); + } + + public init(): void { + this.router.get("/:owner/:repo.svg", (req: Request, res: Response) => { + const owner = req.params.owner as string; + const repo = req.params.repo as string; + + this.fetchDetails(owner, repo).then((result) => { + request(`https://img.shields.io/badge/farming%20mod%20completion-${result.percentage}%25-green.svg`).pipe(res); + }).catch((error) => { + res.status(400).json({ message: `${repo} is not a mod!` }); + }); + }); + + this.router.get("/:owner/:repo", (req: Request, res: Response) => { + const owner = req.params.owner as string; + const repo = req.params.repo as string; + + this.fetchDetails(owner, repo).then((result) => { + res.status(200).json(result); + }).catch((error) => { + res.status(400).json({ message: `${repo} is not a mod!` }); + }); + }); + } + + private fetchDetails(owner: string, repo: string, path: string = ""): Promise { + return new Promise((resolve, reject) => { + this.repoFileFetcher.fetch({ + owner: owner, + repo: repo, + path: path, + }).then((files) => { + this.referenceRepo.fetch().then((fileMap) => { + let successfulFiles: number = 0; + const missingFiles: string[] = new Array(); + + for (const file of fileMap.values()) { + if (files.has(file.name)) { + successfulFiles++; + continue; + } + missingFiles.push(file.name); + } + + resolve({ + percentage: parseFloat(Math.round((successfulFiles / fileMap.size) * 100).toPrecision(2)), + numberTexturesMissing: fileMap.size - successfulFiles, + missingFiles: missingFiles, + }); + }); + }).catch((error) => { + reject(error); + }); + }); + } +} diff --git a/src/index.ts b/src/index.ts index 99faf96..396df90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import * as logger from "winston"; import { AwardsRouter } from "./api/awards"; import { FarmingRouter } from "./api/farming"; +import { NetherRouter } from "./api/nether"; import { TexturePackRouter } from "./api/texture-pack"; import { ApplicationWrapper } from "./bootstrap/application-wrapper"; import { DevelopmentConfig, ProductionConfig } from "./config/index"; @@ -22,6 +23,7 @@ appWrapper.configure((app) => { app.use("/", new TexturePackRouter(repoFileFetcher).router); app.use("/farming", new FarmingRouter(repoFileFetcher).router); app.use("/awards", new AwardsRouter(repoFileFetcher).router); + app.use("/nether", new NetherRouter(repoFileFetcher).router); }); appWrapper.start();