Passed
Pull Request — main (#17)
by Yume
01:15
created

models.*Mem.ComputeEfactor   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
package models
2
3
import (
4
	"gorm.io/gorm"
5
)
6
7
// Mem structure
8
type Mem struct {
9
	gorm.Model
10
	UserID     uint `json:"user_id" example:"1"`
11
	User       User
12
	CardID     uint `json:"card_id" example:"1"`
13
	Card       Card
14
	Quality    uint    `json:"quality" example:"0"` // [0: Blackout - 1: Error with choices - 2: Error with hints - 3: Error - 4: Good with hints - 5: Perfect]
15
	Repetition uint    `json:"repetition" example:"0" `
16
	Efactor    float32 `json:"e_factor" example:"2.5"`
17
	Interval   uint    `json:"interval" example:"0"`
18
}
19
20
func (m *Mem) ComputeEfactor(oldEfactor float32, quality uint) {
21
	eFactor := oldEfactor + (0.1 - (5.0-float32(quality))*(0.08+(5-float32(quality)))*0.02)
22
23
	if eFactor < 1.3 {
24
		m.Efactor = 1.3
25
	} else {
26
		m.Efactor = eFactor
27
	}
28
}
29
30
func (m *Mem) ComputeTrainingEfactor(oldEfactor float32, quality uint) {
31
	eFactor := oldEfactor + (0.1 - (5.0-float32(quality))*(0.08+(5-float32(quality)))*0.02)
32
	computedEfactor := (oldEfactor + eFactor) / 2
33
	if computedEfactor < 1.3 {
34
		m.Efactor = 1.3
35
	} else {
36
		m.Efactor = computedEfactor
37
	}
38
}
39
40
func (m *Mem) GetMemType() CardType {
41
	if m.Efactor <= 2 || m.Repetition < 2 || (m.Efactor <= 2.3 && m.Repetition < 4) {
42
		return CardMCQ
43
	}
44
45
	return m.Card.Type
46
}
47
48
func (m *Mem) ComputeInterval(oldInterval uint, eFactor float32, repetition uint) {
49
	switch repetition {
50
	case 0:
51
		m.Interval = 1
52
	case 1, 2:
53
		m.Interval = 2
54
	case 3:
55
		m.Interval = 3
56
	default:
57
		m.Interval = uint(float32(oldInterval)*eFactor*0.75) + 1
58
	}
59
}
60