forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
99 lines (84 loc) · 2.48 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
89
90
91
92
93
94
95
96
97
98
99
package telebot
import (
"encoding/json"
"strconv"
)
// 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"`
Photo *Photo `json:"photo"`
// (Optional)
Text string `json:"text"`
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]string{
"user_id": user.Recipient(),
}
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = msgID
}
data, err := b.Raw("getGameHighScores", params)
if err != nil {
return nil, err
}
var resp struct {
Result []GameHighScore
}
if err := json.Unmarshal(data, &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]string{
"user_id": user.Recipient(),
"score": strconv.Itoa(score.Score),
"force": strconv.FormatBool(score.Force),
"disable_edit_message": strconv.FormatBool(score.NoEdit),
}
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = msgID
}
data, err := b.Raw("setGameScore", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}