mirror of
https://github.com/Findus23/se-simulator.git
synced 2024-09-11 06:33:48 +02:00
way faster random quiz by preshuffling questions
This commit is contained in:
parent
3660aa18cd
commit
e1fb10d9c2
7 changed files with 49 additions and 9 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -10,3 +10,4 @@ config.py
|
|||
|
||||
web/static/css/
|
||||
!web/static/css/.gitkeep
|
||||
count.txt
|
||||
|
|
|
@ -42,7 +42,7 @@ class Question(BaseModel):
|
|||
user = ForeignKeyField(User)
|
||||
site = ForeignKeyField(Site)
|
||||
datetime = DateTimeField()
|
||||
random = IntegerField()
|
||||
random = IntegerField(null=True)
|
||||
|
||||
|
||||
class Answer(BaseModel):
|
||||
|
|
21
server.py
21
server.py
|
@ -1,6 +1,6 @@
|
|||
import subprocess
|
||||
import time
|
||||
from random import shuffle
|
||||
from random import shuffle, randint
|
||||
|
||||
import sass
|
||||
from flask import render_template, send_from_directory, abort, session, jsonify, make_response, redirect, url_for
|
||||
|
@ -33,6 +33,9 @@ limiter = Limiter(
|
|||
headers_enabled=True
|
||||
)
|
||||
|
||||
question_count = utils.load_question_count()
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def git_hash():
|
||||
return dict(git_hash=subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode().strip())
|
||||
|
@ -93,10 +96,18 @@ def quiz(difficulty):
|
|||
if difficulty not in ["easy", "hard"]:
|
||||
return abort(404)
|
||||
time1 = time.time()
|
||||
question = Question.select(Question, Title, User, Site) \
|
||||
.join(Title).switch(Question) \
|
||||
.join(User).switch(Question) \
|
||||
.join(Site).where((Question.upvotes - Question.downvotes >= 0)).order_by(SQL("RAND()")).limit(1).get()
|
||||
while True:
|
||||
random = randint(0, question_count - 1)
|
||||
print(random)
|
||||
try:
|
||||
question = Question.select(Question, Title, User, Site) \
|
||||
.join(Title).switch(Question) \
|
||||
.join(User).switch(Question) \
|
||||
.join(Site).where((Question.upvotes - Question.downvotes >= 0) & (Question.random == random)).get()
|
||||
except DoesNotExist:
|
||||
continue
|
||||
break
|
||||
|
||||
if difficulty == "easy":
|
||||
sites = [question.site]
|
||||
query = Site.select().where((Site.last_download.is_null(False)) & (Site.id != question.site.id)) \
|
||||
|
|
15
shuffle.py
Normal file
15
shuffle.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
import utils
|
||||
from models import *
|
||||
from extra_data import site_colors
|
||||
|
||||
query = Question.select().order_by(SQL("RAND()"))
|
||||
|
||||
count = query.count()
|
||||
utils.save_question_count(count)
|
||||
i = 0
|
||||
for question in query:
|
||||
question.random = i
|
||||
i += 1
|
||||
if i % 50 == 0:
|
||||
print("{}/{}".format(i, count))
|
||||
question.save()
|
3
todb.py
3
todb.py
|
@ -57,8 +57,7 @@ def add_question(site, count=100):
|
|||
title = titles[i]
|
||||
user = users[i]
|
||||
time = datetime.now()
|
||||
question = Question.create(text=text, title_id=title, user_id=user, site_id=site, datetime=time,
|
||||
random=utils.rand())
|
||||
question = Question.create(text=text, title_id=title, user_id=user, site_id=site, datetime=time)
|
||||
num_answers = random.randint(1, 4)
|
||||
answers = Answer.select().where((Answer.site == site) & (Answer.question.is_null())).limit(num_answers)
|
||||
for answer in answers:
|
||||
|
|
11
utils.py
11
utils.py
|
@ -130,5 +130,14 @@ def is_light_color(hex):
|
|||
""" https://stackoverflow.com/a/596241 """
|
||||
r, g, b = hex_to_rgb(hex[1:])
|
||||
brightness = 0.2126 * r + 0.7152 * g + 0.0722 * b
|
||||
print(brightness)
|
||||
return brightness > 245
|
||||
|
||||
|
||||
def save_question_count(count):
|
||||
with open('count.txt', 'w') as f:
|
||||
f.write(str(count))
|
||||
|
||||
|
||||
def load_question_count():
|
||||
with open('count.txt', 'r') as f:
|
||||
return int(f.readline())
|
||||
|
|
|
@ -20,6 +20,11 @@ body {
|
|||
font-size: 15px;
|
||||
color: #111;
|
||||
}
|
||||
@media (max-width: 40rem){
|
||||
.container {
|
||||
margin: 0 10px;
|
||||
}
|
||||
}
|
||||
|
||||
pre > code {
|
||||
white-space: pre-wrap;
|
||||
|
|
Loading…
Reference in a new issue