package main import ( "encoding/json" "log" "strconv" "time" "github.com/streadway/amqp" ) func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) { var x ChatWarsMessage conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue) failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ") defer conn.Close() ch, err := conn.Channel() failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to open a channel") defer ch.Close() q, err := ch.QueueDeclare( "msg", // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a queue") m, err := ch.Consume( q.Name, // queue "", // consumer true, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args ) failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to register a consumer") for d := range m { // log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body)) err = json.Unmarshal(d.Body, &x) logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body)) if err == nil { msgs <- x } } log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.") } func SQLCWMsgWorker(id int, msgs <-chan ChatWarsMessage, objIds chan<- int64) { log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Starting.") for m := range msgs { objId, err := putUnprocessedMsg(m) logOnError(err, "SQLCWMsgWorker["+strconv.Itoa(id)+"] : Inserting message.") if err == nil && objId != 0 { // log.Printf("SQLCWMsgWorker["+strconv.Itoa(id)+"] : Message inserted (%d).\n", objId) objIds <- objId } } log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.") } 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) 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) for i = 0; i < len(msgParsingRules) && msgParsingRules[i].re.FindStringSubmatch(m.Text) == nil; i++ { } if i == len(msgParsingRules) { log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Can't find message type in rules(%d)\n%s\n", objId, m.Text) } else { r := msgParsingRules[i].re switch msgParsingRules[i].MsgTypeID { case objSubTypeMessageReport: log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : War report identified (%d)\n", objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Castle : %s (%d)\n", r.ReplaceAllString(m.Text, "${Castle}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Guild : %s (%d)\n", r.ReplaceAllString(m.Text, "${Guild}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : User : %s (%d)\n", r.ReplaceAllString(m.Text, "${User}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Attack : %s (%d)\n", r.ReplaceAllString(m.Text, "${Attack}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : AttackMod : %s (%d)\n", r.ReplaceAllString(m.Text, "${AttackMod}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Defense : %s (%d)\n", r.ReplaceAllString(m.Text, "${Defense}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Level : %s (%d)\n", r.ReplaceAllString(m.Text, "${Level}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Exp : %s (%d)\n", r.ReplaceAllString(m.Text, "${Exp}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Gold : %s (%d)\n", r.ReplaceAllString(m.Text, "${Gold}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Stock : %s (%d)\n", r.ReplaceAllString(m.Text, "${Stock}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Stamina : %s (%d)\n", r.ReplaceAllString(m.Text, "${Stamina}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Crit : %s (%d)\n", r.ReplaceAllString(m.Text, "${Crit}"), objId) case objSubTypeMessageMiniWar: t, err := time.Parse("02/01/06 15:04", r.ReplaceAllString(m.Text, "${Time}")) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : objSubTypeMessageMiniWar parsing time : "+r.ReplaceAllString(m.Text, "${Time}")) cwm := ChatWarsMessageMiniWar{ Time: t, Report: make(map[string]ChatWarsMessageMiniWarCastle), } g, err := strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold1}")) s, err := strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock1}")) rep := ChatWarsMessageMiniWarCastle{ Gardian: r.ReplaceAllString(m.Text, "${Gardian1}"), Result: r.ReplaceAllString(m.Text, "${Result1}"), Gold: int32(g), Stock: int32(s), } cwm.Report[r.ReplaceAllString(m.Text, "${Loot1}")] = rep g, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold2}")) s, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock2}")) rep = ChatWarsMessageMiniWarCastle{ Gardian: r.ReplaceAllString(m.Text, "${Gardian2}"), Result: r.ReplaceAllString(m.Text, "${Result2}"), Gold: int32(g), Stock: int32(s), } cwm.Report[r.ReplaceAllString(m.Text, "${Loot2}")] = rep g, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold3}")) s, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock3}")) rep = ChatWarsMessageMiniWarCastle{ Gardian: r.ReplaceAllString(m.Text, "${Gardian3}"), Result: r.ReplaceAllString(m.Text, "${Result3}"), Gold: int32(g), Stock: int32(s), } cwm.Report[r.ReplaceAllString(m.Text, "${Loot3}")] = rep g, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold4}")) s, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock4}")) rep = ChatWarsMessageMiniWarCastle{ Gardian: r.ReplaceAllString(m.Text, "${Gardian4}"), Result: r.ReplaceAllString(m.Text, "${Result4}"), Gold: int32(g), Stock: int32(s), } cwm.Report[r.ReplaceAllString(m.Text, "${Loot4}")] = rep g, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold5}")) s, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock5}")) rep = ChatWarsMessageMiniWarCastle{ Gardian: r.ReplaceAllString(m.Text, "${Gardian5}"), Result: r.ReplaceAllString(m.Text, "${Result5}"), Gold: int32(g), Stock: int32(s), } cwm.Report[r.ReplaceAllString(m.Text, "${Loot5}")] = rep g, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold6}")) s, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock6}")) rep = ChatWarsMessageMiniWarCastle{ Gardian: r.ReplaceAllString(m.Text, "${Gardian6}"), Result: r.ReplaceAllString(m.Text, "${Result6}"), Gold: int32(g), Stock: int32(s), } cwm.Report[r.ReplaceAllString(m.Text, "${Loot6}")] = rep g, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold7}")) s, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock7}")) rep = ChatWarsMessageMiniWarCastle{ Gardian: r.ReplaceAllString(m.Text, "${Gardian7}"), Result: r.ReplaceAllString(m.Text, "${Result7}"), Gold: int32(g), Stock: int32(s), } cwm.Report[r.ReplaceAllString(m.Text, "${Loot7}")] = rep //b, err := json.Marshal(cwm) if (len(r.ReplaceAllString(m.Text, "${Loot1}")) < 6 || len(r.ReplaceAllString(m.Text, "${Loot1}")) > 11) || (len(r.ReplaceAllString(m.Text, "${Loot2}")) < 6 || len(r.ReplaceAllString(m.Text, "${Loot2}")) > 11) { log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] :\n%s\n", m.Text) } /*else { log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : objSubTypeMessageMiniWar\n%s\n", string(b)) } /* if err == nil { log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : War time : %s (%d)\n", r.ReplaceAllString(m.Text, "${Time}"), objId) } */ case objSubTypeMessageAuctionAnnounce: cwm := ChatWarsMessageAuctionAnnounce{ ObjID64: objId, } l, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Lot}"), 10, 32) cwm.LotID = int32(l) cwm.Item = r.ReplaceAllString(m.Text, "${Item}") cwm.Cond = r.ReplaceAllString(m.Text, "${Cond}") cwm.Quality = r.ReplaceAllString(m.Text, "${Quality}") cwm.Seller = r.ReplaceAllString(m.Text, "${Seller}") cwm.Buyer = r.ReplaceAllString(m.Text, "${Buyer}") p, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Price}"), 10, 32) cwm.Price = int32(p) cwm.Status = r.ReplaceAllString(m.Text, "${Status}") cwm.End, _ = fromChatWarsDate(r.ReplaceAllString(m.Text, "${End}")) err = insertMsgAuctionAnnounce(&cwm) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : AuctionAnnounce") err = setObjSubTypeId(objId, objSubTypeMessageAuctionAnnounce) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(AuctionAnnounce)") case objSubTypeMessageTimeAck: /* log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Time (%d)\n", objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Time : %s (%d)\n", r.ReplaceAllString(m.Text, "${Time}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Hour : %s (%d)\n", r.ReplaceAllString(m.Text, "${Hour}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Minute : %s (%d)\n", r.ReplaceAllString(m.Text, "${Minute}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Day : %s (%d)\n", r.ReplaceAllString(m.Text, "${Day}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Month : %s (%d)\n", r.ReplaceAllString(m.Text, "${Month}"), objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Year : %s (%d)\n", r.ReplaceAllString(m.Text, "${Year}"), objId) */ _, 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") err = setObjSubTypeId(objId, objSubTypeMessageTimeAck) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(objSubTypeMessageTimeAck)") default: log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Unknwon message type in rule %d : %d (%d)\n%s\n", msgParsingRules[i].ID, msgParsingRules[i].MsgTypeID, objId, m.Text) } } } } log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Closing.") } func SQLJobWorker(id int) { log.Printf("SQLJobWorker[" + strconv.Itoa(id) + "] : Starting.") for true { jobs, err := loadCurrentJobs() logOnError(err, "SQLJobWorker["+strconv.Itoa(id)+"] : loadCurrentJobs") if len(jobs) > 0 { log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : %d jobs.\n", len(jobs)) } for _, j := range jobs { switch j.JobTypeID { case objSubTypeJobRescanMsg: jobRescan(j) case objSubTypeJobSetJobDone: jobSetDone(j) default: log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID) } } if len(jobs) < SQLJobSliceSize { time.Sleep(100 * time.Millisecond) } } log.Printf("SQLJobWorker[" + strconv.Itoa(id) + "] : Closing.") } /* func BotMsgWorker(id int, botMsg <-chan */