Search code examples
pythondjangoarchitecturecode-organization

How to best organize the rules component of a Django system?


I'm designing (and ultimately writing) a system in Django that consists of two major components:

  1. A Game Manager: this is essentially a data-entry piece. Trusted (non-public) users will enter information on a gaming system, such as options that a player may have. The interface for this is solely the Django admin console, and it doesn't "do" anything except hold the information.
  2. A Character Manager: this is the consumer of the above data. Public users will create characters in the role-playing systems defined above, pulling from the options entered by those trusted users. This is a separate app in the project from Django's standpoint.

There's one piece I'm not sure where to put, however, and that is the "rules" that are associated with each game. Essentially, for each game put into the first app, there are a sets of prerequisites, limitations, and other business logic specific to that game. (There's also similarly-structured logic that will be common to all games.) The logic is going to be coded in Python, rather than user-entered.

That logic is used in the process of validating a particular character, but is associated with a particular game and will need to be swapped out dynamically. Is it a separate app, or should it be validation tied to the forms of the Character Manager? Or can it be both?

This is the first Django app I've built from scratch (rather than chewing on someone else's code), and I'm new to the Python philosophy to boot, so I'm all ears on this.

Thanks in advance.


Solution

  • I would create subdirectory named rules in the app with game logic and there create module named after each game, that you would like serve. Then create a common interface for those modules, that will be utilized by your games and import proper rules module by name (if your game is called adom, then simply __import__('rules.adom') inside the main game engine and call game specific methods.

    If your games don't create own models and views, then there seems to be no reason to create specific app for each of them. This is a delicate matter, because code used is based on data stored in the database. Didn't you think about storing additional gaming scripts inside the database to and then exec them? This seems more natural: a game is set of data and additional scripts associated with that game.