I'm currently trying to create my own rules engine, I mean by this a system with a GUI where users can create conditions with different settings to execute certain tasks. I'm not sure about the structure so I'm requesting some help here.
First of all there are condition targets, this is to distinguish where we should get the original data from. Since this is an game context I could have:
- Player
- Entity
- Server
- ...
These will later be used to create conditions like "Check Player
- has been connected this week
"
Knowning this, I think I should have some sort of interface
named ConditionExecutor
that will hold a target? Each condition should also have an execute method that will be called to check if the condition is fulfilled or not, and this is where things get confusing for me.
Let's say I have an interface ConditionExecutor
, I think I would also need a PlayerConditionExecutor
for all the executors related to players, but these are only interfaces (I think they should be).
Then I would have the actual classes that will handle each condition. For example something like:
public class PlayerOnlineExecutor implements PlayerConditionExecutor {
private ConditionExecutionType conditionExecutionType = ConditionExecutionType.PLAYER_ONLINE_STATUS;
public boolean execute(Player player) {
return player.isOnline();
}
}
This executor would then be registered somewhere, but I'm still confused by how I can structure this. The condition shown above is fairly simple, but there could be extra data required like this one:
public class PlayerHoursPlayedExecutor implements PlayerConditionExecutor {
private ConditionExecutionType conditionExecutionType = ConditionExecutionType.PLAYER_PLAYED_TIME;
public boolean execute(Player player, int hoursThreshold) {
return player.getTotalTimePlayed() >= hoursThreshold;
}
}
As you see, here we need extra data, but this could be anything. This condition requires an extra integer, but it could require some other data is well. I feel like this is not possible with interfaces but maybe I'm missing something.
I would finally have a manager ConditionsManager
which will then retrieve all the conditions based on the condition type (see example above) and execute each condition. The issue is that each condition may require a different target (player, entity, server,...) and could require a bunch of different metadata.
If anyone could bring me in the good direction, or show me certain design patterns that may be useful here, would be awesome.
PS: In the examples above you'll see PlayerConditionExecutor
and ConditionExecutor
, here they are but I think they are wrong:
public interface PlayerConditionExecutor extends ConditionExecutor {
<T> boolean execute(Player p, T ...args);
}
public interface ConditionExecutor<T> {
<A> boolean execute(T target, A ...args);
}
Aucun commentaire:
Enregistrer un commentaire