2019-04-15 13:21:16 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-02 12:11:27 +02:00
|
|
|
"database/sql"
|
2019-05-03 05:58:36 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-06-03 03:19:06 +02:00
|
|
|
"math/rand"
|
2019-06-03 03:21:59 +02:00
|
|
|
"sync"
|
2019-05-03 05:58:36 +02:00
|
|
|
"time"
|
2019-06-03 03:21:59 +02:00
|
|
|
|
|
|
|
"gopkg.in/gcfg.v1"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
2019-04-15 13:21:16 +02:00
|
|
|
)
|
|
|
|
|
2019-05-02 12:11:27 +02:00
|
|
|
type Config struct {
|
|
|
|
Telegram struct {
|
2019-05-03 05:58:36 +02:00
|
|
|
URL string
|
|
|
|
Token string
|
2019-05-02 12:11:27 +02:00
|
|
|
}
|
|
|
|
SQL struct {
|
2019-05-03 05:58:36 +02:00
|
|
|
Driver string
|
|
|
|
Type string
|
|
|
|
Address string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
2019-05-02 12:11:27 +02:00
|
|
|
}
|
|
|
|
Rabbit struct {
|
2019-05-03 05:58:36 +02:00
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Host string
|
2019-06-28 06:50:35 +02:00
|
|
|
Path string
|
2019-05-02 12:11:27 +02:00
|
|
|
}
|
2019-05-03 05:00:09 +02:00
|
|
|
Bot struct {
|
2019-05-17 10:12:02 +02:00
|
|
|
Admin int64
|
2019-05-03 05:58:36 +02:00
|
|
|
Guildname string
|
|
|
|
Guild string
|
2019-05-03 05:00:09 +02:00
|
|
|
}
|
2019-05-02 12:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-05-03 05:58:36 +02:00
|
|
|
config = flag.String("config", "chirpnest.cfg", "config file path")
|
|
|
|
initdb = flag.Bool("initdb", false, "initialize bot database")
|
2019-05-02 12:11:27 +02:00
|
|
|
|
2019-05-03 05:58:36 +02:00
|
|
|
db *sql.DB
|
2019-05-11 05:29:09 +02:00
|
|
|
b *tb.Bot
|
2019-05-03 05:58:36 +02:00
|
|
|
|
2019-06-03 03:19:06 +02:00
|
|
|
cfg Config
|
|
|
|
|
|
|
|
RndSrc *rand.Rand
|
|
|
|
RndMu sync.Mutex
|
|
|
|
|
2019-05-06 09:25:57 +02:00
|
|
|
MQCWMsgQueue chan ChatWarsMessage
|
|
|
|
SQLMsgIdentifyQueue chan int64
|
2019-05-16 04:21:04 +02:00
|
|
|
TGCmdQueue chan TGCommand
|
2019-05-16 14:41:28 +02:00
|
|
|
MQTGCmdQueue chan TGCommand
|
2019-05-31 14:24:27 +02:00
|
|
|
JobQueue chan Job
|
2019-06-03 03:19:06 +02:00
|
|
|
|
2019-06-14 05:03:43 +02:00
|
|
|
msgParsingRules map[int]MessageParsingRule
|
2019-06-28 06:50:35 +02:00
|
|
|
clientsQueue map[int64]*MQSession
|
2019-06-03 10:43:22 +02:00
|
|
|
|
2019-06-14 05:03:43 +02:00
|
|
|
clientsCW *sync.Map
|
|
|
|
clientsKeepAlive *sync.Map
|
2019-05-02 12:11:27 +02:00
|
|
|
)
|
|
|
|
|
2019-04-15 13:21:16 +02:00
|
|
|
func PrintText(m *tb.Message) {
|
|
|
|
fmt.Printf("[%d] %s(%d) | %s(%d) : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-05-02 12:11:27 +02:00
|
|
|
log.Println("Starting Chirpnest...")
|
2019-05-03 05:58:36 +02:00
|
|
|
|
2019-05-02 12:11:27 +02:00
|
|
|
// Parsing config
|
2019-05-03 05:58:36 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2019-06-03 03:19:06 +02:00
|
|
|
// randomize
|
2019-06-03 03:21:31 +02:00
|
|
|
RndSrc = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
|
2019-06-03 03:19:06 +02:00
|
|
|
|
2019-05-02 12:14:07 +02:00
|
|
|
err := gcfg.ReadFileInto(&cfg, *config)
|
2019-05-11 05:29:09 +02:00
|
|
|
failOnError(err, "Parsing config")
|
2019-05-03 05:58:36 +02:00
|
|
|
|
2019-05-02 12:11:27 +02:00
|
|
|
// Connecting to DB
|
|
|
|
switch cfg.SQL.Driver {
|
2019-05-03 05:58:36 +02:00
|
|
|
case "mysql":
|
2019-05-09 12:36:45 +02:00
|
|
|
db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database+"?parseTime=true")
|
2019-05-11 05:29:09 +02:00
|
|
|
failOnError(err, "Opening MySQL connection")
|
2019-05-03 05:58:36 +02:00
|
|
|
defer db.Close()
|
|
|
|
|
2019-05-02 12:11:27 +02:00
|
|
|
}
|
2019-05-03 05:58:36 +02:00
|
|
|
|
2019-05-02 12:11:27 +02:00
|
|
|
// Check if any issue
|
|
|
|
err = db.Ping()
|
2019-05-11 05:29:09 +02:00
|
|
|
failOnError(err, "Checking MySQL connection")
|
|
|
|
log.Println("SQL connection initialized")
|
2019-05-03 05:58:36 +02:00
|
|
|
|
|
|
|
if *initdb {
|
2019-05-02 12:16:05 +02:00
|
|
|
initDB()
|
2019-05-25 09:36:03 +02:00
|
|
|
_, _ = addObjCastle(`🦌`, `Deerhorn`)
|
|
|
|
_, _ = addObjCastle(`🐉`, `Dragonscale`)
|
|
|
|
_, _ = addObjCastle(`🦅`, `Highnest`)
|
|
|
|
_, _ = addObjCastle(`🌑`, `Moonlight`)
|
|
|
|
_, _ = addObjCastle(`🥔`, `Potato`)
|
|
|
|
_, _ = addObjCastle(`🦈`, `Sharkteeth`)
|
|
|
|
_, _ = addObjCastle(`🐺`, `Wolfpack`)
|
2019-06-11 04:14:19 +02:00
|
|
|
_, _ = addObjCastle(`⛔`, `Unknwon`)
|
2019-05-25 10:56:13 +02:00
|
|
|
_, _ = addObjGuild(``, `No Guild`)
|
2019-05-02 12:16:05 +02:00
|
|
|
}
|
2019-05-03 05:58:36 +02:00
|
|
|
|
2019-06-02 15:33:37 +02:00
|
|
|
resetMsgParsingRules()
|
2019-05-08 12:35:43 +02:00
|
|
|
msgParsingRules, err = loadMsgParsingRules()
|
2019-05-08 13:15:17 +02:00
|
|
|
logOnError(err, "Message parsing rules")
|
2019-06-02 15:33:37 +02:00
|
|
|
|
2019-05-25 09:25:11 +02:00
|
|
|
err = loadObjCastle()
|
|
|
|
logOnError(err, "Caching castles")
|
2019-06-02 15:33:37 +02:00
|
|
|
|
2019-05-25 09:25:11 +02:00
|
|
|
err = loadObjGuild()
|
|
|
|
logOnError(err, "Caching guilds")
|
2019-06-02 15:33:37 +02:00
|
|
|
|
2019-05-25 10:59:14 +02:00
|
|
|
err = loadObjUser()
|
|
|
|
logOnError(err, "Caching user")
|
2019-06-02 15:33:37 +02:00
|
|
|
|
2019-06-02 11:30:04 +02:00
|
|
|
err = loadObjItem()
|
2019-06-02 15:33:37 +02:00
|
|
|
logOnError(err, "Caching items")
|
|
|
|
resetObjItem()
|
2019-05-08 12:13:47 +02:00
|
|
|
|
2019-06-09 14:17:05 +02:00
|
|
|
err = loadObjMsg()
|
|
|
|
logOnError(err, "Caching msgs")
|
|
|
|
|
2019-05-11 05:29:09 +02:00
|
|
|
// Registering bot
|
|
|
|
b, err := tb.NewBot(tb.Settings{
|
|
|
|
Token: cfg.Telegram.Token,
|
|
|
|
URL: cfg.Telegram.URL,
|
|
|
|
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
|
|
|
|
})
|
|
|
|
failOnError(err, "Registering bot")
|
|
|
|
log.Println("Bot registered")
|
|
|
|
|
2019-06-09 13:48:52 +02:00
|
|
|
log.Printf("URL : %s\n", b.URL)
|
|
|
|
log.Printf("Token : %s\n", b.Token)
|
|
|
|
|
2019-05-11 05:37:33 +02:00
|
|
|
go BotHandlers(b)
|
2019-05-11 05:36:17 +02:00
|
|
|
u := tb.User{
|
2019-05-11 05:36:38 +02:00
|
|
|
ID: int(cfg.Bot.Admin),
|
2019-05-11 05:36:17 +02:00
|
|
|
}
|
|
|
|
b.Send(&u, "Bot restarted")
|
2019-05-04 11:01:57 +02:00
|
|
|
|
2019-05-18 16:29:48 +02:00
|
|
|
MQCWMsgQueue = make(chan ChatWarsMessage, MQCWMsgQueueSize)
|
|
|
|
SQLMsgIdentifyQueue = make(chan int64, SQLMsgIdentifyQueueSize)
|
|
|
|
TGCmdQueue = make(chan TGCommand, TGCmdQueueSize)
|
|
|
|
MQTGCmdQueue = make(chan TGCommand, MQTGCmdQueueSize)
|
2019-05-31 14:24:27 +02:00
|
|
|
JobQueue = make(chan Job, JobQueueSize)
|
2019-06-28 06:50:35 +02:00
|
|
|
clientsQueue = make(map[int64]*MQSession)
|
2019-06-14 06:01:59 +02:00
|
|
|
|
2019-06-03 10:43:22 +02:00
|
|
|
clientsCW = new(sync.Map)
|
2019-06-14 05:03:43 +02:00
|
|
|
clientsKeepAlive = new(sync.Map)
|
2019-05-06 04:54:24 +02:00
|
|
|
|
2019-05-06 09:25:57 +02:00
|
|
|
for w := 1; w <= MQGetMsgWorkers; w++ {
|
2019-05-04 11:15:33 +02:00
|
|
|
go MQGetMsgWorker(w, MQCWMsgQueue)
|
|
|
|
}
|
2019-05-06 09:25:57 +02:00
|
|
|
for w := 1; w <= SQLCWMsgWorkers; w++ {
|
2019-05-16 14:44:59 +02:00
|
|
|
go SQLCWMsgWorker(w, MQCWMsgQueue, SQLMsgIdentifyQueue)
|
2019-05-04 11:01:57 +02:00
|
|
|
}
|
2019-05-06 09:25:57 +02:00
|
|
|
for w := 1; w <= SQLIdentifyMsgWorkers; w++ {
|
|
|
|
go SQLIdentifyMsgWorker(w, SQLMsgIdentifyQueue)
|
2019-05-05 13:29:28 +02:00
|
|
|
}
|
2019-05-31 14:30:08 +02:00
|
|
|
for w := 1; w <= JobWorkers; w++ {
|
|
|
|
go JobWorker(w, JobQueue)
|
2019-05-10 11:23:47 +02:00
|
|
|
}
|
2019-05-16 04:16:35 +02:00
|
|
|
for w := 1; w <= TGCmdWorkers; w++ {
|
|
|
|
go TGCmdWorker(w, b, TGCmdQueue)
|
|
|
|
}
|
2019-05-16 14:39:12 +02:00
|
|
|
for w := 1; w <= MQTGCmdWorkers; w++ {
|
|
|
|
go MQTGCmdWorker(w, MQTGCmdQueue)
|
|
|
|
}
|
2019-05-31 14:24:27 +02:00
|
|
|
go SQLJobWorker()
|
2019-05-17 09:02:35 +02:00
|
|
|
go MQKeepAliveWorker()
|
|
|
|
go MQTidyKeepAliveWorker()
|
2019-04-15 13:21:16 +02:00
|
|
|
|
2019-05-11 06:54:12 +02:00
|
|
|
log.Println("Bot started !")
|
2019-04-15 13:21:16 +02:00
|
|
|
|
2019-05-03 05:58:36 +02:00
|
|
|
// Main loop
|
|
|
|
for {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
2019-04-15 13:21:16 +02:00
|
|
|
|
2019-05-03 05:58:36 +02:00
|
|
|
}
|