chirpnest/msg.go
2020-10-20 11:07:52 +02:00

760 lines
33 KiB
Go

package main
import (
"errors"
"log"
"regexp"
"strconv"
"strings"
"time"
)
func getMsgParsingRule(m *ChatWarsMessage) (*MessageParsingRule, error) {
var i int
for i = 0; i < len(msgParsingRules); i++ {
if msgParsingRules[i].re.FindStringSubmatch(m.Text) != nil {
if (msgParsingRules[i].ChatID64 == 0 || msgParsingRules[i].ChatID64 == m.ChatID64) && (msgParsingRules[i].SenderUserID64 == 0 || msgParsingRules[i].SenderUserID64 == m.TGSenderUserID64) {
break
}
}
}
if i == len(msgParsingRules) {
return nil, errors.New("Can't find message type in rules.")
} else {
r := msgParsingRules[i]
return &r, nil
}
}
func parseSubTypeMessageInspectAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageInspectAck, error) {
cwm := ChatWarsMessageInspectAck{}
cwm.Name = r.ReplaceAllString(m.Text, "${Item}")
log.Printf("parseSubTypeMessageInspectAck : Name : %s\n", cwm.Name)
return &cwm, nil
}
func parseSubTypeMessageExchangeAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageExchangeAck, error) {
cwm := ChatWarsMessageExchangeAck{}
deals := []ChatWarsExchangeDeal{}
log.Printf("parseSubTypeMessageExchangeAck : ActiveDeals : %s\n", r.ReplaceAllString(m.Text, "${ActiveDeals}"))
log.Printf("parseSubTypeMessageExchangeAck : MaxDeals : %s\n", r.ReplaceAllString(m.Text, "${MaxDeals}"))
cwm.ActiveDeals, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${ActiveDeals}"), 10, 64)
cwm.MaxDeals, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${MaxDeals}"), 10, 64)
if cwm.ActiveDeals > 0 {
re := regexp.MustCompile(`([a-zA-Z ]+)\n([0-9]+) x ([0-9]+)💰 \[(.*)\] (.*)\n`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Deals}"), -1) {
i := getObjItemID(``, l[1])
q, _ := strconv.ParseInt(l[2], 10, 64)
p, _ := strconv.ParseInt(l[3], 10, 64)
l := l[4]
if i != 0 {
deal := ChatWarsExchangeDeal{
ItemID64: i,
Quantity: q,
Price: p,
Link: l,
}
deals = append(deals, deal)
}
}
}
cwm.DealList = deals
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageQuestResult(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageQuestResult, error) {
cwm := ChatWarsMessageQuestResult{}
/* look for 351 go_quest_ack, etc .. */
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageStockAnyAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageStockAnyAck, error) {
cwm := ChatWarsMessageStockAnyAck{}
items := []ChatWarsItems{}
re := regexp.MustCompile(`(📃){0,1}(['a-zA-Z ]+) \(([0-9]+)\)( /view_[a-z0-9]+){0,1}\n{0,1}`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Items}"), -1) {
// fmt.Printf("parseSubTypeMessageStockAnyAck : %+q\n", l)
i := getObjItemID(``, l[2])
q, _ := strconv.ParseInt(l[3], 10, 64)
if i != 0 {
item := ChatWarsItems{
ItemID64: i,
Quantity: q,
}
items = append(items, item)
}
}
cwm.Stock = items
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageAlchStockAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageStockAnyAck, error) {
cwm := ChatWarsMessageStockAnyAck{}
items := []ChatWarsItems{}
re := regexp.MustCompile(`\n\/aa_([a-z0-9]{2,3}) ([a-zA-Z ']+) x ([0-9]+)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Stuff}"), -1) {
i := getObjItemID(l[1], ``)
q, _ := strconv.ParseInt(l[3], 10, 64)
if i != 0 {
item := ChatWarsItems{
ItemID64: i,
Quantity: q,
}
items = append(items, item)
}
}
cwm.Stock = items
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageGDepositReq(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGDepositReq, error) {
cwm := ChatWarsMessageGDepositReq{}
cwm.ItemID64 = getObjItemID(r.ReplaceAllString(m.Text, "${Item}"), ``) // FIXME, handle uXXX items ?
cwm.Quantity, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Quantity}"), 10, 64)
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageGDepositAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGDepositAck, error) {
cwm := ChatWarsMessageGDepositAck{}
cwm.ItemID64 = getObjItemID(``, r.ReplaceAllString(m.Text, "${Item}"))
cwm.Quantity, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Quantity}"), 10, 64)
gDepositForwardMux.Lock()
for i := range gDepositForwardMsg {
msg, err := getObjMsg(gDepositForwardMsg[i])
logOnError(err, "parseSubTypeMessageGDepositAck : gDepositForwardMsg")
if err == nil && m.TGUserID64 == msg.TGUserID64 {
rule, err := getMsgParsingRule(msg)
logOnError(err, "parseSubTypeMessageGDepositAck : getMsgParsingRule")
cwmreq, err := parseSubTypeMessageGDepositReq(msg, rule.re)
logOnError(err, "parseSubTypeMessageGDepositAck : parseSubTypeMessageGDepositReq")
if err == nil && cwmreq.ItemID64 == cwm.ItemID64 && cwmreq.Quantity == cwm.Quantity {
if m.Date.UTC().Before(msg.Date.UTC().Add(1 * time.Minute)) {
clientFwdCWMsg(msg.TGUserID64, m.ID64, m.ChatID64, cfg.Bot.Depositchat)
}
gDepositForwardMsg[i] = gDepositForwardMsg[len(gDepositForwardMsg)-1]
gDepositForwardMsg = gDepositForwardMsg[:len(gDepositForwardMsg)-1]
break
}
}
}
gDepositForwardMux.Unlock()
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageStockAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageStockAck, error) {
cwm := ChatWarsMessageStockAck{}
items := []ChatWarsItems{}
re := regexp.MustCompile(`\n([a-zA-Z ❄️💘']+) \(([0-9]+)\)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Items}"), -1) {
i := getObjItemID(``, l[1])
q, _ := strconv.ParseInt(l[2], 10, 64)
if i != 0 {
item := ChatWarsItems{
ItemID64: i,
Quantity: q,
}
items = append(items, item)
}
}
re = regexp.MustCompile(`\n\/sg_[0-9]+ ([^\(]+) \(([0-9]+)\)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Items}"), -1) {
i := getObjItemID(``, l[1])
q, _ := strconv.ParseInt(l[2], 10, 64)
if i != 0 {
item := ChatWarsItems{
ItemID64: i,
Quantity: q,
}
items = append(items, item)
}
}
cwm.Used, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Used}"), 10, 64)
cwm.Available, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Available}"), 10, 64)
cwm.Stock = items
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageReportAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageReportAck, error) {
cwm := ChatWarsMessageReportAck{}
re := regexp.MustCompile(`(` +
`(?P<Cry>Your battle cries were successful)|` +
`(You were outplayed by (?P<Outplayer>.*))|` +
`(?P<Stamina>🔋Stamina restored)|` +
`(🏅Peacekeeping (?P<Peacekeeping>.*))|` +
`(🏅Enraged (?P<Enraged>.*))|` +
`(?P<Crit>⚡Critical strike)|` +
`(Lost\: (?P<Lost>.*))|` +
`(You were healed by (?P<Healer>.*))|` +
`(You were poisoned by (?P<Poisoner>.*))|` +
`(Got hit by an arrow from (?P<Archer>.*), (?P<Skill>.*) skill decreased)|` +
`(Got hit by an arrow from (?P<Archer>.*))|` +
`(Your (?P<Broken>.*) has been broken)|` +
`(⚡Battle Cry. You were inspired by (?P<CryInspirer>.*))|` +
`.+)`)
for i, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Extra}"), -1) {
if len(re.ReplaceAllString(l[0], "${Cry}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Cry\n", i)
} else if len(re.ReplaceAllString(l[0], "${Outplayer}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Outplayer\n", i)
} else if len(re.ReplaceAllString(l[0], "${Stamina}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Stamina\n", i)
} else if len(re.ReplaceAllString(l[0], "${Peacekeeping}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Peacekeeping\n", i)
} else if len(re.ReplaceAllString(l[0], "${Enraged}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Enraged\n", i)
} else if len(re.ReplaceAllString(l[0], "${Crit}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Crit\n", i)
} else if len(re.ReplaceAllString(l[0], "${Lost}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Lost item : %s\n", i, re.ReplaceAllString(l[0], "${Lost}"))
} else if len(re.ReplaceAllString(l[0], "${Healer}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Healer : %s\n", i, re.ReplaceAllString(l[0], "${Healer}"))
} else if len(re.ReplaceAllString(l[0], "${Poisoner}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Poisoner : %s\n", i, re.ReplaceAllString(l[0], "${Poisoner}"))
} else if len(re.ReplaceAllString(l[0], "${Archer}")) != 0 && len(re.ReplaceAllString(l[0], "${Skill}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Archer : %s / Skill : %s\n", i, re.ReplaceAllString(l[0], "${Archer}"), re.ReplaceAllString(l[0], "${Skill}"))
} else if len(re.ReplaceAllString(l[0], "${Archer}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Archer : %s\n", i, re.ReplaceAllString(l[0], "${Archer}"))
} else if len(re.ReplaceAllString(l[0], "${Broken}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : Broken\n", i)
} else if len(re.ReplaceAllString(l[0], "${CryInspirer}")) != 0 {
// log.Printf("parseSubTypeMessageReport : Extra[%d] : CryInspirer\n", i)
} else {
log.Printf("parseSubTypeMessageReport : Extra[%d] : %s\n", i, l[0])
}
}
/*
log.Printf("parseSubTypeMessageReport : War report identified\n")
log.Printf("parseSubTypeMessageReport : Castle : %s\n", r.ReplaceAllString(m.Text, "${Castle}"))
log.Printf("parseSubTypeMessageReport : Guild : %s\n", r.ReplaceAllString(m.Text, "${Guild}"))
log.Printf("parseSubTypeMessageReport : User : %s\n", r.ReplaceAllString(m.Text, "${User}"))
log.Printf("parseSubTypeMessageReport : Attack : %s\n", r.ReplaceAllString(m.Text, "${Attack}"))
log.Printf("parseSubTypeMessageReport : AttackMod : %s\n", r.ReplaceAllString(m.Text, "${AttackMod}"))
log.Printf("parseSubTypeMessageReport : Defense : %s\n", r.ReplaceAllString(m.Text, "${Defense}"))
log.Printf("parseSubTypeMessageReport : Level : %s\n", r.ReplaceAllString(m.Text, "${Level}"))
log.Printf("parseSubTypeMessageReport : Exp : %s\n", r.ReplaceAllString(m.Text, "${Exp}"))
log.Printf("parseSubTypeMessageReport : Gold : %s\n", r.ReplaceAllString(m.Text, "${Gold}"))
log.Printf("parseSubTypeMessageReport : Stock : %s\n", r.ReplaceAllString(m.Text, "${Stock}"))
log.Printf("parseSubTypeMessageReport : Stamina : %s\n", r.ReplaceAllString(m.Text, "${Stamina}"))
log.Printf("parseSubTypeMessageReport : Crit : %s\n", r.ReplaceAllString(m.Text, "${Crit}"))
*/
return &cwm, nil
}
func parseSubTypeMessageOrderbookAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageOrderbookAck, error) {
cwm := ChatWarsMessageOrderbookAck{}
/*
log.Printf("parseSubTypeMessageOrderbookAck : Orderbook identified\n")
log.Printf("parseSubTypeMessageOrderbookAck : Name : %s\n", r.ReplaceAllString(m.Text, "${Name}"))
log.Printf("parseSubTypeMessageOrderbookAck : Code : %s\n", r.ReplaceAllString(m.Text, "${Code}"))
log.Printf("parseSubTypeMessageOrderbookAck : Qty : %s\n", r.ReplaceAllString(m.Text, "${Qty}"))
log.Printf("parseSubTypeMessageOrderbookAck : Gold : %s\n", r.ReplaceAllString(m.Text, "${Gold}"))
log.Printf("parseSubTypeMessageOrderbookAck : Qty1 : %s\n", r.ReplaceAllString(m.Text, "${Qty1}"))
log.Printf("parseSubTypeMessageOrderbookAck : Price1 : %s\n", r.ReplaceAllString(m.Text, "${Price1}"))
log.Printf("parseSubTypeMessageOrderbookAck : Qty2 : %s\n", r.ReplaceAllString(m.Text, "${Qty2}"))
log.Printf("parseSubTypeMessageOrderbookAck : Price2 : %s\n", r.ReplaceAllString(m.Text, "${Price2}"))
log.Printf("parseSubTypeMessageOrderbookAck : Qty3 : %s\n", r.ReplaceAllString(m.Text, "${Qty3}"))
log.Printf("parseSubTypeMessageOrderbookAck : Price3 : %s\n", r.ReplaceAllString(m.Text, "${Price3}"))
*/
return &cwm, nil
}
func parseSubTypeMessageAuctionAnnounce(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageAuctionAnnounce, error) {
cwm := ChatWarsMessageAuctionAnnounce{}
l, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Lot}"), 10, 32)
cwm.LotID = int32(l)
cwm.ItemID64 = getObjItemID(``, r.ReplaceAllString(m.Text, "${Item}"))
cwm.Cond = r.ReplaceAllString(m.Text, "${Cond}")
cwm.Mod = r.ReplaceAllString(m.Text, "${Mod}")
cwm.Quality = r.ReplaceAllString(m.Text, "${Quality}")
switch cwm.Quality {
case `Fine`:
cwm.Quality = `e`
case `High`:
cwm.Quality = `d`
case `Great`:
cwm.Quality = `c`
case `Excellent`:
cwm.Quality = `b`
case `Masterpiece`:
cwm.Quality = `a`
case `Epic Fine`:
cwm.Quality = `se`
case `Epic High`:
cwm.Quality = `sd`
default:
cwm.Quality = ``
}
cwm.SellerUserID64 = getObjUserID(r.ReplaceAllString(m.Text, "${SellerUser}"))
cwm.SellerGuildID64 = getObjGuildID(r.ReplaceAllString(m.Text, "${SellerGuild}"))
cwm.SellerCastleID64 = getObjCastleID(r.ReplaceAllString(m.Text, "${SellerCastle}"))
cwm.BuyerUserID64 = getObjUserID(r.ReplaceAllString(m.Text, "${BuyerUser}"))
cwm.BuyerGuildID64 = getObjGuildID(r.ReplaceAllString(m.Text, "${BuyerGuild}"))
cwm.BuyerCastleID64 = getObjCastleID(r.ReplaceAllString(m.Text, "${BuyerCastle}"))
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}"))
return &cwm, nil
}
func parseSubTypeMessageWithdrawRcv(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageWithdrawRcv, error) {
cwm := ChatWarsMessageWithdrawRcv{}
items := []ChatWarsItems{}
re := regexp.MustCompile(`(.*) x (.*)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Items}"), -1) {
i := getObjItemID(``, l[1])
q, _ := strconv.ParseInt(l[2], 10, 64)
if i != 0 {
item := ChatWarsItems{
ItemID64: i,
Quantity: q,
}
items = append(items, item)
}
}
cwm.ItemList = items
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageGStockAnyAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGStock, error) {
cwm := ChatWarsMessageGStock{}
stock := []ChatWarsItems{}
re := regexp.MustCompile(`\n([0-9a-z]+) (.*) x ([0-9]+)`)
rq := regexp.MustCompile(`[a-z][0-9]+(?P<Quality>[a-z]+)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Stock}"), -1) {
i := getObjItemID(l[1], l[2])
q, _ := strconv.ParseInt(l[3], 10, 64)
if i != 0 {
item := ChatWarsItems{
Code: l[1],
Name: l[2],
ItemID64: i,
Quantity: q,
}
if rq.MatchString(l[1]) {
item.Quality = rq.ReplaceAllString(l[1], "${Quality}")
}
stock = append(stock, item)
}
}
re = regexp.MustCompile(`\n(u[0-9]+)([ᵃᵇᶜᵈᵉ]{0,1}) (.*)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Stock}"), -1) {
i := getObjItemID(``, l[3])
if i != 0 {
item := ChatWarsItems{
Code: l[1],
Name: l[3],
ItemID64: i,
Quantity: 1,
Quality: l[2],
}
switch l[2] {
case ``:
item.Quality = `a`
case ``:
item.Quality = `b`
case ``:
item.Quality = `c`
case ``:
item.Quality = `d`
case ``:
item.Quality = `e`
default:
item.Quality = ``
}
stock = append(stock, item)
}
}
cwm.Stock = stock
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageTributesStatsAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageTributesStatsAck, error) {
cwm := ChatWarsMessageTributesStatsAck{}
cwm.Msg = m
tributes := []ChatWarsTribute{}
re := regexp.MustCompile(`(\n(?P<Date>[1-9]{2} [a-zA-Z]+ [0-9]{4} [0-9]{2}:[0-9]{2}) - (?P<Item>[a-zA-Z ]+) x (?P<Quantity>[0-9]+), (?P<Exp>[0-9]+)🔥)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Tributes}"), -1) {
tribute := ChatWarsTribute{}
d, err := fromChatWarsDate(re.ReplaceAllString(l[0], "${Date}"))
logOnError(err, "parseSubTypeMessageTributesStatsAck : fromChatWarsDate")
tribute.Date = d
tribute.ItemID64 = getSilentObjItemID(``, re.ReplaceAllString(l[0], "${Item}"))
tribute.Quantity, err = strconv.ParseInt(re.ReplaceAllString(l[0], "${Quantity}"), 10, 64)
logOnError(err, "parseSubTypeMessageTributesStatsAck : ParseInt")
tribute.Exp, err = strconv.ParseInt(re.ReplaceAllString(l[0], "${Exp}"), 10, 64)
logOnError(err, "parseSubTypeMessageTributesStatsAck : ParseInt")
tributes = append(tributes, tribute)
}
cwm.Tributes = tributes
return &cwm, nil
}
func parseSubTypeMessageShopMainReq(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageShopMainReq, error) {
cwm := ChatWarsMessageShopMainReq{}
cwm.Msg = m
cwm.Link = r.ReplaceAllString(m.Text, "${Link}")
log.Printf("parseSubTypeMessageShopMainReq : Link : %s\n", cwm.Link)
return &cwm, nil
}
func parseSubTypeMessageShopMainAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageShopMainAck, error) {
cwm := ChatWarsMessageShopMainAck{}
cwm.Msg = m
cwm.Name = r.ReplaceAllString(m.Text, "${Name}")
cwm.ShopNumber, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Number}"), 10, 64)
cwm.Link = r.ReplaceAllString(m.Text, "${Link}")
cwm.User = r.ReplaceAllString(m.Text, "${User}")
cwm.Mana, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Mana}"), 10, 64)
cwm.ManaTotal, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${ManaTotal}"), 10, 64)
cwm.Class = r.ReplaceAllString(m.Text, "${Class}")
cwm.CastleID64 = getObjCastleID(r.ReplaceAllString(m.Text, "${Castle}"))
cwm.Fees = r.ReplaceAllString(m.Text, "${Fees}")
cwm.Guru = r.ReplaceAllString(m.Text, "${Guru}")
if r.ReplaceAllString(m.Text, "${Open}") == `open` {
cwm.Open = true
} else {
cwm.Open = false
}
/*
log.Printf("parseSubTypeMessageShopMainAck : Name : %s\n", cwm.Name)
log.Printf("parseSubTypeMessageShopMainAck : Number : %d\n", cwm.ShopNumber)
log.Printf("parseSubTypeMessageShopMainAck : Link : %s\n", cwm.Link)
log.Printf("parseSubTypeMessageShopMainAck : User : %s\n", cwm.User)
log.Printf("parseSubTypeMessageShopMainAck : Mana : %d\n", cwm.Mana)
log.Printf("parseSubTypeMessageShopMainAck : ManaTotal : %d\n", cwm.ManaTotal)
log.Printf("parseSubTypeMessageShopMainAck : Class : %s\n", cwm.Class)
log.Printf("parseSubTypeMessageShopMainAck : Castle : %d\n", cwm.CastleID64)
log.Printf("parseSubTypeMessageShopMainAck : Fees : %s\n", cwm.Fees)
log.Printf("parseSubTypeMessageShopMainAck : Guru : %s\n", cwm.Guru)
log.Printf("parseSubTypeMessageShopMainAck : Status : %v\n", cwm.Open)
*/
return &cwm, nil
}
func parseSubTypeMessageGListAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGListAck, error) {
cwm := ChatWarsMessageGListAck{}
cwm.Msg = m
cwm.Members = make([]ChatWarsGuildMember, 0)
//log.Printf("parseSubTypeMessageGListAck : Guild : %s\n", r.ReplaceAllString(m.Text, "${GuildName}"))
// log.Printf("parseSubTypeMessageGListAck : Members : %s\n", r.ReplaceAllString(m.Text, "${Members}"))
re := regexp.MustCompile(`\n#(?P<Id>[0-9]+) (?P<Class1>(⚒|⚗️|📦|🏹|⚔️|🛡))(?P<Class2>(⚒|⚗️|📦|🏹|⚔️|🛡){0,1})(?P<Level>[0-9]+) \[(?P<Status>.*)\] (?P<Name>[a-zA-z0-9 ]+)`)
for _, l := range re.FindAllString(r.ReplaceAllString(m.Text, "${Members}"), -1) {
//log.Printf("parseSubTypeMessageGListAck : %s\n", l)
m := ChatWarsGuildMember{}
m.Name = re.ReplaceAllString(l, "${Name}")
m.Class1 = re.ReplaceAllString(l, "${Class1}")
m.Class2 = re.ReplaceAllString(l, "${Class2}")
m.Status = re.ReplaceAllString(l, "${Status}")
i, _ := strconv.ParseInt(re.ReplaceAllString(l, "${Level}"), 10, 64)
m.Level = int(i)
//log.Printf("parseSubTypeMessageGListAck : %s : %s/%s [%d] => %s\n", m.Name, m.Class1, m.Class2, m.Level, m.Status)
cwm.Members = append(cwm.Members, m)
}
return &cwm, nil
}
func parseSubTypeMessageGDefListAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGDefListAck, error) {
cwm := ChatWarsMessageGDefListAck{}
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageGAtkListAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGAtkListAck, error) {
cwm := ChatWarsMessageGAtkListAck{}
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageMeAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageMeAck, error) {
var (
i int64
err error
)
cwm := ChatWarsMessageMeAck{
CWUserID64: getObjUserID(r.ReplaceAllString(m.Text, "${Player}")),
CWGuildID64: getObjGuildID(r.ReplaceAllString(m.Text, "${Guild}")),
State: r.ReplaceAllString(m.Text, "${State}"),
Class: r.ReplaceAllString(m.Text, "${Class}"),
}
i, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${ExpNow}"), 10, 64)
cwm.ExpNow = i
i, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${ExpLvl}"), 10, 64)
cwm.ExpLvl = i
i, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Level}"), 10, 64)
cwm.Level = i
i, err = strconv.ParseInt(r.ReplaceAllString(m.Text, "${ManaNow}"), 10, 64)
if err != nil {
cwm.ManaNow = 0
} else {
cwm.ManaNow = i
}
i, err = strconv.ParseInt(r.ReplaceAllString(m.Text, "${ManaMax}"), 10, 64)
if err != nil {
cwm.ManaMax = 0
} else {
cwm.ManaMax = i
}
/*
log.Printf("parseSubTypeMessageMeAck : Me report identified\n")
log.Printf("parseSubTypeMessageMeAck : Castle : %s\n", r.ReplaceAllString(m.Text, "${Castle}"))
log.Printf("parseSubTypeMessageMeAck : Level : %s\n", r.ReplaceAllString(m.Text, "${Level}"))
log.Printf("parseSubTypeMessageMeAck : Class : %s\n", cwm.Class)
log.Printf("parseSubTypeMessageMeAck : Atk : %s\n", r.ReplaceAllString(m.Text, "${Atk}"))
log.Printf("parseSubTypeMessageMeAck : Def : %s\n", r.ReplaceAllString(m.Text, "${Def}"))
log.Printf("parseSubTypeMessageMeAck : ExpNow : %s\n", r.ReplaceAllString(m.Text, "${ExpNow}"))
log.Printf("parseSubTypeMessageMeAck : ExpLvl : %s\n", r.ReplaceAllString(m.Text, "${ExpLvl}"))
log.Printf("parseSubTypeMessageMeAck : StaminaNow : %s\n", r.ReplaceAllString(m.Text, "${StaminaNow}"))
log.Printf("parseSubTypeMessageMeAck : StaminaMax : %s\n", r.ReplaceAllString(m.Text, "${StaminaMax}"))
log.Printf("parseSubTypeMessageMeAck : ManaNow : %s\n", r.ReplaceAllString(m.Text, "${ManaNow}"))
log.Printf("parseSubTypeMessageMeAck : ManaMax : %s\n", r.ReplaceAllString(m.Text, "${ManaMax}"))
log.Printf("parseSubTypeMessageMeAck : Gold : %s\n", r.ReplaceAllString(m.Text, "${Gold}"))
log.Printf("parseSubTypeMessageMeAck : Pogs : %s\n", r.ReplaceAllString(m.Text, "${Pogs}"))
log.Printf("parseSubTypeMessageMeAck : Gems : %s\n", r.ReplaceAllString(m.Text, "${Gems}"))
log.Printf("parseSubTypeMessageMeAck : AtkGear : %s\n", r.ReplaceAllString(m.Text, "${AtkGear}"))
log.Printf("parseSubTypeMessageMeAck : DefGear : %s\n", r.ReplaceAllString(m.Text, "${DefGear}"))
log.Printf("parseSubTypeMessageMeAck : BagUsed : %s\n", r.ReplaceAllString(m.Text, "${BagUsed}"))
log.Printf("parseSubTypeMessageMeAck : BagDispo : %s\n", r.ReplaceAllString(m.Text, "${BagDispo}"))
*/
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageGRolesAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGRolesAck, error) {
cwm := ChatWarsMessageGRolesAck{
Msg: m,
}
/*
BartenderID64: getObjUserID(r.ReplaceAllString(m.Text, "${Bartender}")),
CommanderID64: getObjUserID(r.ReplaceAllString(m.Text, "${Commander}")),
SquireID64: getObjUserID(r.ReplaceAllString(m.Text, "${Squire}")),
TreasurerID64: getObjUserID(r.ReplaceAllString(m.Text, "${Treasurer}")),
*/
if len(r.ReplaceAllString(m.Text, "${Bartender}")) > 0 {
cwm.BartenderID64 = getObjUserID(r.ReplaceAllString(m.Text, "${Bartender}"))
} else {
cwm.BartenderID64 = 0
}
if len(r.ReplaceAllString(m.Text, "${Commander}")) > 0 {
cwm.CommanderID64 = getObjUserID(r.ReplaceAllString(m.Text, "${Commander}"))
} else {
cwm.CommanderID64 = 0
}
if len(r.ReplaceAllString(m.Text, "${Squire}")) > 0 {
cwm.SquireID64 = getObjUserID(r.ReplaceAllString(m.Text, "${Squire}"))
} else {
cwm.SquireID64 = 0
}
if len(r.ReplaceAllString(m.Text, "${Treasurer}")) > 0 {
cwm.TreasurerID64 = getObjUserID(r.ReplaceAllString(m.Text, "${Treasurer}"))
} else {
cwm.TreasurerID64 = 0
}
/*
log.Printf("parseSubTypeMessageGRolesAck : Guild roles Ack identified\n")
log.Printf("parseSubTypeMessageGRolesAck : Bartender : %s\n", r.ReplaceAllString(m.Text, "${Bartender}"))
log.Printf("parseSubTypeMessageGRolesAck : Commander : %s\n", r.ReplaceAllString(m.Text, "${Commander}"))
log.Printf("parseSubTypeMessageGRolesAck : Squire : %s\n", r.ReplaceAllString(m.Text, "${Squire}"))
log.Printf("parseSubTypeMessageGRolesAck : Treasurer : %s\n", r.ReplaceAllString(m.Text, "${Treasurer}"))
log.Printf("cwm.BartenderID64 : %d\n", cwm.BartenderID64)
log.Printf("cwm.CommanderID64 : %d\n", cwm.CommanderID64)
log.Printf("cwm.SquireID64 : %d\n", cwm.SquireID64)
log.Printf("cwm.TreasurerID64 : %d\n", cwm.TreasurerID64)
*/
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageGoQuestAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGoQuestAck, error) {
cwm := ChatWarsMessageGoQuestAck{}
/*
log.Printf("parseSubTypeMessageGoQuestAck : Go Quest Ack report identified\n")
log.Printf("parseSubTypeMessageGoQuestAck : Place : %s\n", r.ReplaceAllString(m.Text, "${Place}"))
log.Printf("parseSubTypeMessageGoQuestAck : Time : %s\n", r.ReplaceAllString(m.Text, "${Time}"))
*/
switch quest := strings.ToLower(r.ReplaceAllString(m.Text, "${Place}")); quest {
case "forest":
cwm.QuestTypeID64 = cacheObjSubType[`quest_forest`]
case "mountains":
cwm.QuestTypeID64 = cacheObjSubType[`quest_mountains`]
default:
log.Printf("parseSubTypeMessageGoQuestAck : no reference for quest `%s`\n", quest)
cwm.QuestTypeID64 = cacheObjSubType[`quest_forest`]
}
i, err := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Time}"), 10, 64)
if err == nil {
cwm.Duration = time.Duration(i) * time.Minute
} else {
cwm.Duration = 0 * time.Minute
}
cwm.Msg = m
return &cwm, nil
}
func parseSubTypeMessageUnionWar(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageUnionWar, error) {
//re := regexp.MustCompile('^(🏠Trade union news:\\n)(?P<Union>(?s:[^(🎪State of exclusive rights)]*))(🎪State of exclusive rights:)(?P<Fair>(?s:.*))$')
/*
"^(🏠Trade union news:\\n){0,1}` +
`(((?P<Union>[A-Z][a-z]+ [A-Z][a-z]+) was (?P<Status>(easily defended|defended successfully|breached.|easily breached.|closely breached.))(Attackers have pillage union for (?<Loot>[0-9]+) stock):\\n` +
`(🎖Attack leaders: (?P<AtkCastle1>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser1>[^,]+)(,(?P<AtkCastle2>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser2>[^,]+)){0,1}(,(?P<AtkCastle3>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser3>[^,]+)){0,1}(,(?P<AtkCastle4>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser4>[^,]+)){0,1}){0,1}(\\n){0,1}` +
`(🎖Defense leaders: (?P<DefCastle1>[🐉🦅🐺🦈🦌🥔🌑])(?P<DefUser1>[^,]+)(,(?P<DefCastle2>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser2>[^,]+)){0,1}(,(?P<DefCastle3>[🐉🦅🐺🦈🦌🥔🌑])(?P<DefUser3>[^,]+)){0,1}(,(?P<DefCastle4>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser4>[^,]+)){0,1}){0,1}(\\n){0,1})(\\n)*)*` +
`(🎪State of exclusive rights:\\n){0,1}` +
`(((?P<Fair>[A-Z][a-z]+ [A-Z][a-z]+) (?P<Status>(was protected|belongs to)( (?P<Attacker>[A-Z][a-z]+ [A-Z][a-z]+):)\\n` +
`(🎖Attack leaders: (?P<AtkCastle1>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser1>[^,]+)(,(?P<AtkCastle2>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser2>[^,]+)){0,1}(,(?P<AtkCastle3>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser3>[^,]+)){0,1}(,(?P<AtkCastle4>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser4>[^,]+)){0,1}){0,1}(\\n){0,1}` +
`(🎖Defense leaders: (?P<DefCastle1>[🐉🦅🐺🦈🦌🥔🌑])(?P<DefUser1>[^,]+)(,(?P<DefCastle2>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser2>[^,]+)){0,1}(,(?P<DefCastle3>[🐉🦅🐺🦈🦌🥔🌑])(?P<DefUser3>[^,]+)){0,1}(,(?P<DefCastle4>[🐉🦅🐺🦈🦌🥔🌑])(?P<AtkUser4>[^,]+)){0,1}){0,1}(\\n){0,1})(\\n)*)*` +
`$)"
*/
cwm := ChatWarsMessageUnionWar{}
/*
log.Printf("parseSubTypeMessageUnionWar : War report identified\n")
log.Printf("parseSubTypeMessageUnionWar : Castle : %s\n", r.ReplaceAllString(m.Text, "${Castle}"))
log.Printf("parseSubTypeMessageUnionWar : Guild : %s\n", r.ReplaceAllString(m.Text, "${Guild}"))
log.Printf("parseSubTypeMessageUnionWar : User : %s\n", r.ReplaceAllString(m.Text, "${User}"))
log.Printf("parseSubTypeMessageUnionWar : Attack : %s\n", r.ReplaceAllString(m.Text, "${Attack}"))
log.Printf("parseSubTypeMessageUnionWar : AttackMod : %s\n", r.ReplaceAllString(m.Text, "${AttackMod}"))
log.Printf("parseSubTypeMessageUnionWar : Defense : %s\n", r.ReplaceAllString(m.Text, "${Defense}"))
log.Printf("parseSubTypeMessageUnionWar : Level : %s\n", r.ReplaceAllString(m.Text, "${Level}"))
log.Printf("parseSubTypeMessageUnionWar : Exp : %s\n", r.ReplaceAllString(m.Text, "${Exp}"))
log.Printf("parseSubTypeMessageUnionWar : Gold : %s\n", r.ReplaceAllString(m.Text, "${Gold}"))
log.Printf("parseSubTypeMessageUnionWar : Stock : %s\n", r.ReplaceAllString(m.Text, "${Stock}"))
log.Printf("parseSubTypeMessageUnionWar : Stamina : %s\n", r.ReplaceAllString(m.Text, "${Stamina}"))
log.Printf("parseSubTypeMessageUnionWar : Crit : %s\n", r.ReplaceAllString(m.Text, "${Crit}"))
*/
return &cwm, nil
}
func parseSubTypeMessageDuelFight(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageDuelFight, error) {
cwm := ChatWarsMessageDuelFight{}
if r.ReplaceAllString(m.Text, "${Duelist1}") == r.ReplaceAllString(m.Text, "${WinDuelist}") {
cwm.WinCastle = r.ReplaceAllString(m.Text, "${Castle1}")
cwm.WinGuild = r.ReplaceAllString(m.Text, "${Guild1}")
cwm.WinUser = r.ReplaceAllString(m.Text, "${Duelist1}")
cwm.WinLife, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Life1}"), 10, 64)
cwm.LossCastle = r.ReplaceAllString(m.Text, "${Castle2}")
cwm.LossGuild = r.ReplaceAllString(m.Text, "${Guild2}")
cwm.LossUser = r.ReplaceAllString(m.Text, "${Duelist2}")
cwm.LossLife, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Life2}"), 10, 64)
} else {
cwm.LossCastle = r.ReplaceAllString(m.Text, "${Castle1}")
cwm.LossGuild = r.ReplaceAllString(m.Text, "${Guild1}")
cwm.LossUser = r.ReplaceAllString(m.Text, "${Duelist1}")
cwm.LossLife, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Life1}"), 10, 64)
cwm.WinCastle = r.ReplaceAllString(m.Text, "${Castle2}")
cwm.WinGuild = r.ReplaceAllString(m.Text, "${Guild2}")
cwm.WinUser = r.ReplaceAllString(m.Text, "${Duelist2}")
cwm.WinLife, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Life2}"), 10, 64)
}
cwm.Weapon = r.ReplaceAllString(m.Text, "${Weapon}")
return &cwm, nil
}
func parseSubTypeMessageTimeAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageTimeAck, error) {
cwm := ChatWarsMessageTimeAck{}
cwm.ObjID64 = m.ObjID64
t, 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, "parseSubTypeMessageTimeAck : fromChatWarsDate")
cwm.RealTime = t
cwm.TimeOfDay = r.ReplaceAllString(m.Text, "${Time}")
cwm.Hour, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Hour}"), 10, 64)
cwm.Minute, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Minute}"), 10, 64)
cwm.Day, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Day}"), 10, 64)
cwm.Month = int64(chatWarsMonth[r.ReplaceAllString(m.Text, "${Month}")])
cwm.Year, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Year}"), 10, 64)
cwm.Weather = r.ReplaceAllString(m.Text, "${Weather}")
cwm.WeatherNext = r.ReplaceAllString(m.Text, "${WeatherNext}")
return &cwm, nil
}
func parseSubTypeMessageQuestResultAmbush(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageQuestResultAmbush, error) {
var lvlSum, qtySum int64
cwm := ChatWarsMessageQuestResultAmbush{}
re := regexp.MustCompile(`([0-9]){0,1}( x ){0,1}([a-zA-Z ]*) lvl\.([0-9]*)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Monsters}"), -1) {
log.Printf("parseSubTypeMessageQuestResultAmbush : %s\n", l[1])
log.Printf("parseSubTypeMessageQuestResultAmbush : %s\n", l[2])
log.Printf("parseSubTypeMessageQuestResultAmbush : %s\n", l[3])
log.Printf("parseSubTypeMessageQuestResultAmbush : %s\n", l[4])
qty, _ := strconv.ParseInt(l[1], 10, 64)
log.Printf("parseSubTypeMessageQuestResultAmbush : qty : %d\n", qty)
if qty == 0 {
qty = 1
}
lvl, _ := strconv.ParseInt(l[4], 10, 64)
log.Printf("parseSubTypeMessageQuestResultAmbush : lvl : %d\n", lvl)
lvlSum = lvlSum + lvl
qtySum = qtySum + qty
}
cwm.Level = lvlSum / qtySum
cwm.Msg = m
log.Printf("parseSubTypeMessageQuestResultAmbush : Level : %d\n", cwm.Level)
return &cwm, nil
}
func parseSubTypeMessagePillageInc(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessagePillageInc, error) {
cwm := ChatWarsMessagePillageInc{}
cwm.Attacker = r.ReplaceAllString(m.Text, "${Attacker}")
cwm.Castle = r.ReplaceAllString(m.Text, "${Castle}")
cwm.Guild = r.ReplaceAllString(m.Text, "${Guild}")
return &cwm, nil
}
func parseSubTypeMessageJobGWithdrawAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageJobGWithdrawAck, error) {
cwm := ChatWarsMessageJobGWithdrawAck{}
cwm.Ref = r.ReplaceAllString(m.Text, "${Ref}")
cwm.Msg = m
return &cwm, nil
}