Files
sineQL/test-server-pokemon/handler.js
Kayne Ruse 128a42aaa6 I remember why I stopped working on this, read more
it's because the query parser is impossible to understand, and the handlers
are too long for this to be usable. It's a nice idea, but I can't do this
while drinking.
2021-01-19 01:50:14 +11:00

184 lines
5.0 KiB
JavaScript

/* DOCS: handler parameter types
parent: Type | null
scalars: [{ typeName: String, name: String, filter: any | null }, ...]
matching: Boolean
*/
const database = require('./pokemon.json');
//the handler routines
const handler = {
Pokemon: (parent, scalars, matching) => {
let pokemon = database;
//if this is a sub-query of Pokemon (a form), use the parent to narrow the search
if (parent && parent.typeName == 'Pokemon') {
//filter based on parent object + scalars
pokemon = pokemon.filter(poke => {
return scalars.every(scalar => poke[scalar.name] == parent.context[scalar.name]);
});
pokemon = pokemon.map(poke => poke.forms)[0];
}
//I am being matched (if true, ALL present scalars will have a filter field)
if (matching) {
console.log('matching');
//check every scalar to every poke - a single false match is a miss on that poke
pokemon = pokemon.filter(poke => {
return scalars.every(scalar => {
//handle each type of scalar
switch (scalar.typeName) {
case 'String':
case 'Integer':
case 'Float':
case 'Boolean':
return poke[scalar.name] == scalar.filter; //dumb comparison for now
default:
throw `Unknown scalar typeName in handler: ${scalar.typeName} (${scalar.name})`;
}
});
});
//if there are no pokemon left, then the filters missed matches
if (pokemon.length == 0) {
return [];
}
}
//scalars are being matched on their own
if (scalars.some(s => s.filter)) {
//check every scalar to every poke - a single match is a hit
pokemon = pokemon.filter(poke => {
return scalars.some(scalar => {
//handle each type of scalar
switch (scalar.typeName) {
case 'String':
case 'Integer':
case 'Float':
case 'Boolean':
return poke[scalar.name] == scalar.filter; //dumb comparison for now
default:
throw `Unknown scalar typeName in handler: ${scalar.typeName} (${scalar.name})`;
}
});
});
//if there are no pokemon left, then the filters missed matches
if (pokemon.length == 0) {
return [];
}
}
//process (filter out unwanted fields) and return the array of pokemon
return pokemon.map(poke => {
let ret = {};
//that's a big O(damn)
scalars.forEach(scalar => {
ret[scalar.name] = poke[scalar.name];
});
return ret;
});
},
Stats: (parent, scalars, matching) => {
if (!parent || parent.typeName != 'Pokemon') {
throw 'Stats must be inside a Pokemon query';
}
console.log('mark 1');
//skip unknown/empty pokemon stats
let pokemon = database.filter(poke => poke.base_stats != null);
//if this is a sub-query of a Pokemon (already checked), use the parent to narrow the search
pokemon = pokemon.filter(poke => {
return scalars.every(scalar => poke[scalar.name] == parent.context[scalar.name]);
});
console.log('mark 2', pokemon);
//handle forms instead of normal queries
if (pokemon.length == 0) {
pokemon = database.filter(poke => poke.base_stats != null);//skip unknown/empty pokemon stats
pokemon = pokemon.map(p => p.forms);
pokemon = [].concat(...pokemon);
pokemon = pokemon.filter(poke => {
return poke.forms.some(form => {
return scalars.every(scalar => form[scalar.name] == parent.context[scalar.name]);
});
});
}
console.log('mark 3');
//I am being matched (if true, ALL present scalars will have a filter field)
if (matching) {
//check every scalar to every poke - a single false match is a miss on that poke
pokemon = pokemon.filter(poke => {
return scalars.every(scalar => {
//handle each type of scalar
switch (scalar.typeName) {
case 'Integer':
return poke.base_stats[scalar.name] === parseInt(scalar.filter); //dumb comparison for now
default:
throw `Unhandled scalar typeName in Stats handler: ${scalar.typeName} (${scalar.name})`;
}
});
});
//if there are no pokemon left, then the filters missed matches
if (pokemon.length == 0) {
return [];
}
}
console.log('mark 4');
//scalars are being matched on their own
if (scalars.some(s => s.filter)) {
//check every scalar to every poke - a single match is a hit
pokemon = pokemon.filter(poke => {
return scalars.some(scalar => {
//handle each type of scalar
switch (scalar.typeName) {
case 'Integer':
return poke.base_stats[scalar.name] === parseInt(scalar.filter); //dumb comparison for now
default:
throw `Unhandled scalar typeName in Stats handler: ${scalar.typeName} (${scalar.name})`;
}
});
});
//if there are no pokemon left, then the filters missed matches
if (pokemon.length == 0) {
return [];
}
}
console.log('mark 5');
//process (filter out unwanted fields) and return the array of pokemon
return pokemon.map(poke => {
let ret = {};
//that's a big O(damn)
scalars.forEach(scalar => {
ret[scalar.name] = poke.base_stats[scalar.name];
});
return ret;
});
},
};
module.exports = handler;