From 11a9903a8939faf9e9fdc677de13e58a8a19671f Mon Sep 17 00:00:00 2001 From: shoopea Date: Sun, 1 Mar 2020 19:54:30 +0800 Subject: [PATCH] update withdraw --- client.go | 12 +++---- def.go | 2 +- job.go | 98 ++++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 75 insertions(+), 37 deletions(-) diff --git a/client.go b/client.go index 19e7d5a..86d2d6c 100644 --- a/client.go +++ b/client.go @@ -100,7 +100,7 @@ func getLockedRandomClient() (*ChirpClient, error) { func setClientBusy(userID64 int64, from time.Time, duration time.Duration) error { if clt, ok := getLockedClient(userID64, false); ok { if from.UTC().Add(duration).After(time.Now().UTC()) { - clt.GameIdle = false + clt.CWIdle = false clt.CWBusyUntil = from.UTC().Add(duration) log.Printf("setClientBusy[%s] : set for %s.\n", clt.Login, duration.String()) } else { @@ -117,7 +117,7 @@ func setClientIdle(userID64 int64, from time.Time) error { if clt, ok := getLockedClient(userID64, false); ok { if from.UTC().After(clt.CWLastUpdate.UTC()) { clt.CWBusyUntil = from - clt.GameIdle = true + clt.CWIdle = true clt.CWLastUpdate = from } else { } @@ -132,7 +132,7 @@ func getLockedIdleClient() (*ChirpClient, error) { muxClients.RLock() ids := make([]int64, 0) for _, c := range clients { - if c.GameIdle { + if c.CWIdle { ids = append(ids, c.TGUserID64) } } @@ -155,7 +155,7 @@ func getLockedAllIdleClientID64() ([]int64, error) { muxClients.RLock() ids := make([]int64, 0) for _, c := range clients { - if c.GameIdle { + if c.CWIdle { ids = append(ids, c.TGUserID64) clients[c.TGUserID64].Mux.Lock() } @@ -169,7 +169,7 @@ func getAllIdleClientID64() ([]int64, error) { muxClients.RLock() ids := make([]int64, 0) for _, c := range clients { - if c.GameIdle { + if c.CWIdle { ids = append(ids, c.TGUserID64) } } @@ -292,7 +292,7 @@ func clientMsgMeAck(m *ChatWarsMessageMeAck) { clientSendCWMsg(m.Msg.TGUserID64, "/g_roles") } if m.State == `🛌Rest` { - clt.GameIdle = true + clt.CWIdle = true } } } diff --git a/def.go b/def.go index d277a8c..de1c1c2 100644 --- a/def.go +++ b/def.go @@ -119,7 +119,7 @@ type ChirpClient struct { CWClass string `json:"class"` CWBusyUntil time.Time `json:"busy_until"` CWLastUpdate time.Time `json:"last_update"` - GameIdle bool `json:"game_idle"` + CWIdle bool `json:"game_idle"` BotIdle bool `json:"bot_idle"` Mux sync.Mutex `json:"-"` Config *ChirpConfig `json:"config"` diff --git a/job.go b/job.go index 255ee15..95804e1 100644 --- a/job.go +++ b/job.go @@ -1498,10 +1498,11 @@ func jobVaultUserStatus(j Job) { func jobGWithdraw(j Job) { var ( - p JobPayloadGWithdraw - p2 JobPayloadGetVault - b []byte - vault map[string]int64 + p JobPayloadGWithdraw + p2 JobPayloadGetVault + b []byte + vault map[string]int64 + stock, cmd string ) log.Printf("jobGWithdraw[%d] : Starting handling job.\n", j.ID64) @@ -1512,7 +1513,7 @@ func jobGWithdraw(j Job) { err = json.Unmarshal(j.Payload, &p) logOnError(err, "jobGWithdraw : Unmarshal payload") - if p.Status == 0 { + if p.Status == 0 { // fresh request, need to get vault to match items p2.JobCallbackID64 = j.ID64 p2.ItemTypeList = make([]int64, 0) @@ -1542,7 +1543,8 @@ func jobGWithdraw(j Job) { rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) return - } else if p.Status == 1 { + } + if p.Status == 1 { // match with vault and proceed /* loop through items and get unique/inspect */ b = getJobPayload(p.VaultJobID64) err = json.Unmarshal(b, &p2) @@ -1564,21 +1566,38 @@ func jobGWithdraw(j Job) { } } } - p.Status = 2 - err = setJobPayloadJSON(j.ID64, p) log.Printf("jobGWithdraw[%d] : received GetVault job.\n", j.ID64) if p.Inspecting != `` { + p.Status = 2 + err = setJobPayloadJSON(j.ID64, p) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_g_inspect_req`]) setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_inspect_ack`]) setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_invalid_action`]) clientSendCWMsg(j.UserID64, fmt.Sprintf("/g_inspect_%s", p.Inspecting)) log.Printf("jobGWithdraw[%d] : Inspecting missing unique item.\n", j.ID64) return + } else { + if p.Validated { + p.Status = 5 + } else { + p.Status = 3 + } + err = setJobPayloadJSON(j.ID64, p) } } + if p.Status == 2 { // need to inspect uniques + } + if p.Status == 3 { // need to get validation + + } + if p.Status == 4 { // verify validation + } + if p.Status == 5 { // validated + } if j.Trigger != 0 { id, err := getObjSubTypeId(j.Trigger) @@ -1657,7 +1676,7 @@ func jobGWithdraw(j Job) { return } } - case cacheObjSubType[`msg_msg_job_gwithdraw_ack`]: + case cacheObjSubType[`msg_job_gwithdraw_ack`]: log.Printf("jobGWithdraw[%d] : Handling withdrawal validation ack.\n", j.ID64) cwm, err := parseSubTypeMessageJobGWithdrawAck(m, rule.re) logOnError(err, "jobGWithdraw : parseSubTypeMessageJobGWithdrawAck") @@ -1693,6 +1712,7 @@ func jobGWithdraw(j Job) { return } else { p.Validated = true + err = setJobPayloadJSON(j.ID64, p) } } else { err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) @@ -1700,29 +1720,28 @@ func jobGWithdraw(j Job) { setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_job_gwithdraw_ack`]) return } - case cacheObjSubType[`msg_withdraw_code`]: - log.Printf("jobGWithdraw[%d] : Handling withdraw code.\n", j.ID64) + case cacheObjSubType[`msg_withdraw_req`]: + log.Printf("jobGWithdraw[%d] : Handling withdraw req.\n", j.ID64) if false /* check if it's ours */ { - for _, d := range p.CleanupMsg { - if d.TGSenderUserID64 == int64(bot.Me.ID) { - delmsg := tb.StoredMessage{ - MessageID: fmt.Sprintf("%d", d.ID64), - ChatID: d.ChatID64, - } - err = bot.Delete(delmsg) - logOnError(err, "jobGWithdraw : Delete") - } else { - - } - - } - p.CleanupMsg = []ChatWarsMessage{} + p.CleanupMsg = append(p.CleanupMsg, m) + err = setJobPayloadJSON(j.ID64, p) } else { err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) logOnError(err, "jobGWithdraw : rescheduleJob") - setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_job_gwithdraw_ack`]) + setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_withdraw_req`]) } - + return + case cacheObjSubType[`msg_withdraw_code`]: + log.Printf("jobGWithdraw[%d] : Handling withdraw code.\n", j.ID64) + if false /* check if it's ours */ { + p.CleanupMsg = append(p.CleanupMsg, m) + err = setJobPayloadJSON(j.ID64, p) + } else { + err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + logOnError(err, "jobGWithdraw : rescheduleJob") + setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_withdraw_code`]) + } + return default: log.Printf("jobGWithdraw[%d] : No handling for this message.\n", j.ID64) } @@ -1730,12 +1749,13 @@ func jobGWithdraw(j Job) { } log.Printf("jobGWithdraw[%d] : Preparing withdrawal guild link.\n", j.ID64) - var stock string for _, i := range p.Items { if i.Available > i.Required { stock = fmt.Sprintf("%s\n%d x %s", stock, i.Required, i.Name) + cmd = fmt.Sprintf("%s %s %d", cmd, i.Code, i.Required) } else if i.Available > 0 { stock = fmt.Sprintf("%s\n%d x %s", stock, i.Available, i.Name) + cmd = fmt.Sprintf("%s %s %d", cmd, i.Code, i.Available) } } @@ -1747,8 +1767,26 @@ func jobGWithdraw(j Job) { if err == nil { c.Mux.Unlock() } - setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_withdraw_code`]) - setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_withdraw_req`]) + + if !c.CWIdle { + d := c.CWBusyUntil - time.Now().UTC() + cmd := TGCommand{ + Type: commandReplyMsg, + Text: fmt.Sprintf("Busy for %s. Withdrawal queued.", d.String()), + FromMsgID64: p.MsgID64, + FromChatID64: p.ChatID64, + ParseMode: cmdParseModeHTML, + } + TGCmdQueue <- cmd + err = rescheduleJob(j.ID64, 0, c.CWBusyUntil.Add(1*time.Second).UTC()) + logOnError(err, "jobGWithdraw : rescheduleJob") + } else { + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_withdraw_code`]) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_withdraw_req`]) + err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + logOnError(err, "jobGWithdraw : rescheduleJob") + clientSendCWMsg(j.UserID64, fmt.Sprintf("/g_withdraw %s", cmd)) + } return } else { err := setJobPayloadJSON(j.ID64, p)