Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Last active October 25, 2015 09:19
Show Gist options
  • Save hirokazumiyaji/db3b72d1053d79995b15 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/db3b72d1053d79995b15 to your computer and use it in GitHub Desktop.
Techlunch
# coding: utf-8
from collections import defaultdict
from django.db import models
from django.utils.functional import cached_property
class PlayerUnit(models.Model):
id = models.UUIDField(primary_key=True)
player_id = models.UUIDField(db_index=True)
unit_id = models.IntegerField()
class Meta(object):
index_together = (
('player_id', 'unit_id'),
)
@cached_property
def skills(self):
return PlayerUnitSkill.get_player_unit_skills_by_player_unit(
self.player_id, self.player_unit_id)
class PlayerUnitSkill(models.Model):
id = models.UUIDField(primary_key=True)
player_id = models.UUIDField(db_index=True)
player_unit_id = models.UUIDField()
class Meta(object):
index_together = (
('player_id', 'player_unit_id),
)
@classmethod
def get_player_unit_skills_by_player_unit(cls, player_id, player_unit_id):
player_unit_skills = cls._get_player_unit_skills_by_player(player_id)
return player_unit_skills[(player_id, player_unit_id)]
@classmethod
@cache_per_request
def _get_player_unit_skills_by_player(cls, player_id):
player_unit_skills = defaultdict(list);
for player_unit_skill in cls.objects.filter(player_id=player_id):
player_unit_skills[(player_unit_skill.player_id, player_unit_skill.player_unit_id)].append(
player_unit_skill)
return player_unit_skills
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment