Skip to content

Instantly share code, notes, and snippets.

@jaukia
Last active January 14, 2019 18:36
Show Gist options
  • Save jaukia/848c8dc12df0f28f8f8b6550f80e35c1 to your computer and use it in GitHub Desktop.
Save jaukia/848c8dc12df0f28f8f8b6550f80e35c1 to your computer and use it in GitHub Desktop.
Play text adventure games in your own language
const vorpal = require('vorpal')();
const frotz = require('frotz-interfacer');
const translate = require('@k3rn31p4nic/google-translate-api');
const chalk = require('chalk');
const pMap = require('p-map');
// change the language by switching the country code
const sourceLanguage = 'fi';
// change the infocom game by adjusting this
const gameFile = './WISHBRIN.DAT';
//const gameFile = './node_modules/frotz-interfacer/frotz/data/zork1/DATA/ZORK1.DAT';
const fromEnglish = {from: 'en', to: sourceLanguage};
const toEnglish = {from: sourceLanguage, to: 'en'};
const interfacer = new frotz({
executable: '/usr/local/bin/dfrotz',
gameImage: gameFile,
saveFile: './gamestate.sav',
/*saveFile: './gamestate-'+Math.random().toString(36).substr(2, 9)+'.sav',*/
outputFilter: frotz.filter
});
const printLocalized = (val) => vorpal.log(val);
const printInEnglish = (val) => vorpal.log(chalk.gray("["+val+"]"));
const translateAllFromEnglish = (itemArray) =>
pMap(itemArray, async item => ({
original: item,
translated: (await translate(item, fromEnglish)).text
}), {concurrency: 10});
const runGameEngineStep = (input) =>
new Promise((resolve, reject) =>
interfacer.iteration(input, async (error, output) =>
(error && error.error) ? reject(error) : resolve(output.pretty)
));
const runGameStepLocalized = async (gameCommand = ' ') => {
const gameOutput = await runGameEngineStep(gameCommand);
const allTranslations = await translateAllFromEnglish(gameOutput);
for(let i of allTranslations) {
printLocalized(i.translated);
printInEnglish(i.original);
}
};
// handle errors for this separately?
const handleCommand = async (args, cb) => {
const { text: command } = await translate(args.words.join(' '), toEnglish);
printInEnglish(command);
await runGameStepLocalized(command);
cb();
};
(async () => {
try {
vorpal.delimiter(">>").show().catch('[words...]', 'Catches commands').action(handleCommand);
await runGameStepLocalized();
} catch(error) {
console.log('Error while executing,', error);
}
})();
@jaukia
Copy link
Author

jaukia commented Jan 12, 2019

Code is quite hackish, sorry about that. Some synonyms cause problems when using this with Finnish at least, for example "lue esitettä" doesn't work, since "esite" gets translated to "brochure" while Zork understands only "leaflet". "Lue leaflet" works though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment