update g withdraw

This commit is contained in:
shoopea 2020-01-30 12:41:47 +08:00
parent 1fc694532a
commit dec85e812c

193
job.go
View File

@ -1402,13 +1402,13 @@ func jobVaultUserStatus(j Job) {
func jobGWithdraw(j Job) {
var (
p JobPayloadGWithdraw
p2 JobPayloadGetVault
b []byte
p JobPayloadGWithdraw
p2 JobPayloadGetVault
b []byte
vault map[string]int64
)
log.Printf("jobGWithdraw[%d] : Starting.\n", j.ID64)
log.Printf("jobGWithdraw[%d] : Starting handling job.\n", j.ID64)
err := setJobStart(j.ID64)
logOnError(err, "jobGWithdraw : setJobStart")
@ -1442,6 +1442,8 @@ func jobGWithdraw(j Job) {
err = setJobPayloadJSON(j.ID64, p)
logOnError(err, "jobGWithdraw : setJobPayloadJSON(p)")
log.Printf("jobGWithdraw[%d] : Calling GetVault job.\n", j.ID64)
rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
return
} else if p.Status == 1 {
@ -1449,7 +1451,7 @@ func jobGWithdraw(j Job) {
b = getJobPayload(p.VaultJobID64)
err = json.Unmarshal(b, &p2)
logOnError(err, "jobGStock : Unmarshal(p2)")
vault = make(map[string]int64)
for _, i := range p2.Vault {
vault[i.Code] = i.Quantity
@ -1458,7 +1460,26 @@ func jobGWithdraw(j Job) {
for k, req := range p.Items {
ava, _ := vault[req.Code]
p.Items[k].Available = ava
if ava == 0 && regexp.MustMatch()
if ok, _ := regexp.MatchString(`^u[0-9]+$`, req.Code); ok {
if ava > 0 {
p.Items[k].Inspect = false
} else if p.Inspecting == `` {
p.Inspecting = req.Code
}
}
}
p.Status = 2
err = setJobPayloadJSON(j.ID64, p)
log.Printf("jobGWithdraw[%d] : received GetVault job.\n", j.ID64)
if p.Inspecting != `` {
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
}
}
@ -1467,11 +1488,22 @@ func jobGWithdraw(j Job) {
id, err := getObjSubTypeId(j.Trigger)
logOnError(err, "jobGWithdraw : getObjSubType("+strconv.FormatInt(j.Trigger, 10)+")")
if err == nil {
if id == cacheObjSubType[`msg_inspect_ack`] {
m, err := getObjMsg(j.Trigger)
logOnError(err, "jobGWithdraw : getObjMsg")
rule, err := getMsgParsingRule(m)
logOnError(err, "jobGWithdraw : getMsgParsingRule")
m, err := getObjMsg(j.Trigger)
logOnError(err, "jobGWithdraw : getObjMsg")
rule, err := getMsgParsingRule(m)
logOnError(err, "jobGWithdraw : getMsgParsingRule")
p.CleanupMsg = append(p.CleanupMsg, *m)
switch id {
case cacheObjSubType[`msg_g_inspect_req`]:
log.Printf("jobGWithdraw[%d] : Deleting unique inspect req.\n", j.ID64)
err = setJobPayloadJSON(j.ID64, p)
logOnError(err, "jobGWithdraw : setJobPayloadJSON")
err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
logOnError(err, "jobGWithdraw : rescheduleJob")
case cacheObjSubType[`msg_inspect_ack`]:
log.Printf("jobGWithdraw[%d] : Handling unique inspect ack.\n", j.ID64)
cwm, err := parseSubTypeMessageInspectAck(m, rule.re)
logOnError(err, "jobGWithdraw : parseSubTypeMessageInspectAck")
for k, req := range p.Items {
@ -1484,75 +1516,53 @@ func jobGWithdraw(j Job) {
}
p.Inspecting = ``
p.CleanupMsg = append(p.CleanupMsg, *m)
for _, req := range p.Items {
for k, req := range p.Items {
if req.Inspect {
p2 := JobPayloadMsgDel{
MsgTypeID64: cacheObjSubType[`msg_g_inspect_req`],
Delay: (10 * time.Second),
ObjMsgID64: 0,
}
b2, _ := json.Marshal(p2)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
p.Inspecting = req.Code
b, _ := json.Marshal(p)
p.Items[k].Inspect = false
err = setJobPayloadJSON(j.ID64, p)
logOnError(err, "jobGWithdraw : setJobPayloadJSON")
err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
logOnError(err, "jobGWithdraw : rescheduleJob")
id, err := createJob(cacheObjSubType[`job_gwithdraw`], objJobPriority, j.UserID64, 0, time.Unix(maxUnixTimestamp, 0).UTC(), b)
logOnError(err, "jobGWithdraw : createJob")
setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_invalid_action`])
setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_inspect_ack`])
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_g_inspect_req`])
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_invalid_action`])
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_inspect_ack`])
clientSendCWMsg(j.UserID64, fmt.Sprintf("/g_inspect_%s", p.Inspecting))
err = setJobDone(j.ID64)
logOnError(err, "jobGWithdraw : setJobDone")
return
}
}
} else if id == cacheObjSubType[`msg_invalid_action`] {
case cacheObjSubType[`msg_invalid_action`]:
log.Printf("jobGWithdraw[%d] : Handling invalid unique item.\n", j.ID64)
for k, req := range p.Items {
if req.Code == p.Inspecting {
p.Items[k].Available = 1
p.Items[k].Available = 0
p.Items[k].Inspect = false
break
}
}
p.Inspecting = ``
m, err := getObjMsg(j.Trigger)
logOnError(err, "jobGWithdraw : getObjMsg")
if err == nil {
p.CleanupMsg = append(p.CleanupMsg, *m)
}
for _, req := range p.Items {
for k, req := range p.Items {
if req.Inspect {
p2 := JobPayloadMsgDel{
MsgTypeID64: cacheObjSubType[`msg_g_inspect_req`],
Delay: (10 * time.Second),
ObjMsgID64: 0,
}
b2, _ := json.Marshal(p2)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
p.Inspecting = req.Code
b, _ := json.Marshal(p)
p.Items[k].Inspect = false
err = setJobPayloadJSON(j.ID64, p)
logOnError(err, "jobGWithdraw : setJobPayloadJSON")
err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
logOnError(err, "jobGWithdraw : rescheduleJob")
id, err := createJob(cacheObjSubType[`job_gwithdraw`], objJobPriority, j.UserID64, 0, time.Unix(maxUnixTimestamp, 0).UTC(), b)
logOnError(err, "jobGWithdraw : createJob")
setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_invalid_action`])
setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_inspect_ack`])
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_g_inspect_req`])
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_invalid_action`])
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_inspect_ack`])
clientSendCWMsg(j.UserID64, fmt.Sprintf("/g_inspect_%s", p.Inspecting))
err = setJobDone(j.ID64)
logOnError(err, "jobGWithdraw : setJobDone")
return
}
}
} else if id == cacheObjSubType[`msg_msg_job_gwithdraw_ack`] {
m, err := getObjMsg(j.Trigger)
logOnError(err, "jobGWithdraw : getObjMsg")
rule, err := getMsgParsingRule(m)
logOnError(err, "jobGWithdraw : getMsgParsingRule")
case cacheObjSubType[`msg_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")
in, err := hex.DecodeString(cwm.Ref)
@ -1565,10 +1575,10 @@ func jobGWithdraw(j Job) {
out := make([]byte, len(in))
c.Decrypt(out, in)
id, _ := binary.Uvarint(out[:8])
jobID64 := int64(id)
id, _ = binary.Uvarint(out[8:16])
userID64 := int64(id)
id1, _ := binary.Uvarint(out[:8])
jobID64 := int64(id1)
id2, _ = binary.Uvarint(out[8:16])
userID64 := int64(id2)
if jobID64 == j.ID64 {
if userID64 == cwm.Msg.TGSenderUserID64 {
@ -1579,31 +1589,17 @@ func jobGWithdraw(j Job) {
FromChatID64: cwm.Msg.ChatID64,
}
TGCmdQueue <- cmd
p.CleanupMsg = append(p.CleanupMsg, *m)
b, err := json.Marshal(p)
err = setJobPayload(j.ID64, b)
logOnError(err, "jobGWithdraw : setJobPayload")
err = setJobPayloadJSON(j.ID64, p)
logOnError(err, "jobGWithdraw : setJobPayloadJSON")
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`])
return
} else {
p.Validated = true
for _, d := range p.CleanupMsg {
delmsg := tb.StoredMessage{
MessageID: fmt.Sprintf("%d", d.ID64),
ChatID: d.ChatID64,
}
err = bot.Delete(delmsg)
logOnError(err, "jobGWithdraw : Delete")
}
p.CleanupMsg = []ChatWarsMessage{}
delmsg := tb.StoredMessage{
MessageID: fmt.Sprintf("%d", cwm.Msg.ID64),
ChatID: cwm.Msg.ChatID64,
}
err = bot.Delete(delmsg)
logOnError(err, "jobGWithdraw : Delete")
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_withdraw_code`])
setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_withdraw_req`])
// TODO
}
} else {
err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
@ -1613,6 +1609,31 @@ func jobGWithdraw(j Job) {
}
return
case cacheObjSubType[`msg_withdraw_code`]:
log.Printf("jobGWithdraw[%d] : Handling withdraw code.\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{}
} 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`])
}
default:
log.Printf("jobGWithdraw[%d] : No handling for this message.\n", j.ID64)
}
}
}
@ -1625,6 +1646,7 @@ 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 {
@ -1635,10 +1657,10 @@ func jobGWithdraw(j Job) {
}
if len(stock) > 0 {
b, _ := json.Marshal(p)
id, err := createJob(cacheObjSubType[`job_gwithdraw`], objJobPriority, j.UserID64, 0, time.Unix(maxUnixTimestamp, 0).UTC(), b)
logOnError(err, "jobGWithdraw : createJob")
setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_job_gwithdraw_ack`])
err := jobSetPayloadJSON(j.ID64, p)
logOnError(err, "jobGWithdraw : jobSetPayloadJSON")
setJobCallback(j.ID64, int64(bot.Me.ID), cacheObjSubType[`msg_job_gwithdraw_ack`])
sha256 := sha256.Sum256([]byte(cfg.Telegram.Token))
sha128 := sha256[:aes.BlockSize]
@ -1646,7 +1668,7 @@ func jobGWithdraw(j Job) {
in := make([]byte, 0)
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(id))
binary.LittleEndian.PutUint64(buf, uint64(j.ID64))
in = append(in, buf...)
binary.LittleEndian.PutUint64(buf, uint64(p.UserID64))
in = append(in, buf...)
@ -1680,6 +1702,7 @@ func jobGWithdraw(j Job) {
ParseMode: cmdParseModeHTML,
}
TGCmdQueue <- cmd
return
} else {
cmd := TGCommand{
Type: commandReplyMsg,