def edit_game(request, game_id): game = get_object_or_404(Game, id=game_id) initial_data = { 'title': game.title, 'hook': game.hook, 'scenario': game.scenario, 'required_character_status': game.required_character_status, 'start_time': convert_to_localtime(game.scheduled_start_time), 'cell': game.cell, } GameForm = make_game_form(user=request.user, game_status=game.status) if request.method == 'POST': if not request.user.has_perm('edit_game', game): raise PermissionDenied( "You don't have permission to edit this Game event") form = GameForm(request.POST, initial=initial_data) if form.is_valid(): game.title = form.cleaned_data['title'] game.hook = form.cleaned_data['hook'] start_time = form.cleaned_data['scheduled_start_time'] if "timezone" in form.cleaned_data: account = request.user.account account.timezone = form.cleaned_data["timezone"] account.save() start_time = change_time_to_current_timezone(start_time) if game.is_scheduled(): game.open_invitations = form.cleaned_data['open_invitations'] game.scheduled_start_time = start_time game.required_character_status = form.cleaned_data[ 'required_character_status'] game.scenario = form.cleaned_data['scenario'] game.cell = form.cleaned_data['cell'] if form.cleaned_data['scenario']: game.scenario = form.cleaned_data['scenario'] with transaction.atomic(): game.save() if game.is_scheduled() and hasattr( form.changed_data, 'invite_all_members' ) and form.cleaned_data['invite_all_members']: for member in game.cell.cellmembership_set.exclude( member_player=game.gm): game_invite = Game_Invite( invited_player=member.member_player, relevant_game=game, invite_text=game.hook, as_ringer=False) game_invite.save() return HttpResponseRedirect( reverse('games:games_view_game', args=(game.id, ))) else: print(form.errors) return None else: # Build a game form. form = GameForm(initial=initial_data) context = { 'game': game, 'form': form, } return render(request, 'games/edit_game.html', context)
def send_contractor_on_game(self, character): game = Game( scenario=self.scenario, title="title", creator=self.user2, gm=self.user2, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) game.save() attendance = Game_Attendance( relevant_game=game, notes="notes", outcome=WIN, attending_character=character, ) game_invite = Game_Invite(invited_player=character.player, relevant_game=game, as_ringer=False, ) attendance.save() game_invite.attendance = attendance game_invite.save() game.give_rewards() return attendance
def invite_players(request, game_id): game = get_object_or_404(Game, id=game_id) if not request.user.has_perm('edit_game', game) or not game.is_scheduled(): raise PermissionDenied( "You don't have permission to edit this Game event, or it has already started" ) initial_data = {"message": game.hook} if request.method == 'POST': form = CustomInviteForm(request.POST, initial=initial_data) if form.is_valid(): player = get_object_or_404( User, username__iexact=form.cleaned_data['username']) if get_queryset_size( player.game_invite_set.filter(relevant_game=game)) > 0: #player is already invited. Maybe update invitation instead? raise PermissionDenied("Player already invited") if player == game.creator or player == game.gm: raise PermissionDenied( "You can't invite players to someone else's game") game_invite = Game_Invite( invited_player=player, relevant_game=game, invite_text=form.cleaned_data['message'], as_ringer=form.cleaned_data['invite_as_ringer']) with transaction.atomic(): game_invite.save() game_invite.notify_invitee(request, game) return HttpResponseRedirect( reverse('games:games_view_game', args=(game.id, ))) else: print(form.errors) return None else: return HttpResponseRedirect( reverse('games:games_view_game', args=(game.id, )))
def accept_invite(request, game_id): game = get_object_or_404(Game, id=game_id) if not game.player_can_rsvp(request.user): raise PermissionDenied(game.reason_player_cannot_rsvp(request.user)) invite = get_object_or_none( request.user.game_invite_set.filter(relevant_game=game.id)) if request.method == 'POST': if not invite: # player self-invite invite = Game_Invite(invited_player=request.user, relevant_game=game) if game.scenario in request.user.scenario_set.all(): invite.as_ringer = True form = make_accept_invite_form(invite)(request.POST) if form.is_valid(): game_attendance = invite.attendance with transaction.atomic(): invite.save() if game_attendance: game_attendance.attending_character = form.cleaned_data[ 'attending_character'] game_attendance.save() else: game_attendance = Game_Attendance( attending_character=form. cleaned_data['attending_character'], relevant_game=game, ) game_attendance.save() invite.is_declined = False invite.attendance = game_attendance invite.save() if invite.as_ringer and not form.cleaned_data[ 'attending_character']: #Reveal scenario to ringer game.scenario.played_discovery(request.user) return HttpResponseRedirect( reverse('games:games_view_game', args=(game.id, ))) else: print(form.errors) return None else: can_view_scenario = False if request.user.has_perm("view_scenario", game.scenario): can_view_scenario = True # Build a accept form. if not invite: # if game is open for self-invites, make a temp invite that we don't save so we can make a form invite = Game_Invite(invited_player=request.user, relevant_game=game) if game.scenario in request.user.scenario_set.all(): invite.as_ringer = True form = make_accept_invite_form(invite) context = { 'form': form, 'game': game, 'invite': invite, 'can_view_scenario': can_view_scenario, } return render(request, 'games/accept_invite.html', context)
def test_archive_game_death(self): self.assertEquals(self.char_user1_cell.unspent_rewards().count(), 0) self.assertEquals(self.char_user1_cell.unspent_gifts().count(), 0) self.assertEquals(self.char_user1_cell.completed_games().count(), 0) self.assertEquals(self.char_user1_cell.number_of_victories(), 0) self.assertEquals(self.char_user1_cell.number_of_losses(), 0) self.assertEquals(self.char_user1_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user1_cell.exp_earned(), EXP_NEW_CHAR) self.assertEquals( self.user1.rewarded_player .filter(rewarded_character=None, is_charon_coin=True) .filter(is_void=False).all().count(), 0) game = Game( title="title", creator=self.user2, gm=self.user2, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) game.save() attendance = Game_Attendance( relevant_game=game, notes="notes", outcome=OUTCOME[2][0], # death attending_character=self.char_user1_cell, ) game_invite = Game_Invite(invited_player=self.user1, relevant_game=game, as_ringer=False, ) attendance.save() game_invite.attendance = attendance game_invite.save() game.give_rewards() self.assertEquals(self.char_user1_cell.unspent_rewards().count(), 0) self.assertEquals(self.char_user1_cell.unspent_gifts().count(), 0) self.assertEquals(self.char_user1_cell.completed_games().count(), 1) self.assertEquals(self.char_user1_cell.number_of_victories(), 0) self.assertEquals(self.char_user1_cell.number_of_losses(), 0) self.assertEquals(self.char_user1_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user1_cell.exp_earned(), EXP_NEW_CHAR) self.assertEquals(self.char_user1_cell.is_dead(), True) self.assertEquals( self.user1.rewarded_player .filter(rewarded_character=None, is_charon_coin=True) .filter(is_void=False).all().count(), 1)
def create_game(request): if request.method == 'POST': form = make_game_form(user=request.user, game_status=GAME_STATUS[0][0])(request.POST) if form.is_valid(): game = Game( title=form.cleaned_data['title'], creator=request.user, gm=request.user, required_character_status=form. cleaned_data['required_character_status'], hook=form.cleaned_data['hook'], created_date=timezone.now(), scheduled_start_time=form.cleaned_data['scheduled_start_time'], open_invitations=form.cleaned_data['open_invitations'], status=GAME_STATUS[0][0], cell=form.cleaned_data['cell']) if form.cleaned_data['scenario']: game.scenario = form.cleaned_data['scenario'] with transaction.atomic(): game.save() if form.cleaned_data['invite_all_members']: for member in game.cell.cellmembership_set.exclude( member_player=game.gm): game_invite = Game_Invite( invited_player=member.member_player, relevant_game=game, invite_text=game.hook, as_ringer=False) game_invite.save() game_invite.notify_invitee(request, game) game_url = reverse('games:games_view_game', args=(game.id, )) messages.add_message( request, messages.SUCCESS, mark_safe( "Your Game has been created Successfully." "<br>" "<a href='" + game_url + "'> Click Here</a> " "if you do not want to invite anyone else at this time.")) return HttpResponseRedirect( reverse('games:games_invite_players', args=(game.id, ))) else: print(form.errors) return None else: # Build a game form. form = make_game_form(user=request.user, game_status=GAME_STATUS[0][0]) context = { 'form': form, } return render(request, 'games/edit_game.html', context)
def test_archive_game_gm_rewards_ratio(self): self.assertEquals(self.user2.experiencereward_set.filter(rewarded_character=None).all().count(), 0) self.assertEquals( self.user2.rewarded_player.filter(rewarded_character=None, is_charon_coin=False).filter(is_void=False).all().count(), 0) game = Game( title="title", creator=self.user2, gm=self.user2, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) game.save() attendance = Game_Attendance( relevant_game=game, notes="notes", outcome=WIN, attending_character=self.char_user1_cell, ) game_invite = Game_Invite(invited_player=self.user1, relevant_game=game, as_ringer=False, ) attendance.save() game_invite.attendance = attendance game_invite.save() attendance = Game_Attendance( relevant_game=game, notes="notes", outcome=DEATH, attending_character=self.char_user2_cell, ) game_invite = Game_Invite(invited_player=self.user2, relevant_game=game, as_ringer=False, ) attendance.save() game_invite.attendance = attendance game_invite.save() game.give_rewards() self.assertEquals(self.user2.experiencereward_set.filter(rewarded_character=None).all().count(), 1) self.assertEquals(self.user2.rewarded_player.filter(rewarded_character=None, is_charon_coin=False).filter(is_void=False).all().count(), 1)
def test_archive_game_victory_not_in_cell(self): self.assertEquals(self.char_user2_nocell.unspent_rewards().count(), 0) self.assertEquals(self.char_user2_nocell.unspent_gifts().count(), 0) self.assertEquals(self.char_user2_nocell.completed_games().count(), 0) self.assertEquals(self.char_user2_nocell.number_of_victories(), 0) self.assertEquals(self.char_user2_nocell.number_of_losses(), 0) self.assertEquals(self.char_user2_nocell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user2_nocell.exp_earned(), EXP_NEW_CHAR) self.assertEquals(self.user2.game_invite_set.filter(attendance__is_confirmed=False).exclude(is_declined=True).all().count(), 0) game = Game( title="title", creator=self.user1, gm=self.user1, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) game.save() attendance = Game_Attendance( relevant_game=game, notes="notes", outcome=OUTCOME[0][0], # victory attending_character=self.char_user2_nocell, is_confirmed = False, ) game_invite = Game_Invite(invited_player=self.user2, relevant_game=game, as_ringer=False, ) attendance.save() game_invite.attendance = attendance game_invite.save() game.give_rewards() self.assertEquals(self.char_user2_nocell.unspent_rewards().count(), 0) self.assertEquals(self.char_user2_nocell.unspent_gifts().count(), 0) self.assertEquals(self.char_user2_nocell.completed_games().count(), 0) self.assertEquals(self.char_user2_nocell.number_of_victories(), 0) self.assertEquals(self.char_user2_nocell.number_of_losses(), 0) self.assertEquals(self.char_user2_nocell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user2_nocell.exp_earned(), EXP_NEW_CHAR) self.assertEquals(self.user2.game_invite_set.filter(attendance__is_confirmed=False).exclude(is_declined=True).all().count(), 1)
def test_cannot_double_invite(self): game = Game( title="title", creator=self.user1, gm=self.user1, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) game.save() game_invite = Game_Invite(invited_player=self.user2, relevant_game=game, as_ringer=False, ) game_invite.save() game_invite2 = Game_Invite(invited_player=self.user2, relevant_game=game, as_ringer=False, ) with self.assertRaises(IntegrityError): game_invite2.save()
def setUp(self): self.user1 = User.objects.create_user(username='******', email='jacob@…', password='******') make_profile_for_new_user(None, self.user1) self.user2 = User.objects.create_user(username='******', email='jacob@2…', password='******') make_profile_for_new_user(None, self.user2) self.cell_owner = User.objects.create_user(username='******', email='jacob@23…', password='******') self.cell = Cell.objects.create(name="cell name", creator=self.cell_owner, setting_name="world name", setting_description="Test description") self.char1 = Character.objects.create( name="testchar1", tagline="they test so bad!", player=self.user1, appearance="they're ugly because that's better.", age="13 years", sex="taco bell", concept_summary="generic but relatable", ambition="eat a dragon's heart", private=True, pub_date=timezone.now(), edit_date=timezone.now(), cell=self.cell) self.char2 = Character.objects.create( name="testchar2", tagline="they test so bad!", player=self.user1, appearance="they're ugly because that's better.", age="13 years", sex="taco bell", concept_summary="generic but relatable", ambition="eat a dragon's heart", private=True, pub_date=timezone.now(), edit_date=timezone.now(), cell=self.cell) self.game1 = Game( title="title", creator=self.user2, gm=self.user2, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) self.game1.save() self.attendance_game1_char1 = Game_Attendance( relevant_game=self.game1, notes="notes", outcome=WIN, attending_character=self.char1, ) game_invite = Game_Invite(invited_player=self.user1, relevant_game=self.game1, as_ringer=False) self.attendance_game1_char1.save() game_invite.attendance = self.attendance_game1_char1 game_invite.save() self.game1.give_rewards() self.game2 = Game( title="title", creator=self.user2, gm=self.user2, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) self.game2.save() self.attendance_game2_char1 = Game_Attendance( relevant_game=self.game2, notes="notes", outcome=WIN, attending_character=self.char1, ) game_invite = Game_Invite(invited_player=self.user1, relevant_game=self.game2, as_ringer=False) self.attendance_game2_char1.save() game_invite.attendance = self.attendance_game2_char1 game_invite.save() self.game1.give_rewards() self.games = [] self.char1_attendances = [] for x in range(10): new_game = Game( title="title", creator=self.user2, gm=self.user2, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) self.games.append(new_game) new_game.save() new_attendance = Game_Attendance( relevant_game=self.games[x], notes="notes", outcome=WIN, attending_character=self.char1, ) self.char1_attendances.append(new_attendance) new_attendance.save() game_invite = Game_Invite(invited_player=self.user1, relevant_game=new_game, as_ringer=False) game_invite.attendance = new_attendance game_invite.save() new_game.give_rewards()
def test_archive_game_recalculate_golden_ratio(self): with transaction.atomic(): game = Game( title="title", creator=self.user2, gm=self.cell_owner, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) game.save() attendance = Game_Attendance( relevant_game=game, notes="notes", outcome=WIN, attending_character=self.char_user1_cell, ) game_invite = Game_Invite(invited_player=self.user1, relevant_game=game, as_ringer=False, ) attendance.save() game_invite.attendance = attendance game_invite.save() game.give_rewards() self.assertFalse(game.achieves_golden_ratio()) self.assertEquals(self.user1.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user1.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user1.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.user2.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user2.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user2.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.cell_owner.profile.get_avail_improvements().count(), 0) self.assertEquals(self.cell_owner.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.cell_owner.profile.get_avail_exp_rewards().count(), 1) # cell_owner gmed the game attendance2 = Game_Attendance( relevant_game=game, notes="notes", outcome=DEATH, attending_character=self.char_user2_cell, ) game_invite = Game_Invite(invited_player=self.user2, relevant_game=game, as_ringer=False, ) attendance2.save() game_invite.attendance = attendance2 game_invite.save() attendance2.give_reward() game.refresh_from_db() game.recalculate_golden_ratio(False) self.assertTrue(game.achieves_golden_ratio()) self.assertEquals(self.user1.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user1.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user1.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.user2.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user2.profile.get_avail_charon_coins().count(), 1) # from dying self.assertEquals(self.user2.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.cell_owner.profile.get_avail_improvements().count(), 1) # from ratio self.assertEquals(self.cell_owner.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.cell_owner.profile.get_avail_exp_rewards().count(), 1) # cell_owner gmed the game attendance2.refresh_from_db() attendance2.change_outcome(new_outcome=WIN, is_confirmed=True) game.refresh_from_db() game.recalculate_golden_ratio(True) self.assertFalse(game.achieves_golden_ratio()) self.assertEquals(self.user1.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user1.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user1.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.user2.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user2.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user2.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.cell_owner.profile.get_avail_improvements().count(), 0) # no more ratio reward self.assertEquals(self.cell_owner.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.cell_owner.profile.get_avail_exp_rewards().count(), 1) # cell_owner gmed the game
def test_archive_game_change_attendance_confirmed(self): with transaction.atomic(): game = Game( title="title", creator=self.user2, gm=self.user2, created_date=timezone.now(), scheduled_start_time=timezone.now(), actual_start_time=timezone.now(), end_time=timezone.now(), status=GAME_STATUS[6][0], cell=self.cell, ) game.save() attendance = Game_Attendance( relevant_game=game, notes="notes", outcome=WIN, attending_character=self.char_user1_cell, ) game_invite = Game_Invite(invited_player=self.user1, relevant_game=game, as_ringer=False, ) attendance.save() game_invite.attendance = attendance game_invite.save() game.give_rewards() self.assertEquals(self.char_user1_cell.unspent_rewards().count(), 1) self.assertEquals(self.char_user1_cell.unspent_gifts().count(), 1) self.assertEquals(self.char_user1_cell.completed_games().count(), 1) self.assertEquals(self.char_user1_cell.number_of_victories(), 1) self.assertEquals(self.char_user1_cell.number_of_losses(), 0) self.assertEquals(self.char_user1_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user1_cell.exp_earned(), EXP_NEW_CHAR + EXP_WIN) self.assertEquals(self.user1.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user1.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user1.profile.get_avail_exp_rewards().count(), 0) attendance.refresh_from_db() attendance.change_outcome(new_outcome = WIN, is_confirmed = False, attending_character = self.char_user2_cell) self.assertEquals(self.char_user1_cell.is_dead(), False) self.assertEquals(self.char_user1_cell.unspent_rewards().count(), 0) self.assertEquals(self.char_user1_cell.unspent_gifts().count(), 0) self.assertEquals(self.char_user1_cell.completed_games().count(), 0) self.assertEquals(self.char_user1_cell.number_of_victories(), 0) self.assertEquals(self.char_user1_cell.number_of_losses(), 0) self.assertEquals(self.char_user1_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user1_cell.exp_earned(), EXP_NEW_CHAR) self.assertEquals(self.char_user2_cell.is_dead(), False) self.assertEquals(self.char_user2_cell.unspent_rewards().count(), 0) self.assertEquals(self.char_user2_cell.unspent_gifts().count(), 0) self.assertEquals(self.char_user2_cell.completed_games().count(), 0) self.assertEquals(self.char_user2_cell.number_of_victories(), 0) self.assertEquals(self.char_user2_cell.number_of_losses(), 0) self.assertEquals(self.char_user2_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user2_cell.exp_earned(), EXP_NEW_CHAR) self.assertEquals(self.user1.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user1.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user1.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.user2.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user2.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user2.profile.get_avail_exp_rewards().count(), 1) # User 2 gmed the game attendance.refresh_from_db() attendance.change_outcome(new_outcome=WIN, is_confirmed=True, attending_character=self.char_user2_cell) self.assertEquals(self.char_user1_cell.is_dead(), False) self.assertEquals(self.char_user1_cell.unspent_rewards().count(), 0) self.assertEquals(self.char_user1_cell.unspent_gifts().count(), 0) self.assertEquals(self.char_user1_cell.completed_games().count(), 0) self.assertEquals(self.char_user1_cell.number_of_victories(), 0) self.assertEquals(self.char_user1_cell.number_of_losses(), 0) self.assertEquals(self.char_user1_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user1_cell.exp_earned(), EXP_NEW_CHAR) self.assertEquals(self.char_user2_cell.is_dead(), False) self.assertEquals(self.char_user2_cell.unspent_rewards().count(), 1) self.assertEquals(self.char_user2_cell.unspent_gifts().count(), 1) self.assertEquals(self.char_user2_cell.completed_games().count(), 1) self.assertEquals(self.char_user2_cell.number_of_victories(), 1) self.assertEquals(self.char_user2_cell.number_of_losses(), 0) self.assertEquals(self.char_user2_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user2_cell.exp_earned(), EXP_NEW_CHAR + EXP_WIN) self.assertEquals(self.user1.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user1.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user1.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.user2.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user2.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user2.profile.get_avail_exp_rewards().count(), 1) # User 2 gmed the game attendance.refresh_from_db() attendance.change_outcome(new_outcome = RINGER_VICTORY, is_confirmed = False) # We switched the character to one owned by user2, so now user2 gets the rewards self.assertEquals(self.char_user1_cell.is_dead(), False) self.assertEquals(self.char_user1_cell.unspent_rewards().count(), 0) self.assertEquals(self.char_user1_cell.unspent_gifts().count(), 0) self.assertEquals(self.char_user1_cell.completed_games().count(), 0) self.assertEquals(self.char_user1_cell.number_of_victories(), 0) self.assertEquals(self.char_user1_cell.number_of_losses(), 0) self.assertEquals(self.char_user1_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user1_cell.exp_earned(), EXP_NEW_CHAR) self.assertEquals(self.char_user2_cell.is_dead(), False) self.assertEquals(self.char_user2_cell.unspent_rewards().count(), 0) self.assertEquals(self.char_user2_cell.unspent_gifts().count(), 0) self.assertEquals(self.char_user2_cell.completed_games().count(), 0) self.assertEquals(self.char_user2_cell.number_of_victories(), 0) self.assertEquals(self.char_user2_cell.number_of_losses(), 0) self.assertEquals(self.char_user2_cell.stats_snapshot.sources.count(), 0) self.assertEquals(self.char_user2_cell.exp_earned(), EXP_NEW_CHAR) self.assertEquals(self.user1.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user1.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user1.profile.get_avail_exp_rewards().count(), 0) self.assertEquals(self.user2.profile.get_avail_improvements().count(), 0) self.assertEquals(self.user2.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user2.profile.get_avail_exp_rewards().count(), 1) # User 2 gmed the game attendance.refresh_from_db() attendance.change_outcome(new_outcome=RINGER_VICTORY, is_confirmed=True) # We switched the character to one owned by user2, so now user2 gets the rewards self.assertEquals(self.user2.profile.get_avail_improvements().count(), 1) self.assertEquals(self.user2.profile.get_avail_charon_coins().count(), 0) self.assertEquals(self.user2.profile.get_avail_exp_rewards().count(), 2) # User 2 gmed the game
def finalize_create_ex_game_for_cell(request, cell_id, gm_user_id, players): if not request.user.is_authenticated: raise PermissionDenied("Log in, yo") cell = get_object_or_404(Cell, id=cell_id) if not cell.player_can_manage_games(request.user): raise PermissionDenied( "You do not have permission to manage Game Events for this Cell") gm = get_object_or_404(User, id=gm_user_id) player_list = [ get_object_or_404(User, id=player_id) for player_id in players.split('+') if not player_id == str(gm.id) ] GenInfoForm = make_archive_game_general_info_form(gm) ArchivalOutcomeFormSet = formset_factory(ArchivalOutcomeForm, extra=0) if request.method == 'POST': general_form = GenInfoForm(request.POST, prefix="general") outcome_formset = ArchivalOutcomeFormSet(request.POST, prefix="outcome", initial=[{ 'player_id': x.id, 'invited_player': x } for x in player_list]) if general_form.is_valid() and outcome_formset.is_valid(): form_gm = get_object_or_404(User, id=general_form.cleaned_data['gm_id']) if not cell.get_player_membership(form_gm): raise PermissionDenied( "Only players who are members of a Cell can GM games inside it." ) with transaction.atomic(): occurred_time = general_form.cleaned_data['occurred_time'] if "timezone" in general_form.cleaned_data: account = request.user.account account.timezone = general_form.cleaned_data["timezone"] account.save() occurred_time = change_time_to_current_timezone( occurred_time) #TODO: check to see if the game has the exact same time as existing game and fail. game = Game( title=general_form.cleaned_data['title'], creator=request.user, gm=form_gm, created_date=timezone.now(), scheduled_start_time=occurred_time, actual_start_time=occurred_time, end_time=occurred_time, status=GAME_STATUS[6][0], cell=cell, ) if general_form.cleaned_data['scenario']: game.scenario = general_form.cleaned_data['scenario'] game.save() for form in outcome_formset: player = get_object_or_404( User, id=form.cleaned_data['player_id']) attendance = Game_Attendance( relevant_game=game, notes=form.cleaned_data['notes'], outcome=form.cleaned_data['outcome'], ) game_invite = Game_Invite( invited_player=player, relevant_game=game, as_ringer=False, ) game_invite.save() if 'attending_character' in form.cleaned_data \ and not form.cleaned_data['attending_character'] is None: if hasattr(form.cleaned_data['attending_character'], 'cell'): attendance.attending_character = form.cleaned_data[ 'attending_character'] if not form.cleaned_data[ 'attending_character'].cell == cell: attendance.is_confirmed = False else: attendance.is_confirmed = False else: game_invite.as_ringer = True attendance.is_confirmed = False if game.creator.id == player.id: attendance.is_confirmed = True attendance.save() game_invite.attendance = attendance game_invite.save() game.give_rewards() return HttpResponseRedirect( reverse('cells:cells_view_cell', args=(cell.id, ))) else: print(general_form.errors) print(outcome_formset.errors) return None else: general_form = GenInfoForm(prefix="general") outcome_formset = ArchivalOutcomeFormSet(prefix="outcome", initial=[{ 'player_id': x.id, 'invited_player': x } for x in player_list]) context = { 'general_form': general_form, 'outcome_formset': outcome_formset, 'cell': cell, 'cell_id': cell_id, 'gm_user_id': gm_user_id, 'players': players, 'gm': gm, } return render(request, 'games/edit_archive_game.html', context)
def accept_invite(request, game_id): if not request.user.is_authenticated or request.user.is_anonymous: raise PermissionDenied("You must log in to accept this Game invite") game = get_object_or_404(Game, id=game_id) if not game.is_scheduled( ) or game.creator.id == request.user.id or game.gm.id == request.user.id: raise PermissionDenied( "This Game has already started, or you're the GM!") invite = get_object_or_none( request.user.game_invite_set.filter(relevant_game=game)) if not invite and not game.open_invitations: raise PermissionDenied( "This is awkward. . . You, uh, haven't been invited to this Game") if request.method == 'POST': if not invite: # player self-invite invite = Game_Invite(invited_player=request.user, relevant_game=game) if game.scenario in request.user.scenario_set.all(): invite.as_ringer = True form = make_accept_invite_form(invite)(request.POST) if form.is_valid(): game_attendance = invite.attendance with transaction.atomic(): invite.save() if game_attendance: game_attendance.attending_character = form.cleaned_data[ 'attending_character'] game_attendance.save() else: game_attendance = Game_Attendance( attending_character=form. cleaned_data['attending_character'], relevant_game=game, ) game_attendance.save() invite.is_declined = False invite.attendance = game_attendance invite.save() if invite.as_ringer and not form.cleaned_data[ 'attending_character']: #Reveal scenario to ringer game.scenario.played_discovery(request.user) return HttpResponseRedirect( reverse('games:games_view_game', args=(game.id, ))) else: print(form.errors) return None else: can_view_scenario = False if request.user.has_perm("view_scenario", game.scenario): can_view_scenario = True # Build a accept form. if not invite: # if game is open for self-invites, make a temp invite that we don't save so we can make a form invite = Game_Invite(invited_player=request.user, relevant_game=game) if game.scenario in request.user.scenario_set.all(): invite.as_ringer = True form = make_accept_invite_form(invite) context = { 'form': form, 'game': game, 'invite': invite, 'can_view_scenario': can_view_scenario, } return render(request, 'games/accept_invite.html', context)
def edit_game(request, game_id): game = get_object_or_404(Game, id=game_id) initial_data = { 'hook': game.hook, 'scenario': game.scenario, 'required_character_status': game.required_character_status, 'start_time': convert_to_localtime(game.scheduled_start_time), 'cell': game.cell, 'only_over_18': game.is_nsfw, 'invitation_mode': game.invitation_mode, 'list_in_lfg': game.list_in_lfg, 'allow_ringers': game.allow_ringers, 'max_rsvp': game.max_rsvp, 'gametime_url': game.gametime_url, 'mediums': game.mediums.all(), } GameForm = make_game_form(user=request.user) if not game.player_can_edit(request.user): raise PermissionDenied( "You don't have permission to edit this Game event.") if not game.is_scheduled(): raise PermissionDenied( "You cannot edit a Game event once it has started.") if request.method == 'POST': form = GameForm(request.POST, initial=initial_data) if form.is_valid(): title = form.cleaned_data[ 'title'] if "title" in form.cleaned_data and form.cleaned_data[ 'title'] else "untitled" game.title = title game.hook = form.cleaned_data['hook'] start_time = form.cleaned_data['scheduled_start_time'] if "timezone" in form.cleaned_data: account = request.user.account account.timezone = form.cleaned_data["timezone"] account.save() start_time = change_time_to_current_timezone(start_time) game.scheduled_start_time = start_time game.required_character_status = form.cleaned_data[ 'required_character_status'] game.scenario = form.cleaned_data['scenario'] game.cell = form.cleaned_data['cell'] game.invitation_mode = form.cleaned_data['invitation_mode'] game.list_in_lfg = form.cleaned_data['list_in_lfg'] game.allow_ringers = form.cleaned_data['allow_ringers'] game.max_rsvp = form.cleaned_data['max_rsvp'] game.gametime_url = form.cleaned_data['gametime_url'] if 'only_over_18' in form.cleaned_data: game.is_nsfw = form.cleaned_data['only_over_18'] if form.cleaned_data['scenario']: game.scenario = form.cleaned_data['scenario'] game.title = game.scenario.title with transaction.atomic(): game.save() game.mediums.set(form.cleaned_data['mediums']) if hasattr(form.changed_data, 'invite_all_members' ) and form.cleaned_data['invite_all_members']: for member in game.cell.cellmembership_set.exclude( member_player=game.gm): game_invite = Game_Invite( invited_player=member.member_player, relevant_game=game, invite_text=game.hook, as_ringer=False) game_invite.save() game_invite.notify_invitee(request, game) return HttpResponseRedirect( reverse('games:games_view_game', args=(game.id, ))) else: print(form.errors) return None else: # Build a game form. form = GameForm(initial=initial_data) context = { 'game': game, 'form': form, } return render(request, 'games/edit_game.html', context)
def create_game(request, cell_id=None): if not request.user.is_authenticated: raise PermissionDenied("You must be logged in to create a Game") cell = None if cell_id: cell = get_object_or_404(Cell, id=cell_id) GameForm = make_game_form(user=request.user) if request.method == 'POST': form = GameForm(request.POST) if form.is_valid(): start_time = form.cleaned_data['scheduled_start_time'] if "timezone" in form.cleaned_data: account = request.user.account account.timezone = form.cleaned_data["timezone"] account.save() start_time = change_time_to_current_timezone(start_time) title = form.cleaned_data[ 'title'] if "title" in form.cleaned_data and form.cleaned_data[ 'title'] else "untitled" form_cell = form.cleaned_data['cell'] if not form_cell.get_player_membership(request.user): raise PermissionDenied("You are not a member of this World.") if not (form_cell.player_can_manage_games(request.user) or form_cell.player_can_run_games(request.user)): raise PermissionDenied( "You do not have permission to run Games in this World") game = Game( title=title, creator=request.user, gm=request.user, required_character_status=form. cleaned_data['required_character_status'], hook=form.cleaned_data['hook'], created_date=timezone.now(), scheduled_start_time=start_time, status=GAME_STATUS[0][0], cell=form_cell, invitation_mode=form.cleaned_data['invitation_mode'], list_in_lfg=form.cleaned_data['list_in_lfg'], allow_ringers=form.cleaned_data['allow_ringers'], max_rsvp=form.cleaned_data['max_rsvp'], gametime_url=form.cleaned_data['gametime_url'], ) if 'only_over_18' in form.cleaned_data: game.is_nsfw = form.cleaned_data['only_over_18'] if form.cleaned_data['scenario']: game.scenario = form.cleaned_data['scenario'] game.title = game.scenario.title with transaction.atomic(): game.save() game.mediums.set(form.cleaned_data['mediums']) if form.cleaned_data['invite_all_members']: for member in game.cell.cellmembership_set.exclude( member_player=game.gm): game_invite = Game_Invite( invited_player=member.member_player, relevant_game=game, invite_text=game.hook, as_ringer=False) if member.member_player.has_perm( "view_scenario", game.scenario): game_invite.as_ringer = True game_invite.save() game_invite.notify_invitee(request, game) messages.add_message( request, messages.SUCCESS, mark_safe("Your Game has been created Successfully.")) return HttpResponseRedirect( reverse('games:games_invite_players', args=(game.id, ))) else: print(form.errors) return None else: # Build a game form. form = GameForm(initial={"cell": cell}) context = { 'form': form, } return render(request, 'games/edit_game.html', context)