As a side-project I tried to port Stellar Crisis 2.2 (a 15 year old
browser game written in the C-like langage Pike for the Roxen server)
to Django. So far, unfortunately, I failed. It seems, Django isn't the
right tool to create such kind of application :(

SC keeps all persistent data two global dicts that contain other dicts
containing more dicts and arrays. I tried to map the data onto
database tables but doing this, most of the elegance of Python seems
to vanish. My code is actually worse than the original Pike code.

Here's an example (Pike):

 p[d[series][game]["f"][ixb]["owner"]]["kills"]++;

"p" is the global dict of players (all player data are kept in another
dict). "d" is the global dict of all other game data. Games are
instances of series and "f" probably means fleet and stands for a dict
of all ships. A ship is owned by a player and the server stores how
many other players a player has beaten. The Django equivalent seems to
be:

 g = Series.objects.get(name=series).game_set.get(no=game)
 p = Player.objects.get(name=g.fleet_set.get(no=ixb).owner)
 p.kills += 1
 p.save()

I want to stay close to the original source code (even if I do not
consider it the best style) to make porting the code more straight
forward. Therefore, I added helper methods to the models so that I can
use [] to access a series' game. The resulting code looks like is:

 player = p[d[series][game].f(ixb).owner]
 player.kills += 1
 player.save()

However, I still need three lines of code where the original just used
one line because I have to explicitly save the state change.

The original Pike code contains all game logic in one very large
function (~800 lines of code) that does a lot of modifications. Adding
explicit save() methods all over the place seems to be a bad (and
inefficient) way to add persistence.

I'd like to freely modify database objects once loaded into memory and
then put everything that was modified with just one command back to
the database. Most ORMs work that way, I think. Objects are loaded
once within a transaction, know whether they are modified, dependent
objects can be automatically added or deleted and all modifications of
the database take place in one bulk operation at the end of the
transaction.

Has anybody extended Django's ORM in such a way? Can it be replaced
with some other mapper? I recently heard about Storm, a new mapper
written by the Ubuntu guys...  I'd prefer to not have to write my own
ORM. I've done that more than once in other language :)

My ad hoc idea is to add a __setattr__ method to all models after
their post_init signal and that method would register the model object
as modified with some transaction context. However, because Django's
ORM doesn't honor object identity, things get very inefficient very
soon. I'd also need to overwrite delete() to make sure that I do not
save deleted objects.

But then, it seems that each time I ask a game for its fleets or
systems, a new database query is executed and I have to cache them
manually and reset the cache manually if I add new ships or systems.
I'm afraid, I over-bend Django's ORM here...

-- 
Stefan Matthias Aust

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to