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) {
// 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{
MsgID64: m.ID64,
ChatID64: m.ChatID64,
Status: 0,
clts, err := getAllIdleClientID64()
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: "Busy, please retry later.",
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
}
b, _ := json.Marshal(p)
t := time.Now().UTC().Add(1 * time.Second)
_, err = createJob(cacheObjSubType[`job_gstock`], objJobPriority, userID64, 0, t, b)
TGCmdQueue <- c
return
}
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: fmt.Sprintf("%s", err),
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
}
TGCmdQueue <- c
} else {
c := TGCommand{
Type: commandReplyMsg,
Text: "Stock requested",
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
}
TGCmdQueue <- c
j := JobPayloadShops{
Status: 0,
ChatID64: m.ChatID64,
MsgID64: m.ID64,
}
b, err := json.Marshal(j)
logOnError(err, "botShops : Marshal")
jobID64, err := createJob(cacheObjSubType[`job_shops`], objJobPriority, clts[0], 0, time.Unix(maxUnixTimestamp, 0).UTC(), b)
for i, id := range clts {
j2 := JobPayloadShopsSlave{
JobCallbackID64: jobID64,
Status: 0,
Slaves: len(clts),
Shops: make([]string, 0),
}
*/
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{
Type: commandReplyMsg,
Text: "Not implemented",
Text: "Shops coming",
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
}
TGCmdQueue <- c
return
}

View File

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

View File

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

15
def.go
View File

@ -557,6 +557,21 @@ type JobPayloadGetVault struct {
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 (
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) {
var (
p JobPayloadGetVault

View File

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