1
|
|
|
package core |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"memnixrest/app/database" |
5
|
|
|
"memnixrest/app/models" |
6
|
|
|
"time" |
7
|
|
|
|
8
|
|
|
"github.com/gofiber/fiber/v2" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
// ComputeInterval function |
12
|
|
|
func ComputeInterval(r *models.Mem) uint { |
13
|
|
|
switch r.Repetition { |
14
|
|
|
case 0: |
15
|
|
|
return 1 |
16
|
|
|
case 1, 2: |
17
|
|
|
return 2 |
18
|
|
|
case 3: |
19
|
|
|
return 3 |
20
|
|
|
default: |
21
|
|
|
return uint(float32(r.Interval)*r.Efactor*0.75) + 1 |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
func ComputeQualitySuccess(memType int, repetition uint) uint { |
26
|
|
|
|
27
|
|
|
if memType == 2 { |
28
|
|
|
return 3 |
29
|
|
|
} else { |
30
|
|
|
if repetition > 3 { |
31
|
|
|
return 5 |
32
|
|
|
} |
33
|
|
|
return 4 |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
func ComputeQualityFailed(memType int, repetition uint) uint { |
38
|
|
|
if memType == 2 { |
39
|
|
|
if repetition <= 3 { |
40
|
|
|
return 0 |
41
|
|
|
} |
42
|
|
|
return 1 |
43
|
|
|
} |
44
|
|
|
if repetition <= 4 { |
45
|
|
|
return 1 |
46
|
|
|
} |
47
|
|
|
return 2 |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
func ComputeEfactor(r *models.Mem) float32 { |
52
|
|
|
eFactor := r.Efactor + (0.1 - (5.0-float32(r.Quality))*(0.08+(5-float32(r.Quality)))*0.02) |
53
|
|
|
|
54
|
|
|
if eFactor < 1.3 { |
55
|
|
|
return 1.3 |
56
|
|
|
} |
57
|
|
|
return eFactor |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
func UpdateMemDate(mem *models.Mem) { |
61
|
|
|
db := database.DBConn |
62
|
|
|
|
63
|
|
|
memDate := new(models.MemDate) |
64
|
|
|
|
65
|
|
|
_ = db.Joins("Card").Joins("User").Joins("Deck").Where("mem_dates.user_id = ? AND mem_dates.card_id = ?", |
66
|
|
|
mem.UserID, mem.CardID).First(&memDate).Error |
67
|
|
|
|
68
|
|
|
memDate.NextDate = time.Now().AddDate(0, 0, int(mem.Interval)) |
69
|
|
|
|
70
|
|
|
db.Save(memDate) |
71
|
|
|
|
72
|
|
|
//TODO: Return error |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// UpdateMem function |
77
|
|
|
func UpdateMem(_ *fiber.Ctx, r *models.Mem, validation models.CardResponseValidation) { |
78
|
|
|
//TODO: Rewrite functions |
79
|
|
|
|
80
|
|
|
db := database.DBConn |
81
|
|
|
|
82
|
|
|
mem := new(models.Mem) |
83
|
|
|
|
84
|
|
|
mem.UserID, mem.CardID = r.UserID, r.CardID |
85
|
|
|
|
86
|
|
|
var memType int |
87
|
|
|
|
88
|
|
|
if r.Efactor <= 2 || r.Repetition < 2 || (r.Efactor <= 2.3 && r.Repetition < 4) { |
89
|
|
|
memType = 2 |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if validation.Validate { |
93
|
|
|
mem.Interval = ComputeInterval(r) |
94
|
|
|
mem.Repetition = r.Repetition + 1 |
95
|
|
|
r.Quality = ComputeQualitySuccess(memType, r.Repetition) |
96
|
|
|
|
97
|
|
|
} else { |
98
|
|
|
mem.Repetition = 0 |
99
|
|
|
mem.Interval = 0 |
100
|
|
|
r.Quality = ComputeQualityFailed(memType, r.Repetition) |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
mem.Efactor = ComputeEfactor(r) |
104
|
|
|
|
105
|
|
|
db.Save(r) |
106
|
|
|
db.Create(mem) |
107
|
|
|
|
108
|
|
|
UpdateMemDate(mem) |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|