-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
79 lines (66 loc) · 1.88 KB
/
index.ts
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
import path from 'path';
import fs from 'fs';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { config as loadEnv } from 'dotenv';
import { GAMES } from './src/games';
import { Game } from './src/types/Game';
import { Sagrada } from './src/games/sagrada';
import { Mastodon } from './src/mastodon';
import { NextStationLondon } from './src/games/next-station-london';
const schedule: { [hour: number]: Game } = {
9: Sagrada,
13: NextStationLondon,
17: Sagrada,
21: NextStationLondon,
};
interface MainArgs {
gameName?: string;
dryRun?: boolean;
}
const main = async ({ gameName, dryRun = false }: MainArgs) => {
const hour = new Date().getHours();
const game = gameName
? GAMES.find((game) => game.name.toLowerCase() === gameName.toLowerCase())
: schedule[hour];
if (!game) {
if (gameName) {
console.log(`Unrecognised game name "${gameName}"`);
} else {
console.log(`No game scheduled for ${hour}:00.`);
}
return;
}
const output = game.play();
if (dryRun) {
if (output.stringRepresentation) {
console.log(output.stringRepresentation);
}
const outputFile = path.relative('.', './output.png');
fs.writeFileSync(outputFile, output.imageBuffer, 'binary');
} else {
const mastodon = new Mastodon(
process.env.INSTANCE_URL || '',
process.env.ACCESS_TOKEN || ''
);
const { id: imageId } = await mastodon.uploadImage(
output.imageBuffer,
output.altText
);
await mastodon.postStatus(game.name, [imageId]);
}
};
const argv = yargs(hideBin(process.argv))
.option('game', {
alias: 'g',
type: 'string',
description: 'The name of the game to run',
})
.option('dry-run', {
alias: 'd',
type: 'boolean',
description: "Play the game, but don't post to mastodon",
})
.parseSync();
loadEnv();
main({ gameName: argv.game, dryRun: argv.dryRun });