diff --git a/gocw2.sample.cfg b/gocw2.sample.cfg index 77cbfdf..ce48586 100644 --- a/gocw2.sample.cfg +++ b/gocw2.sample.cfg @@ -3,4 +3,10 @@ user = guest password = guest host = localhost:5672 sendqueue = chatwars -receivequeue = guest \ No newline at end of file +receivequeue = guest + +[listen] +war = false +warmini = false +auctions = false + \ No newline at end of file diff --git a/main.go b/main.go index 5e5e1ca..1f0775f 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,11 @@ type Config struct { SendQueue string ReceiveQueue string } + Listen struct { + War bool + WarMini bool + Auctions bool + } } var ( @@ -66,6 +71,7 @@ var ( ownUserID64 = int64(0) ownUserID32 = int32(0) + lastOwnTDMsg = time.Time MQCWMsgQueue chan ChatWarsMessage MQCWCmdQueue chan ChatWarsCommand ) @@ -150,7 +156,12 @@ func main() { go MQReceiveMsgWorker(w, MQCWCmdQueue) } + lastOwnTDMsg = time.Now() + go ListenCW(client) + go ListenMQ(client, MQCWCmdQueue) + go ListenMe(client) + fmt.Println("Started !") // Main loop diff --git a/td.go b/td.go index f256a3c..05de93e 100644 --- a/td.go +++ b/td.go @@ -14,13 +14,46 @@ func ListenCW(c *tdlib.Client) { updateMsg := (*msg).(*tdlib.UpdateNewMessage) chatID := updateMsg.Message.ChatID forwardInfo := updateMsg.Message.ForwardInfo - if (chatID == user_chtwrsbot || chatID == chat_war || chatID == chat_war_mini || chatID == chat_auction) && forwardInfo == nil { + if (chatID == user_chtwrsbot || + (chatID == chat_war && cfg.Listen.War) || + (chatID == chat_war_mini && cfg.Listen.WarMini) || + (chatID == chat_auction && cfg.Listen.Auctions)) && + forwardInfo == nil { return true } return false } + receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100) + for newMsg := range receiver.Chan { + lastOwnTDMsg = time.Now() + } +} + +func ListenMQ(c *tdlib.Client, msgs <-chan ChatWarsCommand) { + for m := range msgs { + for now := time.Now(); lastOwnTDMsg.Add(time.Second) > now; { + log.Printf("ListenMQ : channel busy, waiting before acting...\n") + time.Sleep(time.Until(lastOwnTDMsg.Add(time.Second))) + now = time.Now() + } + log.Printf("ListenMQ : Consuming msg\n") + + } +} + +func ListenMe(c *tdlib.Client) { + eventFilter := func(msg *tdlib.TdMessage) bool { + updateMsg := (*msg).(*tdlib.UpdateNewMessage) + senderUserID := updateMsg.Message.SenderUserID + if senderUserID == ownUserID32 { + return true + } else { + return false + } + } + receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100) for newMsg := range receiver.Chan { updateMsg := (newMsg).(*tdlib.UpdateNewMessage)