def handle(self, *args, **options): try: self.game = Game.imminent_game() except Game.DoesNotExist: self.stderr.write("No currently-running game found.") call_command('newgame', stdout=self.stdout, stderr=self.stderr) self.game = Game.imminent_game() self.schools = list(School.objects.all()) self.dorms = list(Building.dorms().all()) if len(self.schools) == 0 or len(self.dorms) == 0: call_command( 'loaddata', os.path.join('HVZ', 'main', 'fixtures', 'production.json'), stdout=self.stdout, stderr=self.stderr, ) self.schools = list(School.objects.all()) self.dorms = list(Building.dorms().all()) num_players = options.get('players') or NUM_PLAYERS num_ozs = options.get('ozs') or NUM_OZS self.password = options.get('password') or PASSWORD self.year = settings.NOW().year players = self.make_players(num_players) Player.objects.bulk_create(players) self.pick_ozs(num_ozs)
def make_meals(self, num_meals): humans = list(Player.current_players().filter(team='H')) zombies = list(Player.current_players().filter(team='Z')) g = Game.imminent_game() days = (g.end_date - g.start_date).days day = g.start_date while day < g.end_date: meals_per_hour = [0 for i in xrange(24)] for i in xrange(int(num_meals / days)): meals_per_hour[int(random.gauss(13, 4)) % 24] += 1 for hour in xrange(24): for meal in xrange(meals_per_hour[hour]): z = int(random.random() * len(zombies)) h = int(random.random() * len(humans)) Meal( eater=zombies[z], eaten=humans[h], time=datetime.combine(day, time(hour=hour)), location=random.choice(Building.objects.all()), ).save() # Instead of retrieving the entire zombie and human lists # again, why don't we just add the human to the zombie # horde ourselves? zombies.append(humans.pop(h)) day += timedelta(days=1)
def clean(self, *args, **kwargs): cleaned_data = super(MealForm, self).clean(*args, **kwargs) g = Game.imminent_game() # Check that day and time exist if not 'day' in cleaned_data: raise forms.ValidationError("Somehow you didn't specify the day.") if not 'time' in cleaned_data: raise forms.ValidationError("Somehow you didn't select a time.") feed_date = g.start_date + datetime.timedelta( days=int(cleaned_data['day'])) if feed_date < g.start_date: raise forms.ValidationError("Can't have eaten before the game!") if feed_date > g.end_date: raise forms.ValidationError( "Can't have eaten after the game ended!") feed_time = datetime.datetime.combine(feed_date, cleaned_data['time']) if feed_time > settings.NOW(): raise forms.ValidationError("You can't eat in the future, bro.") cleaned_data['time'] = feed_time return cleaned_data
def save(self, commit=True): """Save the player, creating or updating the user if necessary.""" player = super(RegisterForm, self).save(commit=False) def grab(s): return self.cleaned_data.get(s) email = grab('email') password = grab('password1') try: user = User.objects.get(email=email) user.set_password(password) except User.DoesNotExist: user = User.objects.create_user( email=email, username=email, password=password, ) finally: user.first_name = grab("first_name") user.last_name = grab("last_name") user.full_clean() user.save() player.user = user player.game = Game.imminent_game() if commit == True: player.save() return player
def get_offset(): """Return the number of days since game start.""" try: g = Game.imminent_game() except Game.DoesNotExist: return 0 return (settings.NOW().date() - g.start_date).days
def clean(self, *args, **kwargs): cleaned_data = super(MealForm, self).clean(*args, **kwargs) g = Game.imminent_game() # Check that day and time exist if not 'day' in cleaned_data: raise forms.ValidationError("Somehow you didn't specify the day.") if not 'time' in cleaned_data: raise forms.ValidationError("Somehow you didn't select a time.") feed_date = g.start_date + datetime.timedelta(days=int(cleaned_data['day'])) if feed_date < g.start_date: raise forms.ValidationError("Can't have eaten before the game!") if feed_date > g.end_date: raise forms.ValidationError("Can't have eaten after the game ended!") feed_time = datetime.datetime.combine(feed_date, cleaned_data['time']) if feed_time > settings.NOW(): raise forms.ValidationError("You can't eat in the future, bro.") cleaned_data['time'] = feed_time return cleaned_data
def handle(self, *args, **options): try: self.game = Game.imminent_game() except Game.DoesNotExist: # Create game and players call_command( 'randomplayers', players=options.get('players'), ozs=options.get('ozs'), password=options.get('password'), stdout=self.stdout, stderr=self.stderr, ) self.game = Game.imminent_game() humans = Player.current_players().filter(team='H') zombies = Player.current_players().filter(team='Z') if not len(humans) or not len(zombies): self.stderr.write( '\n'.join(["There are no players in the current game.", "To generate a batch of random players, run", " ./manage.py randomplayers"])) return num_meals = options.get('meals') or len(humans) / 2 self.stdout.write("Creating {} meals".format(num_meals)) if not num_meals: return if len(humans) < num_meals: self.stderr.write( "There are only {} humans in the current game.".format(len(humans)) ) num_meals = len(humans) meals = self.make_meals(num_meals) Meal.objects.bulk_create(meals)
def weekday_choices(): """Returns a list of offsets from the start of the current game.""" try: g = Game.imminent_game() except Game.DoesNotExist: return [(i, CAL.formatweekday(i, 3)) for i in xrange(7)] # Bounds on when the meal can have occurred start = g.start_date end = min(settings.NOW().date(), g.end_date) return ((i, CAL.formatweekday((start + datetime.timedelta(days=i)).weekday(), 3)) for i in range((end - start).days + 1))
def weekday_choices(): """Returns a list of offsets from the start of the current game.""" try: g = Game.imminent_game() except Game.DoesNotExist: return xrange(7) # Bounds on when the meal can have occurred start = g.start_date end = min(settings.NOW().date(), g.end_date) return ( (i, CAL.formatweekday((start+datetime.timedelta(days=i)).weekday(), 3)) for i in range((end-start).days+1) )
def test_invalid_time(self): """Ensure that eating times only occur within the game's timeline.""" g = Game.imminent_game() num_z = Player.objects.filter(team="Z").count() self.assertEqual(Meal.objects.count(), 0) m = MEAL.copy() m["day"] = -1 c = Client() c.post(reverse("login"), ROB_ZOMBIE) c.post(reverse("feed_eat"), m) # Ensure that no meal was created, and no new zombies have spawned. self.assertEqual(Meal.objects.count(), 0) self.assertEqual(Player.objects.filter(team="Z").count(), num_z)
def handle(self, *args, **options): try: self.game = Game.imminent_game() except Game.DoesNotExist: self.stderr.write("No currently-running game found.") return num_players = options.get('players') num_ozs = options.get('ozs') or NUM_OZS self.password = options.get('password') or PASSWORD self.year = settings.NOW().year players = self.make_players(num_players) Player.objects.bulk_create(players) self.pick_ozs(num_ozs)
def save(self, commit=True): """Save the player, creating or updating the user if necessary.""" player = super(RegisterForm, self).save(commit=False) def grab(s): return self.cleaned_data.get(s) email = grab('email') password = grab('password1') try: user = User.objects.select_for_update().get(email=email) user.set_password(password) except User.DoesNotExist: # Slice is there because of a 30 character limit on # usernames... we need a custom user model in the future. # FIXED: the longerusernameandemail app makes usernames and # emails the same length and that length is 255 characters user = User.objects.create_user( email=email, username=email, password=password, ) user.first_name = grab("first_name") user.last_name = grab("last_name") user.full_clean() user.save() player.user = user player.game = Game.imminent_game() if commit == True: player.save() return player
def get_queryset(self): return Player.objects.all().filter(game=Game.imminent_game())
def get_queryset(self): game = Game.imminent_game() return (Player.objects.filter(game=game). select_related('user'). annotate(meal_count=Count('meal_set')))
def get_queryset(self): game = Game.imminent_game() team = self.player.team return Plot.get_visible(game, team).select_related('mission')