coding utf-8 import logging import datetime from mongoengine import Do

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-
import logging
import datetime
from mongoengine import Document
from mongoengine.queryset import queryset_manager
from mongoengine.fields import (StringField, DateTimeField, BooleanField, IntField)
from utils.mongoengine_pagination import Pagination
from utils.secure_cookies import CryptoSessionCookies
from conf.config import SECRET_KEY
logger = logging.getLogger(__name__)
class Player(Document):
"""
MongoDB model to store users login information
player_id - unique ID for player generate_hash(name, device_id)
"""
meta = {
'collection': 'players',
'indexes': ['-created_at', 'device_id', 'ready_to_play'],
}
player_id = StringField()
name = StringField(max_length=30)
device_id = StringField(max_length=80)
active = BooleanField(default=True)
ready_to_play = BooleanField(default=False)
playing = BooleanField(default=False)
cookie = StringField()
wins = IntField(default=0)
loses = IntField(default=0)
runaways = IntField(default=0) # when player shotdown or close application during play
created_at = DateTimeField(default=datetime.datetime.now)
@queryset_manager
def pagination(doc_cls, queryset, page, per_page):
return Pagination(queryset, page, per_page)
@staticmethod
def generate_player_hash(name, device_id):
data = {
'name': name,
'device_id': device_id,
}
return CryptoSessionCookies(SECRET_KEY).crypt(data)
def check_player_hash(self, name, device_id):
data = {
'name': name,
'device_id': device_id,
}
data = CryptoSessionCookies(SECRET_KEY).decrypt(data)
if (data['name'] == self.name) and (data['device_id'] == self.device_id):
return True
else:
return False
def init_player(self, active=True, ready_to_play=False, **kwargs):
# save uniq player id
data = {
'name': self.name,
'device_id': self.device_id,
}
self.player_id = CryptoSessionCookies(SECRET_KEY).crypt(data)
self.active = active
self.ready_to_play = ready_to_play
def generate_access_token(self):
return CryptoSessionCookies(SECRET_KEY).crypt(self.device_id + self.name + SECRET_KEY)
@staticmethod
def authenticate(player_id, access_token):
player_doc = Player.objects(player_id=player_id).first()
if player_doc is not None:
if player_doc.generate_access_token() == access_token:
return player_doc
return None
@staticmethod
def add_player(name, device_id):
player_doc = Player.objects(name=name, device_id=device_id).first()
if player_doc is None:
player_doc = Player(name=name, device_id=device_id)
player_doc.init_player()
player_doc.save()
return player_doc
return None
def is_active(self):
return self.active
def is_ready(self):
return self.ready_to_play
def __unicode__(self):
return u'<Player: %s %s>' % (self.name, self.device_id)