This commit is contained in:
shoopea 2019-05-09 17:19:02 +08:00
parent ea5812e98c
commit 419f917b3b
4 changed files with 83 additions and 87 deletions

62
def.go
View File

@ -1,12 +1,5 @@
package main package main
import (
"errors"
"regexp"
"strconv"
"time"
)
type ChatWarsMessage struct { type ChatWarsMessage struct {
UserID64 int64 `json:"user_id"` UserID64 int64 `json:"user_id"`
SenderUserID64 int64 `json:"sender_user_id"` SenderUserID64 int64 `json:"sender_user_id"`
@ -16,6 +9,19 @@ type ChatWarsMessage struct {
Text string `json:"text"` Text string `json:"text"`
} }
type ChatWarsMessageAuctionAnnounce struct {
MsgID64 int64 `json:"msg_id"`
LotID int32 `json:"lot_id"`
Item string `json:"item"`
Cond string `json:"cond"`
Quality string `json:"quality"`
Seller string `json:"seller"`
Buyer string `json:"buyer"`
Price int32 `json:"price"`
Status string `json:"status"`
End int64 `json:"end"`
}
type MessageParsingRule struct { type MessageParsingRule struct {
ID int32 ID int32
Priority int32 Priority int32
@ -102,45 +108,3 @@ var (
11: 30, 11: 30,
12: 31} 12: 31}
) )
func fromChatWarsDate(d string) (t time.Time, err error) {
r := regexp.MustCompile(`(?P<Day>[0-9]{2}) (?P<Month>(Wintar|Hornung|Lenzin|Ōstar|Winni|Brāh|Hewi|Aran|Witu|Wīndume|Herbist|Hailag)) (?P<Year>[0-9]{4})( (?P<Hour>[0-9]{2}):(?P<Minute>[0-9]{2})){0,1}`)
if r.FindStringSubmatch(d) != nil {
// log.Printf("fromChatWarsDate : Day : %s\n", r.ReplaceAllString(d, "${Day}"))
// log.Printf("fromChatWarsDate : Month : %s\n", r.ReplaceAllString(d, "${Month}"))
// log.Printf("fromChatWarsDate : Year : %s\n", r.ReplaceAllString(d, "${Year}"))
// log.Printf("fromChatWarsDate : Hour : %s\n", r.ReplaceAllString(d, "${Hour}"))
// log.Printf("fromChatWarsDate : Minute : %s\n", r.ReplaceAllString(d, "${Minute}"))
year, _ := strconv.Atoi(r.ReplaceAllString(d, "${Year}"))
day, _ := strconv.Atoi(r.ReplaceAllString(d, "${Day}"))
hour, _ := strconv.Atoi(r.ReplaceAllString(d, "${Hour}"))
minute, _ := strconv.Atoi(r.ReplaceAllString(d, "${Minute}"))
days := int(0)
for i := 1059; i < year; i++ {
days = days + 365
}
for i := 1; i < chatWarsMonth[r.ReplaceAllString(d, "${Month}")]; i++ {
if _, special := chatWarsDaysSpecial[year][i]; special {
days = days + chatWarsDaysSpecial[year][i]
} else {
days = days + chatWarsDays[i]
}
}
for i := 1; i < day; i++ {
days++
}
t, _ := time.Parse(time.RFC3339, "2017-11-18T21:00:00+00:00")
t = t.AddDate(0, 0, (days / 3))
t = t.Add(time.Hour * time.Duration(days%3) * 8)
t = t.Add(time.Minute * time.Duration(hour) * 20)
t = t.Add(time.Second * time.Duration(minute) * 20)
return t, nil
} else {
return time.Now(), errors.New("Wrong format")
}
}
func toChatWarsDate(t time.Time) (s string, err error) {
return "test", nil
}

42
sql.go
View File

@ -139,7 +139,7 @@ func initDB() {
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
failOnError(err, "initDB : create table obj_msg_report") failOnError(err, "initDB : create table obj_msg_report")
_, err = db.Exec(`CREATE TABLE obj_auction_announce ( _, err = db.Exec(`CREATE TABLE obj_msg_auction_announce (
obj_id BIGINT UNSIGNED NOT NULL obj_id BIGINT UNSIGNED NOT NULL
,lot_id BIGINT UNSIGNED NOT NULL ,lot_id BIGINT UNSIGNED NOT NULL
,item VARCHAR(80) ,item VARCHAR(80)
@ -152,7 +152,7 @@ func initDB() {
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (lot_id) ,UNIQUE KEY (lot_id)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
failOnError(err, "initDB : create table obj_auction_announce") failOnError(err, "initDB : create table obj_msg_auction_announce")
_, err = db.Exec(`CREATE TABLE msg_rules ( _, err = db.Exec(`CREATE TABLE msg_rules (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT
@ -1270,38 +1270,26 @@ func insertMsgReport(objId int64, war_date int32, atk int32, def int32, exp int3
return nil return nil
} }
func insertAuctionAnnounce(objId int64, war_date int32, atk int32, def int32, exp int32, gold int32, stock int32, crit bool, stamina bool) error { func insertMsgAuctionAnnounce(m *ChatWarsMessageAuctionAnnounce) error {
objSubTypeId, err := getObjSubTypeId(objId) objSubTypeId, err := getObjSubTypeId(objId)
if err != nil { if err != nil {
return err return err
} }
if objSubTypeId != objSubTypeMessageUnknown { if objSubTypeId != objSubTypeMessageUnknown && objSubTypeId != objSubTypeMessageAuctionAnnounce {
return errors.New("Message is not of type Unknown") return errors.New("Message type mismatch")
} }
/*
obj_id BIGINT UNSIGNED NOT NULL stmt, err := db.Prepare(`INSERT INTO obj_msg_auction_announce (obj_id, lot_id, item, cond, quality, seller, buyer, status, end)
,lot_id BIGINT UNSIGNED NOT NULL VALUES (?, ?, ?, ?, ?, ?, ?, ?, FROM_UNIXTIME(?));`)
,item VARCHAR(80) if err != nil {
,cond VARCHAR(32) return err
,quality VARCHAR(32) }
,seller VARCHAR(32) defer stmt.Close()
,buyer VARCHAR(32)
,status VARCHAR(32)
,end TIMESTAMP NOT NULL
stmt, err := db.Prepare(`INSERT INTO obj_msg_report (obj_id, war_date, attack, defense, gold, stock, exp, stamina, crit) _, err = stmt.Exec(m.MsgID64, m.LotID, m.Item, m.Cond, m.Quality, m.Seller, m.Buyer, m.Status, m.End)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?FROM_UNIXTIME(?), ?);`) if err != nil {
if err != nil { return err
return err }
}
defer stmt.Close()
_, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.UserID64, m.SenderUserID64, m.Date, m.Text)
if err != nil {
return err
}
*/
return nil return nil
} }

View File

@ -15,3 +15,45 @@ func logOnError(err error, msg string) {
log.Printf("%s: %s", msg, err) log.Printf("%s: %s", msg, err)
} }
} }
func fromChatWarsDate(d string) (t time.Time, err error) {
r := regexp.MustCompile(`(?P<Day>[0-9]{2}) (?P<Month>(Wintar|Hornung|Lenzin|Ōstar|Winni|Brāh|Hewi|Aran|Witu|Wīndume|Herbist|Hailag)) (?P<Year>[0-9]{4})( (?P<Hour>[0-9]{2}):(?P<Minute>[0-9]{2})){0,1}`)
if r.FindStringSubmatch(d) != nil {
// log.Printf("fromChatWarsDate : Day : %s\n", r.ReplaceAllString(d, "${Day}"))
// log.Printf("fromChatWarsDate : Month : %s\n", r.ReplaceAllString(d, "${Month}"))
// log.Printf("fromChatWarsDate : Year : %s\n", r.ReplaceAllString(d, "${Year}"))
// log.Printf("fromChatWarsDate : Hour : %s\n", r.ReplaceAllString(d, "${Hour}"))
// log.Printf("fromChatWarsDate : Minute : %s\n", r.ReplaceAllString(d, "${Minute}"))
year, _ := strconv.Atoi(r.ReplaceAllString(d, "${Year}"))
day, _ := strconv.Atoi(r.ReplaceAllString(d, "${Day}"))
hour, _ := strconv.Atoi(r.ReplaceAllString(d, "${Hour}"))
minute, _ := strconv.Atoi(r.ReplaceAllString(d, "${Minute}"))
days := int(0)
for i := 1059; i < year; i++ {
days = days + 365
}
for i := 1; i < chatWarsMonth[r.ReplaceAllString(d, "${Month}")]; i++ {
if _, special := chatWarsDaysSpecial[year][i]; special {
days = days + chatWarsDaysSpecial[year][i]
} else {
days = days + chatWarsDays[i]
}
}
for i := 1; i < day; i++ {
days++
}
t, _ := time.Parse(time.RFC3339, "2017-11-18T21:00:00+00:00")
t = t.AddDate(0, 0, (days / 3))
t = t.Add(time.Hour * time.Duration(days%3) * 8)
t = t.Add(time.Minute * time.Duration(hour) * 20)
t = t.Add(time.Second * time.Duration(minute) * 20)
return t, nil
} else {
return time.Now(), errors.New("Wrong format")
}
}
func toChatWarsDate(t time.Time) (s string, err error) {
return "test", nil
}

View File

@ -100,17 +100,19 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Stamina : %s (%d)\n", r.ReplaceAllString(m.Text, "${Stamina}"), 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) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Crit : %s (%d)\n", r.ReplaceAllString(m.Text, "${Crit}"), objId)
case objSubTypeMessageAuctionAnnounce: case objSubTypeMessageAuctionAnnounce:
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Auction announce (%d)\n", objId) m := ChatWarsMessageAuctionAnnounce{
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Lot : %s (%d)\n", r.ReplaceAllString(m.Text, "${Lot}"), objId) ID64: objId,
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Item : %s (%d)\n", r.ReplaceAllString(m.Text, "${Item}"), objId) }
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Seller : %s (%d)\n", r.ReplaceAllString(m.Text, "${Seller}"), objId) m.Lot, _ = strconv.Atoi(r.ReplaceAllString(m.Text, "${Lot}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Buyer : %s (%d)\n", r.ReplaceAllString(m.Text, "${Buyer}"), objId) m.Cond = r.ReplaceAllString(m.Text, "${Cond}")
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Price : %s (%d)\n", r.ReplaceAllString(m.Text, "${Price}"), objId) m.Quality = r.ReplaceAllString(m.Text, "${Quality}")
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Status : %s (%d)\n", r.ReplaceAllString(m.Text, "${Status}"), objId) m.Seller = r.ReplaceAllString(m.Text, "${Seller}")
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : End : %s (%d)\n", r.ReplaceAllString(m.Text, "${End}"), objId) m.Buyer = r.ReplaceAllString(m.Text, "${Buyer}")
t, err := fromChatWarsDate(r.ReplaceAllString(m.Text, "${End}")) m.Price, _ = strconv.Atoi(r.ReplaceAllString(m.Text, "${Price}"))
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : AuctionAnnounce : end") m.Status = r.ReplaceAllString(m.Text, "${Status}")
log.Println(t) t, _ := fromChatWarsDate(r.ReplaceAllString(m.Text, "${End}"))
m.End = t.Unix()
insertMsgAuctionAnnounce(&m)
case objSubTypeMessageTime: case objSubTypeMessageTime:
/* /*
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Time (%d)\n", objId) log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Time (%d)\n", objId)