I creating a game that use a AI. This AI'API has to 3 methods written in an Angular Service
Here's a the simplified code:
app.service('AI', [function(){
return {
offer: angular.noop,
accept: angular.noop,
reject: angular.noop
}
}])
The difficult part is that the methods implementation (and so, how AI react) can change according to several parameters (nationality, age etc...).
I thought about use angular Decorator but I will need to choose between several decorator. I can create files/implementations:
but how can I say:
"I have a 18 years old player... load less-than-20.decorator.js and apply decorator"
OR
"I have a 18 years old player... use this decorator instead of this one"
To resume: I want to do a conditional decorator (loading).
After some search I found a way to do this: Instead of return $delegate I can return the original service but I think it's not a pretty way...
Have you got a better solution?
Have you considered using a factory instead of a service?
// teenagerAI and adultAI could be values, constants, factories...
app.factory('createAI', function (teenagerAI, adultAI) {
return function (aiType) {
switch (aiType) {
case 'teenager':
return teenagerAI;
case 'adult':
return adultAI;
}
};
});
Then you can inject createAI
and use it:
createAI('adult');