Node dependencies:
- wikibase-sdk
- bluereq
- vorpal
Node dependencies:
| const wbk = require('wikibase-sdk')({ | |
| instance: 'http:/mud-query.chiborg.cloud', | |
| sparqlEndpoint: 'http://mud-query.chiborg.cloud/proxy/wdqs/bigdata/namespace/wdq/sparql' | |
| }) | |
| const breq = require('bluereq') | |
| const promiseRequest = url => { | |
| // Make a GET request | |
| return breq.get(url) | |
| // The previous request evaluates to bluebird Promise object, which has a `get` method | |
| // we can use to access directly the response body instead of having to deal | |
| // with the whole response object | |
| .get('body') | |
| } | |
| function describe_room_exits( results ) { | |
| const exits = Object.entries(results[0]); | |
| if ( exits.length === 0 ) { | |
| return "You are trapped!"; | |
| } | |
| return exits.map( | |
| ( [direction, room] ) => { | |
| const directionName = direction.substr(5); | |
| return `To the ${directionName} you see ${room.label}` | |
| } ).join("\n") | |
| } | |
| function query_exits( room, describe_exits ) { | |
| const sparql = ` | |
| SELECT ?room_north ?room_northLabel ?room_northDescription | |
| ?room_south ?room_southLabel ?room_southDescription | |
| ?room_west ?room_westLabel ?room_westDescription | |
| ?room_east ?room_eastLabel ?room_eastDescription | |
| WHERE { | |
| OPTIONAL { wd:${room} wdt:P1 ?room_north .} | |
| OPTIONAL { wd:${room} wdt:P2 ?room_south .} | |
| OPTIONAL { wd:${room} wdt:P3 ?room_west .} | |
| OPTIONAL { wd:${room} wdt:P6 ?room_east .} | |
| SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } | |
| } | |
| ` | |
| const url = wbk.sparqlQuery(sparql) | |
| promiseRequest(url) | |
| .then(wbk.simplify.sparqlResults) | |
| .then( describe_exits ) | |
| } | |
| function describe_room( result ) { | |
| return `${result[0].room.label} | |
| ${result[0].room.description}` | |
| } | |
| function query_room_description( player, describe_room ) { | |
| const sparql = ` | |
| SELECT ?room ?roomLabel ?roomDescription | |
| WHERE { | |
| wd:${player} wdt:P4 wd:Q8 . | |
| wd:${player} wdt:P7 ?room . | |
| SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } | |
| } | |
| ` | |
| const url = wbk.sparqlQuery(sparql) | |
| promiseRequest(url) | |
| .then(wbk.simplify.sparqlResults) | |
| .then( describe_room ) | |
| } | |
| function query_other_players_description( player, describe_other_players ) { | |
| const sparql = ` | |
| SELECT ?otherPlayer ?otherPlayerLabel ?otherPlayerDescription | |
| WHERE { | |
| wd:${player} wdt:P7 ?activePlayerRoom . | |
| ?otherPlayer wdt:P7 ?activePlayerRoom . | |
| ?otherPlayer wdt:P4 wd:Q8 . | |
| FILTER( ?otherPlayer != wd:${player} ) . | |
| SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } | |
| } | |
| ` | |
| const url = wbk.sparqlQuery(sparql) | |
| promiseRequest(url) | |
| .then(wbk.simplify.sparqlResults) | |
| .then( describe_other_players ) | |
| } | |
| function describe_other_players( results ) { | |
| return results.map( playerResult => { | |
| return `${playerResult.otherPlayer.label} is here` | |
| } ).join("\n") | |
| } | |
| function write_console( descriptionFunc ) { | |
| return function( results ) { | |
| console.log( descriptionFunc( results ) ); | |
| } | |
| } | |
| /* | |
| vorpal | |
| .mode('repl') | |
| .description('Enters the user into a REPL session.') | |
| .delimiter('>') | |
| .action(function(command, callback) { | |
| // TODO store player id, | |
| // TODO go directions | |
| this.log(command); | |
| }); | |
| */ | |
| // TODO do state from the vorpal interactive command line instead | |
| const playerId = process.argv[2] | |
| // Todo: Find a way to write the descriptions in call order instead of in server response order | |
| query_room_description( playerId, write_console( describe_room ) ); | |
| query_exits('Q2', write_console( describe_room_exits ) ); | |
| query_other_players_description( playerId, write_console(describe_other_players) ); |