galvanize-game-mechanics
v1.0.0
Published
Package to provide game mechanics for Galvanize exercises.
Downloads
2
Readme
Galvanize Game Mechanics
A small package that gives you some game mechanics with which to build other projects.
Libraries:
Castles
Create a Kingdom with up to three castles. Each castle is randomly assigned health and can be attacked.
Kingdom
Example on how to use kingdoms.
const Kingdom = require('galvanize-game-mechanics').Kingdom
// With no name, will be assigned a random name
const kingdom = new Kingdom()
// This will create a new Kingdom with the specific
// name of 'My Kingdom'
const otherKingdom = new Kingdom('My Kingdom')
// If you want to rename a kingdom, you can assign a
// new name to it.
kingdom.name = 'My Secondary Kingdom'
// Creates a new Castle called 'Castle A'
kingdom.createCastle('Castle A')
// Creates a new Castle with a random name
kingdom.createCastle()
// The last castle will not be created as there are already
// three castles at that point
kingdom.createCastle() // Does create a castle
kingdom.createCastle() // Will not create a castle
// Select castles via the `.castles` property
const castle = kingdom.castles[0]
// Will attack the castle for a random amount of damage.
// The `result` will be the `castle`
const resultA = kingdom.attackCastle(castle.id)
// Will rebuild the castle for a random amount of health.
// The `result` will be the `castle`
const resultB = kingdom.buildCastle(castle.id)
// Will return `false` if the castle cannot be found
const invalidCastle = kingdom.attackCastle('INVALID ID') // returns `false`
Fortune Teller
Ask the fortune teller a question and we promise you'll receive a spooky and mystical response! 🔮
FortuneTeller
Example on how to use the fortune teller.
const FortuneTeller = require('galvanize-game-mechanics').FortuneTeller
// Ask the FortuneTeller a question with the .ask method which
// returns a native JavaScript Promise.
const ask1 = FortuneTeller.ask('Will I became rich and famous?')
ask1.then(result => {
// `result` will return an object with two keys: `question` and `response`.
// `question` is the question you asked and `response` is a random fortune.
console.log(result)
})
// If you call the `.ask()` method without a question, it will return an error.
const ask2 = FortuneTeller.ask()
ask1.catch(error => {
// `error` will return an object with a single key, `error`. That value
// will be an object with the key `message`.
console.error(error)
})
// You can speed up the time it takes to answer a question by entering an
// optional second argument with the amount of time to wait in milliseconds.
const ask3 = FortuneTeller.ask('Will I become rich and famous?', 100)
ask3.then(result => {
// This will resolve faster than the previous successful `.ask()`
console.log(result)
})
Users Service
Create, retrieve, and remove users that are validated through the joi library.
UsersService
Example on how to use UsersService.
const UsersService = require('galvanize-game-mechanics').UsersService
// Will return an empty array
UsersService.users
// Users must be entered as an object and should include:
// - a valid email
// - a username with numbers, letters, and underscores only
const valid = {
email: '[email protected]',
username: 'users_service'
}
const user = UsersService.create(valid)
// The user will now have a UUID under the 'id' property
console.log(user.id, user)
// Returns the user object
UsersService.find(user.id)
// Will now return an array of one item
UsersService.users
// Will throw a ValidationError. To access the message of
// the error, call `.message` on the caught error
const invalid = {
email: 'users.service',
username: 'users-service'
}
UsersService.create(invalid)
// Removes the user object from the service. It will return
// the user that has been deleted.
UsersService.destroy(user.id)