This commit is contained in:
shoopea 2019-05-31 10:02:21 +08:00
parent b3ff06a1f7
commit ead8295404
6 changed files with 123 additions and 18 deletions

View File

@ -1 +1,8 @@
ChirpNestBot
- [ ] Crontab : engine, user interface
- [ ] Impersonate
- [ ] Items parsing/identification
- [ ] Withdrawal bot
- [x] Foray interception
- [ ] Tribute interception

7
def.go
View File

@ -77,6 +77,9 @@ type ChatWarsMessageMeAck struct {
UserID64 int64 `json:"user_id"`
GuildID64 int64 `json:"guild_id"`
State string `json:"state"`
Level int64 `json:"level"`
ExpVal int64 `json:"exp_val"`
ExpTarget int64 `json:"exp_target"`
}
type ChatWarsMessageGRolesAck struct {
@ -218,6 +221,8 @@ const (
objTypeCastle = 8
objTypeFair = 9
objTypeUnion = 10
objTypeTribute = 11
objTypeExperience = 12
castleDeer = 1
castleDragon = 2
@ -301,6 +306,8 @@ const (
objSubTypeCastle = 801
objSubTypeFair = 901
objSubTypeUnion = 1001
objSubTypeTribute = 1101
objSubTypeExperience = 1201
objJobStatusNew = 0
objJobStatusPillageGo = 1

9
msg.go
View File

@ -31,11 +31,20 @@ func parseSubTypeMessageReportAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWa
}
func parseSubTypeMessageMeAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageMeAck, error) {
var i int64
cwm := ChatWarsMessageMeAck{
UserID64: getObjUserID(r.ReplaceAllString(m.Text, "${Player}")),
GuildID64: getObjGuildID(r.ReplaceAllString(m.Text, "${Guild}")),
State: r.ReplaceAllString(m.Text, "${State}"),
}
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
/*
log.Printf("parseSubTypeMessageReport : Me report identified\n")
log.Printf("parseSubTypeMessageReport : Class : %s\n", r.ReplaceAllString(m.Text, "${Class}"))

57
obj.go
View File

@ -95,7 +95,7 @@ func addObjMsg(msgID64 int64, msgChatID64 int64, msgUserID64 int64, msgSenderUse
logOnError(err, "addObjMsg : exec insert obj_msg")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjMsg : rollback exec insert obj")
logOnError(err2, "addObjMsg : rollback exec insert obj_msg")
return 0, err
}
@ -144,7 +144,7 @@ func addObjCastle(logo string, name string) (int64, error) {
logOnError(err, "addObjCastle : exec insert obj_castle")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjCastle : rollback exec insert obj")
logOnError(err2, "addObjCastle : rollback exec insert obj_castle")
return 0, err
}
@ -228,7 +228,7 @@ func addObjGuild(tag string, name string) (int64, error) {
logOnError(err, "addObjGuild : exec insert obj_guild")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjGuild : rollback exec insert obj")
logOnError(err2, "addObjGuild : rollback exec insert obj_guild")
return 0, err
}
@ -322,7 +322,7 @@ func addObjUser(name string) (int64, error) {
logOnError(err, "addObjUser : exec insert obj_user")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjUser : rollback exec insert obj")
logOnError(err2, "addObjUser : rollback exec insert obj_user")
return 0, err
}
@ -384,3 +384,52 @@ func getObjMsgDate(objID64 int64) (time.Time, error) {
return m.Date, nil
}
}
func addObjXP(userID64 int64, val int64, target int64, level int64, date time.Time) (int64, error) {
tx, err := db.Begin()
logOnError(err, "addObjXP : start transaction")
if err != nil {
return 0, err
}
res, err := tx.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
VALUES (` + strconv.Itoa(objTypeExperience) + `,` + strconv.Itoa(objSubTypeExperience) + `);`)
logOnError(err, "addObjXP : exec insert obj")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjXP : rollback insert obj")
return 0, err
}
objId, err := res.LastInsertId()
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjXP : rollback get lastInsertId")
return 0, err
}
stmt, err := tx.Prepare(`INSERT INTO obj_xp (obj_id, user_id, val, target, level, date)
VALUES (?, ?, ?, ?, ?, ?);`)
logOnError(err, "addObjXP : prepare insert obj_xp")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjXP : rollback prepare insert obj_xp")
return 0, err
}
defer stmt.Close()
_, err = stmt.Exec(objId, userID64, val, target, level, date)
logOnError(err, "addObjXP : exec insert obj_xp")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjXP : rollback exec insert obj_xp")
return 0, err
}
err = tx.Commit()
logOnError(err, "addObjXP : commit")
if err != nil {
return 0, err
}
return objId, nil
}

37
sql.go
View File

@ -271,6 +271,35 @@ func initDB() {
failOnError(err, "initDB : create table obj_item")
log.Println("initDB : obj_item created ...")
_, err = db.Exec(`CREATE TABLE obj_tribute (
obj_id BIGINT UNSIGNED NOT NULL
,user_id BIGINT UNSIGNED NOT NULL
,item_id BIGINT UNSIGNED NOT NULL
,quantity SMALLINT NOT NULL
,xp SMALLINT NOT NULL
,date DATETIME NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
,FOREIGN KEY (item_id) REFERENCES obj_item(obj_id) ON DELETE CASCADE
,UNIQUE KEY (user_id, date)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
failOnError(err, "initDB : create table obj_tribute")
log.Println("initDB : obj_tribute created ...")
_, err = db.Exec(`CREATE TABLE obj_xp (
obj_id BIGINT UNSIGNED NOT NULL
,user_id BIGINT UNSIGNED NOT NULL
,val BIGINT UNSIGNED NOT NULL
,target BIGINT UNSIGNED NOT NULL
,level SMALLINT UNSIGNED NOT NULL
,date DATETIME NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
,UNIQUE KEY (user_id, date)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
failOnError(err, "initDB : create table obj_xp")
log.Println("initDB : obj_xp created ...")
_, err = db.Exec(`CREATE TABLE obj_job (
obj_id BIGINT UNSIGNED NOT NULL
,priority SMALLINT NOT NULL
@ -311,9 +340,6 @@ func initDB() {
_, err = db.Exec(`INSERT INTO code_obj_sub_type (id, intl_id, name, obj_type_id)
VALUES (` + strconv.Itoa(objSubTypeUser) + `, "user", "User", ` + strconv.Itoa(objTypeUser) + `)
,(` + strconv.Itoa(objSubTypeGuild) + `, "guild", "Guild", ` + strconv.Itoa(objTypeGuild) + `)
,(` + strconv.Itoa(objSubTypeCastle) + `, "castle", "Castle", ` + strconv.Itoa(objTypeCastle) + `)
,(` + strconv.Itoa(objSubTypeUnion) + `, "union", "Union", ` + strconv.Itoa(objTypeUnion) + `)
,(` + strconv.Itoa(objSubTypeFair) + `, "fair", "Fair", ` + strconv.Itoa(objTypeFair) + `)
,(` + strconv.Itoa(objSubTypeMessageUnknown) + `, "unknown", "Unknown", ` + strconv.Itoa(objTypeMessage) + `)
,(` + strconv.Itoa(objSubTypeMessageWar) + `, "war", "War report", ` + strconv.Itoa(objTypeMessage) + `)
,(` + strconv.Itoa(objSubTypeMessageMiniWar) + `, "mini_war", "Mini war reprot", ` + strconv.Itoa(objTypeMessage) + `)
@ -382,6 +408,11 @@ func initDB() {
,(` + strconv.Itoa(objSubTypeItemRecipe) + `, "item_recipe", "Time", ` + strconv.Itoa(objTypeItem) + `)
,(` + strconv.Itoa(objSubTypeItemPart) + `, "item_part", "Time", ` + strconv.Itoa(objTypeItem) + `)
,(` + strconv.Itoa(objSubTypeItemOther) + `, "item_other", "Time", ` + strconv.Itoa(objTypeItem) + `)
,(` + strconv.Itoa(objSubTypeCastle) + `, "castle", "Castle", ` + strconv.Itoa(objTypeCastle) + `)
,(` + strconv.Itoa(objSubTypeUnion) + `, "union", "Union", ` + strconv.Itoa(objTypeUnion) + `)
,(` + strconv.Itoa(objSubTypeFair) + `, "fair", "Fair", ` + strconv.Itoa(objTypeFair) + `)
,(` + strconv.Itoa(objSubTypeTribute) + `, "fair", "Fair", ` + strconv.Itoa(objTypeTribute) + `)
,(` + strconv.Itoa(objSubTypeExperience) + `, "fair", "Fair", ` + strconv.Itoa(objTypeExperience) + `)
;`)
failOnError(err, "initDB : populate table code_obj_sub_type")
log.Println("initDB : code_obj_sub_type populated ...")

View File

@ -145,6 +145,8 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMeAck.")
cwm.Msg = m
clientMsgMeAck(cwm)
_, err = addObjXP(cwm.UserID64, cwm.ExpVal, cwm.ExpTarget, cwm.Level, m.Date)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : addObjXP(MeAck)")
err = setObjSubTypeId(objId, objSubTypeMessageMeAck)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(MeAck)")
case objSubTypeMessageGoQuestAck: