Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"graphql-type-json": "^0.2.1",
"immer": "^1.2.1",
"lodash": "^4.17.5",
"lodash.flatten": "^4.4.0",
"normalize.css": "^8.0.0",
"prop-types": "^15.5.9",
"random-js": "^1.0.8",
Expand Down
13 changes: 9 additions & 4 deletions src/api/models/message.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// @flow
import firebase from '@firebase/app'
import '@firebase/firestore'
import CommandParser from '../utils/commandParser'

const commandParser = new CommandParser()
import performRolls from '../utils/performRolls'
import { getMessageResult } from 'common/commandParser'

export const getMessagesByGameId = async (id: string, _first: number) => {
const collection = firebase
Expand Down Expand Up @@ -62,7 +61,13 @@ export const listenToNewMessages = (id: string) => (callback: Function) => {
export const sendMessage = (id: string, text: string) => {
const messagesCollection = firebase.firestore().collection(`messages`)

const result = commandParser.getMessageResult(text)
let result = getMessageResult(text)
if (result) {
result = performRolls(result)
}
if (result) {
result = JSON.stringify(result)
}

const message = {
from: firebase.auth().currentUser.uid,
Expand Down
2 changes: 1 addition & 1 deletion src/api/types/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Message = `
type Message {
id: ID!
from: String!
result: [JSON!]
result: String
text: String!
timestamp: Date!
}
Expand Down
110 changes: 0 additions & 110 deletions src/api/utils/__tests__/commandParser.tests.js

This file was deleted.

96 changes: 0 additions & 96 deletions src/api/utils/commandParser.js

This file was deleted.

30 changes: 30 additions & 0 deletions src/api/utils/performRolls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @flow
import Random from 'random-js'
import type { MessageResult } from 'common/types'

const random = new Random(Random.engines.mt19937().autoSeed())

const performRolls = (result: MessageResult): MessageResult => {
if (result.type !== 'rolls') {
return result
}

result.rolls = result.rolls.map(actions => {
const newActions = actions.map(action => {
if (action.type !== 'roll') {
return action
}

return {
...action,
result: random.integer(1, action.die)
}
})

return newActions
})

return result
}

export default performRolls
14 changes: 14 additions & 0 deletions src/common/__tests__/commandParser.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @flow
import { getCommandName } from '../commandParser'

describe('Command parser', () => {
it('should correctly identify command names', () => {
const result = getCommandName('/roll something')
expect(result).toEqual('/roll')
})

it('should return null when no command exists', () => {
const result = getCommandName('something')
expect(result).toBeNull()
})
})
25 changes: 25 additions & 0 deletions src/common/commandParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @flow
import { getRollResult } from './rollCommand'
import type { MessageResult } from 'common/types'

export function getMessageResult(text: string): ?MessageResult {
const command = getCommandName(text)

if (command == '/r' || command == '/roll' || command === null) {
const result = getRollResult(text)
return result
}

return null
}

export function getCommandName(text: string): ?string {
if (text[0] == '/') {
const firstSpace = text.indexOf(' ')
const command = text.slice(0, firstSpace)

return command
}

return null
}
Loading