-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.js
121 lines (105 loc) · 3.33 KB
/
player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const dockerode = require("dockerode");
const tarfs = require("tar-fs");
const settings = require("./settings");
const unzip = require('./lib/unzip');
const matchmaker = require('./matchmaker');
const rimraf = require('rimraf');
const endOfLine = require('os').EOL;
const docker = new dockerode(settings.docker_connection_opts);
const dockerHelper = require('./lib/docker');
class PlayerFacade {
/**
* @param {Collection} players
* @param {Collection} events
*/
constructor(players, events) {
this.players = players;
this.events = events;
}
static async initFacade(db) {
const players = await db.collection('players');
const events = await db.collection('events');
return new PlayerFacade(players, events);
}
/**
* Creates a new Player, prepares Dockerfile and enqueues him to playing queue.
* @param {string} zipFile path to zipfile
* @param {string} name of player
* @param {string} platform used in project
*/
async createNew(zipFile, name, platform) {
//TODO set player not ready and ready after building the image
try {
let dir = zipFile + "_extracted";
await unzip(dir, zipFile);
await this.rebuildPlayerImage(dir, name);
rimraf.sync(dir);
rimraf.sync(zipFile);
const existingPlayer = await this.players.find({name: name}).toArray();
if (!existingPlayer || existingPlayer.length === 0) {
await this.players.insertOne({name});
}
await this.enqueue(name);
matchmaker.triggerMatchMaker(); //no await, only trigger
} catch (err) {
console.log(err);
}
}
/**
* enqueues a new player to event queue
*/
async enqueue(name) {
//TODO lock
let eventSelector = {'$or': [{player1: name}, {player2: name}]};
const otherPlayers = await this.players.find({name: {'$ne': name}}).toArray();
await this.events.updateMany(eventSelector, {$set: {played: false}});
const existingEvents = await this.events.find(eventSelector).toArray();
const existingEventPlayers = existingEvents.map(event => {
return event.player1 === name ? event.player2 : event.player1;
});
const newEvents = [];
for (let i = 0; i < otherPlayers.length; i++) {
if (!existingEventPlayers.includes(otherPlayers[i].name)) {
newEvents.push({
insertOne: {
player1: name,
player2: otherPlayers[i].name,
played: false
}
});
}
}
if (newEvents.length) {
await this.events.bulkWrite(newEvents);
}
}
async rebuildPlayerImage(buildFolder, playerName) {
//TODO pipe log into mongodb
const pack = tarfs.pack(buildFolder);
let stream = await docker.buildImage(pack, {t: dockerHelper.getImageName(playerName)});
let buildLogBuffer = await this.readStreamAsBuffer(stream);
let decodedBuildLog = this.decodeBuildLog(buildLogBuffer.toString());
await this.players.updateOne({name: playerName}, {$set: {lastBuildLog: decodedBuildLog}});
}
async readStreamAsBuffer(stream) {
const chunks = [];
return new Promise((resolve) => {
stream.on('data', function (chunk) {
chunks.push(chunk);
});
stream.on('end', function () {
resolve(Buffer.concat(chunks));
});
});
}
decodeBuildLog(buildLogStream) {
return buildLogStream
.split(endOfLine)
.filter(line => line.length > 0)
.map(line => JSON.parse(line))
.filter(line => Object.keys(line).includes("stream"))
.map(line => line["stream"])
.join("");
}
}
module.exports = exports = PlayerFacade;