Skip to content

Commit

Permalink
✨ Game view (#42)
Browse files Browse the repository at this point in the history
* 🎨 copy Link to commons

* 🎨 move Toolbar to Toolbars folder and rename it AppToolbar

* 🚧 designing the game view

* 🎨 Create GameView comp. for the game Layout, and move all Game related comp underneath

* 💄 nightmode for GameView with not 50 but 2 shades of grey

* 💄 switch Grey Tone for GameView

* 💄 fix width 100% and margin-bottom for last transitions in GameView

* 🚧 WIP query for trialGame

* 🚧 create game object for trials in a graphQL query && updating some front display

* ♻️ Refactor deletePage in graphQL resolver from forEach to Map

* 🎨♻️ Create metadata object for Effects and Conditions in EffectService

* ♻️ Refactor the mapping from object to use the key as value for options from EffectService

* 🎨💄 move Trial and Game containers to their respective Views. Create DisplayStats comp in TrialToolbar

* 💄 change style of DisplayStats

* ✨🎨 add DisplayObjects to TrialToolbar too

* 🐛🚨 fixing no key in iterator for react in DisplayStats/Objects and GamePage comp.

* ➕ add normalizr

* 🚧 add schema and normalization for the book metaData

* ♻️ modify deletePage resolver in RootSchema to change only toPage attr

* ♻️ wrong double negations in GameTransition

* 💄♻️ change margin on transitionButton

* ♻️ refactor usage of EffectService in Effect and Condition Input

* ♻️ change wrong variable name on previous refacto
  • Loading branch information
Okazari authored Jun 15, 2018
1 parent df860d9 commit 1a263ea
Show file tree
Hide file tree
Showing 70 changed files with 4,237 additions and 757 deletions.
81 changes: 74 additions & 7 deletions graphql/RootSchema.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { makeExecutableSchema } = require('graphql-tools')
const GraphQLJSON = require('graphql-type-json')
const Book = require('../models/BookModel')
const Page = require('../models/PageModel')
const Stat = require('../models/StatModel')
const User = require('../models/UserModel')
const Effect = require('../models/EffectModel')
const Transition = require('../models/TransitionModel')
const ObjectModel = require('../models/ObjectModel')
const { getProjection } = require('./Helpers')
const Game = require('../models/GameModel.js')
const SHA512 = require('crypto-js/sha512')

const bookType = `
Expand Down Expand Up @@ -59,7 +60,14 @@ const transitionType = `
conditionOperator: String
`

const gameType = `
currentPageId : ID
playerId : ID
`

const typeDefs = `
scalar MAP
type Book {
id: ID
${bookType}
Expand Down Expand Up @@ -135,15 +143,31 @@ const typeDefs = `
${transitionType}
}
type Game {
id: ID
${gameType}
book: Book
stats: MAP
objects: [ID]
}
input GameInput {
id: ID!
${gameType}
book: BookInput
stats: MAP
objects: [ID]
}
type Query {
books(author: ID, draft: Boolean): [Book]
book(id: ID!): Book
author(id: ID!): User
page(bookId: ID!, pageId: ID!): Page
tryGame(bookId: ID!, playerId: ID!) : Game
}
type Mutation {
createBook(author: ID!): User
updateBook(book: BookInput!): Book
deleteBook(id: ID!): User
Expand Down Expand Up @@ -179,6 +203,8 @@ const typeDefs = `
updatePageTransitionCondition(bookId: ID!, pageId: ID!, transitionId: ID!, condition: EffectInput!): Effect
deletePageTransitionCondition(bookId: ID!, pageId: ID!, transitionId: ID!, conditionId: ID!): Transition
createGame(bookId: ID!, playerId: ID!) : Game
updatePassword(userId: ID!, oldPassword: String!, newPassword: String!, confirmation: String!): User
}
`
Expand All @@ -205,11 +231,8 @@ const findBookById = (bookId) => {
return Book.findById(bookId).then(book => easier(book, () => book.save()))
}

const findUserById = userId => {
return User.findById(userId).then(user => easier(user, () => user.save()))
}

const resolvers = {
MAP: GraphQLJSON,
Query: {
book: (obj, args = {}, context, info) => {
const { id } = args
Expand All @@ -226,6 +249,29 @@ const resolvers = {
},
author: (obj, { id }, context, info) => User.findById(id),
page: (obj, { bookId, pageId }, context, info) => Book.findById(bookId).then(book => book.pages.id(pageId)),

tryGame: (_, { bookId, playerId }) => Book.findById(bookId).then(book => {
const stats = book.stats.reduce((acc, stat) => {
return {
...acc,
[stat._id]: stat.initValue,
}
}, {})

const objects = book.objects.reduce((acc, object) => {
// console.log(object.atStart)
if (object.atStart) return [...acc, object._id]
return acc
}, [])

return new Game({
currentPageId: book.startingPageId || book.pages[0].id,
playerId,
book,
stats,
objects,
})
}),
},
Book: {
author: (book) => {
Expand Down Expand Up @@ -284,6 +330,14 @@ const resolvers = {
.save()
}),
deletePage: (_, { bookId, pageId }) => findBookById(bookId).then(book => {

book.ressource.pages.forEach(page => {
page.transitions.forEach(transition => {
if (transition.toPage && transition.toPage === pageId) {
transition.toPage = undefined
}
})
})
return book.deleteOne('pages', pageId)
.save()
}),
Expand Down Expand Up @@ -375,6 +429,19 @@ const resolvers = {
.deleteOne('effects', effectId)
.save()
}),

createGame: (_, { bookId, playerId }) => {
// TODO: implement this (same as tryGame Query, but saving in Database)
// const game = new Game({
// playerId,
// currentPageId,
// bookId: book._id,
// book,
// bookStatus:'up-to-date',
// stats,
// }).then(game => game)
},

updatePassword: async (_, { userId, oldPassword, newPassword, confirmation }) => {
const paramsNotEmpty = oldPassword === '' || newPassword === '' || confirmation === ''
const passwordMatch = newPassword !== confirmation
Expand All @@ -389,7 +456,7 @@ const resolvers = {
{ password: SHA512(newPassword).toString() },
).then(user => {
if (!user) {
throw new Error('Error3')
throw new Error('Error')
return null
}
delete user.password
Expand Down
1 change: 1 addition & 0 deletions models/GameModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const GameSchema = new Schema({
book: Book,
bookStatus: String,
stats: Schema.Types.Mixed,
objects: Schema.Types.Mixed,
tree: [{ Type: Schema.Types.Mixed, default: [] }],
})

Expand Down
2 changes: 1 addition & 1 deletion models/PageSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ module.exports = new Schema({
text: String,
description: String,
backgroundMusic: String,
transitions: [Transition],
transitions: {type:[Transition], default: []},
effects: [Effect],
}, { minimize: false })
Loading

0 comments on commit 1a263ea

Please sign in to comment.