-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtragen.js
83 lines (70 loc) · 2.15 KB
/
tragen.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
'use strict';
const fs = require('fs');
const path_mod = require('path');
var transcriptdir = null;
var metadata = null;
var path = null;
var fd = null;
function set_transcriptdir(path)
{
transcriptdir = path;
/* We try to create a directory for transcripts at init time.
This will usually fail because there's already a directory there.
*/
try {
// should be async, I know
fs.mkdirSync(transcriptdir);
}
catch (ex) {}
}
function set_metadata(obj)
{
metadata = Object.assign({}, obj); // copy
}
/* This is called by the GlkOte recording handler for every game turn.
The obj argument contains the player's input and the game's output,
in GlkOte JSON form. We write this to the transcript file, being
careful to follow it with a newline.
If this is the first turn, we first write out the metadata stanza (if
we have one).
*/
function record_update(obj)
{
if (path === null) {
// First time! Open the file.
var writemeta = false;
//TODO: more human-readable filenames would be great, but saving that in the autorestore info is hard.
path = path_mod.join(transcriptdir, obj.sessionId+'.glktra');
try {
// Will fail if path already exists.
fd = fs.openSync(path, "ax");
writemeta = true;
}
catch (ex) {
// We are appending to an existing file.
try {
fd = fs.openSync(path, "a");
}
catch (ex) {
// Could not open transcript at all.
}
}
if (fd === null) {
console.log('Could not open auto-transcript file', path);
return;
}
if (writemeta && metadata != null) {
var metaobj = {
metadata: metadata,
timestamp: (new Date().getTime())
};
fs.writeSync(fd, JSON.stringify(metaobj)+'\n');
}
}
if (fd === null)
return;
fs.writeSync(fd, JSON.stringify(obj)+'\n');
}
exports.set_transcriptdir = set_transcriptdir;
exports.set_metadata = set_metadata;
exports.record_update = record_update;