Skip to content

Commit

Permalink
Merge pull request #10 from marcmascarell/dev
Browse files Browse the repository at this point in the history
Easier server creation
  • Loading branch information
marcmascarell authored May 19, 2018
2 parents aafb76a + f3239f2 commit cb84841
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 38 deletions.
6 changes: 4 additions & 2 deletions game-server-setup/initServerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ const getConfigFromServerStore = () => {

const applyGameSpecificConfig = () => {
return new Promise((resolve, reject) => {
const matchName = serverConfig.id ? `#${serverConfig.id.value}` : serverName.replace(/-/g, ' ')
const gameServerCfg = `
set sv_hostname "COD1 Community (discord.gg/yaKkZMF) ^1- ^7Match #${serverConfig.id.value}"
set scr_motd "Server provided by COD1 Community. Join ^1discord.gg/yaKkZMF"
set sv_hostname "COD Rebirth ^1- ^7Match ${matchName}"
set scr_motd "Server provided by COD Rebirth. Join the COD1 Community: ^1discord.gg/yaKkZMF"
set rconpassword "${serverConfig.rcon.value}"
set g_allowVoteMap "1"
set g_password "${serverConfig.password.value}"
set g_privatepassword "${secrets.server.privatepassword}"
set sv_privateclients "2"
`
try {
console.log('Replacing game specific config file ('+gameSpecificServerConfigPath+')...')
Expand Down
27 changes: 8 additions & 19 deletions src/Listeners/CreateMatchServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,26 @@ import gameServerManager from '../Server/gameServerManager'
import ServerLimitReached from '../Errors/ServerLimitReached'
import Match from "../Models/Match"
import firestore from "../firestore"
import utils from "../Utilities/utils"
import moment from "moment"

export default class CreateMatchServer extends Listener {

handle({match} : {match: Match}) {
const channel = match.getChannel()
const serverName = match.getServerName()
const gameServersCollection = firestore
.getClient()
.collection('gameservers')

gameServersCollection
.doc(serverName)
.set({
id: match.id,
name: serverName,
maps: match.maps.split(','),
slots: match.maxPlayers + 2,
password: utils.getPasswordForServer(serverName),
rcon: utils.getRconForServer(serverName),
status: 'creating',
creationRequestedAt: moment().toISOString(),
});
console.log(`Creating server for match #${match.id}...`)

gameServerManager
.create({
.create(serverName, {
id: match.id,
name: serverName
maps: match.maps.split(','),
slots: match.maxPlayers + 2,
})
.then(() => {
const gameServersCollection = firestore
.getClient()
.collection('gameservers')

gameServersCollection
.where('name', '==', serverName) // server name allows us to distinguish between dev/prod
.where('status', '==', 'online')
Expand Down
48 changes: 37 additions & 11 deletions src/Server/gameServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import secrets from '../secrets'
import ServerLimitReached from '../Errors/ServerLimitReached'
import utils from "../Utilities/utils"
import _ from "lodash"
import firestore from "../firestore"
import moment from "moment"

const digitalOcean = new DigitalOcean(secrets.digitalOceanToken);

Expand Down Expand Up @@ -43,25 +45,54 @@ const getLatestSnapshot = () => {
})
}

const create = ({id, name} : {id: number, name: string}) => {
const match = {id, name}
console.log(`Creating server for match #${match.id}...`)
/**
*
* @param {string} name
* @param {id?: number; maps: Array<string>; slots: number} options
* @returns {Promise<any>}
*/
const create = (
name: string,
options : {
id?: number,
maps: Array<string>,
slots: number,
}
) => {
return new Promise((resolve, reject) => {
isLimitReached()
.then(isLimitReached => {
if (isLimitReached) {
throw new ServerLimitReached()
}

const gameServersCollection = firestore
.getClient()
.collection('gameservers')

gameServersCollection
.doc(name)
.set(Object.assign(
{},
{
name,
password: utils.getPasswordForServer(name),
rcon: utils.getRconForServer(name),
status: 'creating',
creationRequestedAt: moment().toISOString(),
},
options
));

digitalOcean.Droplet.create({
name: name,
region: 'fra1',
size: 's-1vcpu-1gb',
image: 34460940,
image: 34490469,
// ssh_keys?: string[];
tags: [
getServerTag(),
getMatchTag(match)
name
]
}).subscribe((server : any) => {
if (server.name) {
Expand All @@ -83,11 +114,6 @@ const getServerTag = () => {
return utils.isDevelopment() ? `test-codserver` : `codserver`
}

const getMatchTag = (match) => {
// Avoid dev environment from interfere official servers
return utils.isDevelopment() ? `test-match-${match.id}` : `match-${match.id}`
}

const destroy = (match : Match) => {
console.log('server to destroy', match.server)
console.log('from match', match)
Expand All @@ -98,7 +124,7 @@ const destroy = (match : Match) => {
return;
}

digitalOcean.Droplet.delete(getMatchTag(match)).subscribe(() => {
digitalOcean.Droplet.delete(utils.getServerNameForMatch(match)).subscribe(() => {
console.log('droplet delete', match.id)
resolve()
}, (err: Error) => {
Expand Down
5 changes: 5 additions & 0 deletions src/Utilities/getSnap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import gameServerManager from "../Server/gameServerManager"

gameServerManager.getLatestSnapshot()

process.exit(0)
8 changes: 2 additions & 6 deletions src/Utilities/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ const getPasswordForServer = (serverName : string) => {
return crypto.createHash('md5').update(serverName + secrets.passwordSalt).digest("hex").substring(2, 7)
}

/**
*
* @param match map-carentan-S-slots-6-S-match-30
*/
const getServerNameForMatch = (match : Match) => {
return `${isDevelopment ? 'Test-' : ''}Match-${match.id}`
const getServerNameForMatch = ({ id }) => {
return `${isDevelopment ? 'Test-' : ''}Match-${id}`
}

const prettifyMapName = (name) => {
Expand Down
14 changes: 14 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gameServerManager from "./Server/gameServerManager"

const express = require('express')
const app = express()
const port = 5001
Expand All @@ -6,10 +8,22 @@ app.get('/', (request, response) => {
response.send('Hello from Express!')
})

app.get('/api/choice', function (req, res) {
console.log('id: ' + req.query.id);

gameServerManager.create(`Custom-Server`, {
maps: ['carentan'],
slots: 4,
})
});


app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}

console.log(`server is listening on ${port}`)
})


0 comments on commit cb84841

Please sign in to comment.