Class: KeepYourHead::FlashcardTraining

Inherits:
Object
  • Object
show all
Defined in:
lib/Keepyourhead/Training.rb

Constant Summary collapse

StufenSkipTime =

in hours

[ 
	1, 3, 6, 
	1*24, 2*24, 4*24, 7*24, 14*24,
	1*30*24, 2*30*24 
].map{ |h| h * 60 * 60 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, questionsMax, timeMax) ⇒ FlashcardTraining

timemax in seconds



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
# File 'lib/Keepyourhead/Training.rb', line 42

def initialize( database, questionsMax, timeMax )
	@stacksize = 7

	@questionsMax, @timeMax = questionsMax, timeMax

	@flashcardsAsked = Set.new
	@flashcardsKnown = Set.new
	@flashcardsUnknown = Set.new

	@timeStarted = Time.now

	flashcardsAll = database.collectFlashcards.select { |fc| fc.active? }
	@flashcards = []
	questionsMax.times {
		if flashcardsAll.length > 0 then
			idx = rand( flashcardsAll.length )
			@flashcards << flashcardsAll.delete_at(idx)
		end
	}

	@countStart = @flashcards.length

	@flashcard = nil

	@database = database

	nextFlashcard
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



23
24
25
# File 'lib/Keepyourhead/Training.rb', line 23

def database
  @database
end

#flashcardObject (readonly)

Returns the value of attribute flashcard.



21
22
23
# File 'lib/Keepyourhead/Training.rb', line 21

def flashcard
  @flashcard
end

#flashcardsObject (readonly)

Returns the value of attribute flashcards.



22
23
24
# File 'lib/Keepyourhead/Training.rb', line 22

def flashcards
  @flashcards
end

#flashcardsAskedObject (readonly)

Returns the value of attribute flashcardsAsked.



29
30
31
# File 'lib/Keepyourhead/Training.rb', line 29

def flashcardsAsked
  @flashcardsAsked
end

#flashcardsKnownObject (readonly)

Returns the value of attribute flashcardsKnown.



29
30
31
# File 'lib/Keepyourhead/Training.rb', line 29

def flashcardsKnown
  @flashcardsKnown
end

#flashcardsUnknownObject (readonly)

Returns the value of attribute flashcardsUnknown.



29
30
31
# File 'lib/Keepyourhead/Training.rb', line 29

def flashcardsUnknown
  @flashcardsUnknown
end

#questionsMaxObject (readonly)

Returns the value of attribute questionsMax.



28
29
30
# File 'lib/Keepyourhead/Training.rb', line 28

def questionsMax
  @questionsMax
end

#timeMaxObject (readonly)

Returns the value of attribute timeMax.



26
27
28
# File 'lib/Keepyourhead/Training.rb', line 26

def timeMax
  @timeMax
end

#timeStartedObject (readonly)

Returns the value of attribute timeStarted.



25
26
27
# File 'lib/Keepyourhead/Training.rb', line 25

def timeStarted
  @timeStarted
end

Instance Method Details

#closeObject



124
125
126
# File 'lib/Keepyourhead/Training.rb', line 124

def close
	database.saveAllChanged
end

#nextFlashcardObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/Keepyourhead/Training.rb', line 111

def nextFlashcard
#		if @flashcards.length > 0 and progressTime < 1.0 then
	if progressFlashcards < 1.0 and progressTime < 1.0 then
		assert @flashcards.length > 0

		idx = rand( [@flashcards.length, @stacksize].min )
		@flashcard = @flashcards.delete_at( @flashcards.length - 1 - idx )
		@flashcards.push @flashcard
	else
		@flashcard = nil
	end
end

#onKnownObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/Keepyourhead/Training.rb', line 72

def onKnown
	@flashcardsKnown << @flashcard
	@flashcardsAsked << @flashcard

	@flashcard.nextCheck = Time.now + StufenSkipTime[[@flashcard.level, StufenSkipTime.length-1].min]
	@flashcard.level = @flashcard.level + 1

	@flashcards.pop

	nextFlashcard
end

#onUnknownObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/Keepyourhead/Training.rb', line 84

def onUnknown
	@flashcardsUnknown << @flashcard
	@flashcardsAsked << @flashcard


	@flashcard.nextCheck = Time.now - 60
	@flashcard.level = 0
	
	nextFlashcard
end

#progressFlashcardsObject



95
96
97
98
99
100
101
# File 'lib/Keepyourhead/Training.rb', line 95

def progressFlashcards
	if @countStart > 0 then
		(@countStart - @flashcards.length) / @countStart
	else
		1.0
	end
end

#progressTimeObject



107
108
109
# File 'lib/Keepyourhead/Training.rb', line 107

def progressTime
	(Time.now - @timeStarted) / @timeMax
end

#timeUsedObject



103
104
105
# File 'lib/Keepyourhead/Training.rb', line 103

def timeUsed
	Time.now - @timeStarted
end