session.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const games = require('./games')
  2. const VM = require('./vm')
  3. const session = async (message, state) => {
  4. const ret = await (async () => {
  5. if (message.content.startsWith('?') || message.content.startsWith('!')) {
  6. message.reply('_For a list of bot commands, type \`/help\`._')
  7. }
  8. if (message.content.startsWith('/')) {
  9. const args = message.content.split(' ').filter(x => x)
  10. const cmd = args.shift().toLowerCase().substr(1)
  11. if (commands[cmd]) {
  12. return await commands[cmd](message, state, ...args)
  13. } else {
  14. doNotUnderstand(message)
  15. }
  16. } else if (!state.doneIntro) {
  17. intro(message)
  18. return { doneIntro: true }
  19. } else if (!state.doneIntro2) {
  20. listGames(message, state)
  21. return { doneIntro2: true }
  22. } else if (!state.game) {
  23. return await chooseGame(message)
  24. } else {
  25. return await play(message, state)
  26. }
  27. })()
  28. console.log(`${message.author.username}: ${message.content}`)
  29. return Object.assign({}, ret, {
  30. content: message.content,
  31. username: message.author.username
  32. })
  33. }
  34. const intro = message => message.reply(`Hello, ${message.author}. Would you like to play a game?`)
  35. const listGames = (message, state, text) => {
  36. message.reply(
  37. (text ? text + '\n' : '') +
  38. (state.game
  39. ? `You are currently playing ${games[state.game].name}.\n`
  40. : `You may choose one of the following adventures:\n`) +
  41. games.map((game, i) => `${i + 1}. ${game.name}`).join('\n')
  42. )
  43. }
  44. const chooseGame = async message => {
  45. let game = games[parseInt(message.content) - 1] ||
  46. games[message.content.trim().toLowerCase()]
  47. if (game) {
  48. const gameState = await VM.play(game.data)
  49. message.reply(`_Loading ${game.name}..._\n\n${game.format(gameState.output)}`)
  50. return {
  51. game: game.name,
  52. gameState
  53. }
  54. } else {
  55. doNotUnderstand(message)
  56. return {}
  57. }
  58. }
  59. const doNotUnderstand = message => message.reply(`I do not understand '${message.content}'. Do you need \`/help\`?`)
  60. const play = async (message, state) => {
  61. const game = games[state.game]
  62. try {
  63. const gameState = await VM.play(game.data, state.gameState.save, message.content)
  64. message.reply(game.format(gameState.output))
  65. return {
  66. input: message.content,
  67. gameState
  68. }
  69. } catch (ex) {
  70. console.error(ex)
  71. }
  72. }
  73. const help = async (message, state) => {
  74. message.reply(
  75. `Available commands:
  76. /help - Shows this message.
  77. /reset - Restarts the game you have selected.
  78. /exit - Closes the game you have selected.
  79. /games - List games
  80. `)
  81. }
  82. const reset = async (message, state) => {
  83. if (state.game) {
  84. return chooseGame(Object.assign(Object.create(message), {content: state.game}))
  85. } else {
  86. listGames(message, state, 'You do not have a game loaded.')
  87. }
  88. }
  89. const exit = async (message, state) => {
  90. if (state.game) {
  91. message.reply(`Shutting down ${state.game}`)
  92. return { game: null, gameState: null }
  93. } else {
  94. listGames(message, 'You do not have a game loaded.')
  95. }
  96. }
  97. commands = {
  98. help, reset, exit, games: listGames
  99. }
  100. module.exports = session