generalize order/random

This commit is contained in:
Damien Elmes 2011-04-21 09:15:08 +09:00
parent 974324e3dd
commit b69031cd4f
2 changed files with 37 additions and 24 deletions

View file

@ -749,18 +749,19 @@ queue = 2 %s and due <= :lim order by %s limit %d""" % (
# Resetting # Resetting
########################################################################## ##########################################################################
# fixme: order
def forgetCards(self, ids): def forgetCards(self, ids):
"Put cards back in the new queue." "Put cards back in the new queue."
sql = """ sql = """
update cards set mod=%d, due=fid, type=0, queue=0, ivl=0, data=''""" % intTime() update cards set type=0, queue=0, ivl=0, data=''"""
sids = ids2str(ids) sids = ids2str(ids)
sql += " where id in "+sids sql += " where id in "+sids
self.deck.db.execute(sql) self.deck.db.execute(sql)
if self.deck.randomNew(): if self.deck.randomNew():
# we need to re-randomize now # we need to re-randomize now
self.randomizeNewCards(ids) self.randomizeNewCards(ids)
else:
# order by fact id and shift to end of queue, and set mod
pass
def rescheduleCards(self, ids, min, max): def rescheduleCards(self, ids, min, max):
"Reset cards and schedule with new interval in days (min, max)." "Reset cards and schedule with new interval in days (min, max)."
@ -789,25 +790,28 @@ where id = :id""", vals)
# Random<->ordered new cards # Random<->ordered new cards
########################################################################## ##########################################################################
def randomizeCards(self, cardIds=None): def sortCards(self, cids, start=1, step=1, shuffle=False):
scids = ids2str(cids)
now = intTime() now = intTime()
query = "select distinct fid from cards where type = 0" fids = self.deck.db.list(
if cardIds: ("select distinct fid from cards where type = 0 and id in %s "
query += " and id in %s" % ids2str(cardIds) "order by fid") % scids)
fids = self.deck.db.list(query) # determine fid ordering
data = [{'fid': fid, due = {}
'rand': random.randrange(1, 1000000), if shuffle:
'now': now} for fid in fids] random.shuffle(fids)
self.deck.db.executemany(""" for c, fid in enumerate(fids):
update cards due[fid] = start+c*step
set due = :rand + ord, # reorder cards
mod = :now d = []
where fid = :fid for id, fid in self.deck.db.execute(
and type = 0""", data) "select id, fid from cards where type = 0 and id in "+scids):
d.append(dict(now=now, due=due[fid], cid=id))
self.deck.db.executemany(
"update cards set due = :due, mod = :now where id = :cid""", d)
def randomizeCards(self):
self.sortCards(self.deck.db.list("select id from cards"), shuffle=True)
def orderCards(self): def orderCards(self):
self.deck.db.execute(""" self.sortCards(self.deck.db.list("select id from cards"))
update cards set
due = fid,
mod = :now
where type = 0""", now=time.time())

View file

@ -693,9 +693,18 @@ def test_reorder():
f = d.newFact() f = d.newFact()
f['Front'] = u"one" f['Front'] = u"one"
d.addFact(f) d.addFact(f)
f = d.newFact()
f['Front'] = u"two"
d.addFact(f)
assert f.cards()[0].due == f.id assert f.cards()[0].due == f.id
d.sched.randomizeCards() found=False
assert f.cards()[0].due != f.id # 50/50 chance of being reordered
for i in range(20):
d.sched.randomizeCards()
if f.cards()[0].due != f.id:
found=True
break
assert found
d.sched.orderCards() d.sched.orderCards()
assert f.cards()[0].due == f.id assert f.cards()[0].due == f.id