Beispiel #1
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)