-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathta.go
104 lines (90 loc) · 1.88 KB
/
ta.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 main
import (
"github.com/albrow/zoom"
_ "gopkg.in/telebot.v3"
tele "gopkg.in/telebot.v3"
"math/rand"
)
type TA struct {
UserID string
Name string
Probability int
CurrentQAID string
AnsweredCount int
AssignedCount int
Questions []string
}
var (
TAs *zoom.Collection
)
func (ta *TA) ModelID() string {
return ta.UserID
}
func (ta *TA) SetModelID(modelID string) {
ta.UserID = modelID
}
func newTA(userid string) *TA {
return &TA{
UserID: userid,
Name: "",
Probability: 100,
CurrentQAID: "",
AnsweredCount: 0,
AssignedCount: 0,
Questions: []string{},
}
}
func CreateTAs() {
_TAs, err := pool.NewCollectionWithOptions(&TA{},
zoom.DefaultCollectionOptions.WithIndex(true))
if err != nil {
// handle error
panic(err)
}
TAs = _TAs
}
func DispatchTA() (*TA, int, error) {
count, err := TAs.Count()
if err != nil {
return nil, 0, err
}
index := rand.Intn(count)
var tas []*TA
query := TAs.NewQuery().Offset(uint(index)).Limit(1)
if err := query.Run(&tas); err != nil || len(tas) != 1 {
return nil, 0, err
}
selectedTA := tas[0]
for rand.Intn(100) > selectedTA.Probability {
index = rand.Intn(count)
query = TAs.NewQuery().Offset(uint(index)).Limit(1)
if err := query.Run(&tas); err != nil || len(tas) != 1 {
return nil, 0, err
}
selectedTA = tas[0]
}
return selectedTA, count, nil
}
func TryGetTA(c tele.Context, menu *tele.ReplyMarkup) (*TA, error) {
userid := HexID(c.Sender())
ta := newTA(userid)
if err := TAs.Find(userid, ta); err != nil {
return nil, c.Send("Error!", menu)
}
return ta, nil
}
func GetTA(id string) *TA {
ta := newTA(id)
if err := TAs.Find(id, ta); err != nil {
return nil
}
return ta
}
func IsTA(c tele.Context) bool {
userid := HexID(c.Sender())
ta := newTA(userid)
if err := TAs.Find(userid, ta); err != nil {
return false
}
return ta != nil
}