add game api and change minecraft api

This commit is contained in:
cyber-dream 2022-09-08 04:26:53 +03:00
parent b1bc2b10f6
commit ea9397d2d5
12 changed files with 168 additions and 77 deletions

View File

@ -1,6 +1,6 @@
import express from "express"; import express from "express";
import routes from "./routes/routes"; import games_routes from "./routes/public/games/games.route"
import minecraft_routes from "./routes/public/minecraft/minecraft_routes"; import minecraft_routes from "./routes/public/games/minecraft/minecraft_routes";
class App { class App {
public server; public server;
@ -17,7 +17,7 @@ class App {
} }
routes() { routes() {
this.server.use(routes); this.server.use('/games', games_routes);
this.server.use('/minecraft', minecraft_routes) this.server.use('/minecraft', minecraft_routes)
} }
} }

View File

@ -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()
}
}

View File

@ -1,35 +1,17 @@
import { IHeliosDistribution, IMinecraftServer } from "./helios_distribution" import { IHeliosDistribution, IMinecraftServer } from "./helios_distribution"
import { Schema, model, connect, Model } from 'mongoose'; 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 ]
});
const MinecraftServer = model('Server', minecraftServerSchema)
export default class Minecraft { export default class Minecraft {
constructor(){ public async getServers() {
this.getDistribution = this.getDistribution.bind(this) 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())
});
public async getDistribution(){ return servers_list
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
} }
public async writeServer() { public async writeServer() {
@ -44,11 +26,5 @@ export default class Minecraft{
}) })
await newSrv.save() await newSrv.save()
} }
private async getServers(): Promise<IMinecraftServer[]>{
var servers:IMinecraftServer[] = []
await connect('mongodb://172.17.0.2:27017/minecraft');
return servers
}
} }

View File

@ -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<IMinecraftServer>('server', MinecraftServerSchema)
export default MinecraftServer;

View File

@ -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<IGame>('game', GameSchema)
export default Game;

View File

@ -1,7 +1,5 @@
import express from "express"; import express from "express";
import routes from "./routes/public_routes"; import routes from "./routes/public_routes";
import minecraft_routes from "./routes/public/minecraft/minecraft_routes";
class PublicApi { class PublicApi {
public server; public server;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -1,9 +0,0 @@
import { Router } from "express";
const routes = Router();
routes.get("/", (req, res) => {
return res.json({ message: "Hello World" });
});
export default routes;