This commit is contained in:
shoopea 2019-06-08 23:28:07 +08:00
parent b8d1e012da
commit 53ea74fae9
6 changed files with 117 additions and 10 deletions

32
bot.go
View File

@ -32,6 +32,8 @@ func BotHandlers(b *tb.Bot) {
b.Handle("/g_stock", botGStock)
b.Handle("/msg_export_all", botMsgExportAll)
b.Handle(tb.OnPhoto, botPhoto)
b.Handle(tb.OnChannelPost, botChannelPost)
b.Handle(tb.OnQuery, botQuery)
@ -179,6 +181,36 @@ func botMsgRescanAll(m *tb.Message) {
return
}
func botMsgExportAll(m *tb.Message) {
if !m.Private() {
return
}
f, err := exportMessages()
logOnError(err, "botMsgExportAll : exportMessages")
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: `Error exporting messages`,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
c := TGCommand{
Type: commandReplyFile,
File: f,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}
func botMsgDump(m *tb.Message) {
var res string
r := regexp.MustCompile("^[0-9]+$")

25
def.go
View File

@ -8,6 +8,10 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
)
type DataBackup struct {
Messages []ChatWarsMessage `json:"messages"`
}
type MQKeepAlive struct {
UserID64 int64 `json:"user_id"`
Nickname string `json:"nick"`
@ -32,13 +36,14 @@ type ChatWarsClient struct {
}
type TGCommand struct {
Type int64 `json:"type"`
FromChatID64 int64 `json:"from_chat_id"`
FromUserID64 int64 `json:"from_user_id"`
FromMsgID64 int64 `json:"from_msg_id"`
ToChatID64 int64 `json:"to_chat_id"`
ToUserID64 int64 `json:"to_user_id"`
Text string `json:"text"`
Type int64 `json:"type"`
FromChatID64 int64 `json:"from_chat_id"`
FromUserID64 int64 `json:"from_user_id"`
FromMsgID64 int64 `json:"from_msg_id"`
ToChatID64 int64 `json:"to_chat_id"`
ToUserID64 int64 `json:"to_user_id"`
Text string `json:"text"`
File tb.File `json:"file"`
}
type ChatWarsCastle struct {
@ -192,6 +197,10 @@ type Job struct {
Payload []byte
}
type JobPayloadMsgRefresh struct {
ObjID64 int64 `json:"obj_id"`
}
type JobPayloadPillage struct {
ObjID64 int64 `json:"obj_id"`
}
@ -235,6 +244,7 @@ const (
commandSendMsg = 3
commandDeleteMsg = 4
commandRefreshMsg = 5
commandSendFile = 6
objTypeUser = 1
objTypeGuild = 2
@ -331,6 +341,7 @@ const (
objSubTypeJobRescanMsg = 606
objSubTypeJobSetJobDone = 607
objSubTypeJobMsgClient = 608
objSubTypeJobMsgRefresh = 609
objSubTypeItemResource = 701
objSubTypeItemAlch = 702
objSubTypeItemMisc = 703

View File

@ -367,6 +367,8 @@ func resetObjItem() {
reloadObjItem(`p20`, `Potion of Morph`, objSubTypeItemMisc, 1) // PROPAGATE TO WIKI
reloadObjItem(`p21`, `Bottle of Morph`, objSubTypeItemMisc, 1) // PROPAGATE TO WIKI
reloadObjItem(`pmp`, `Pumpkin`, objSubTypeItemMisc, -1)
reloadObjItem(`pol`, `Proof of Loyalty`, objSubTypeItemMisc, -1)
reloadObjItem(`tde`, `deerhorn earlooms`, objSubTypeItemMisc, -1)
reloadObjItem(`r01`, `Champion Sword recipe`, objSubTypeItemRecipe, 10)
reloadObjItem(`r02`, `Trident recipe`, objSubTypeItemRecipe, 10)
reloadObjItem(`r03`, `Hunter Bow recipe`, objSubTypeItemRecipe, 10)

40
msg.go
View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"fmt"
"log"
"regexp"
@ -269,3 +270,42 @@ func parseSubTypeMessagePillageInc(m *ChatWarsMessage, r *regexp.Regexp) (*ChatW
return &cwm, nil
}
func exportMessages() (tb.File, error) {
var f tb.File
bkp := DataBackup{}
ids := getSQLListID64(`SELECT om.obj_id id FROM obj_msg om;`)
for _, id := range ids {
m := getMsg(id)
append(bkp.Messages, m)
}
b, err := json.Marshal(bkp)
logOnError(err, "exportMessages : Marshal")
if err != nil {
return nil, err
}
zbuf := new(bytes.Buffer)
zw := zip.NewWriter(zbuf)
zf, err := zw.Create(`backup.json`)
logOnError(err, "exportMessages : Create")
if err != nil {
return nil, err
}
_, err = zf.Write(b)
logOnError(err, "exportMessages : Write")
if err != nil {
return nil, err
}
err = zw.Close()
logOnError(err, "exportMessages : Close")
if err != nil {
return nil, err
}
f = f.FromReader(bytes.NewReader(zbuf.Bytes()))
return f, nil
}

1
sql.go
View File

@ -417,6 +417,7 @@ func initDB() {
,(` + strconv.Itoa(objSubTypeJobRescanMsg) + `, "job_rescan_msg", "Rescan message job", ` + strconv.Itoa(objTypeJob) + `)
,(` + strconv.Itoa(objSubTypeJobSetJobDone) + `, "job_set_done", "Set job as done job", ` + strconv.Itoa(objTypeJob) + `)
,(` + strconv.Itoa(objSubTypeJobMsgClient) + `, "job_msg_client", "Send message via client", ` + strconv.Itoa(objTypeJob) + `)
,(` + strconv.Itoa(objSubTypeJobMsgRefresh) + `, "job_msg_refresh", "Refresh message from client", ` + strconv.Itoa(objTypeJob) + `)
,(` + strconv.Itoa(objSubTypeItemResource) + `, "item_res", "Time", ` + strconv.Itoa(objTypeItem) + `)
,(` + strconv.Itoa(objSubTypeItemAlch) + `, "item_alch", "Time", ` + strconv.Itoa(objTypeItem) + `)
,(` + strconv.Itoa(objSubTypeItemMisc) + `, "item_misc", "Time", ` + strconv.Itoa(objTypeItem) + `)

View File

@ -184,6 +184,13 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
cwm.ObjID64 = objId
err = insertMsgAuctionAnnounce(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgAuctionAnnounce")
if cwm.End.Add(15 * time.Second).After(time.Now()) {
p := JobPayloadMsgRefresh{
ObjID64: m.ObjID64,
}
b, _ := json.Marshal(&p)
_, err = createJob(objSubTypeJobMsgRefresh, objJobPriority, m.UserID64, cwm.End.Add(15*time.Second).After(time.Now()), b)
}
case objSubTypeMessageTimeAck:
_, err := fromChatWarsDate(r.ReplaceAllString(m.Text, "${Day}") + " " + r.ReplaceAllString(m.Text, "${Month}") + " " + r.ReplaceAllString(m.Text, "${Year}") + " " + r.ReplaceAllString(m.Text, "${Hour}") + ":" + r.ReplaceAllString(m.Text, "${Minute}"))
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : fromChatWarsDate")
@ -264,6 +271,20 @@ func TGCmdWorker(id int, b *tb.Bot, cmds <-chan TGCommand) {
_, err := b.Send(&ch, c.Text)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Send")
}
case commandSendFile:
if c.ToChatID64 != 0 {
ch := tb.Chat{
ID: c.ToChatID64,
}
_, err := b.Send(&ch, c.File)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : File")
} else if c.ToUserID64 != 0 {
ch := tb.Chat{
ID: c.ToUserID64,
}
_, err := b.Send(&ch, c.File)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : File")
}
default:
}
@ -280,10 +301,10 @@ func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
err = clientsQueue[c.FromUserID64].Channel.Publish(
"", // exchange
"", // exchange
clientsQueue[c.FromUserID64].Queue.Name, // routing key
false, // mandatory
false, // immediate
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Body: []byte(j),