This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
172 lines (163 loc) · 3.99 KB
/
player.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"github.com/CuteReimu/bingo-server/myws"
"github.com/dgraph-io/badger/v3"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"time"
)
var robotPlayer = Player{Token: "训练用毛玉", Name: "训练用毛玉"}
func (s *bingoServer) buildPlayerInfo(token string) (*myws.Message, []string, error) {
var message = &myws.Message{}
var tokens []string
err := db.View(func(txn *badger.Txn) error {
player, err := GetPlayer(txn, token)
if err != nil {
return err
}
message.Trigger = player.Name
if len(player.RoomId) == 0 {
return nil
}
room, err := GetRoom(txn, player.RoomId)
if IsErrKeyNotFound(err) {
return nil
} else if err != nil {
return err
}
message.Data, tokens, err = PackRoomInfo(txn, room)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, nil, err
}
if message.Data == nil {
message.Data = &RoomInfoSc{}
tokens = append(tokens, token)
}
return message, tokens, nil
}
func (s *bingoServer) getAllPlayersInRoom(token string) (trigger string, tokens []string, err error) {
err = db.View(func(txn *badger.Txn) error {
player, err := GetPlayer(txn, token)
if err != nil {
return err
}
trigger = player.Name
if len(player.RoomId) == 0 {
return nil
}
room, err := GetRoom(txn, player.RoomId)
if IsErrKeyNotFound(err) {
return nil
} else if err != nil {
return err
}
tokens = append(room.Players, room.Host)
return nil
})
if err != nil {
return "", nil, err
}
return trigger, tokens, nil
}
func (s *bingoServer) NotifyPlayerInfo(token, reply string, winnerIdx ...int32) {
message, tokens, err := s.buildPlayerInfo(token)
if len(winnerIdx) > 0 {
message.Data.(*RoomInfoSc).Winner = winnerIdx[0]
}
if err != nil {
log.Errorf("db error: %+v", err)
} else {
for _, token1 := range tokens {
if conn, ok := s.tokenConnMap[token1]; ok {
if token1 != token {
conn.Send(message)
} else {
conn.Send(&myws.Message{
MsgName: message.MsgName,
Reply: reply,
Trigger: message.Trigger,
Data: message.Data,
})
}
}
}
}
}
func (s *bingoServer) NotifyPlayersInRoom(token, reply string, message *myws.Message) {
trigger, tokens, err := s.getAllPlayersInRoom(token)
message.Trigger = trigger
if err != nil {
log.Errorf("db error: %+v", err)
} else {
for _, token1 := range tokens {
if conn, ok := s.tokenConnMap[token1]; ok {
if token1 != token {
conn.Send(message)
} else {
conn.Send(&myws.Message{
MsgName: message.MsgName,
Reply: reply,
Trigger: trigger,
Data: message.Data,
})
}
}
}
}
}
func GetPlayer(txn *badger.Txn, token string) (*Player, error) {
if token == robotPlayer.Token {
return &robotPlayer, nil
}
key := append([]byte("token: "), []byte(token)...)
item, err := txn.Get(key)
if IsErrKeyNotFound(err) {
return nil, errors.Wrap(err, "cannot find this token")
} else if err != nil {
return nil, errors.WithStack(err)
}
var player Player
err = item.Value(func(val []byte) error {
return proto.Unmarshal(val, &player)
})
if err != nil {
return nil, errors.WithStack(err)
}
return &player, nil
}
func GetPlayerOrNew(txn *badger.Txn, token string) (*Player, error) {
if token == robotPlayer.Token {
return &robotPlayer, nil
}
key := append([]byte("token: "), []byte(token)...)
item, err := txn.Get(key)
if IsErrKeyNotFound(err) {
return new(Player), nil
} else if err != nil {
return nil, errors.WithStack(err)
}
var player Player
err = item.Value(func(val []byte) error {
return proto.Unmarshal(val, &player)
})
if err != nil {
return nil, errors.WithStack(err)
}
return &player, nil
}
func SetPlayer(txn *badger.Txn, player *Player) error {
if player.Token == robotPlayer.Token {
return nil
}
key := append([]byte("token: "), []byte(player.Token)...)
val, err := proto.Marshal(player)
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(txn.SetEntry(badger.NewEntry(key, val).WithTTL(24 * time.Hour)))
}