Created
January 14, 2021 07:13
-
-
Save Ratstail91/b2c6e53ac67f2c8933a98f97c9cc66bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pool from '../utilities/database'; | |
import { log } from '../utilities/logging'; | |
import { determineSelectedCreature, getYourCreatures } from '../reusable'; | |
import speciesIndex from '../gameplay/species.json'; | |
export const apiYourCreaturesBreed = (req, res) => { | |
//handle all outcomes | |
const handleRejection = (obj) => { | |
res.status(400).write(log(obj.msg, obj.extra ? obj.extra.toString() : '')); | |
res.end(); | |
} | |
const handleSuccess = (obj) => { | |
// log(obj.msg, obj.extra.toString()); | |
res.status(200).json(obj.msg); | |
res.end(); | |
} | |
return new Promise((resolve, reject) => resolve(req.body)) | |
.then(checkBreedingTotal) | |
.then(determineSelectedCreature) | |
.then(checkNotBattling) | |
.then(checkNotTraining) | |
.then(markAsBreeding) | |
.then(getYourCreatures) | |
.then(fields => { return { msg: fields, extra: ''}; }) | |
.then(handleSuccess) | |
.catch(handleRejection) | |
; | |
}; | |
export const checkBreedingTotal = (fields) => new Promise((resolve, reject) => { | |
const query = 'SELECT COUNT(*) AS total FROM creatures WHERE profileId IN (SELECT id FROM profiles WHERE accountId = ?) AND breeding = TRUE;'; | |
return pool.promise().query(query, [fields.id]) | |
.then(results => results[0][0].total) | |
.then(total => total < 2 ? resolve(fields) : reject({ msg: 'You can breed two creatures at a time', extra: total })) | |
.catch(e => reject({ msg: 'checkBreedingTotal error', extra: e })) | |
; | |
}); | |
//more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment