# Path: activity/models.py
# class Event(models.Model):
#     timestamp = models.DateTimeField(auto_now=True)
#     type = models.TextField(choices=(
#         ("chat", "Chat"), ("rpg", "RPG"), ("other", "Other"))
#     )
#     message = models.TextField()
#     user = models.ForeignKey(get_user_model())
# 
# Path: rpg/models.py
# class Title(models.Model):
#     name = models.CharField(max_length=40, unique=True, db_index=True)
# 
#     def __unicode__(self):
#         return self.name
# 
# Path: emergent/websockets.py
# class PusherWrapper(object):
#     def __init__(self):
#     def send(self, channel, event, msg):


from django.views.generic import View
from braces.views import AjaxResponseMixin, JSONResponseMixin
from django.contrib.auth import get_user_model
from activity.models import Event
from rpg.models import Title
from emergent.websockets import Pusher
from datetime import datetime
import random
# Create your views here.from datetime import datetime


class AddCoin(JSONResponseMixin, AjaxResponseMixin, View):
    def get_ajax(self, request, *args, **kwargs):
        amount = random.randint(1, 100)
        self.request.user.credits += amount
        self.request.user.save()
        json_dict = {
            'credits': self.request.user.credits,
        }
        msg = {
            "type": "rpg",
            "user": self.request.user.username,
            "message": "<strong><a href='/users/%s'>%s</a></strong> get %s coins for fun" % (
                self.request.user.username,
                self.request.user.username,
                amount
            )
        }
        Event(timestamp=datetime.now(), user=self.request.user, type=msg['type'], message=msg['message']).save()

# Path: activity/models.py
# class Event(models.Model):
#     timestamp = models.DateTimeField(auto_now=True)
#     type = models.TextField(choices=(
#         ("chat", "Chat"), ("rpg", "RPG"), ("other", "Other"))
#     )
#     message = models.TextField()
#     user = models.ForeignKey(get_user_model())
# 
# Path: rpg/models.py
# class Title(models.Model):
#     name = models.CharField(max_length=40, unique=True, db_index=True)
# 
#     def __unicode__(self):
#         return self.name
# 
# Path: emergent/websockets.py
# class PusherWrapper(object):
#     def __init__(self):
#     def send(self, channel, event, msg):


# Path: rpg/views.py
from django.views.generic import View
from braces.views import AjaxResponseMixin, JSONResponseMixin
from django.contrib.auth import get_user_model
from activity.models import Event
from rpg.models import Title
from emergent.websockets import Pusher
from datetime import datetime
import random

# Create your views here.from datetime import datetime


class AddCoin(JSONResponseMixin, AjaxResponseMixin, View):
    def get_ajax(self, request, *args, **kwargs):
        amount = random.randint(1, 100)
        self.request.user.credits += amount
        self.request.user.save()
        json_dict = {
            'credits': self.request.user.credits,
        }
        msg = {
            "type": "rpg",
            "user": self.request.user.username,
            "message": "<strong><a href='/users/%s'>%s</a></strong> get %s coins for fun" % (
                self.request.user.username,
                self.request.user.username,
                amount
            )
        }
        Event(timestamp=datetime.now(), user=self.request.user, type=msg['type'], message=msg['message']).save()

        Pusher.send("activity", "my_event", msg)