update client

This commit is contained in:
shoopea 2020-02-03 10:54:34 +08:00
parent 41d7e2a78f
commit de3e06de2c
7 changed files with 87 additions and 8 deletions

View File

@ -95,7 +95,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.CWIdle = false
clt.GameIdle = false
clt.CWBusyUntil = from.UTC().Add(duration)
log.Printf("setClientBusy[%s] : set for %s.\n", clt.Login, duration.String())
} else {
@ -112,7 +112,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.CWIdle = true
clt.GameIdle = true
clt.CWLastUpdate = from
log.Printf("setClientIdle[%s] : updated.\n", clt.Login)
} else {
@ -129,7 +129,7 @@ func getLockedIdleClient() (*ChirpClient, error) {
muxClients.RLock()
ids := make([]int64, 0)
for _, c := range clients {
if c.CWIdle {
if c.GameIdle {
ids = append(ids, c.TGUserID64)
fmt.Printf("getLockedIdleClient : appending %s (%d).\n", c.Login, c.TGUserID64)
}
@ -244,7 +244,7 @@ func clientMsgMeAck(m *ChatWarsMessageMeAck) {
clientSendCWMsg(m.Msg.TGUserID64, "/g_roles")
}
if m.State == `🛌Rest` {
clt.CWIdle = true
clt.GameIdle = true
}
}
}

22
cron.go
View File

@ -14,11 +14,12 @@ import (
func startCron() *cron.Cron {
c := cron.New(cron.WithLocation(time.UTC))
c.AddFunc("15 7,15,23 * * *", cronSendWarReport)
c.AddFunc("58 6,14,22 * * *", cronSetDef)
c.AddFunc("02 1,3,5,9,11,13,17,19,21 * * *", cronGetHammerTime)
c.AddFunc("10 7,15,23 * * *", cronGetHammerTime)
c.AddFunc("12 7,15,23 * * *", cronGetHammerTime)
c.AddFunc("13 3,7,11,15,19,23 * * *", cronTribute)
c.AddFunc("14 7,15,23 * * *", cronCheckVaultLimit)
c.AddFunc("15 7,15,23 * * *", cronSendWarReport)
c.AddFunc("@every 1m", cronSaveClients)
c.Start()
return c
@ -118,3 +119,20 @@ func cronSaveClients() {
logOnError(err, "cronSaveClients : Rename")
}
func cronCheckVaultLimit() {
clt, err := getLockedRandomClient()
logOnError(err, "cronGetHammerTime : getLockedRandomClient")
if err != nil {
return
}
clt.Mux.Unlock()
p := JobPayloadCheckVaultLimit{
Status: 0,
}
b, _ := json.Marshal(p)
t := time.Now().UTC().Add(1 * time.Second)
_, err = createJob(cacheObjSubType[`job_check_vault_limit`], objJobPriority, clt.TGUserID64, 0, t, b)
}

View File

@ -734,6 +734,11 @@
"name": "Craft all items summary job",
"obj_type": "job"
},
{
"intl_id": "job_check_vault_limit",
"name": "Check vault resource limit",
"obj_type": "job"
},
{
"intl_id": "item_res",
"name": "Resource",

View File

@ -1093,7 +1093,7 @@
],
"craft": {
"cmd": "/brew_62",
"mana": 10,
"mana": 15,
"items": [
{
"code": "44",

8
def.go
View File

@ -85,7 +85,8 @@ type ChirpClient struct {
CWClass string `json:"class"`
CWBusyUntil time.Time `json:"busy_until"`
CWLastUpdate time.Time `json:"last_update"`
CWIdle bool `json:"idle"`
GameIdle bool `json:"game_idle"`
BotIdle bool `json:"bot_idle"`
Mux sync.Mutex `json:"-"`
}
@ -506,6 +507,11 @@ type JobPayloadCraftAll struct {
VaultJobID64 int64 `json:"vault_job_id"`
}
type JobPayloadCheckVaultLimit struct {
Status int64 `json:"status"`
VaultJobID64 int64 `json:"vault_job_id"`
}
type JobPayloadGetVault struct {
Status int64 `json:"status"`
JobCallbackID64 int64 `json:"job_callback_id"`

48
job.go
View File

@ -2098,6 +2098,54 @@ func jobCraftAll(j Job) {
}
func jobCheckVaultLimit(j Job) {
var (
p JobPayloadCheckVaultLimit
p2 JobPayloadGetVault
b []byte
)
err := setJobStart(j.ID64)
logOnError(err, "jobCheckVaultLimit : setJobStart")
err = json.Unmarshal(j.Payload, &p)
logOnError(err, "jobCheckVaultLimit : Unmarshal payload")
if p.Status == 0 {
p2.JobCallbackID64 = j.ID64
p2.ItemTypeList = make([]int64, 0)
p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_res`])
p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_alch`])
b, err = json.Marshal(p2)
logOnError(err, "jobCheckVaultLimit : Marshal(p2)")
jobID64, err := createJob(cacheObjSubType[`job_get_vault`], objJobPriority, j.UserID64, 0, time.Now().UTC(), b)
p.Status = 1
p.VaultJobID64 = jobID64
b, err = json.Marshal(p)
logOnError(err, "jobCheckVaultLimit : Marshal(p)")
err = setJobPayloadJSON(j.ID64, p)
logOnError(err, "jobCheckVaultLimit : setJobPayloadJSON(p)")
rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
return
}
b = getJobPayload(p.VaultJobID64)
err = json.Unmarshal(b, &p2)
logOnError(err, "jobCheckVaultLimit : Unmarshal(p2)")
err = setJobDone(j.ID64)
logOnError(err, "jobCheckVaultLimit : setJobDone")
return
}
func jobGetVault(j Job) {
var (
p JobPayloadGetVault

View File

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