This commit is contained in:
shoopea 2019-06-09 20:17:05 +08:00
parent 1001c82cf6
commit 0c64ac7c63
7 changed files with 46 additions and 86 deletions

60
bot.go
View File

@ -100,7 +100,7 @@ func botHelp(m *tb.Message) {
/timer <ETA> "msg" - schedule msg for client in ETA
/g_stock - check guild's vault
/msg_export - export message database
/msg_load - import message database from quoted file`,
/msg_load <URL> - import message database from URL`,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
@ -264,63 +264,7 @@ func botMsgLoad(m *tb.Message) {
if !m.Private() {
return
}
if m.ReplyTo != nil {
r := m.ReplyTo
if r.Document != nil {
f := r.Document.File
if f.OnDisk() {
c := TGCommand{
Type: commandReplyMsg,
Text: "File on disk !",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
} else if f.InCloud() {
c := TGCommand{
Type: commandReplyMsg,
Text: "File in cloud !",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
x, err := json.Marshal(m.ReplyTo)
logOnError(err, "botMsgLoad : Marshal")
// log.Printf("URL : %s\n", b.URL)
// log.Printf("Token : %s\n", b.Token)
log.Printf("File : %s\n", string(x))
//url := fmt.Sprintf("%s/file/bot%s/%s", b.URL, b.Token, f.FilePath)
} else {
c := TGCommand{
Type: commandReplyMsg,
Text: `Document found but can't find file.`,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
} else {
c := TGCommand{
Type: commandReplyMsg,
Text: `Message is not a document.`,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
} else {
c := TGCommand{
Type: commandReplyMsg,
Text: `You need to reply to the message with the backup.`,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
return
}
@ -336,7 +280,7 @@ func botMsgDump(m *tb.Message) {
} else if objTypeId != objTypeMessage {
res = `This is not a message reference`
} else {
cwm, _ := getMsg(objId)
cwm, _ := getObjMsg(objId)
b, _ := json.Marshal(cwm)
res = string(b)
}

4
job.go
View File

@ -266,7 +266,7 @@ func jobPillage(j Job) {
}
TGCmdQueue <- s
} else if len(ids) == 1 { // we've got a match, job is done whether we prevented the pillage or not
m, err := getMsg(ids[0])
m, err := getObjMsg(ids[0])
logOnError(err, "jobPillage : getMsg(objSubTypeMessagePillageGo, objSubTypeMessagePillageTimeout)")
if err == nil {
if m.Date.Add(60 * time.Second).After(time.Now()) {
@ -337,7 +337,7 @@ func jobPillage(j Job) {
and omx.date between addtime(oj.schedule, '-30') and oj.schedule;`)
if len(ids) > 0 { // we did, so we reschedule the job to check the outcome and wait
m, err := getMsg(ids[0])
m, err := getObjMsg(ids[0])
logOnError(err, "jobPillage : getMsg(objSubTypeMessageGo)")
if err == nil {
s := TGCommand{

View File

@ -126,6 +126,9 @@ func main() {
logOnError(err, "Caching items")
resetObjItem()
err = loadObjMsg()
logOnError(err, "Caching msgs")
// Registering bot
b, err := tb.NewBot(tb.Settings{
Token: cfg.Telegram.Token,

2
msg.go
View File

@ -281,7 +281,7 @@ func zipMessages() ([]byte, error) {
log.Printf("zipMessages : Retrieved %d message ids.\n", len(ids))
i := 0
for _, id := range ids {
m, err := getMsg(id)
m, err := getObjMsg(id)
logOnError(err, "zipMessages : getMsg")
if err == nil {
msgs = append(msgs, *m)

42
obj.go
View File

@ -10,11 +10,11 @@ import (
)
var (
cacheObjCastle *sync.Map
cacheObjGuild *sync.Map
cacheObjUser *sync.Map
cacheObjItem *sync.Map
cacheOjbMessage *sync.Map
cacheObjCastle *sync.Map
cacheObjGuild *sync.Map
cacheObjUser *sync.Map
cacheObjItem *sync.Map
cacheOjbMsg *sync.Map
)
func getObjTypeId(objId int64) (int64, error) {
@ -113,6 +113,38 @@ func addObjMsg(msgID64 int64, msgChatID64 int64, msgUserID64 int64, msgSenderUse
return objId, nil
}
func getObjMsg(objId int64) (*ChatWarsMessage, error) {
var m *ChatWarsMessage
if v, ok := cacheObjMsg.Load(objId); ok {
m := v.(ChatWarsMessage)
return m, nil
}
stmt, err := db.Prepare(`SELECT om.msg_id, om.chat_id, om.user_id, om.sender_user_id, om.date, om.text FROM obj_msg om WHERE om.obj_id = ?`)
if err != nil {
return m, err
}
defer stmt.Close()
m = new(ChatWarsMessage)
err = stmt.QueryRow(objId).Scan(&m.ID64, &m.ChatID64, &m.UserID64, &m.SenderUserID64, &m.Date, &m.Text)
if err != nil {
return m, err
}
cacheObjMsg.Store(objId, *m)
return m, nil
}
func loadObjMsg() error {
cacheObjMsg = new(sync.Map)
return nil
}
func addObjCastle(logo string, name string) (int64, error) {
tx, err := db.Begin()
logOnError(err, "addObjCastle : start transaction")

19
sql.go
View File

@ -436,25 +436,6 @@ func initDB() {
log.Println("initDB : Database set up")
}
func getMsg(objId int64) (*ChatWarsMessage, error) {
var m *ChatWarsMessage
stmt, err := db.Prepare(`SELECT om.msg_id, om.chat_id, om.user_id, om.sender_user_id, om.date, om.text FROM obj_msg om WHERE om.obj_id = ?`)
if err != nil {
return m, err
}
defer stmt.Close()
m = new(ChatWarsMessage)
err = stmt.QueryRow(objId).Scan(&m.ID64, &m.ChatID64, &m.UserID64, &m.SenderUserID64, &m.Date, &m.Text)
if err != nil {
return m, err
}
return m, nil
}
func insertMsgReport(objId int64, war_date int32, atk int32, def int32, exp int32, gold int32, stock int32, crit bool, stamina bool) error {
objSubTypeId, err := getObjSubTypeId(objId)
if err != nil {

View File

@ -76,7 +76,7 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
var i int
//log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
for objId := range objIds {
m, err := getMsg(objId)
m, err := getObjMsg(objId)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Retrieving message.")
if err == nil {
//log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Message retrieved (%d)\n%s\n", objId, m.Text)