I'm developing an online board game creator, where users can define their own board games from top to bottom, and amongst this is the ability to define your own "Win Conditions" from pre-defined components called "Win Condition Rules". As a for instance, someone may create a Win Condition that is defined as such:
Rule1 OR ( Rule2 AND Rule3 ) OR Rule4
Due to the fact that these rules are stored in a database, the Rules need to be stored as IDs, along with the boolean operators that seperate them, and any aggregating factors (i.e. - brackets). As such, when it comes to determining if a Win Condition is met, the rules themselves need to be evaluated out into OO code that looks like this:
DefeatAllOpponents::IsMet($params) || ( CaptureTerritories::IsMet($params) && CaptureZones::IsMet($params) ) || CaptureAndHoldKeyTerritory($params)
Obvisouly, once I have a nested array structure given in the first code block above, generating a PHP string of the second code block is reasonably trivial. After this, the code block needs to be evaluated using the eval() function. Overall I think it's a neat solution, as it retains the dynamism of users being able to create rich and complex Win Conditions, while also avoiding the problem of user entered strings that may cause a security issue on the server during evaluation.
Anyone else got stories of where they have used eval() as a necessity?

New Topic/Question
Reply


MultiQuote





|