models.py 2.5 KB

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