src/app/records.go (view raw)
1package app
2
3import (
4 "log"
5 "time"
6)
7
8func (e *Entry) GetOdds() *uint {
9 v := uint(100)
10 for _, s := range e.SubEntries {
11 v = v * s.Odds / 100 // for some reason, i can't use *= here
12 }
13 return &v
14}
15
16func (e *Entry) DidWin() *bool {
17 v := true
18 for _, s := range e.SubEntries {
19 if !s.Won {
20 v = false
21 return &v
22 }
23 }
24 return &v
25}
26
27func (e *Entry) GetDate() *time.Time {
28 if len(e.SubEntries) == 0 {
29 return nil
30 }
31
32 last := e.SubEntries[0].Date
33 for _, s := range e.SubEntries {
34 if s.Date.After(last) {
35 last = s.Date
36 }
37 }
38 return &last
39}
40
41func (e *Entry) GetValue() (value int) {
42 if e.Won == nil || e.Odds == nil {
43 log.Fatalf("please, update e.Won and e.Odds first")
44 }
45
46 if e.Bookmaker.IsExchange() {
47 r := (int(e.Amount) * (int(*e.Odds) - 100)) / 100
48 if *e.Won {
49 value = int(e.Amount) - int(e.Amount)*int(e.Commission)/10000
50 } else {
51 value = int(e.Refund) - r
52 }
53 } else {
54 if *e.Won {
55 value = (int(e.Amount) * int(*e.Odds) / 100) - int(e.Amount)
56 } else {
57 value = -int(e.Amount) + int(e.Refund)
58 }
59 }
60
61 value += int(e.Bonus)
62 return
63}
64
65func (r *Record) GetDate() *time.Time {
66 if len(r.Entries) == 0 {
67 return nil
68 }
69
70 last := *r.Entries[0].Date
71 for _, e := range r.Entries {
72 if e.Date.After(last) {
73 last = *e.Date
74 }
75 }
76 return &last
77}
78
79func FillEntryValues(entries []Entry) ([]Entry, int) {
80 var total int
81 for i := range entries {
82 entries[i].Odds = entries[i].GetOdds()
83 entries[i].Won = entries[i].DidWin()
84 entries[i].Date = entries[i].GetDate()
85 v := entries[i].GetValue()
86 entries[i].Value = &v
87 total += v
88 }
89 return entries, total
90}
91
92func FillRecordValues(records []Record) ([]Record, int) {
93 var total int
94 for i := range records {
95 _, v := FillEntryValues(records[i].Entries)
96 records[i].Date = records[i].GetDate()
97 records[i].Value = &v
98 total += v
99 }
100 return records, total
101}
102
103func GetRecords() (records []Record, total int, err error) {
104 err = DB.Preload("Entries.Bookmaker").Preload("Entries.Account").Preload("Entries.SubEntries").Find(&records).Error
105 if err != nil {
106 return
107 }
108
109 records, total = FillRecordValues(records)
110 return
111}
112
113func GetRecord(id uint) (record Record, err error) {
114 err = DB.Preload("Entries.SubEntries").First(&record, id).Error
115 if err != nil {
116 return
117 }
118
119 records, _ := FillRecordValues([]Record{record})
120 return records[0], nil
121}