{"id":27181318,"url":"https://github.com/substack/adventure","last_synced_at":"2025-04-09T15:03:00.746Z","repository":{"id":18203784,"uuid":"21336713","full_name":"workshopper/adventure","owner":"workshopper","description":"quickly hack together an adventure workshop for nodeschool","archived":false,"fork":false,"pushed_at":"2022-12-30T18:19:38.000Z","size":115,"stargazers_count":111,"open_issues_count":8,"forks_count":20,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-30T02:33:02.574Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/workshopper.png","metadata":{"files":{"readme":"readme.markdown","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-30T01:53:29.000Z","updated_at":"2024-09-06T12:09:20.000Z","dependencies_parsed_at":"2023-01-13T19:42:32.777Z","dependency_job_id":null,"html_url":"https://github.com/workshopper/adventure","commit_stats":null,"previous_names":["substack/adventure"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/workshopper%2Fadventure","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/workshopper%2Fadventure/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/workshopper%2Fadventure/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/workshopper%2Fadventure/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/workshopper","download_url":"https://codeload.github.com/workshopper/adventure/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248055275,"owners_count":21040156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-09T15:01:00.210Z","updated_at":"2025-04-09T15:03:00.740Z","avatar_url":"https://github.com/workshopper.png","language":"JavaScript","readme":"# adventure\n\nquickly hack together a [nodeschool](http://nodeschool.io) adventure\n\nThis is an alternative to the\n[workshopper](https://www.npmjs.org/package/workshopper)\nmodule, which you should also look at.\n\n`workshopper` is more convention-driven and fully-featured, but expects a\nparticular (configurable) filesystem organization for problems.\n\n`adventure` is entirely api-driven and has fewer configuration options.\n\n# tutorial\n\nYou can fork this tutorial from the\n[example-adventure](https://github.com/substack/example-adventure) repo.\n\nFirst make a `runner.js`. This is the file you can wire up to the `package.json`\n`\"bin\"` field.\n\n``` js\n#!/usr/bin/env node\n\nvar adventure = require('adventure');\nvar shop = adventure('example-adventure');\n\nshop.add('dinosaurs', function () { return require('./dinosaurs') });\nshop.add('robots', function () { return require('./robots') });\nshop.add('wowsers', function () { return require('./wowsers') });\n\nshop.execute(process.argv.slice(2));\n```\n\nYou simply `.add(name, fn)` each of the adventures in your problem set and then\n`.execute()` the adventure with the command-line arguments.\n\nThe interface to problem files is very simple. The simplest version of a problem\nis just an object with a `.problem` string and `.verify` function.\n\nHere's what we can put in `dinosaurs/index.js`:\n\n``` js\nexports.problem = 'Make a dinosaur sound.\\n'\n    + 'Use `$ADVENTURE_COMMAND verify YOUR_TEXT...` to make your sound.'\n;\n\nexports.verify = function (args, cb) {\n    if (/RAWR/.test(args)) {\n        console.log('Wow that is a convincing dinosaur.\\n');\n        cb(true);\n    }\n    else if (/rawr/i.test(args)) {\n        console.log('Close, but too quiet. Try louder.\\n');\n        cb(false);\n    }\n    else {\n        console.log(\"That doesn't sound like a dinosaur at all.\\n\");\n        cb(false);\n    }\n};\n```\n\nYou don't need to put this in a file necessarily even, you just need to return\nan object with these properties from the function you pass to `.add()`.\n\nYour `verify(args, cb)` function will get the arguments passed to it on the\ncommand-line and a callback that you can use to indicate whether the solution\nwas successful or not.\n\nYou can return many different kinds of objects in your `.problem` or `.solution`\nfunctions: a string, a buffer, a stream, or a function that returns a string, a\nbuffer, or a stream.\n\nNow in `robots/index.js` we can use streams for the problem and solution:\n\n``` js\nvar fs = require('fs');\nvar path = require('path');\n\nexports.problem = fs.createReadStream(__dirname + '/problem.txt');\nexports.solution = fs.createReadStream(__dirname + '/solution.txt');\n\nexports.verify = function (args, cb) {\n    var res = require(path.resolve(args[0]));\n    if (/beep/.test(res) \u0026\u0026 /boop/.test(res)) {\n        console.log('That sounds about right!\\n');\n        cb(true);\n    }\n    else if (/beep/.test(res) || /boop/.test(res)) {\n        console.log('Hmm that sounds partly convincing but try harder.\\n');\n        cb(false);\n    }\n    else {\n        console.log(\"That doesn't sound like a robot at all.\\n\");\n        cb(false);\n    }\n};\n```\n\nFinally, we can use\n[adventure-verify](https://npmjs.org/package/adventure-verify)\nto verify solutions using [tape](https://npmjs.org/package/tape) with\nfriendly [colorized tap output](https://npmjs.org/package/tap-colorize).\n\nIn `wowsers/index.js` we can use\n[adventure-verify](https://npmjs.org/package/adventure-verify) to do:\n\n``` js\nvar fs = require('fs');\nvar path = require('path');\nvar verify = require('adventure-verify');\n\nexports.problem = fs.createReadStream(__dirname + '/problem.txt');\nexports.solution = fs.createReadStream(__dirname + '/solution.txt');\n\nexports.verify = verify({ modeReset: true }, function (args, t) {\n    var f = require(path.resolve(args[0]));\n    t.equal(typeof f, 'function', 'you exported a function');\n    t.equal(f(2,3), 6, '2 * 3 = 6');\n    t.equal(f(1,1), 1, '1 * 1 = 1');\n    t.equal(f(0.5,0.5), 0.25, '0.5 * 0.5 = 0.25');\n    t.end();\n});\n```\n\nHere we use `modeReset` so that when a user does `console.log()` or\n`console.error()` in their solution, their text shows up as the terminal default\ninstead of getting mixed up with the TAP colors.\n\nNow just fill in the `problem.txt` and `solution.txt` files and you will have a\nworking nodeschool-style adventure! Yay!\n\n# methods\n\n``` js\nvar adventure = require('adventure')\n```\n\n## var shop = adventure(opts)\n\nCreate a new nodeschool workshop adventure.\n\noptions are:\n\n* `opts.name` - name of your adventure (required)\n* `opts.command` - the name of the adventure command (inferred from `opts.name`)\n* `opts.title` - title to use for your adventure\n(default: `opts.name.toUpperCase()`)\n* `opts.datadir` - directory used to store the current level and the list of\ncompleted levels. default: `'~/.config/' + opts.name`\n\n* `opts.colors` - object mapping color types to `[r,g,b]` arrays\n* `opts.colors.pass` - show passing solution messages with this color\n* `opts.colors.fail` - show failing solution messages with this color\n* `opts.colors.info` - show extra info with this color\n\n* `opts.fg` - menu foreground color\n* `opts.bg` - menu background color\n\n* `opts.autoclose` - whether to close stdin automatically after the menu is\nshown\n\nIf `opts` is a string, it will be treated as the `opts.name`.\n\n## shop.add(name, fn)\n\nYour `fn()` should return a problem object in the format described below.\n\n## shop.execute(args)\n\nRun whatever commands are specified in the command-line `args`.\n\n## shop.showMenu(opts)\n\nIf you don't want to let `.execute()` show the menu, you can show the menu\nyourself explicitly with `.showMenu()`.\n\nThe options are:\n\n* `opts.fg` - foreground color\n* `opts.bg` - background color\n* `opts.title` - menu title text\n* `opts.autoclose` - whether to close stdin automatically after the menu is\nshown\n\n## shop.select(name)\n\nYou can explicitly select a level with this method if you don't want to rely on\nthe user to select a menu for themselves from the graphical menu.\n\n# problem object format\n\nProblems must have a `verify()` function. All other fields are optional.\n\n## problem.verify(args, cb)\n\nThis function will be called when a user attempts to verify a problem with the\n`verify` command on the command-line.\n\nYou will get an array of the arguments given after the `verify` command in\n`args`.\n\nYou must call `cb(ok)` with `ok`, a boolean containing whether the solution was\nacceptible.\n\nCheck out [adventure-verify](https://npmjs.org/package/adventure-verify)\nfor a higher-level way of verifying solutions with\n[tape](https://npmjs.org/package/tape).\n\n## problem.run(args)\n\nThis function will be called when the user uses the `run` command from the\ncommand-line. You can implement this if you want to but it doesn't make sense\nfor all problems.\n\n## problem.problem\n\nThis message will be displayed when a user selects the problem from the menu.\n\n`problem.problem` can be a string, a buffer, a stream, or a function that\nreturns a string, a buffer, or a stream.\n\n## problem.solution\n\nThis message will be displayed when a user successfully completes a problem,\nafter the success notification.\n\n`problem.solution` can be a string, a buffer, a stream, or a function that\nreturns a string, a buffer, or a stream.\n\n## problem.pass\n\nThis message will be displayed when a user successfully completes a level. The\ndefault `problem.pass` is says `YOUR SOLUTION IS CORRECT` in a box of made of\n`@`s.\n\n`problem.pass` can be a string, a buffer, a stream, or a function that\nreturns a string, a buffer, or a stream.\n\n## problem.fail\n\nThis message will be displayed when a user's solution fails to pass all the\ntests. The default `problem.fail` is says `YOUR SOLUTION IS NOT CORRECT` in a\nbox of made of `#`s.\n\n`problem.fail` can be a string, a buffer, a stream, or a function that\nreturns a string, a buffer, or a stream.\n\n# events\n\n## shop.on('pass', function (name) {})\n\nThis event fires when a solution passed.\n\n## shop.on('fail', function (name) {})\n\nThis event fires when a solution failed.\n\n## shop.on('finished', function () {})\n\nThis event fires when all the levels are completed.\n\n# problem variables\n\nThese variables will be automatically replaced any time you use them in any of\nthe problem messages, whether in a string, a buffer, a stream, or a function\nthat returns a string, a buffer, or a stream.\n\n* `$ADVENTURE_NAME` - the name of the adventure\n* `$ADVENTURE_COMMAND` - the name of the adventure command\n\n# usage\n\nThe `.execute(args)` function accepts these commands:\n\n```\n$COMMAND\n$COMMAND menu\n\n  Show the menu.\n\n$COMMAND verify [ARGS...]\n\n  Verify the currently selected problem with ARGS.\n\n$COMMAND run [ARGS...]\n\n  Run the currently selected problem with ARGS.\n  Not all problems support `run`.\n\n$COMMAND solution\n\n  Show the solution for the currently selected problem.\n\n$COMMAND print\n\n  Print the text of the currently selected level.\n\n$COMMAND selected\n\n  Print the name of the currently selected level.\n \n$COMMAND select LEVEL\n\n  Set the currently selected LEVEL.\n\n$COMMAND list\n\n  List the available levels.\n\n$COMMAND completed\n\n  List the completed levels.\n \n$COMMAND reset\n\n  Reset the list of completed levels.\n\n$COMMAND help\n\n  Show this message.\n\n```\n\n# install\n\nWith [npm](https://npmjs.org) do:\n\n```\nnpm install adventure\n```\n\n# license\n\nMIT\n","funding_links":[],"categories":["Raw Builders"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsubstack%2Fadventure","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsubstack%2Fadventure","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsubstack%2Fadventure/lists"}