-
Notifications
You must be signed in to change notification settings - Fork 27
/
matcher.go
49 lines (33 loc) · 947 Bytes
/
matcher.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
package main
import "log"
func matchUsers(chatIDs <-chan int64) {
for c := range chatIDs {
user, err := retrieveUser(c)
if err != nil {
log.Printf("Error in matcher: %s", err)
continue
}
if !user.Available || user.MatchChatID.Valid {
log.Println("User already assigned")
continue
}
availableUsers, err := retrieveAvailableUsers(c)
if err != nil {
log.Printf("Error retrieving available users: %s", err)
continue
}
if len(availableUsers) == 0 {
continue
}
shuffle(availableUsers)
match := availableUsers[0]
createMatch(user, match)
}
}
func createMatch(user User, match User) {
query := "UPDATE users SET match_chat_id = ? WHERE id = ?"
db.Exec(query, user.ChatID, match.ID)
db.Exec(query, match.ChatID, user.ID)
telegram.SendMessage(match.ChatID, "You have been matched, have fun!", emptyOpts)
telegram.SendMessage(user.ChatID, "You have been matched, have fun!", emptyOpts)
}