/
dbfunc.py
113 lines (96 loc) · 3.65 KB
/
dbfunc.py
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
111
112
113
'''
Created on Dec 3, 2012
@author: mark
'''
import logging
import random
from secrets import makeHash, testHash, makeCookieHash
from dbdef import User, Question, db
from cache_util import mems, memg
def getGenreList():
genreList = ['general','math','geography', 'trivia']
return genreList
def getQuestionKeys(genre, update=False):
'''memset all the KEYS under the GENRE return the keys'''
questions = memg(genre)
if (not questions) or update:
questions = Question.all(keys_only=True).filter("genre =", genre)
questions = [x for x in questions]
mems(genre, questions)
return questions
def createQuestion(q, a, c1, c2, c3, genre):
'''db and memset the question with the KEY'''
quest = Question(question=q, rating=1200.0, votes=0, answer=a, choice1=c1,choice2=c2,choice3=c3, genre=genre)
quest.put()
mems( str(quest.key()), quest )
logging.info("question: " + q + " created.")
def getQuestion(genre):
'''to get a question by genre'''
if not genre:
genre = "general"
questions = getQuestionKeys(genre)
if not questions:
logging.error("dbfunc.py line 39: couldn't return a question for: " + genre)
questions = getQuestionKeys("general")
ranChoice = random.choice(questions)
#not in cache get from db set cache
q = memg( str(ranChoice) )
if not q:
q = db.get(ranChoice)
mems(str(ranChoice), q)
q = q.asDict()
options = [q['answer'], q['choice1'], q['choice2'], q['choice3']]
random.shuffle(options)
q['options'] = options
q['hashed'] = makeCookieHash(q['answer'])
return q
def voteQuestion(up, key):#up=1 down=0 that's "up?"
question = Question.get(key)
if up:
question.votes += 1
else:
question.votes -= 1
if question.votes < -10:
db.delete(key)
getQuestionKeys(update=True)
else:
question.put()
###user functions
def createUser(username, password, email=None):
password = makeHash(password)
exists = User.all().filter("username =", username).get()
if exists:
return "User " + username + " taken."
if email:
user = User(username=username, password=password, email=email, rating=1200.0)
else:
user = User(username=username, password=password, rating=1200.0)
user.put()
logging.info("User: " + username + " created!")
def checkCred(username, password):
user = User.all().filter("username =", username).get()
if user:
passMatch = testHash(user.password, password)
if passMatch:
return True, user.username, user.rating
return False, None, None
#updateRaing and subfunctions
#These give an ELO rating
def getExpectation(rating_1, rating_2):
'''generates the expected for rating_1 for modifyRating function'''
return (1.0 / (1.0 + pow(10, ((rating_2 - rating_1) / 400))))
def modifyRating(rating, expected, actual, kfactor=20):#actual -> loss=0 win=1
'''wrapped by getNewRatings'''
return (rating + kfactor * (actual - expected))
def getNewRatings(rating_1, rating_2, result): #result for player1
'''return new elo ratings for player1 and player2'''
player1 = modifyRating(rating_1, getExpectation(rating_1, rating_2), result)
player2 = modifyRating(rating_2, getExpectation(rating_2, rating_1), 1 - result)
return player1, player2
def updateRating(username, userRating, qK, qRating, result):
user = User.all().filter("username =", username).get()
question = Question.get(qK)
user.rating, question.rating = getNewRatings(userRating, qRating, result)
user.put()
question.put()
return str(user.rating)