예제 #1
0
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)
예제 #2
0
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)
예제 #3
0
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)