Skip to content

Commit

Permalink
Improve dice statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
juusaw committed Feb 14, 2019
1 parent c3bbfec commit 1bddec8
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,28 @@ function calculateMean(multiplier: number, sides: number, constant: number) {
return multiplier * (sides + 1) / 2 + constant
}

function calculateMax(multiplier: number, sides: number, constant: number) {
return multiplier * sides + constant
}

function calculateMin(multiplier: number, sides: number, constant: number) {
return multiplier + constant
}

const RollStatsType = new GraphQLObjectType({
name: 'Statistics',
fields: {
average: { type: GraphQLFloat, resolve: (parent: {dice: Dice}) => calculateMean(...parent.dice) },
max: { type: GraphQLInt, resolve: (parent: {dice: Dice}) => calculateMax(...parent.dice)},
min: { type: GraphQLInt, resolve: (parent: {dice: Dice}) => calculateMin(...parent.dice)}
}
})

const RollResultType = new GraphQLObjectType({
name: 'Roll',
fields: {
result: { type: GraphQLInt, resolve: (parent: {dice: Dice}) => countResult(...parent.dice) },
statistics: { type: GraphQLFloat, resolve: (parent: {dice: Dice}) => calculateMean(...parent.dice) }
statistics: { type: RollStatsType, resolve: parent => parent }
}
})

Expand Down

0 comments on commit 1bddec8

Please sign in to comment.