123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- from django.conf import settings
- from django.db import models
- from django.contrib.auth.models import User
- import json
- class GameSession(models.Model):
- user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
- session_key = models.CharField(max_length=40, null=True, blank=True)
- day = models.IntegerField(default=1)
- cash = models.DecimalField(max_digits=10, decimal_places=2, default=2000.00)
- debt = models.DecimalField(max_digits=10, decimal_places=2, default=5500.00)
- location = models.CharField(max_length=50, default='Downtown')
- inventory = models.TextField(default='{}') # JSON string
- capacity = models.IntegerField(default=100)
- high_score = models.DecimalField(max_digits=10, decimal_places=2, default=0)
- interest = models.DecimalField(max_digits=10, decimal_places=2, default=settings.INTEREST_RATE)
- max_days = models.DecimalField(max_digits=10, decimal_places=2, default=settings.MAX_DAYS)
- is_active = models.BooleanField(default=True) # Track active games
- completed = models.BooleanField(default=False) # Track completed games
- final_score = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
- created_at = models.DateTimeField(auto_now_add=True)
- updated_at = models.DateTimeField(auto_now=True)
- def get_inventory(self):
- return json.loads(self.inventory) if self.inventory else {}
- def set_inventory(self, inv_dict):
- self.inventory = json.dumps(inv_dict)
- def __str__(self):
- if self.user:
- return f"Game {self.id} - {self.user.email} - Day {self.day}"
- return f"Game {self.id} - Anonymous - Day {self.day}"
- class HighScore(models.Model):
- player_name = models.CharField(max_length=100)
- user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
- score = models.DecimalField(max_digits=10, decimal_places=2)
- days_played = models.IntegerField()
- created_at = models.DateTimeField(auto_now_add=True)
- class Meta:
- ordering = ['-score']
- def __str__(self):
- return f"{self.player_name}: ${self.score}"
- class PlayerStats(models.Model):
- """Track overall player statistics"""
- user = models.OneToOneField(User, on_delete=models.CASCADE)
- games_played = models.IntegerField(default=0)
- games_won = models.IntegerField(default=0) # Positive net worth
- best_score = models.DecimalField(max_digits=10, decimal_places=2, default=0)
- total_profit = models.DecimalField(max_digits=12, decimal_places=2, default=0)
- favorite_location = models.CharField(max_length=50, blank=True)
- created_at = models.DateTimeField(auto_now_add=True)
- updated_at = models.DateTimeField(auto_now=True)
- def __str__(self):
- return f"Stats for {self.user.email}"
|