small refactoring message cleaning
This commit is contained in:
@ -7,9 +7,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"github.com/hako/durafmt"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type achievement uint8
|
||||
@ -53,11 +51,10 @@ var allAchievement = []achievement{
|
||||
}
|
||||
|
||||
type playerInfo struct {
|
||||
playerID string
|
||||
playerMessageID int
|
||||
isOnline bool
|
||||
achievements []achievement
|
||||
messageText string
|
||||
playerID string
|
||||
isOnline bool
|
||||
achievements []achievement
|
||||
messageText string
|
||||
}
|
||||
|
||||
func (p *playerInfo) setOnline(v bool) *playerInfo {
|
||||
@ -74,8 +71,6 @@ const (
|
||||
)
|
||||
|
||||
func (p *playerInfo) updatePlayerInfo(ctx context.Context) error {
|
||||
botApi := getBotApi(ctx)
|
||||
conf := getConfig(ctx)
|
||||
db := getDB(ctx)
|
||||
|
||||
lines := make([]string, 0, 10)
|
||||
@ -132,46 +127,21 @@ func (p *playerInfo) updatePlayerInfo(ctx context.Context) error {
|
||||
text := strings.Join(lines, "\n")
|
||||
|
||||
if text != p.messageText {
|
||||
messStorage := getMessageStorage(ctx)
|
||||
err := messStorage.apply(ctx, fmt.Sprintf("playerBoard:%s", p.playerID), 0, func(interface{}) (string, interface{}) {
|
||||
return text, nil
|
||||
})
|
||||
|
||||
var c tgbotapi.Chattable
|
||||
|
||||
if p.playerMessageID == 0 {
|
||||
mess := tgbotapi.NewMessage(conf.ChatID, text)
|
||||
mess.ParseMode = tgbotapi.ModeMarkdown
|
||||
|
||||
c = mess
|
||||
} else {
|
||||
mess := tgbotapi.NewEditMessageText(conf.ChatID, p.playerMessageID, text)
|
||||
mess.ParseMode = tgbotapi.ModeMarkdown
|
||||
|
||||
c = mess
|
||||
}
|
||||
|
||||
m, err := botApi.Send(c)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to send message")
|
||||
return err
|
||||
}
|
||||
|
||||
p.playerMessageID = m.MessageID
|
||||
p.messageText = text
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *playerInfo) deletePlayerInfo(ctx context.Context) error {
|
||||
botApi := getBotApi(ctx)
|
||||
conf := getConfig(ctx)
|
||||
|
||||
mess := tgbotapi.NewDeleteMessage(conf.ChatID, p.playerMessageID)
|
||||
_, err := botApi.Send(mess)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to send message")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *playerInfo) setAchievement(ach achievement) {
|
||||
for _, a := range p.achievements {
|
||||
if a == ach {
|
||||
@ -249,11 +219,13 @@ func (pb *playersBoard) getPlayerBoard(playerID string) *playerInfo {
|
||||
pb.mx.Lock()
|
||||
defer pb.mx.Unlock()
|
||||
|
||||
pb.players = append(pb.players, &playerInfo{
|
||||
pi := &playerInfo{
|
||||
playerID: playerID,
|
||||
})
|
||||
}
|
||||
|
||||
return pb.players[len(pb.players)-1]
|
||||
pb.players = append(pb.players, pi)
|
||||
|
||||
return pi
|
||||
}
|
||||
|
||||
func (pb *playersBoard) updateAchievements(ctx context.Context, ach achievement) error {
|
||||
@ -347,19 +319,3 @@ func (pb *playersBoard) update(ctx context.Context, as ...achievement) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pb *playersBoard) run(ctx context.Context) {
|
||||
log := getLogger(ctx)
|
||||
|
||||
<-ctx.Done()
|
||||
|
||||
pb.mx.Lock()
|
||||
defer pb.mx.Unlock()
|
||||
|
||||
for _, p := range pb.players {
|
||||
err := p.deletePlayerInfo(ctx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user