update withdraw

This commit is contained in:
shoopea 2020-03-01 19:54:30 +08:00
parent 62cd5b4501
commit 11a9903a89
3 changed files with 75 additions and 37 deletions

View File

@ -100,7 +100,7 @@ func getLockedRandomClient() (*ChirpClient, error) {
func setClientBusy(userID64 int64, from time.Time, duration time.Duration) error { func setClientBusy(userID64 int64, from time.Time, duration time.Duration) error {
if clt, ok := getLockedClient(userID64, false); ok { if clt, ok := getLockedClient(userID64, false); ok {
if from.UTC().Add(duration).After(time.Now().UTC()) { if from.UTC().Add(duration).After(time.Now().UTC()) {
clt.GameIdle = false clt.CWIdle = false
clt.CWBusyUntil = from.UTC().Add(duration) clt.CWBusyUntil = from.UTC().Add(duration)
log.Printf("setClientBusy[%s] : set for %s.\n", clt.Login, duration.String()) log.Printf("setClientBusy[%s] : set for %s.\n", clt.Login, duration.String())
} else { } else {
@ -117,7 +117,7 @@ func setClientIdle(userID64 int64, from time.Time) error {
if clt, ok := getLockedClient(userID64, false); ok { if clt, ok := getLockedClient(userID64, false); ok {
if from.UTC().After(clt.CWLastUpdate.UTC()) { if from.UTC().After(clt.CWLastUpdate.UTC()) {
clt.CWBusyUntil = from clt.CWBusyUntil = from
clt.GameIdle = true clt.CWIdle = true
clt.CWLastUpdate = from clt.CWLastUpdate = from
} else { } else {
} }
@ -132,7 +132,7 @@ func getLockedIdleClient() (*ChirpClient, error) {
muxClients.RLock() muxClients.RLock()
ids := make([]int64, 0) ids := make([]int64, 0)
for _, c := range clients { for _, c := range clients {
if c.GameIdle { if c.CWIdle {
ids = append(ids, c.TGUserID64) ids = append(ids, c.TGUserID64)
} }
} }
@ -155,7 +155,7 @@ func getLockedAllIdleClientID64() ([]int64, error) {
muxClients.RLock() muxClients.RLock()
ids := make([]int64, 0) ids := make([]int64, 0)
for _, c := range clients { for _, c := range clients {
if c.GameIdle { if c.CWIdle {
ids = append(ids, c.TGUserID64) ids = append(ids, c.TGUserID64)
clients[c.TGUserID64].Mux.Lock() clients[c.TGUserID64].Mux.Lock()
} }
@ -169,7 +169,7 @@ func getAllIdleClientID64() ([]int64, error) {
muxClients.RLock() muxClients.RLock()
ids := make([]int64, 0) ids := make([]int64, 0)
for _, c := range clients { for _, c := range clients {
if c.GameIdle { if c.CWIdle {
ids = append(ids, c.TGUserID64) ids = append(ids, c.TGUserID64)
} }
} }
@ -292,7 +292,7 @@ func clientMsgMeAck(m *ChatWarsMessageMeAck) {
clientSendCWMsg(m.Msg.TGUserID64, "/g_roles") clientSendCWMsg(m.Msg.TGUserID64, "/g_roles")
} }
if m.State == `🛌Rest` { if m.State == `🛌Rest` {
clt.GameIdle = true clt.CWIdle = true
} }
} }
} }

2
def.go
View File

@ -119,7 +119,7 @@ type ChirpClient struct {
CWClass string `json:"class"` CWClass string `json:"class"`
CWBusyUntil time.Time `json:"busy_until"` CWBusyUntil time.Time `json:"busy_until"`
CWLastUpdate time.Time `json:"last_update"` CWLastUpdate time.Time `json:"last_update"`
GameIdle bool `json:"game_idle"` CWIdle bool `json:"game_idle"`
BotIdle bool `json:"bot_idle"` BotIdle bool `json:"bot_idle"`
Mux sync.Mutex `json:"-"` Mux sync.Mutex `json:"-"`
Config *ChirpConfig `json:"config"` Config *ChirpConfig `json:"config"`

86
job.go
View File

@ -1502,6 +1502,7 @@ func jobGWithdraw(j Job) {
p2 JobPayloadGetVault p2 JobPayloadGetVault
b []byte b []byte
vault map[string]int64 vault map[string]int64
stock, cmd string
) )
log.Printf("jobGWithdraw[%d] : Starting handling job.\n", j.ID64) log.Printf("jobGWithdraw[%d] : Starting handling job.\n", j.ID64)
@ -1512,7 +1513,7 @@ func jobGWithdraw(j Job) {
err = json.Unmarshal(j.Payload, &p) err = json.Unmarshal(j.Payload, &p)
logOnError(err, "jobGWithdraw : Unmarshal payload") 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.JobCallbackID64 = j.ID64
p2.ItemTypeList = make([]int64, 0) p2.ItemTypeList = make([]int64, 0)
@ -1542,7 +1543,8 @@ func jobGWithdraw(j Job) {
rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
return return
} else if p.Status == 1 { }
if p.Status == 1 { // match with vault and proceed
/* loop through items and get unique/inspect */ /* loop through items and get unique/inspect */
b = getJobPayload(p.VaultJobID64) b = getJobPayload(p.VaultJobID64)
err = json.Unmarshal(b, &p2) 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) log.Printf("jobGWithdraw[%d] : received GetVault job.\n", j.ID64)
if p.Inspecting != `` { if p.Inspecting != `` {
p.Status = 2
err = setJobPayloadJSON(j.ID64, p)
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_g_inspect_req`]) 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_inspect_ack`])
setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_invalid_action`]) setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_invalid_action`])
clientSendCWMsg(j.UserID64, fmt.Sprintf("/g_inspect_%s", p.Inspecting)) clientSendCWMsg(j.UserID64, fmt.Sprintf("/g_inspect_%s", p.Inspecting))
log.Printf("jobGWithdraw[%d] : Inspecting missing unique item.\n", j.ID64) log.Printf("jobGWithdraw[%d] : Inspecting missing unique item.\n", j.ID64)
return 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 { if j.Trigger != 0 {
id, err := getObjSubTypeId(j.Trigger) id, err := getObjSubTypeId(j.Trigger)
@ -1657,7 +1676,7 @@ func jobGWithdraw(j Job) {
return 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) log.Printf("jobGWithdraw[%d] : Handling withdrawal validation ack.\n", j.ID64)
cwm, err := parseSubTypeMessageJobGWithdrawAck(m, rule.re) cwm, err := parseSubTypeMessageJobGWithdrawAck(m, rule.re)
logOnError(err, "jobGWithdraw : parseSubTypeMessageJobGWithdrawAck") logOnError(err, "jobGWithdraw : parseSubTypeMessageJobGWithdrawAck")
@ -1693,6 +1712,7 @@ func jobGWithdraw(j Job) {
return return
} else { } else {
p.Validated = true p.Validated = true
err = setJobPayloadJSON(j.ID64, p)
} }
} else { } else {
err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) 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`]) setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_job_gwithdraw_ack`])
return return
} }
case cacheObjSubType[`msg_withdraw_code`]: case cacheObjSubType[`msg_withdraw_req`]:
log.Printf("jobGWithdraw[%d] : Handling withdraw code.\n", j.ID64) log.Printf("jobGWithdraw[%d] : Handling withdraw req.\n", j.ID64)
if false /* check if it's ours */ { if false /* check if it's ours */ {
for _, d := range p.CleanupMsg { p.CleanupMsg = append(p.CleanupMsg, m)
if d.TGSenderUserID64 == int64(bot.Me.ID) { err = setJobPayloadJSON(j.ID64, p)
delmsg := tb.StoredMessage{
MessageID: fmt.Sprintf("%d", d.ID64),
ChatID: d.ChatID64,
}
err = bot.Delete(delmsg)
logOnError(err, "jobGWithdraw : Delete")
} else {
}
}
p.CleanupMsg = []ChatWarsMessage{}
} else { } else {
err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
logOnError(err, "jobGWithdraw : rescheduleJob") 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: default:
log.Printf("jobGWithdraw[%d] : No handling for this message.\n", j.ID64) 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) log.Printf("jobGWithdraw[%d] : Preparing withdrawal guild link.\n", j.ID64)
var stock string
for _, i := range p.Items { for _, i := range p.Items {
if i.Available > i.Required { if i.Available > i.Required {
stock = fmt.Sprintf("%s\n%d x %s", stock, i.Required, i.Name) 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 { } else if i.Available > 0 {
stock = fmt.Sprintf("%s\n%d x %s", stock, i.Available, i.Name) 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 { if err == nil {
c.Mux.Unlock() c.Mux.Unlock()
} }
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_code`])
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_withdraw_req`]) 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 return
} else { } else {
err := setJobPayloadJSON(j.ID64, p) err := setJobPayloadJSON(j.ID64, p)