update job shops

This commit is contained in:
shoopea 2020-02-06 23:23:54 +08:00
parent 6ba48248e9
commit 4473b69002
6 changed files with 103 additions and 43 deletions

75
bot.go
View File

@ -774,56 +774,53 @@ func botGStock(m *ChatWarsMessage) {
func botShops(m *ChatWarsMessage) { func botShops(m *ChatWarsMessage) {
// fan out to all active and non idle clients // fan out to all active and non idle clients
/*
clts, err := getLockedAllIdleClient()
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: "Busy, please retry later.",
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
}
TGCmdQueue <- c
return
}
userID64 := clt.TGUserID64
clt.Mux.Unlock()
p := JobPayloadGStock{ clts, err := getAllIdleClientID64()
MsgID64: m.ID64, if err != nil {
ChatID64: m.ChatID64, c := TGCommand{
Status: 0, Type: commandReplyMsg,
Text: "Busy, please retry later.",
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
} }
b, _ := json.Marshal(p) TGCmdQueue <- c
t := time.Now().UTC().Add(1 * time.Second) return
_, err = createJob(cacheObjSubType[`job_gstock`], objJobPriority, userID64, 0, t, b) }
if err != nil { j := JobPayloadShops{
c := TGCommand{ Status: 0,
Type: commandReplyMsg, ChatID64: m.ChatID64,
Text: fmt.Sprintf("%s", err), MsgID64: m.ID64,
FromMsgID64: m.ID64, }
FromChatID64: m.ChatID64, b, err := json.Marshal(j)
} logOnError(err, "botShops : Marshal")
TGCmdQueue <- c jobID64, err := createJob(cacheObjSubType[`job_shops`], objJobPriority, clts[0], 0, time.Unix(maxUnixTimestamp, 0).UTC(), b)
} else {
c := TGCommand{ for i, id := range clts {
Type: commandReplyMsg, j2 := JobPayloadShopsSlave{
Text: "Stock requested", JobCallbackID64: jobID64,
FromMsgID64: m.ID64, Status: 0,
FromChatID64: m.ChatID64, Slaves: len(clts),
} Shops: make([]string, 0),
TGCmdQueue <- c
} }
*/ for j, link := range cfg.Bot.Shops {
if (j / len(clts)) == i {
j2.Shops = append(j2.Shops, link)
}
}
b, err = json.Marshal(j2)
logOnError(err, "botShops : Marshal Slave")
_, err = createJob(cacheObjSubType[`job_shops_slave`], objJobPriority, id, 0, time.Now().UTC(), b)
}
c := TGCommand{ c := TGCommand{
Type: commandReplyMsg, Type: commandReplyMsg,
Text: "Not implemented", Text: "Shops coming",
FromMsgID64: m.ID64, FromMsgID64: m.ID64,
FromChatID64: m.ChatID64, FromChatID64: m.ChatID64,
} }
TGCmdQueue <- c TGCmdQueue <- c
return return
} }

View File

@ -114,9 +114,7 @@ func setClientIdle(userID64 int64, from time.Time) error {
clt.CWBusyUntil = from clt.CWBusyUntil = from
clt.GameIdle = true clt.GameIdle = true
clt.CWLastUpdate = from clt.CWLastUpdate = from
log.Printf("setClientIdle[%s] : updated.\n", clt.Login)
} else { } else {
log.Printf("setClientIdle[%s] : not updated.\n", clt.Login)
} }
clt.Mux.Unlock() clt.Mux.Unlock()
return nil return nil
@ -131,7 +129,6 @@ func getLockedIdleClient() (*ChirpClient, error) {
for _, c := range clients { for _, c := range clients {
if c.GameIdle { if c.GameIdle {
ids = append(ids, c.TGUserID64) ids = append(ids, c.TGUserID64)
fmt.Printf("getLockedIdleClient : appending %s (%d).\n", c.Login, c.TGUserID64)
} }
} }
muxClients.RUnlock() muxClients.RUnlock()
@ -141,7 +138,6 @@ func getLockedIdleClient() (*ChirpClient, error) {
RndMux.Lock() RndMux.Lock()
id := RndSrc.Intn(len(ids)) id := RndSrc.Intn(len(ids))
fmt.Printf("getLockedIdleClient : pulled %s.\n", clients[ids[id]].Login)
RndMux.Unlock() RndMux.Unlock()
clients[ids[id]].Mux.Lock() clients[ids[id]].Mux.Lock()
@ -150,6 +146,33 @@ func getLockedIdleClient() (*ChirpClient, error) {
} }
func getLockedAllIdleClientID64() ([]int64, error) {
muxClients.RLock()
ids := make([]int64, 0)
for _, c := range clients {
if c.GameIdle {
ids = append(ids, c.TGUserID64)
clients[ids[id]].Mux.Lock()
}
}
muxClients.RUnlock()
return ids, nil
}
func getAllIdleClientID64() ([]int64, error) {
muxClients.RLock()
ids := make([]int64, 0)
for _, c := range clients {
if c.GameIdle {
ids = append(ids, c.TGUserID64)
}
}
muxClients.RUnlock()
return ids, nil
}
func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) { func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) {
muxClients.RLock() muxClients.RLock()
if c, ok := clients[id]; ok { if c, ok := clients[id]; ok {

View File

@ -769,6 +769,11 @@
"name": "Shops summary job", "name": "Shops summary job",
"obj_type": "job" "obj_type": "job"
}, },
{
"intl_id": "job_shops_slave",
"name": "Shops summary slave job",
"obj_type": "job"
},
{ {
"intl_id": "job_check_vault_limit", "intl_id": "job_check_vault_limit",
"name": "Check vault resource limit", "name": "Check vault resource limit",

15
def.go
View File

@ -557,6 +557,21 @@ type JobPayloadGetVault struct {
CleanupMsg []ChatWarsMessage `json:"cleanup_msg"` CleanupMsg []ChatWarsMessage `json:"cleanup_msg"`
} }
type JobPayloadShops struct {
MsgID64 int64 `json:"msg_id"`
ChatID64 int64 `json:"chat_id"`
Status int64 `json:"status"`
ShopMainAckMsg []ChatWarsMessage `json:"shop_main_ack_msg"`
CleanupMsg []ChatWarsMessage `json:"cleanup_msg"`
}
type JobPayloadShopsSlave struct {
Status int64 `json:"status"`
JobCallbackID64 int64 `json:"job_callback_id"`
Shops []string `json:"shops"`
Slaves int64 `json:"slaves"`
}
const ( const (
userID64ChtWrsBot = 408101137 userID64ChtWrsBot = 408101137

16
job.go
View File

@ -2179,6 +2179,22 @@ func jobCheckVaultLimit(j Job) {
} }
func jobShops(j Job) {
err = setJobDone(j.ID64)
logOnError(err, "jobShops : setJobDone")
return
}
func jobShopsSlave(j Job) {
err = setJobDone(j.ID64)
logOnError(err, "jobShopsSlave : setJobDone")
return
}
func jobGetVault(j Job) { func jobGetVault(j Job) {
var ( var (
p JobPayloadGetVault p JobPayloadGetVault

View File

@ -633,6 +633,10 @@ func JobWorker(id int, jobs <-chan Job) {
jobCraftAll(j) jobCraftAll(j)
case cacheObjSubType[`job_check_vault_limit`]: case cacheObjSubType[`job_check_vault_limit`]:
jobCheckVaultLimit(j) jobCheckVaultLimit(j)
case cacheObjSubType[`job_shops`]:
jobShops(j)
case cacheObjSubType[`job_shops_slave`]:
jobShopsSlave(j)
default: default:
log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID64) log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID64)
} }