From bfac2a94a2b8982071827c0d4cd095801338a38b Mon Sep 17 00:00:00 2001 From: shoopea Date: Thu, 30 May 2019 12:12:01 +0800 Subject: [PATCH] test --- bot.go | 19 +++++-------------- client.go | 42 ++++++++++++++++++++++++++++++++++++++++++ def.go | 29 ++++++++++++++++++----------- job.go | 23 ++++++----------------- 4 files changed, 71 insertions(+), 42 deletions(-) create mode 100644 client.go diff --git a/bot.go b/bot.go index 01e65de..144cec3 100644 --- a/bot.go +++ b/bot.go @@ -83,13 +83,7 @@ func botTest(m *tb.Message) { return } if _, ok := clientsKeepAlive[m.Chat.ID]; ok { - c := TGCommand{ - Type: commandSendMsg, - Text: "🏅Me", - FromUserID64: m.Chat.ID, - ToChatID64: userID64ChtWrsBot, - } - MQTGCmdQueue <- c + clientSendCWMsg(m.Chat.ID, "🏅Me") c = TGCommand{ Type: commandReplyMsg, @@ -282,15 +276,12 @@ func botTimer(m *tb.Message) { } TGCmdQueue <- c } else { - c := TGCommand{ - Type: commandSendMsg, - Text: r.ReplaceAllString(m.Payload, "${Msg}"), - FromUserID64: m.Chat.ID, - ToChatID64: userID64ChtWrsBot, + p := JobPayloadMsgClient{ + Msg: r.ReplaceAllString(m.Payload, "${Msg}"), } - b, _ := json.Marshal(c) + b, _ := json.Marshal(p) 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") if err != nil { c := TGCommand{ diff --git a/client.go b/client.go new file mode 100644 index 0000000..2f3b528 --- /dev/null +++ b/client.go @@ -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") + } + + } + } +} diff --git a/def.go b/def.go index ead712d..5b9a9d2 100644 --- a/def.go +++ b/def.go @@ -22,11 +22,12 @@ type MQClient struct { } type ChatWarsClient struct { - UserID64 int64 `json:"user_id"` - GuildID64 int64 `json:"guild_id"` - Role string `json:"role"` - State string `json:"state"` - BusyUntil time.Time `json:"busy_until"` + UserID64 int64 `json:"user_id"` + GuildID64 int64 `json:"guild_id"` + Role string `json:"role"` + State string `json:"state"` + BusyUntil time.Time `json:"busy_until"` + LastUpdate time.Time `json:"last_update"` } type TGCommand struct { @@ -130,12 +131,14 @@ type ChatWarsMessageUnionWar struct { } type MessageParsingRule struct { - ID int32 - Priority int32 - Description string - Rule string - MsgTypeID int32 - re *regexp.Regexp + ID int32 + Priority int32 + Description string + Rule string + MsgTypeID int32 + ChatID64 int64 + SenderUserID64 int64 + re *regexp.Regexp } type BotMsg struct { @@ -180,6 +183,10 @@ type JobPayloadSetDone struct { Text string `json:"test"` } +type JobPayloadMsgClient struct { + Msg string `json:"msg"` +} + const ( userID64ChtWrsBot = 408101137 diff --git a/job.go b/job.go index 01d2230..6a5df12 100644 --- a/job.go +++ b/job.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "log" "strconv" "time" ) @@ -263,7 +262,6 @@ func jobPillage(j Job) { ToUserID64: j.UserID64, } 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 m, err := getMsg(ids[0]) logOnError(err, "jobPillage : getMsg(objSubTypeMessagePillageGo, objSubTypeMessagePillageTimeout)") @@ -279,7 +277,6 @@ func jobPillage(j Job) { ToUserID64: j.UserID64, } TGCmdQueue <- s - log.Printf("jobPillage : objSubTypeMessagePillageGo\n", r.ObjID64) } else if msgTypeID64 == objSubTypeMessagePillageWin { s := TGCommand{ Type: commandSendMsg, @@ -287,7 +284,6 @@ func jobPillage(j Job) { ToUserID64: j.UserID64, } TGCmdQueue <- s - log.Printf("jobPillage : objSubTypeMessagePillageWin\n", r.ObjID64) } else if msgTypeID64 == objSubTypeMessagePillageLoss { s := TGCommand{ Type: commandSendMsg, @@ -295,7 +291,6 @@ func jobPillage(j Job) { ToUserID64: j.UserID64, } TGCmdQueue <- s - log.Printf("jobPillage : objSubTypeMessagePillageLoss\n", r.ObjID64) } else if msgTypeID64 == objSubTypeMessagePillageTimeout { s := TGCommand{ Type: commandSendMsg, @@ -303,7 +298,6 @@ func jobPillage(j Job) { ToUserID64: j.UserID64, } TGCmdQueue <- s - log.Printf("jobPillage : objSubTypeMessagePillageTimeout\n", r.ObjID64) } else { s := TGCommand{ Type: commandSendMsg, @@ -311,7 +305,6 @@ func jobPillage(j Job) { ToUserID64: j.UserID64, } 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)) 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 - c := TGCommand{ - Type: commandSendMsg, - Text: "/go", - FromUserID64: j.UserID64, - ToChatID64: userID64ChtWrsBot, - } - MQTGCmdQueue <- c + clientSendCWMsg(j.UserID64, "/go") err = rescheduleJob(j.ID64, j.Status+1, time.Now().Add(30*time.Second)) logOnError(err, "jobPillage : rescheduleJob") @@ -363,14 +350,16 @@ func jobPillage(j Job) { } func jobMsgClient(j Job) { - var c TGCommand + var p JobPayloadMsgClient err := setJobStart(j.ID64) logOnError(err, "jobMsgClient : setJobStart") - err = json.Unmarshal(j.Payload, &c) + err = json.Unmarshal(j.Payload, &p) logOnError(err, "jobMsgClient : Unmarshal payload") - MQTGCmdQueue <- c + if err != nil { + clientSendCWMsg(j.UserID64, p.Msg) + } err = setJobDone(j.ID64) logOnError(err, "joMsgClient : setJobDone")