222 lines
3.9 KiB
Go
222 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
)
|
|
|
|
type command interface {
|
|
run(context.Context)
|
|
}
|
|
|
|
type commandHandler struct {
|
|
commandChan chan command
|
|
}
|
|
|
|
func newCommandHandler() *commandHandler {
|
|
return &commandHandler{
|
|
commandChan: make(chan command, 50),
|
|
}
|
|
}
|
|
|
|
func (h *commandHandler) handle(c command) {
|
|
h.commandChan <- c
|
|
}
|
|
|
|
func (h *commandHandler) run(ctx context.Context) {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case c := <-h.commandChan: // TODO if close ??
|
|
c.run(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
type joinCommand struct {
|
|
name string
|
|
}
|
|
|
|
func (c *joinCommand) run(ctx context.Context) {
|
|
botApi := getBotApi(ctx)
|
|
cfg := getConfig(ctx)
|
|
mc := getMessageCleaner(ctx)
|
|
db := getDB(ctx)
|
|
log := getLogger(ctx)
|
|
pb := getDPlayersBoard(ctx)
|
|
|
|
mess := tgbotapi.NewMessage(cfg.ChatID, fmt.Sprintf("%s подключился", c.name))
|
|
m, err := botApi.Send(mess)
|
|
if err != nil {
|
|
log.Error(err)
|
|
} else {
|
|
mc.add(ctx, m.MessageID, cfg.ChatID, time.Second*30)
|
|
}
|
|
|
|
p, err := db.getPlayerByName(ctx, c.name)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
if p == nil {
|
|
p, err = db.createPlayer(ctx, c.name, time.Now())
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
} else {
|
|
err = db.updatePlayerLastOnline(ctx, p.id, time.Now())
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
pb.getPlayerBoard(p.id).setOnline(true)
|
|
|
|
err = pb.update(ctx, achievementVeryLongOnline)
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
}
|
|
|
|
type quitCommand struct {
|
|
name string
|
|
}
|
|
|
|
func (c *quitCommand) run(ctx context.Context) {
|
|
botApi := getBotApi(ctx)
|
|
cfg := getConfig(ctx)
|
|
mc := getMessageCleaner(ctx)
|
|
db := getDB(ctx)
|
|
log := getLogger(ctx)
|
|
pb := getDPlayersBoard(ctx)
|
|
|
|
mess := tgbotapi.NewMessage(cfg.ChatID, fmt.Sprintf("%s отключился", c.name))
|
|
m, err := botApi.Send(mess)
|
|
if err != nil {
|
|
log.Error(err)
|
|
} else {
|
|
mc.add(ctx, m.MessageID, cfg.ChatID, time.Second*30)
|
|
}
|
|
|
|
p, err := db.getPlayerByName(ctx, c.name)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
err = db.increasePlayerOnlineDuration(ctx, p.id, time.Now().Sub(p.lastOnline))
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
pb.getPlayerBoard(p.id).setOnline(false)
|
|
|
|
err = pb.update(ctx, achievementVeryLongOnline)
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
}
|
|
|
|
type deathCommand struct {
|
|
name string
|
|
deathType string
|
|
}
|
|
|
|
func (c *deathCommand) run(ctx context.Context) {
|
|
db := getDB(ctx)
|
|
log := getLogger(ctx)
|
|
pb := getDPlayersBoard(ctx)
|
|
|
|
p, err := db.getPlayerByName(ctx, c.name)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
err = db.increasePlayerDeath(ctx, p.id, 1)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
err = pb.update(ctx, achievementDeathless, achievementBestFeeder)
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
}
|
|
|
|
type killCommand struct {
|
|
name string
|
|
entity string
|
|
}
|
|
|
|
func (c *killCommand) run(ctx context.Context) {
|
|
botApi := getBotApi(ctx)
|
|
cfg := getConfig(ctx)
|
|
mc := getMessageCleaner(ctx)
|
|
db := getDB(ctx)
|
|
log := getLogger(ctx)
|
|
pb := getDPlayersBoard(ctx)
|
|
|
|
mess := tgbotapi.NewMessage(cfg.ChatID, fmt.Sprintf("%s убил %s", c.name, c.entity))
|
|
m, err := botApi.Send(mess)
|
|
if err != nil {
|
|
log.Error(err)
|
|
} else {
|
|
mc.add(ctx, m.MessageID, cfg.ChatID, time.Second*30)
|
|
}
|
|
|
|
p, err := db.getPlayerByName(ctx, c.name)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
err = db.increasePlayerEntryKills(ctx, p.id, c.entity, 1)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
err = pb.update(ctx, achievementMaxFrags, achievementPeaceable)
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
}
|
|
|
|
type changeLevelCommand struct {
|
|
name string
|
|
newLevel int
|
|
}
|
|
|
|
func (c *changeLevelCommand) run(ctx context.Context) {
|
|
db := getDB(ctx)
|
|
log := getLogger(ctx)
|
|
pb := getDPlayersBoard(ctx)
|
|
|
|
p, err := db.getPlayerByName(ctx, c.name)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
err = db.updatePlayerLevel(ctx, p.id, c.newLevel)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
err = pb.update(ctx, achievementMaxLevel)
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
}
|