Skip to content

Commit

Permalink
Zip game logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Petah committed Sep 3, 2020
1 parent 9cf87ca commit b64e2b5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
11 changes: 6 additions & 5 deletions debug/debug.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
function readJsonFile($file)
{
$content = file_get_contents($file);
$content = gzfile($file);
$content = implode($content);
if (!$content) {
throw new \Exception('Could not load file: ' . $file);
}
Expand All @@ -24,10 +25,10 @@ function readJsonFile($file)
'gameId' => basename($path),
'mtime' => filemtime($path),
'snakes' => array_map(function ($snakePath) use ($path, &$moveJson, $selectedGame, $selectedSnake, $selectedTurn) {
$start = readJsonFile($snakePath . '/0000_start.json');
$start = readJsonFile($snakePath . '/0000_start.json.gz');
$moves = array_map(function ($movePath) {
return trim(preg_replace('/_|move|start|end|.json/', ' ', basename($movePath)));
}, glob($snakePath . '/*.json'));
return trim(preg_replace('/_|move|start|end|.json.gz/', ' ', basename($movePath)));
}, glob($snakePath . '/*.json.gz'));
natsort($moves);
$moves = array_combine($moves, $moves);
if (basename($path) == $selectedGame && $start['body']['you']['id'] == $selectedSnake && isset($moves[$selectedTurn])) {
Expand All @@ -38,7 +39,7 @@ function readJsonFile($file)
if ($selectedTurn == '9999') {
$name = 'end';
}
$moveJson = readJsonFile($snakePath . '/' . $moves[$selectedTurn] . '_' . $name . '.json');
$moveJson = readJsonFile($snakePath . '/' . $moves[$selectedTurn] . '_' . $name . '.json.gz');
}
return [
'id' => $start['body']['you']['id'],
Expand Down
3 changes: 2 additions & 1 deletion games/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.json
*.json
*.json.gz
9 changes: 5 additions & 4 deletions src/lib/writeFile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as fs from 'fs';
import * as zlib from 'zlib';

export const Writer = {
enabled: false,
};

export function writeFile(gameId: string, snakeId: string, type: string, json: any) {
export function writeFile(gameId: string, snakeId: string, snakeName: string, type: string, json: any) {
if (!Writer.enabled) {
return;
}
Expand All @@ -20,15 +21,15 @@ export function writeFile(gameId: string, snakeId: string, type: string, json: a
index = '9999';
}

const directory = `${__dirname}/../../games/${gameId}/${snakeId}/`;
const file = `${directory}/${index}_${type}.json`;
const directory = `${__dirname}/../../games/${gameId}/${snakeName}_${snakeId}/`;
const file = `${directory}/${index}_${type}.json.gz`;

if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, {
recursive: true,
});
}

fs.writeFileSync(file, JSON.stringify(json, null, 4));
fs.writeFileSync(file, zlib.gzipSync(JSON.stringify(json)));
fs.chmodSync(file, 0o777);
}
6 changes: 3 additions & 3 deletions src/server/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class Server {
this.snake.start(request.body);

if (this.saveGame) {
writeFile(request.body.game.id, request.body.you.id, 'start', {
writeFile(request.body.game.id, request.body.you.id, this.snake.constructor.name, 'start', {
body: request.body,
logs,
});
Expand Down Expand Up @@ -118,7 +118,7 @@ export class Server {
}

if (this.saveGame) {
writeFile(request.body.game.id, request.body.you.id, 'move', {
writeFile(request.body.game.id, request.body.you.id, this.snake.constructor.name, 'move', {
body: request.body,
logs,
});
Expand All @@ -140,7 +140,7 @@ export class Server {
try {
log('end', this.snake.constructor.name);
if (this.saveGame) {
writeFile(request.body.game.id, request.body.you.id, 'end', {
writeFile(request.body.game.id, request.body.you.id, this.snake.constructor.name, 'end', {
body: request.body,
logs,
});
Expand Down

0 comments on commit b64e2b5

Please sign in to comment.