Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Created January 14, 2021 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratstail91/b2c6e53ac67f2c8933a98f97c9cc66bc to your computer and use it in GitHub Desktop.
Save Ratstail91/b2c6e53ac67f2c8933a98f97c9cc66bc to your computer and use it in GitHub Desktop.
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