From ea9397d2d5109b4c8f43d54648e2786e3685e967 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Thu, 8 Sep 2022 04:26:53 +0300 Subject: [PATCH] add game api and change minecraft api --- src/app.ts | 6 +-- src/modules/game.module.ts | 30 +++++++++++ .../minecraft/1.12.2/helios_distribution.ts | 2 +- .../1.12.2/minecraft_distribution.ts | 50 +++++-------------- src/modules/minecraft/1.12.2/models.ts | 34 +++++++++++++ src/objects/game.object.ts | 40 +++++++++++++++ src/public_api.ts | 2 - src/routes/public/games/games.route.ts | 26 ++++++++++ .../public/games/minecraft/1_12_2/routes.ts | 21 ++++++++ .../{ => games}/minecraft/minecraft_routes.ts | 0 src/routes/public/minecraft/1_12_2/routes.ts | 25 ---------- src/routes/routes.ts | 9 ---- 12 files changed, 168 insertions(+), 77 deletions(-) create mode 100644 src/modules/game.module.ts create mode 100644 src/modules/minecraft/1.12.2/models.ts create mode 100644 src/objects/game.object.ts create mode 100644 src/routes/public/games/games.route.ts create mode 100644 src/routes/public/games/minecraft/1_12_2/routes.ts rename src/routes/public/{ => games}/minecraft/minecraft_routes.ts (100%) delete mode 100644 src/routes/public/minecraft/1_12_2/routes.ts delete mode 100644 src/routes/routes.ts diff --git a/src/app.ts b/src/app.ts index 35e6dca..2d35745 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,6 @@ import express from "express"; -import routes from "./routes/routes"; -import minecraft_routes from "./routes/public/minecraft/minecraft_routes"; +import games_routes from "./routes/public/games/games.route" +import minecraft_routes from "./routes/public/games/minecraft/minecraft_routes"; class App { public server; @@ -17,7 +17,7 @@ class App { } routes() { - this.server.use(routes); + this.server.use('/games', games_routes); this.server.use('/minecraft', minecraft_routes) } } diff --git a/src/modules/game.module.ts b/src/modules/game.module.ts new file mode 100644 index 0000000..5c65635 --- /dev/null +++ b/src/modules/game.module.ts @@ -0,0 +1,30 @@ +import Game, { IGame, GameStatus, IPlatforms } from "../objects/game.object" +import { Schema, model, connect, Model } from 'mongoose'; + +export default class Games { + public async getGames() { + connect('mongodb://172.17.0.2:27017/game'); + let games = await Game.find() + let games_list: IGame[] = [] + games.forEach(game => { + games_list.push(game.toObject()) + }) + + return games_list + } + + public async writeNewGame(game_id:string, game_name:string, game_icon_url:string, game_backgrounds:string[], game_status:GameStatus, game_platforms:IPlatforms) { + await connect('mongodb://172.17.0.2:27017/game'); + const newGame = new Game({ + id: game_id, + name: game_name, + icon: game_icon_url, + backgrounds: game_backgrounds, + status: game_status, + platforms: game_platforms + }) + + console.log(newGame) + await newGame.save() + } +} \ No newline at end of file diff --git a/src/modules/minecraft/1.12.2/helios_distribution.ts b/src/modules/minecraft/1.12.2/helios_distribution.ts index 1aef7b7..8d26571 100644 --- a/src/modules/minecraft/1.12.2/helios_distribution.ts +++ b/src/modules/minecraft/1.12.2/helios_distribution.ts @@ -43,4 +43,4 @@ interface ForgeModArtifact{ url:Url } -export type ForgeMod = IForgeMod +export type ForgeMod = IForgeMod \ No newline at end of file diff --git a/src/modules/minecraft/1.12.2/minecraft_distribution.ts b/src/modules/minecraft/1.12.2/minecraft_distribution.ts index fbce14e..6e40dd0 100644 --- a/src/modules/minecraft/1.12.2/minecraft_distribution.ts +++ b/src/modules/minecraft/1.12.2/minecraft_distribution.ts @@ -1,38 +1,20 @@ -import {IHeliosDistribution, IMinecraftServer} from "./helios_distribution" -import { Schema, model, connect, Model } from 'mongoose'; +import { IHeliosDistribution, IMinecraftServer } from "./helios_distribution" +import mongoose, { Schema, model, connect, Model } from 'mongoose'; +import MinecraftServer from "./models"; -const SolFileSchema = new Schema({ - id:{ type: String, required: true } - }) - -const minecraftServerSchema = new Schema({ - id: { type: String, required: true }, - name: { type: String, required: true }, - description: { type: String, required: true }, - icon: { type: String, required: true }, - minecraftVersion: { type: String, required: true }, - address: { type: String, required: true }, - share_required_mods:[ SolFileSchema ] - }); +export default class Minecraft { + public async getServers() { + connect('mongodb://172.17.0.2:27017/minecraft'); + let servers = await MinecraftServer.find() + let servers_list: IMinecraftServer[] = [] + servers.forEach(element => { + servers_list.push(element.toObject()) + }); -const MinecraftServer = model('Server', minecraftServerSchema) - -export default class Minecraft{ - constructor(){ - this.getDistribution = this.getDistribution.bind(this) - } - - public async getDistribution(){ - var servers_string = {id:"asd"} - await connect('mongodb://172.17.0.2:27017/minecraft'); - - var servers = await MinecraftServer.find() - console.log(servers) - servers_string = servers[0].toObject() - return servers_string + return servers_list } - public async writeServer(){ + public async writeServer() { await connect('mongodb://172.17.0.2:27017/minecraft'); const newSrv = new MinecraftServer({ id: 'kek', @@ -44,11 +26,5 @@ export default class Minecraft{ }) await newSrv.save() } - - private async getServers(): Promise{ - var servers:IMinecraftServer[] = [] - await connect('mongodb://172.17.0.2:27017/minecraft'); - return servers - } } diff --git a/src/modules/minecraft/1.12.2/models.ts b/src/modules/minecraft/1.12.2/models.ts new file mode 100644 index 0000000..dbb1b2a --- /dev/null +++ b/src/modules/minecraft/1.12.2/models.ts @@ -0,0 +1,34 @@ +import { Document, Schema, model } from "mongoose" + +export interface IMinecraftServer extends Document { + id: string + name: string + description: string + icon: string + minecraftVersion: string + address: string +} + +export const MinecraftServerSchema = new Schema({ + id: { type: String, required: true }, + name: { type: String, required: true }, + description: { type: String, required: true }, + icon: { type: String, required: true }, + minecraftVersion: { type: String, required: true }, + address: { type: String, required: true } +}) + +MinecraftServerSchema.set('toObject', { // Delete mongoDB document fields + virtuals: true, + transform: function (doc, ret) { + delete ret._id; + delete ret.__v; + } +}); + +const SolFileSchema = new Schema({ + id: { type: String, required: true } +}) + +const MinecraftServer = model('server', MinecraftServerSchema) +export default MinecraftServer; \ No newline at end of file diff --git a/src/objects/game.object.ts b/src/objects/game.object.ts new file mode 100644 index 0000000..cc01d3d --- /dev/null +++ b/src/objects/game.object.ts @@ -0,0 +1,40 @@ +import { Document, Schema, model } from "mongoose" + +export enum GameStatus { + "active", + "inactive", + "pending" +} + +export interface IPlatforms { + windows: boolean + linux: boolean, + macOS: boolean +} + +export const platformsSchema = new Schema({ + windows: { type: Schema.Types.Boolean, required: true, default: false }, + linux: { type: Schema.Types.Boolean, required: true, default: false }, + macOS: { type: Schema.Types.Boolean, required: true, default: false } +}) + +export interface IGame extends Document { + id: string, + name: string, + icon: URL, + backgrounds: string[], + status: GameStatus + platforms: IPlatforms +} + +export const GameSchema = new Schema({ + id: { type: String, required: true }, + name: { type: String, required: true }, + icon: { type: String, URL, required: true }, + backgrounds: [{ type: String, required: true }], + status: { type: String, enum: Object.values(GameStatus), default: GameStatus.inactive, required: true }, + platforms: platformsSchema +}) + +const Game = model('game', GameSchema) +export default Game; \ No newline at end of file diff --git a/src/public_api.ts b/src/public_api.ts index 9f59275..7cc1162 100644 --- a/src/public_api.ts +++ b/src/public_api.ts @@ -1,7 +1,5 @@ import express from "express"; - import routes from "./routes/public_routes"; -import minecraft_routes from "./routes/public/minecraft/minecraft_routes"; class PublicApi { public server; diff --git a/src/routes/public/games/games.route.ts b/src/routes/public/games/games.route.ts new file mode 100644 index 0000000..0abde65 --- /dev/null +++ b/src/routes/public/games/games.route.ts @@ -0,0 +1,26 @@ +import { Router } from "express"; +import { GameStatus, IPlatforms } from "../../../objects/game.object" +import Games from "../../../modules/game.module"; + +const games = new Games +const routes = Router(); + +routes.get("/", async (req, res) => { + + let games_list = await games.getGames() + await res.json(games_list); +}); + +routes.get("/writeServer", async (req, res) => { + let game_id: string = 'test' + let game_name: string = 'Test' + let game_icon_url: string = 'http://1.jpg' + let game_backgrounds: string[] = ['http://bg1.jpg', 'http://bg2.jpg'] + let game_status: GameStatus = GameStatus.active + let game_platforms: IPlatforms = { windows: true, linux: false, macOS: false } + await games.writeNewGame(game_id, game_name, game_icon_url, game_backgrounds, game_status, game_platforms) + await res.send("end") +}); + +routes.use +export default routes; diff --git a/src/routes/public/games/minecraft/1_12_2/routes.ts b/src/routes/public/games/minecraft/1_12_2/routes.ts new file mode 100644 index 0000000..abab5d9 --- /dev/null +++ b/src/routes/public/games/minecraft/1_12_2/routes.ts @@ -0,0 +1,21 @@ +import { Router } from "express"; +import Minecraft from "../../../../../modules/minecraft/1.12.2/minecraft_distribution" + +const minecraft = new Minecraft //Is this right? +const routes = Router(); + +routes.get("/", (req, res) => { + return res.json({ message: "yes" }); +}); + +routes.get("/distribution", async (req, res) =>{ + let servers = await minecraft.getServers() + //console.log(kek) + res.send(servers) +}); + +routes.get("/writeServer", (req, res) =>{ + res.send(minecraft.writeServer()) +}); + +export default routes; \ No newline at end of file diff --git a/src/routes/public/minecraft/minecraft_routes.ts b/src/routes/public/games/minecraft/minecraft_routes.ts similarity index 100% rename from src/routes/public/minecraft/minecraft_routes.ts rename to src/routes/public/games/minecraft/minecraft_routes.ts diff --git a/src/routes/public/minecraft/1_12_2/routes.ts b/src/routes/public/minecraft/1_12_2/routes.ts deleted file mode 100644 index 617093e..0000000 --- a/src/routes/public/minecraft/1_12_2/routes.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Router } from "express"; -import Minecraft from "../../../../modules/minecraft/1.12.2/minecraft_distribution" - -import GetMinecraftServers from "../../../../modules/mongoose/db_connect" - -const minecraft = new Minecraft //Is this right? -const routes = Router(); -const test_db = new GetMinecraftServers -routes.get("/", (req, res) => { - return res.json({ message: "yes" }); -}); - -routes.get("/distribution", (req, res) =>{ - res.send(minecraft.getDistribution()) -}); - -routes.get("/writeServer", (req, res) =>{ - res.send(minecraft.writeServer()) -}); - -routes.get("/test", (req, res) =>{ - res.send(test_db.writeTestModel()) -}); - -export default routes; \ No newline at end of file diff --git a/src/routes/routes.ts b/src/routes/routes.ts deleted file mode 100644 index 2c8150c..0000000 --- a/src/routes/routes.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Router } from "express"; - -const routes = Router(); - -routes.get("/", (req, res) => { - return res.json({ message: "Hello World" }); -}); - -export default routes; \ No newline at end of file