2022-06-04 21:57:04 +02:00
|
|
|
from config import CONFIG
|
2022-05-22 16:53:20 +02:00
|
|
|
from flask import (
|
|
|
|
Blueprint, flash, g, redirect, render_template, request, url_for, session
|
|
|
|
)
|
|
|
|
from auth import login_required
|
|
|
|
from create_db import Card, User
|
|
|
|
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
2022-06-04 21:57:04 +02:00
|
|
|
from numpy.random import choice
|
|
|
|
|
2022-05-22 16:53:20 +02:00
|
|
|
bp = Blueprint('deck', __name__)
|
|
|
|
|
|
|
|
engine = create_engine(CONFIG['DB_CONNECTION'])
|
|
|
|
dbsessionmaker = sessionmaker(bind=engine)
|
|
|
|
dbsession = dbsessionmaker()
|
|
|
|
|
|
|
|
@bp.route('/deck', methods=['GET'])
|
2022-06-04 21:57:04 +02:00
|
|
|
def get_collection(user_id):
|
|
|
|
c = dbsession.query(Card).filter(Card.owner_id == user_id).all()
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
|
#2 Probabilistic
|
|
|
|
## problem je, da pri probabilističnem težko nardiš, da pač en dan ni nič v decku, ker pač vedno izbere glede določeno število pač glede na uteži
|
|
|
|
#
|
|
|
|
# to bi bilo dobro narediti direkt iz baze, ne pa da cel collection spremenimo v ta list ob card.objects TODO
|
|
|
|
def generate_weights(user_clltn):
|
|
|
|
"""generiramo uteži za due in new cards posebej; vrne tuple dveh listov za weightse..."""
|
|
|
|
|
|
|
|
weights_due = []
|
|
|
|
weights_new = []
|
|
|
|
for card in user_clltn:
|
|
|
|
due = card.interest_rate
|
|
|
|
|
|
|
|
if due > 0:
|
|
|
|
weights_due.append(due)
|
|
|
|
weights_new.append(0)
|
|
|
|
else:
|
|
|
|
weights_due.append(0)
|
|
|
|
weights_new.append(1)
|
|
|
|
#če se odločimo, da izločimo iteme jih bo tu ignoriralo, če bo ir 0.
|
|
|
|
|
|
|
|
return weights_new, weights_due
|
|
|
|
|
|
|
|
|
|
|
|
def probabilistic_deck_generator(user_id, max_new, max_due):
|
|
|
|
c = get_collection(user_id)
|
|
|
|
deck, dck_due, dck_new, due_cards = [], [], [], 0
|
|
|
|
w_new, w_due = generate_weights(c)
|
|
|
|
|
|
|
|
#treba je sestet non zero weights
|
|
|
|
new_cards = sum(w_new)
|
|
|
|
for w in w_due:
|
|
|
|
if w > 0:
|
|
|
|
due_cards += 1
|
|
|
|
|
|
|
|
#preverimo, če imamo dovolj kartic
|
|
|
|
if new_cards<max_new:
|
|
|
|
max_new = new_cards
|
|
|
|
if due_cards<max_due:
|
|
|
|
max_due = due_cards
|
2022-05-22 16:53:20 +02:00
|
|
|
|
2022-06-04 21:57:04 +02:00
|
|
|
sum_w_new = sum(w_new)
|
|
|
|
sum_w_due = sum(w_due)
|
2022-06-02 22:36:08 +02:00
|
|
|
|
2022-06-04 21:57:04 +02:00
|
|
|
if due_cards:
|
|
|
|
dck_due = list(choice(c, size=max_due, replace=False, p=list(map(lambda x: x/sum_w_due, w_due)))) #garancija, da ni ponovitev
|
|
|
|
if new_cards:
|
|
|
|
dck_new = list(choice(c, size=max_new, replace=False, p=list(map(lambda x: x/sum_w_new, w_new))))
|
2022-06-02 22:36:08 +02:00
|
|
|
|
2022-06-04 21:57:04 +02:00
|
|
|
deck = dck_due + dck_new
|
2022-06-02 22:36:08 +02:00
|
|
|
|
2022-06-04 21:57:04 +02:00
|
|
|
return deck
|
2022-06-02 22:36:08 +02:00
|
|
|
|
2022-06-05 18:56:40 +02:00
|
|
|
def get_deck(deck):
|
|
|
|
"""naredimo string iz cardov"""
|
|
|
|
cards_by_id = ""
|
|
|
|
for card in deck:
|
|
|
|
cards_by_id += str(card.id) + ","
|
|
|
|
|
|
|
|
cards_by_id = cards_by_id[:-1]
|
|
|
|
|
|
|
|
return cards_by_id
|
|
|
|
|
|
|
|
|
2022-06-04 21:57:04 +02:00
|
|
|
#################################################
|
|
|
|
##TODO
|
|
|
|
# config za max_new in max_due
|
|
|
|
#################################################
|