models.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from django.conf import settings
  2. from django.db import models
  3. from django.contrib.auth.models import User
  4. import json
  5. class GameSession(models.Model):
  6. user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
  7. session_key = models.CharField(max_length=40, null=True, blank=True)
  8. day = models.IntegerField(default=1)
  9. cash = models.DecimalField(max_digits=10, decimal_places=2, default=2000.00)
  10. debt = models.DecimalField(max_digits=10, decimal_places=2, default=5500.00)
  11. location = models.CharField(max_length=50, default='Downtown')
  12. inventory = models.TextField(default='{}') # JSON string
  13. capacity = models.IntegerField(default=100)
  14. high_score = models.DecimalField(max_digits=10, decimal_places=2, default=0)
  15. interest = models.DecimalField(max_digits=10, decimal_places=2, default=settings.INTEREST_RATE)
  16. max_days = models.DecimalField(max_digits=10, decimal_places=2, default=settings.MAX_DAYS)
  17. is_active = models.BooleanField(default=True) # Track active games
  18. completed = models.BooleanField(default=False) # Track completed games
  19. final_score = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
  20. created_at = models.DateTimeField(auto_now_add=True)
  21. updated_at = models.DateTimeField(auto_now=True)
  22. def get_inventory(self):
  23. return json.loads(self.inventory) if self.inventory else {}
  24. def set_inventory(self, inv_dict):
  25. self.inventory = json.dumps(inv_dict)
  26. def __str__(self):
  27. if self.user:
  28. return f"Game {self.id} - {self.user.email} - Day {self.day}"
  29. return f"Game {self.id} - Anonymous - Day {self.day}"
  30. class HighScore(models.Model):
  31. player_name = models.CharField(max_length=100)
  32. user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
  33. score = models.DecimalField(max_digits=10, decimal_places=2)
  34. days_played = models.IntegerField()
  35. created_at = models.DateTimeField(auto_now_add=True)
  36. class Meta:
  37. ordering = ['-score']
  38. def __str__(self):
  39. return f"{self.player_name}: ${self.score}"
  40. class PlayerStats(models.Model):
  41. """Track overall player statistics"""
  42. user = models.OneToOneField(User, on_delete=models.CASCADE)
  43. games_played = models.IntegerField(default=0)
  44. games_won = models.IntegerField(default=0) # Positive net worth
  45. best_score = models.DecimalField(max_digits=10, decimal_places=2, default=0)
  46. total_profit = models.DecimalField(max_digits=12, decimal_places=2, default=0)
  47. favorite_location = models.CharField(max_length=50, blank=True)
  48. created_at = models.DateTimeField(auto_now_add=True)
  49. updated_at = models.DateTimeField(auto_now=True)
  50. def __str__(self):
  51. return f"Stats for {self.user.email}"