# Path: game/models.py
# class Game(models.Model):
# class GameSquare(models.Model):
# class GameLog(models.Model):
#     def __unicode__(self):
#     def get_available_games():
#     def created_count(user):
#     def get_games_for_player(user):
#     def get_by_id(id):
#     def create_new(user):
#     def add_log(self, text, user=None):
#     def get_all_game_squares(self):
#     def get_game_square(row, col):
#     def get_square_by_coords(self, coords):
#     def get_game_log(self):
#     def send_game_update(self):
#     def next_player_turn(self):
#     def mark_complete(self, winner):
#     def __unicode__(self):
#     def get_by_id(id):
#     def get_surrounding(self):
#     def claim(self, status_type, user):
#     def __unicode__(self):
#     STATUS_TYPES = (
#         ('Free', 'Free'),
#         ('Selected', 'Selected'),
#         ('Surrounding', 'Surrounding')
#     )


from django.contrib.auth.forms import PasswordResetForm
from django.shortcuts import redirect
from django.views.generic import CreateView, TemplateView, View, FormView
from django.contrib.auth import authenticate, login
from game.forms import *
from game.models import User, Game
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth import REDIRECT_FIELD_NAME, login as auth_login, logout as auth_logout
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.contrib import messages
from django.contrib.auth import get_user
from django.shortcuts import get_object_or_404
import json

        return context


class GameView(TemplateView):
    template_name = 'components/game/game.html'
    game = None

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        # get the game by the id
        self.game = Game.get_by_id(kwargs['game_id'])
        user = get_user(request)
        # check to see if the game is open and available for this user
        # if this player is the creator, just return
        if self.game.creator == user or self.game.opponent == user:
            return super(GameView, self).dispatch(request, *args, **kwargs)

        # if there is no opponent and the game is not yet completed,
        # set the opponent as this user
        if not self.game.opponent and not self.game.completed:
            self.game.opponent = user
            self.game.save()
            return super(GameView, self).dispatch(request, *args, **kwargs)
        else:
            messages.add_message(request, messages.ERROR, 'Sorry, the selected game is not available.')
            return redirect('/lobby/')

    def get_context_data(self, **kwargs):
        context = super(GameView, self).get_context_data(**kwargs)

# Path: game/models.py
# class Game(models.Model):
# class GameSquare(models.Model):
# class GameLog(models.Model):
#     def __unicode__(self):
#     def get_available_games():
#     def created_count(user):
#     def get_games_for_player(user):
#     def get_by_id(id):
#     def create_new(user):
#     def add_log(self, text, user=None):
#     def get_all_game_squares(self):
#     def get_game_square(row, col):
#     def get_square_by_coords(self, coords):
#     def get_game_log(self):
#     def send_game_update(self):
#     def next_player_turn(self):
#     def mark_complete(self, winner):
#     def __unicode__(self):
#     def get_by_id(id):
#     def get_surrounding(self):
#     def claim(self, status_type, user):
#     def __unicode__(self):
#     STATUS_TYPES = (
#         ('Free', 'Free'),
#         ('Selected', 'Selected'),
#         ('Surrounding', 'Surrounding')
#     )


# Path: game/views/views.py
from django.contrib.auth.forms import PasswordResetForm
from django.shortcuts import redirect
from django.views.generic import CreateView, TemplateView, View, FormView
from django.contrib.auth import authenticate, login
from game.forms import *
from game.models import User, Game
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth import REDIRECT_FIELD_NAME, login as auth_login, logout as auth_logout
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.contrib import messages
from django.contrib.auth import get_user
from django.shortcuts import get_object_or_404
import json


        return context


class GameView(TemplateView):
    template_name = 'components/game/game.html'
    game = None

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        # get the game by the id
        self.game = Game.get_by_id(kwargs['game_id'])
        user = get_user(request)
        # check to see if the game is open and available for this user
        # if this player is the creator, just return
        if self.game.creator == user or self.game.opponent == user:
            return super(GameView, self).dispatch(request, *args, **kwargs)

        # if there is no opponent and the game is not yet completed,
        # set the opponent as this user
        if not self.game.opponent and not self.game.completed:
            self.game.opponent = user
            self.game.save()
            return super(GameView, self).dispatch(request, *args, **kwargs)
        else:
            messages.add_message(request, messages.ERROR, 'Sorry, the selected game is not available.')
            return redirect('/lobby/')

    def get_context_data(self, **kwargs):
        context = super(GameView, self).get_context_data(**kwargs)

        context['game'] = self.game