-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
88 lines (76 loc) · 2.37 KB
/
game.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
package crare
// Game object represents a game.
// Their short names acts as unique identifiers.
type Game struct {
Name string `json:"game_short_name"`
Title string `json:"title"`
Description string `json:"description"`
Text string `json:"text"` // (Optional)
Photo *Photo `json:"photo"`
Entities []MessageEntity `json:"text_entities"`
Animation *Animation `json:"animation"`
}
// GameHighScore object represents one row
// of the high scores table for a game.
type GameHighScore struct {
User *User `json:"user"`
Position int `json:"position"`
Score int `json:"score"`
Force bool `json:"force"`
NoEdit bool `json:"disable_edit_message"`
}
// GameScores returns the score of the specified user
// and several of their neighbors in a game.
//
// This function will panic upon nil Editable.
//
// Currently, it returns scores for the target user,
// plus two of their closest neighbors on each side.
// Will also return the top three users
// if the user and his neighbors are not among them.
func (b *Bot) GameScores(user Recipient, msg Editable) ([]GameHighScore, error) {
msgID, chatID := msg.MessageSig()
params := map[string]any{
"user_id": user.Recipient(),
}
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID
} else {
params["chat_id"] = chatID
params["message_id"] = msgID
}
data, err := b.Raw("getGameHighScores", params)
if err != nil {
return nil, err
}
defer ReleaseBuffer(data)
var resp Response[[]GameHighScore]
if err := b.json.NewDecoder(data).Decode(&resp); err != nil {
return nil, err
}
return resp.Result, nil
}
// SetGameScore sets the score of the specified user in a game.
//
// If the message was sent by the bot, returns the edited Message,
// otherwise returns nil and ErrTrueResult.
func (b *Bot) SetGameScore(user Recipient, msg Editable, score GameHighScore) (*Message, error) {
msgID, chatID := msg.MessageSig()
params := map[string]any{
"user_id": user.Recipient(),
"score": score.Score,
"force": score.Force,
"disable_edit_message": score.NoEdit,
}
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID
} else {
params["chat_id"] = chatID
params["message_id"] = msgID
}
data, err := b.Raw("setGameScore", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}