This commit is contained in:
shoopea 2019-05-30 12:12:01 +08:00
parent f919abfd3f
commit bfac2a94a2
4 changed files with 71 additions and 42 deletions

19
bot.go
View File

@ -83,13 +83,7 @@ func botTest(m *tb.Message) {
return return
} }
if _, ok := clientsKeepAlive[m.Chat.ID]; ok { if _, ok := clientsKeepAlive[m.Chat.ID]; ok {
c := TGCommand{ clientSendCWMsg(m.Chat.ID, "🏅Me")
Type: commandSendMsg,
Text: "🏅Me",
FromUserID64: m.Chat.ID,
ToChatID64: userID64ChtWrsBot,
}
MQTGCmdQueue <- c
c = TGCommand{ c = TGCommand{
Type: commandReplyMsg, Type: commandReplyMsg,
@ -282,15 +276,12 @@ func botTimer(m *tb.Message) {
} }
TGCmdQueue <- c TGCmdQueue <- c
} else { } else {
c := TGCommand{ p := JobPayloadMsgClient{
Type: commandSendMsg, Msg: r.ReplaceAllString(m.Payload, "${Msg}"),
Text: r.ReplaceAllString(m.Payload, "${Msg}"),
FromUserID64: m.Chat.ID,
ToChatID64: userID64ChtWrsBot,
} }
b, _ := json.Marshal(c) b, _ := json.Marshal(p)
t := time.Now().Add(d) t := time.Now().Add(d)
objID64, err := createJob(objSubTypeJobMsgClient, objJobPriority, int64(m.Sender.ID), t, b) objID64, err := createJob(objSubTypeJobMsgClient, objJobPriority, int64(m.Chat.ID), t, b)
logOnError(err, "botTimer : createJob") logOnError(err, "botTimer : createJob")
if err != nil { if err != nil {
c := TGCommand{ c := TGCommand{

42
client.go Normal file
View File

@ -0,0 +1,42 @@
package main
import (
"strings"
"time"
)
func clientSendCWMsg(userID64 int64, s string) {
c := TGCommand{
Type: commandSendMsg,
Text: s,
FromUserID64: userID64,
ToChatID64: userID64ChtWrsBot,
}
MQTGCmdQueue <- c
}
func clientMsgMe(userID64 int64, guildID64 int64, state string, timestamp time.Time) {
if _, ok := clientsQueue[userID64]; ok {
if c, ok := clientsCW[userID64]; ok {
if c.LastUpdate.Before(timestamp) {
c.GuildID64 = guildID64
c.State = state
c.LastUpdate = timestamp
if strings.Compare(cacheObjGuild[guildID64].Name, ``) != 0 && strings.Compare(c.Role, ``) == 0 {
clientSendCWMsg(userID64, "/g_roles")
}
}
} else {
c := ChatWarsClient{
GuildID64: guildID64,
State: state,
LastUpdate: timestamp,
}
clientsCW[userID64] = &c
if strings.Compare(cacheObjGuild[guildID64].Name, ``) != 0 {
clientSendCWMsg(userID64, "/g_roles")
}
}
}
}

7
def.go
View File

@ -27,6 +27,7 @@ type ChatWarsClient struct {
Role string `json:"role"` Role string `json:"role"`
State string `json:"state"` State string `json:"state"`
BusyUntil time.Time `json:"busy_until"` BusyUntil time.Time `json:"busy_until"`
LastUpdate time.Time `json:"last_update"`
} }
type TGCommand struct { type TGCommand struct {
@ -135,6 +136,8 @@ type MessageParsingRule struct {
Description string Description string
Rule string Rule string
MsgTypeID int32 MsgTypeID int32
ChatID64 int64
SenderUserID64 int64
re *regexp.Regexp re *regexp.Regexp
} }
@ -180,6 +183,10 @@ type JobPayloadSetDone struct {
Text string `json:"test"` Text string `json:"test"`
} }
type JobPayloadMsgClient struct {
Msg string `json:"msg"`
}
const ( const (
userID64ChtWrsBot = 408101137 userID64ChtWrsBot = 408101137

23
job.go
View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"strconv" "strconv"
"time" "time"
) )
@ -263,7 +262,6 @@ func jobPillage(j Job) {
ToUserID64: j.UserID64, ToUserID64: j.UserID64,
} }
TGCmdQueue <- s TGCmdQueue <- s
log.Printf("More than one outcome for pillage #%d\n", r.ObjID64)
} else if len(ids) == 1 { // we've got a match, job is done whether we prevented the pillage or not } 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 := getMsg(ids[0])
logOnError(err, "jobPillage : getMsg(objSubTypeMessagePillageGo, objSubTypeMessagePillageTimeout)") logOnError(err, "jobPillage : getMsg(objSubTypeMessagePillageGo, objSubTypeMessagePillageTimeout)")
@ -279,7 +277,6 @@ func jobPillage(j Job) {
ToUserID64: j.UserID64, ToUserID64: j.UserID64,
} }
TGCmdQueue <- s TGCmdQueue <- s
log.Printf("jobPillage : objSubTypeMessagePillageGo\n", r.ObjID64)
} else if msgTypeID64 == objSubTypeMessagePillageWin { } else if msgTypeID64 == objSubTypeMessagePillageWin {
s := TGCommand{ s := TGCommand{
Type: commandSendMsg, Type: commandSendMsg,
@ -287,7 +284,6 @@ func jobPillage(j Job) {
ToUserID64: j.UserID64, ToUserID64: j.UserID64,
} }
TGCmdQueue <- s TGCmdQueue <- s
log.Printf("jobPillage : objSubTypeMessagePillageWin\n", r.ObjID64)
} else if msgTypeID64 == objSubTypeMessagePillageLoss { } else if msgTypeID64 == objSubTypeMessagePillageLoss {
s := TGCommand{ s := TGCommand{
Type: commandSendMsg, Type: commandSendMsg,
@ -295,7 +291,6 @@ func jobPillage(j Job) {
ToUserID64: j.UserID64, ToUserID64: j.UserID64,
} }
TGCmdQueue <- s TGCmdQueue <- s
log.Printf("jobPillage : objSubTypeMessagePillageLoss\n", r.ObjID64)
} else if msgTypeID64 == objSubTypeMessagePillageTimeout { } else if msgTypeID64 == objSubTypeMessagePillageTimeout {
s := TGCommand{ s := TGCommand{
Type: commandSendMsg, Type: commandSendMsg,
@ -303,7 +298,6 @@ func jobPillage(j Job) {
ToUserID64: j.UserID64, ToUserID64: j.UserID64,
} }
TGCmdQueue <- s TGCmdQueue <- s
log.Printf("jobPillage : objSubTypeMessagePillageTimeout\n", r.ObjID64)
} else { } else {
s := TGCommand{ s := TGCommand{
Type: commandSendMsg, Type: commandSendMsg,
@ -311,7 +305,6 @@ func jobPillage(j Job) {
ToUserID64: j.UserID64, ToUserID64: j.UserID64,
} }
TGCmdQueue <- s TGCmdQueue <- s
log.Printf("jobPillage : We don't know what happened\n", r.ObjID64)
} }
} }
} }
@ -347,13 +340,7 @@ func jobPillage(j Job) {
err = rescheduleJob(j.ID64, j.Status+1, time.Now().Add(30*time.Second)) err = rescheduleJob(j.ID64, j.Status+1, time.Now().Add(30*time.Second))
logOnError(err, "jobPillage : rescheduleJob(objSubTypeMessageGo)") logOnError(err, "jobPillage : rescheduleJob(objSubTypeMessageGo)")
} else { //no /go in the last 30 sec so we go ahead, send one and reschedule to check again in 25sec } else { //no /go in the last 30 sec so we go ahead, send one and reschedule to check again in 25sec
c := TGCommand{ clientSendCWMsg(j.UserID64, "/go")
Type: commandSendMsg,
Text: "/go",
FromUserID64: j.UserID64,
ToChatID64: userID64ChtWrsBot,
}
MQTGCmdQueue <- c
err = rescheduleJob(j.ID64, j.Status+1, time.Now().Add(30*time.Second)) err = rescheduleJob(j.ID64, j.Status+1, time.Now().Add(30*time.Second))
logOnError(err, "jobPillage : rescheduleJob") logOnError(err, "jobPillage : rescheduleJob")
@ -363,14 +350,16 @@ func jobPillage(j Job) {
} }
func jobMsgClient(j Job) { func jobMsgClient(j Job) {
var c TGCommand var p JobPayloadMsgClient
err := setJobStart(j.ID64) err := setJobStart(j.ID64)
logOnError(err, "jobMsgClient : setJobStart") logOnError(err, "jobMsgClient : setJobStart")
err = json.Unmarshal(j.Payload, &c) err = json.Unmarshal(j.Payload, &p)
logOnError(err, "jobMsgClient : Unmarshal payload") logOnError(err, "jobMsgClient : Unmarshal payload")
MQTGCmdQueue <- c if err != nil {
clientSendCWMsg(j.UserID64, p.Msg)
}
err = setJobDone(j.ID64) err = setJobDone(j.ID64)
logOnError(err, "joMsgClient : setJobDone") logOnError(err, "joMsgClient : setJobDone")