# Path: src/ctf_gameserver/web/scoring/models.py
# class GameControl(models.Model):
#     """
#     Single-row database table to store control information for the competition.
#     """
# 
#     competition_name = models.CharField(max_length=100, default='My A/D CTF')
#     # Start and end times for the whole competition: Make them NULL-able (for the initial state), but not
#     # blank-able (have to be set upon editing); "services_public" is the point at which information about the
#     # services is public, but the actual game has not started yet
#     services_public = models.DateTimeField(null=True)
#     start = models.DateTimeField(null=True)
#     end = models.DateTimeField(null=True)
#     # Tick duration in seconds
#     tick_duration = models.PositiveSmallIntegerField(default=180)
#     # Number of ticks a flag is valid for including the one it was generated in
#     valid_ticks = models.PositiveSmallIntegerField(default=5)
#     current_tick = models.IntegerField(default=-1)
#     flag_prefix = models.CharField(max_length=20, default='FLAG_')
#     registration_open = models.BooleanField(default=False)
#     registration_confirm_text = models.TextField(blank=True)
#     min_net_number = models.PositiveIntegerField(null=True, blank=True)
#     max_net_number = models.PositiveIntegerField(null=True, blank=True)
# 
#     class Meta:
#         verbose_name_plural = 'Game control'
# 
#     @classmethod
#     def get_instance(cls):
#         try:
#             return cls.objects.get()
#         except cls.DoesNotExist:
#             game_control = GameControl()
#             game_control.save()
#             return game_control
# 
#     def clean(self):
#         """
#         Ensures that only one instance of the class gets created.
#         Inspired by https://stackoverflow.com/a/6436008.
#         """
#         cls = self.__class__
# 
#         if cls.objects.count() > 0 and self.id != cls.objects.get().id:
#             # pylint: disable=no-member
#             raise ValidationError(_('Only a single instance of {cls} can be created')
#                                   .format(cls=cls.__name__))
# 
#     def are_services_public(self):
#         """
#         Indicates whether information about the services is public yet.
#         """
#         if self.services_public is None:
#             return False
# 
#         return self.services_public <= timezone.now()
# 
#     def competition_started(self):
#         """
#         Indicates whether the competition has already begun (i.e. running or over).
#         """
#         if self.start is None or self.end is None:
#             return False
# 
#         return self.start <= timezone.now()
# 
#     def competition_over(self):
#         """
#         Indicates whether the competition is already over.
#         """
#         if self.start is None or self.end is None:
#             return False
# 
#         return self.end < timezone.now()


from functools import wraps
from django.shortcuts import redirect
from django.conf import settings
from django.http import JsonResponse
from django.utils.translation import ugettext as _
from django.contrib import messages
from .models import GameControl
def before_competition_required(view):
    """
    View decorator which prohibits access to the decorated view if the competition has already begun (i.e.
    running or over).
    """

    @wraps(view)
    def func(request, *args, **kwargs):
        if GameControl.get_instance().competition_started():
            messages.error(request, _('Sorry, that is only possible before the competition.'))
            return redirect(settings.HOME_URL)

        return view(request, *args, **kwargs)

    return func


def services_public_required(resp_format):
    """
    View decorator which prohibits access to the decorated view if information about the services is not
    public yet.

    Args:
        resp_format: Format of the response when the competition has not yet started. Supported options are
                     'html' and 'json'.
    """

    def decorator(view):
        @wraps(view)
        def func(request, *args, **kwargs):

# Path: src/ctf_gameserver/web/scoring/models.py
# class GameControl(models.Model):
#     """
#     Single-row database table to store control information for the competition.
#     """
# 
#     competition_name = models.CharField(max_length=100, default='My A/D CTF')
#     # Start and end times for the whole competition: Make them NULL-able (for the initial state), but not
#     # blank-able (have to be set upon editing); "services_public" is the point at which information about the
#     # services is public, but the actual game has not started yet
#     services_public = models.DateTimeField(null=True)
#     start = models.DateTimeField(null=True)
#     end = models.DateTimeField(null=True)
#     # Tick duration in seconds
#     tick_duration = models.PositiveSmallIntegerField(default=180)
#     # Number of ticks a flag is valid for including the one it was generated in
#     valid_ticks = models.PositiveSmallIntegerField(default=5)
#     current_tick = models.IntegerField(default=-1)
#     flag_prefix = models.CharField(max_length=20, default='FLAG_')
#     registration_open = models.BooleanField(default=False)
#     registration_confirm_text = models.TextField(blank=True)
#     min_net_number = models.PositiveIntegerField(null=True, blank=True)
#     max_net_number = models.PositiveIntegerField(null=True, blank=True)
# 
#     class Meta:
#         verbose_name_plural = 'Game control'
# 
#     @classmethod
#     def get_instance(cls):
#         try:
#             return cls.objects.get()
#         except cls.DoesNotExist:
#             game_control = GameControl()
#             game_control.save()
#             return game_control
# 
#     def clean(self):
#         """
#         Ensures that only one instance of the class gets created.
#         Inspired by https://stackoverflow.com/a/6436008.
#         """
#         cls = self.__class__
# 
#         if cls.objects.count() > 0 and self.id != cls.objects.get().id:
#             # pylint: disable=no-member
#             raise ValidationError(_('Only a single instance of {cls} can be created')
#                                   .format(cls=cls.__name__))
# 
#     def are_services_public(self):
#         """
#         Indicates whether information about the services is public yet.
#         """
#         if self.services_public is None:
#             return False
# 
#         return self.services_public <= timezone.now()
# 
#     def competition_started(self):
#         """
#         Indicates whether the competition has already begun (i.e. running or over).
#         """
#         if self.start is None or self.end is None:
#             return False
# 
#         return self.start <= timezone.now()
# 
#     def competition_over(self):
#         """
#         Indicates whether the competition is already over.
#         """
#         if self.start is None or self.end is None:
#             return False
# 
#         return self.end < timezone.now()


# Path: src/ctf_gameserver/web/scoring/decorators.py
from functools import wraps
from django.shortcuts import redirect
from django.conf import settings
from django.http import JsonResponse
from django.utils.translation import ugettext as _
from django.contrib import messages
from .models import GameControl

def before_competition_required(view):
    """
    View decorator which prohibits access to the decorated view if the competition has already begun (i.e.
    running or over).
    """

    @wraps(view)
    def func(request, *args, **kwargs):
        if GameControl.get_instance().competition_started():
            messages.error(request, _('Sorry, that is only possible before the competition.'))
            return redirect(settings.HOME_URL)

        return view(request, *args, **kwargs)

    return func


def services_public_required(resp_format):
    """
    View decorator which prohibits access to the decorated view if information about the services is not
    public yet.

    Args:
        resp_format: Format of the response when the competition has not yet started. Supported options are
                     'html' and 'json'.
    """

    def decorator(view):
        @wraps(view)
        def func(request, *args, **kwargs):

            game_control = GameControl.get_instance()