-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodds.go
104 lines (89 loc) · 2.02 KB
/
odds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package odds
import (
"errors"
)
const (
Unknown = -1
Decimal = 0 // check
Fractional = 1
Moneyline = 2 // check
Implied = 3 // check
Malay = 4
Indonesian = 5
HongKong = 6 // check
// Rounding float types
Round = 0
Floor = 1
Ceil = 2
)
type Odd struct{
Type int
DecimalValue float64
FloatPrecision uint8
Value interface{}
}
func NewOdd(oType int, oVal interface{}) (Odd, error){
odd := Odd{Type: oType, Value: oVal, FloatPrecision: 2}
switch oVal.(type) {
case float64:
if oType == Decimal{
odd.DecimalValue = oVal.(float64)
}else if oType == Implied{
odd.DecimalValue = 100.0 / oVal.(float64)
}else{
return Odd{}, errors.New("float64 only accepts 'Decimal' and 'Implied' as odd format.")
}
default:
return Odd{}, errors.New("Odd value data type is not correct.")
}
return odd, nil
}
func (odd *Odd) Precision(newPrecision uint8) Odd{
odd.FloatPrecision = newPrecision
return *odd
}
func (odd *Odd) Valid() bool{
if (odd.Type == Decimal){
return true
}
return false
}
// Returns the Moneyline value as int16 from Odd{}.
func (odd *Odd) Moneyline() int16{
if odd.DecimalValue >= 2.0{
return int16((odd.DecimalValue - 100) * 100)
}else{
return int16((-100) / (odd.DecimalValue - 1))
}
}
// Returns the Implied Probability as float64 from Odd{}.
func (odd *Odd) Probability() float64{
if odd.DecimalValue <= 0{
return 0
}
return (1 / odd.DecimalValue) * 100
}
// Returns Indonesian odds as float64 from Odd{}.
func (odd *Odd) Indonesian() float64{
if odd.DecimalValue >= 2.0{
return (odd.DecimalValue - 1)
}else{
return (-1) / (odd.DecimalValue - 1)
}
}
// Returns Malay odds as float64 from Odd{}.
func (odd *Odd) Malay() float64{
if odd.DecimalValue >= 2.0{
return (-1) / (odd.DecimalValue - 1)
}else{
return (odd.DecimalValue - 1)
}
}
// Returns the decimal value as float64 from Odd{}.
func (odd *Odd) Decimal() float64{
return odd.DecimalValue
}
// Returns HongKong odds as float64 from Odd{}.
func (odd *Odd) HongKong() float64{
return (odd.DecimalValue - 1)
}