test gdeposit

This commit is contained in:
shoopea 2019-08-21 11:46:42 +08:00
parent 26c84a1049
commit 52b36b763b
7 changed files with 713 additions and 669 deletions

15
bot.go
View File

@ -235,7 +235,7 @@ func botMsgRescan(m *tb.Message) {
} }
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
log.Printf("botMsgRescan : json : %s\n", string(b)) log.Printf("botMsgRescan : json : %s\n", string(b))
_, err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanMsg, int64(m.Sender.ID), time.Now().UTC(), b) _, err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanMsg, int64(m.Sender.ID), 0, time.Now().UTC(), b)
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)") logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
if err != nil { if err != nil {
c := TGCommand{ c := TGCommand{
@ -294,7 +294,7 @@ func botMsgRescanAll(m *tb.Message) {
ChatID64: m.Chat.ID, ChatID64: m.Chat.ID,
} }
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
_, err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanAllMsg, int64(m.Sender.ID), time.Now().UTC(), b) _, err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanAllMsg, int64(m.Sender.ID), 0, time.Now().UTC(), b)
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)") logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
if err != nil { if err != nil {
@ -349,7 +349,7 @@ func botBackupExport(m *tb.Message) {
ChatID64: m.Chat.ID, ChatID64: m.Chat.ID,
} }
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
_, err := createJob(objSubTypeJobBackupExport, objJobPriorityBackup, int64(m.Sender.ID), time.Now().UTC(), b) _, err := createJob(objSubTypeJobBackupExport, objJobPriorityBackup, int64(m.Sender.ID), 0, time.Now().UTC(), b)
logOnError(err, "botBackupExport : createJob(objSubTypeJobBackupExport)") logOnError(err, "botBackupExport : createJob(objSubTypeJobBackupExport)")
return return
@ -399,7 +399,7 @@ func botBackupImport(m *tb.Message) {
ChatID64: m.Chat.ID, ChatID64: m.Chat.ID,
} }
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
_, err := createJob(objSubTypeJobBackupImport, objJobPriorityBackup, int64(m.Sender.ID), time.Now().UTC(), b) _, err := createJob(objSubTypeJobBackupImport, objJobPriorityBackup, int64(m.Sender.ID), 0, time.Now().UTC(), b)
logOnError(err, "botBackupImport : createJob(objSubTypeJobBackupImport)") logOnError(err, "botBackupImport : createJob(objSubTypeJobBackupImport)")
return return
@ -575,7 +575,7 @@ func botGStock(m *tb.Message) {
} }
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
t := time.Now().UTC().Add(1 * time.Second) t := time.Now().UTC().Add(1 * time.Second)
_, err := createJob(objSubTypeJobGStock, objJobPriority, int64(m.Chat.ID), t, b) _, err := createJob(objSubTypeJobGStock, objJobPriority, int64(m.Chat.ID), 0, t, b)
if err != nil { if err != nil {
c := TGCommand{ c := TGCommand{
@ -607,6 +607,7 @@ func botGDepositAll(m *tb.Message) {
MsgID64: int64(m.ID), MsgID64: int64(m.ID),
ChatID64: m.Chat.ID, ChatID64: m.Chat.ID,
ResObjID64: nil, ResObjID64: nil,
Status: 0,
} }
p.ResObjID64 = append(p.ResObjID64, getObjItemID(`02`, `Stick`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`02`, `Stick`))
@ -616,7 +617,7 @@ func botGDepositAll(m *tb.Message) {
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
t := time.Now().UTC() t := time.Now().UTC()
_, err := createJob(objSubTypeJobGDeposit, objJobPriority, int64(m.Chat.ID), t, b) _, err := createJob(objSubTypeJobGDeposit, objJobPriority, int64(m.Chat.ID), 0, t, b)
if err != nil { if err != nil {
c := TGCommand{ c := TGCommand{
@ -667,7 +668,7 @@ func botTimer(m *tb.Message) {
} }
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
t := time.Now().UTC().Add(d) t := time.Now().UTC().Add(d)
objID64, err := createJob(objSubTypeJobMsgClient, objJobPriority, int64(m.Chat.ID), t, b) objID64, err := createJob(objSubTypeJobMsgClient, objJobPriority, int64(m.Chat.ID), 0, t, b)
logOnError(err, "botTimer : createJob") logOnError(err, "botTimer : createJob")
if err != nil { if err != nil {
c := TGCommand{ c := TGCommand{

3
def.go
View File

@ -81,6 +81,8 @@ type ChatWarsItem struct {
Code string `json:"code"` Code string `json:"code"`
Name string `json:"name"` Name string `json:"name"`
Weight int `json:"weight"` Weight int `json:"weight"`
Exchange bool `json:"exchange"`
Auction bool `json:"auction"`
} }
type ChatWarsItems struct { type ChatWarsItems struct {
@ -238,6 +240,7 @@ type JobPayloadGDeposit struct {
MsgID64 int64 `json:"msg_id"` MsgID64 int64 `json:"msg_id"`
ChatID64 int64 `json:"chat_id"` ChatID64 int64 `json:"chat_id"`
ResObjID64 []int64 `json:"res_obj_id"` ResObjID64 []int64 `json:"res_obj_id"`
Status int `json:"status"`
} }
type JobPayloadSaveRes struct { type JobPayloadSaveRes struct {

1241
items.go

File diff suppressed because it is too large Load Diff

66
job.go
View File

@ -15,7 +15,7 @@ import (
tb "gopkg.in/tucnak/telebot.v2" tb "gopkg.in/tucnak/telebot.v2"
) )
func createJob(jobTypeID int32, priority int32, userID64 int64, schedule time.Time, payload []byte) (int64, error) { func createJob(jobTypeID int32, priority int32, userID64 int64, trigger int64, schedule time.Time, payload []byte) (int64, error) {
stmt, err := db.Prepare(`INSERT INTO obj (obj_type_id, obj_sub_type_id) stmt, err := db.Prepare(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
VALUES (? , ?);`) VALUES (? , ?);`)
logOnError(err, "createJob : prepare insert obj") logOnError(err, "createJob : prepare insert obj")
@ -37,7 +37,7 @@ func createJob(jobTypeID int32, priority int32, userID64 int64, schedule time.Ti
return 0, err return 0, err
} }
stmt, err = db.Prepare(`INSERT INTO obj_job (obj_id, priority, user_id, status, seq_nr, schedule, is_done, in_work, inserted, pulled, started, ended, payload) stmt, err = db.Prepare(`INSERT INTO obj_job (obj_id, priority, user_id, trigger, seq_nr, schedule, is_done, in_work, inserted, pulled, started, ended, payload)
VALUES (?, ?, ?, ?, NULL, ?, 0, 0, ?, NULL, NULL, NULL, ?);`) VALUES (?, ?, ?, ?, NULL, ?, 0, 0, ?, NULL, NULL, NULL, ?);`)
logOnError(err, "createJob : prepare insert obj_job") logOnError(err, "createJob : prepare insert obj_job")
if err != nil { if err != nil {
@ -45,7 +45,7 @@ func createJob(jobTypeID int32, priority int32, userID64 int64, schedule time.Ti
} }
defer stmt.Close() defer stmt.Close()
_, err = stmt.Exec(objId, priority, userID64, objJobStatusNew, schedule.UTC(), time.Now().UTC(), payload) _, err = stmt.Exec(objId, priority, userID64, trigger, schedule.UTC(), time.Now().UTC(), payload)
logOnError(err, "createJob : insert obj_job") logOnError(err, "createJob : insert obj_job")
if err != nil { if err != nil {
return 0, err return 0, err
@ -56,7 +56,7 @@ func createJob(jobTypeID int32, priority int32, userID64 int64, schedule time.Ti
func createJobCallback(jobTypeID int32, userID64 int64, msgTypeID64 int64, payload []byte) error { func createJobCallback(jobTypeID int32, userID64 int64, msgTypeID64 int64, payload []byte) error {
t, err := time.Parse(time.RFC3339, "9999-12-31T00:00:00+00:00") t, err := time.Parse(time.RFC3339, "9999-12-31T00:00:00+00:00")
jobID64, err := createJob(jobTypeID, objJobPriority, userID64, t, payload) jobID64, err := createJob(jobTypeID, objJobPriority, userID64, 0, t, payload)
if err != nil { if err != nil {
return err return err
} }
@ -109,15 +109,15 @@ func setJobStart(jobId int64) error {
return nil return nil
} }
func rescheduleJob(jobID64 int64, status int32, schedule time.Time) error { func rescheduleJob(jobID64 int64, trigger int64, schedule time.Time) error {
stmt, err := db.Prepare(`UPDATE obj_job j SET j.is_done = 0, j.in_work = 0, j.schedule = ?, j.status = ? WHERE j.obj_id = ?;`) stmt, err := db.Prepare(`UPDATE obj_job j SET j.is_done = 0, j.in_work = 0, j.schedule = ?, j.trigger = ? WHERE j.obj_id = ?;`)
logOnError(err, "rescheduleJob : prepare update obj_job") logOnError(err, "rescheduleJob : prepare update obj_job")
if err != nil { if err != nil {
return err return err
} }
defer stmt.Close() defer stmt.Close()
_, err = stmt.Exec(schedule.UTC(), status, jobID64) _, err = stmt.Exec(schedule.UTC(), trigger, jobID64)
s := fmt.Sprintf("rescheduleJob, update obj_job(%d)", jobID64) s := fmt.Sprintf("rescheduleJob, update obj_job(%d)", jobID64)
logOnError(err, s) logOnError(err, s)
if err != nil { if err != nil {
@ -131,7 +131,7 @@ func loadCurrentJobs() ([]Job, error) {
objId int64 objId int64
jobTypeId int32 jobTypeId int32
userID64 int64 userID64 int64
status int32 trigger int64
payload []byte payload []byte
jobs []Job jobs []Job
) )
@ -145,7 +145,7 @@ func loadCurrentJobs() ([]Job, error) {
return jobs, err return jobs, err
} }
stmt, err := db.Prepare("SELECT o.id, o.obj_sub_type_id, j.status, j.user_id, j.payload FROM obj_job j, obj o WHERE j.obj_id = o.id AND j.is_done = 0 AND j.in_work = 1 AND j.seq_nr = ? ORDER BY j.priority ASC, j.obj_id ASC;") stmt, err := db.Prepare("SELECT o.id, o.obj_sub_type_id, j.trigger, j.user_id, j.payload FROM obj_job j, obj o WHERE j.obj_id = o.id AND j.is_done = 0 AND j.in_work = 1 AND j.seq_nr = ? ORDER BY j.priority ASC, j.obj_id ASC;")
logOnError(err, "loadCurrentJobs : prepare select statement") logOnError(err, "loadCurrentJobs : prepare select statement")
if err != nil { if err != nil {
stmt.Close() stmt.Close()
@ -161,12 +161,12 @@ func loadCurrentJobs() ([]Job, error) {
} }
for rows.Next() { for rows.Next() {
err = rows.Scan(&objId, &jobTypeId, &status, &userID64, &payload) err = rows.Scan(&objId, &jobTypeId, &trigger, &userID64, &payload)
logOnError(err, "loadCurrentJobs : scan query rows") logOnError(err, "loadCurrentJobs : scan query rows")
job := Job{ job := Job{
ID64: objId, ID64: objId,
JobTypeID: jobTypeId, JobTypeID: jobTypeId,
Status: status, Trigger: trigger,
UserID64: userID64, UserID64: userID64,
Payload: payload, Payload: payload,
} }
@ -239,7 +239,7 @@ func jobRescan(j Job) {
Text: fmt.Sprintf("%d messages processed in %s.", len(ids), time.Since(start)), Text: fmt.Sprintf("%d messages processed in %s.", len(ids), time.Since(start)),
} }
b, _ := json.Marshal(r) b, _ := json.Marshal(r)
_, err := createJob(objSubTypeJobSetJobDone, objJobPriorityRescanAllMsg, j.UserID64, time.Now().UTC(), b) _, err := createJob(objSubTypeJobSetJobDone, objJobPriorityRescanAllMsg, j.UserID64, j.ID64, time.Now().UTC(), b)
logOnError(err, "jobRescan : createJob(objSubTypeJobSetJobDone)") logOnError(err, "jobRescan : createJob(objSubTypeJobSetJobDone)")
} else if len(ids) == 1 { } else if len(ids) == 1 {
@ -432,11 +432,11 @@ func jobPillage(j Job) {
} }
TGCmdQueue <- s TGCmdQueue <- s
} }
err = rescheduleJob(j.ID64, j.Status+1, time.Now().Add(30*time.Second).UTC()) err = rescheduleJob(j.ID64, j.Trigger, time.Now().Add(30*time.Second).UTC())
logOnError(err, "jobPillage : rescheduleJob(objSubTypeMessageGo)") logOnError(err, "jobPillage : rescheduleJob(objSubTypeMessageGo)")
} else { //no /go in the last 30 sec so we go ahead, send one and reschedule to check again in 25sec } else { //no /go in the last 30 sec so we go ahead, send one and reschedule to check again in 25sec
clientSendCWMsg(j.UserID64, "/go") clientSendCWMsg(j.UserID64, "/go")
err = rescheduleJob(j.ID64, j.Status+1, time.Now().Add(30*time.Second).UTC()) err = rescheduleJob(j.ID64, j.Trigger, time.Now().Add(30*time.Second).UTC())
logOnError(err, "jobPillage : rescheduleJob") logOnError(err, "jobPillage : rescheduleJob")
} }
@ -712,25 +712,29 @@ func jobGDeposit(j Job) {
err = json.Unmarshal(j.Payload, &p) err = json.Unmarshal(j.Payload, &p)
logOnError(err, "jobGDeposit : Unmarshal payload") logOnError(err, "jobGDeposit : Unmarshal payload")
for i := range p.ResObjID64 { if p.Status == 0 { /* handle remaining resources to be stored */
clientSendCWMsg(p.ChatID64, "/g_stock_res") if len(p.ResObjID64) > 0 {
obj, err := getObjItem(p.ResObjID64[0])
if err != nil {
p2 := JobPayloadGDeposit{
MsgID64: p.MsgID64,
ChatID64: p.ChatID64,
ResObjID64: [1]int64{obj.ObjID64},
Status: 1,
} }
/* err = createJobCallback(objSubTypeJobGDeposit, j.UserID64, objSubTypeMessageOrderbookAck, p2)
clientSendCWMsg(m.Chat.ID, "/g_stock_res") clientSendCWMsg(p.ChatID64, fmt.Sprintf("/t_%s", obj.Code))
clientSendCWMsg(m.Chat.ID, "/g_stock_alch") logOnError(err, "jobGDeposit : createJobCallback")
clientSendCWMsg(m.Chat.ID, "/g_stock_misc")
clientSendCWMsg(m.Chat.ID, "/g_stock_rec")
clientSendCWMsg(m.Chat.ID, "/g_stock_parts")
clientSendCWMsg(m.Chat.ID, "/g_stock_other")
*/
if err == nil {
m := TGCommand{
Type: commandReplyMsg,
Text: "Message sent.",
FromMsgID64: p.MsgID64,
FromChatID64: p.ChatID64,
} }
TGCmdQueue <- m p.ResObjID64 = p.ResObjID64[1:]
}
if len(p.ResObjID64) > 0 {
err = createJobCallback(objSubTypeJobGDeposit, j.UserID64, objSubTypeMessageOrderbookAck, p)
logOnError(err, "jobGDeposit : createJobCallback")
}
return
} else if p.Status == 1 { /* handle that one resource from the objSubTypeMessageOrderbookAck msg */
} }
err = setJobDone(j.ID64) err = setJobDone(j.ID64)

41
obj.go
View File

@ -17,7 +17,9 @@ var (
cacheObjMsg *sync.Map cacheObjMsg *sync.Map
cacheObjItem map[string]ChatWarsItem cacheObjItem map[string]ChatWarsItem
cacheObjItemId map[int64]ChatWarsItem
muxObjItem sync.Mutex muxObjItem sync.Mutex
muxObjItemId sync.Mutex
) )
func getObjTypeId(objId int64) (int64, error) { func getObjTypeId(objId int64) (int64, error) {
@ -587,7 +589,7 @@ func addObjQuest(userID64 int64, questTypeID int, duration time.Duration, date t
return objId, nil return objId, nil
} }
func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64, error) { func addObjItem(code string, name string, itemTypeID64 int64, weight int, exchange bool, auction bool) (int64, error) {
muxObjItem.Lock() muxObjItem.Lock()
defer muxObjItem.Unlock() defer muxObjItem.Unlock()
@ -629,8 +631,8 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64
return 0, err return 0, err
} }
stmt, err := tx.Prepare(`INSERT INTO obj_item (obj_id, intl_id, weight) stmt, err := tx.Prepare(`INSERT INTO obj_item (obj_id, intl_id, weight, exchange, auction)
VALUES (?, ?, ?);`) VALUES (?, ?, ?, ?, ?);`)
logOnError(err, "addObjItem : prepare insert obj_item") logOnError(err, "addObjItem : prepare insert obj_item")
if err != nil { if err != nil {
err2 := tx.Rollback() err2 := tx.Rollback()
@ -639,7 +641,19 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64
} }
defer stmt.Close() defer stmt.Close()
_, err = stmt.Exec(objId, code, weight) var e, a int
if exchange {
e = 1
} else {
e = 0
}
if auction {
a = 1
} else {
a = 0
}
_, err = stmt.Exec(objId, code, weight, e, a)
logOnError(err, "addObjItem : exec insert obj_item") logOnError(err, "addObjItem : exec insert obj_item")
if err != nil { if err != nil {
err2 := tx.Rollback() err2 := tx.Rollback()
@ -661,9 +675,15 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64
c.Code = code c.Code = code
c.Name = name c.Name = name
c.Weight = weight c.Weight = weight
c.Exchange = exchange
c.Auction = auction
cacheObjItem[code] = *c cacheObjItem[code] = *c
cacheObjItem[name] = *c cacheObjItem[name] = *c
muxObjItemId.Lock()
cacheObjItemId[objId] = *c
defer muxObjItemId.Unlock()
return objId, nil return objId, nil
} }
@ -680,6 +700,19 @@ func getObjItemID(c string, n string) int64 {
return i return i
} }
func getObjItem(objItemID64 int64) (*ChatWarsItem, error) {
muxObjItemId.Lock()
defer muxObjItemId.Unlock()
muxObjItem.Lock()
defer muxObjItem.Unlock()
if obj, ok := cacheObjItemId[objItemID64]; ok {
//log.Printf("Matching item name %s with %s.\n", name, obj.Name)
return obj, nil
} else {
return nil, errors.New("Item not found.")
}
}
func getSilentObjItemID(code string, name string) int64 { func getSilentObjItemID(code string, name string) int64 {
muxObjItem.Lock() muxObjItem.Lock()
defer muxObjItem.Unlock() defer muxObjItem.Unlock()

4
sql.go
View File

@ -298,7 +298,7 @@ func initDB() {
obj_id BIGINT UNSIGNED NOT NULL obj_id BIGINT UNSIGNED NOT NULL
,intl_id VARCHAR(32) ,intl_id VARCHAR(32)
,weight SMALLINT NOT NULL ,weight SMALLINT NOT NULL
,trade TINYINT NOT NULL ,exchange TINYINT NOT NULL
,auction TINYINT NOT NULL ,auction TINYINT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (intl_id) ,UNIQUE KEY (intl_id)
@ -353,7 +353,7 @@ func initDB() {
obj_id BIGINT UNSIGNED NOT NULL obj_id BIGINT UNSIGNED NOT NULL
,priority SMALLINT NOT NULL ,priority SMALLINT NOT NULL
,user_id BIGINT UNSIGNED NOT NULL ,user_id BIGINT UNSIGNED NOT NULL
,status SMALLINT NOT NULL ,trigger BIGINT UNSIGNED NOT NULL
,schedule DATETIME NOT NULL ,schedule DATETIME NOT NULL
,is_done TINYINT NOT NULL ,is_done TINYINT NOT NULL
,in_work TINYINT NOT NULL ,in_work TINYINT NOT NULL

View File

@ -408,7 +408,7 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
Date: m.Date, Date: m.Date,
} }
b, _ := json.Marshal(&p) b, _ := json.Marshal(&p)
_, err = createJob(objSubTypeJobPillage, objJobPriority, m.TGUserID64, m.Date.Add(time.Duration(25+rand.Intn(35))*time.Second), b) _, err = createJob(objSubTypeJobPillage, objJobPriority, m.TGUserID64, m.ObjID64, m.Date.Add(time.Duration(25+rand.Intn(35))*time.Second), b)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)")
} }
case objSubTypeMessageMeAck: case objSubTypeMessageMeAck:
@ -455,7 +455,7 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
ObjID64: m.ObjID64, ObjID64: m.ObjID64,
} }
b, _ := json.Marshal(&p) b, _ := json.Marshal(&p)
_, err = createJob(objSubTypeJobMsgRefresh, objJobPriority, m.TGUserID64, cwm.End.Add(5*time.Minute).UTC(), b) _, err = createJob(objSubTypeJobMsgRefresh, objJobPriority, m.TGUserID64, m.ObjID64, cwm.End.Add(5*time.Minute).UTC(), b)
/* hack for autobid - FIXME */ /* hack for autobid - FIXME */
if cwm.Price == 0 && cwm.ItemID64 == getObjItemID(`k05`, `Hunter blade`) { if cwm.Price == 0 && cwm.ItemID64 == getObjItemID(`k05`, `Hunter blade`) {
@ -472,7 +472,7 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
if mc1, mok1 := callbacks[m.TGUserID64]; mok1 { if mc1, mok1 := callbacks[m.TGUserID64]; mok1 {
if mc2, mok2 := mc1[msgParsingRules[i].MsgTypeID]; mok2 { if mc2, mok2 := mc1[msgParsingRules[i].MsgTypeID]; mok2 {
for j := range mc2 { for j := range mc2 {
err := rescheduleJob(mc2[j], objJobStatusNew, time.Now().UTC()) err := rescheduleJob(mc2[j], m.ObjID64, time.Now().UTC())
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : callbacks triggering") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : callbacks triggering")
} }
mc1[msgParsingRules[i].MsgTypeID] = nil mc1[msgParsingRules[i].MsgTypeID] = nil