| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- const games = require('./games')
- const VM = require('./vm')
- const session = async (message, state) => {
- const ret = await (async () => {
- if (message.content.startsWith('?') || message.content.startsWith('!')) {
- message.reply('_For a list of bot commands, type \`/help\`._')
- }
- if (message.content.startsWith('/')) {
- const args = message.content.split(' ').filter(x => x)
- const cmd = args.shift().toLowerCase().substr(1)
- if (commands[cmd]) {
- return await commands[cmd](message, state, ...args)
- } else {
- doNotUnderstand(message)
- }
- } else if (!state.doneIntro) {
- intro(message)
- return { doneIntro: true }
- } else if (!state.doneIntro2) {
- listGames(message, state)
- return { doneIntro2: true }
- } else if (!state.game) {
- return await chooseGame(message)
- } else {
- return await play(message, state)
- }
- })()
- console.log(`${message.author.username}: ${message.content}`)
- return Object.assign({}, ret, {
- content: message.content,
- username: message.author.username
- })
- }
- const intro = message => message.reply(`Hello, ${message.author}. Would you like to play a game?`)
- const listGames = (message, state, text) => {
- message.reply(
- (text ? text + '\n' : '') +
- (state.game
- ? `You are currently playing ${games[state.game].name}.\n`
- : `You may choose one of the following adventures:\n`) +
- games.map((game, i) => `${i + 1}. ${game.name}`).join('\n')
- )
- }
- const chooseGame = async message => {
- let game = games[parseInt(message.content) - 1] ||
- games[message.content.trim().toLowerCase()]
- if (game) {
- const gameState = await VM.play(game.data)
- message.reply(`_Loading ${game.name}..._\n\n${game.format(gameState.output)}`)
- return {
- game: game.name,
- gameState
- }
- } else {
- doNotUnderstand(message)
- return {}
- }
- }
- const doNotUnderstand = message => message.reply(`I do not understand '${message.content}'. Do you need \`/help\`?`)
- const play = async (message, state) => {
- const game = games[state.game]
- try {
- const gameState = await VM.play(game.data, state.gameState.save, message.content)
- message.reply(game.format(gameState.output))
- return {
- input: message.content,
- gameState
- }
- } catch (ex) {
- console.error(ex)
- }
- }
- const help = async (message, state) => {
- message.reply(
- `Available commands:
- /help - Shows this message.
- /reset - Restarts the game you have selected.
- /exit - Closes the game you have selected.
- /games - List games
- `)
- }
- const reset = async (message, state) => {
- if (state.game) {
- return chooseGame(Object.assign(Object.create(message), {content: state.game}))
- } else {
- listGames(message, state, 'You do not have a game loaded.')
- }
- }
- const exit = async (message, state) => {
- if (state.game) {
- message.reply(`Shutting down ${state.game}`)
- return { game: null, gameState: null }
- } else {
- listGames(message, 'You do not have a game loaded.')
- }
- }
- commands = {
- help, reset, exit, games: listGames
- }
- module.exports = session
|