/
recents.go
65 lines (52 loc) · 1.42 KB
/
recents.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
package elemental
import (
"context"
"time"
"github.com/Nv7-Github/Nv7Haven/pb"
"google.golang.org/protobuf/types/known/emptypb"
)
const recentsLength = 30
func (e *Elemental) GetRecents() ([]*pb.RecentCombination, error) {
recents := make([]*pb.RecentCombination, 0, recentsLength)
res, err := e.db.Query("SELECT elem1, elem2, elem3 FROM recents ORDER BY createdon DESC LIMIT ?", recentsLength)
if err != nil {
return nil, err
}
defer res.Close()
for res.Next() {
item := &pb.RecentCombination{}
err = res.Scan(&item.Elem1, &item.Elem2, &item.Elem3)
if err != nil {
return nil, err
}
recents = append(recents, item)
}
return recents, nil
}
func (e *Elemental) NewRecent(recent *pb.RecentCombination) error {
_, err := e.db.Exec("INSERT INTO recents VALUES (?, ?, ?, ?)", recent.Elem1, recent.Elem2, recent.Elem3, time.Now().Unix())
if err != nil {
return err
}
e.recents.L.Lock()
e.recents.Broadcast()
e.recents.L.Unlock()
return nil
}
func (e *Elemental) GetRec(_ context.Context, _ *emptypb.Empty) (*pb.Recents, error) {
recents, err := e.GetRecents()
if err != nil {
return &pb.Recents{}, err
}
return &pb.Recents{Recents: recents}, nil
}
func (e *Elemental) WaitForNextRecent(_ *emptypb.Empty, stream pb.Elemental_WaitForNextRecentServer) error {
e.recents.L.Lock()
e.recents.Wait()
e.recents.L.Unlock()
err := stream.Send(&emptypb.Empty{})
if err != nil {
return err
}
return nil
}