Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
feat: add forum sign
Browse files Browse the repository at this point in the history
  • Loading branch information
starudream committed May 29, 2024
1 parent 6d48e6f commit 844d046
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 11 deletions.
10 changes: 9 additions & 1 deletion api/kuro/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ const (
SourceH5 = "h5"

CodeHasSigned = Code(1511)
)

GameIdMC = "3" // 鸣潮
const (
GameIdPNS = 2 // 战双
GameIdMC = 3 // 鸣潮
)

var GameNames = map[int]string{
GameIdPNS: "战双",
GameIdMC: "鸣潮",
}

type BaseResp[T any] struct {
Code Code `json:"code"`
Msg string `json:"msg"`
Expand Down
6 changes: 4 additions & 2 deletions api/kuro/game.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kuro

import (
"strconv"

"github.com/starudream/go-lib/core/v2/gh"

"github.com/starudream/kuro-task/config"
Expand Down Expand Up @@ -45,7 +47,7 @@ type Role struct {
IsDefault bool `json:"isDefault"`
}

func ListRole(gid string, account config.Account) ([]*Role, error) {
req := R(account).SetFormData(gh.MS{"gameId": gid})
func ListRole(gid int, account config.Account) ([]*Role, error) {
req := R(account).SetFormData(gh.MS{"gameId": strconv.Itoa(gid)})
return Exec[[]*Role](req, "POST", "/gamer/role/list")
}
34 changes: 34 additions & 0 deletions api/kuro/sign_forum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kuro

import (
"strconv"

"github.com/starudream/go-lib/core/v2/gh"

"github.com/starudream/kuro-task/config"
)

type SignForumData struct {
GainVoList []*GainVo `json:"gainVoList"`
ContinueDays int `json:"continueDays"`
}

type GainVo struct {
GainTyp int `json:"gainTyp"`
GainValue int `json:"gainValue"`
}

func SignForum(gid int, account config.Account) (*SignForumData, error) {
req := R(account).SetFormData(gh.MS{"gameId": strconv.Itoa(gid)})
return Exec[*SignForumData](req, "POST", "/user/signIn")
}

type GetSignForumData struct {
ContinueDays int `json:"continueDays"`
HasSignIn bool `json:"hasSignIn"`
}

func GetSignForum(gid int, account config.Account) (*GetSignForumData, error) {
req := R(account).SetFormData(gh.MS{"gameId": strconv.Itoa(gid)})
return Exec[*GetSignForumData](req, "POST", "/user/signIn/info")
}
19 changes: 19 additions & 0 deletions api/kuro/sign_forum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kuro

import (
"testing"

"github.com/starudream/go-lib/core/v2/utils/testutil"

"github.com/starudream/kuro-task/config"
)

func TestSignForum(t *testing.T) {
data, err := SignForum(GameIdMC, config.C().FirstAccount())
testutil.LogNoErr(t, err, data)
}

func TestGetSignForum(t *testing.T) {
data, err := GetSignForum(GameIdMC, config.C().FirstAccount())
testutil.LogNoErr(t, err, data)
}
17 changes: 17 additions & 0 deletions cmd/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,27 @@ func cronRun() error {

func cronJob() {
for i := 0; i < len(config.C().Accounts); i++ {
cronForumAccount(config.C().Accounts[i])
cronGameAccount(config.C().Accounts[i])
}
}

func cronForumAccount(account config.Account) (msg string) {
records, err := job.SignForum(account)
if err != nil {
msg = fmt.Sprintf("%s: %v", records.Name(), err)
slog.Error(msg)
} else {
msg = account.Phone + " " + records.Success()
slog.Info(msg)
}
err = ntfy.Notify(context.Background(), msg)
if err != nil {
slog.Error("cron kuro notify error: %v", err)
}
return
}

func cronGameAccount(account config.Account) (msg string) {
records, err := job.SignGame(account)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ var (
c.Short = "Run sign task"
})

signForumCmd = cobra.NewCommand(func(c *cobra.Command) {
c.Use = "forum <account phone>"
c.Short = "Kuro forum task"
c.RunE = func(cmd *cobra.Command, args []string) error {
_, err := job.SignForum(xGetAccount(args))
return err
}
})

signGameCmd = cobra.NewCommand(func(c *cobra.Command) {
c.Use = "game <account phone>"
c.Short = "Kuro game award"
Expand All @@ -23,6 +32,7 @@ var (
)

func init() {
signCmd.AddCommand(signForumCmd)
signCmd.AddCommand(signGameCmd)

rootCmd.AddCommand(signCmd)
Expand Down
87 changes: 87 additions & 0 deletions job/forum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package job

import (
"cmp"
"fmt"
"slices"
"strings"

"github.com/starudream/go-lib/core/v2/slog"

"github.com/starudream/kuro-task/api/kuro"
"github.com/starudream/kuro-task/config"
)

type SignForumRecord struct {
GameId int
GameName string
HasSigned bool
}

type SignForumRecords []SignForumRecord

func (rs SignForumRecords) Name() string {
return "库街区每日任务"
}

func (rs SignForumRecords) Success() string {
vs := []string{rs.Name() + "完成"}
for i := 0; i < len(rs); i++ {
vs = append(vs,
fmt.Sprintf("在版区【%s】", rs[i].GameName),
fmt.Sprintf(" 打卡成功"),
)
}
return strings.Join(vs, "\n")
}

func SignForum(account config.Account) (SignForumRecords, error) {
_, err := kuro.GetUser(account)
if err != nil {
return nil, fmt.Errorf("get user error: %w", err)
}
return SignForumGames(account)
}

func SignForumGames(account config.Account) (SignForumRecords, error) {
var records []SignForumRecord
for id, name := range kuro.GameNames {
record, err := SignForumGame(kuro.GameIdMC, account)
record.GameId = id
record.GameName = name
slog.Info("sign forum record: %+v", record)
if err != nil {
slog.Error("sign forum error: %w", err)
continue
}
records = append(records, record)
}
slices.SortFunc(records, func(a, b SignForumRecord) int {
return cmp.Compare(a.GameId, b.GameId)
})
return records, nil
}

func SignForumGame(gid int, account config.Account) (record SignForumRecord, err error) {
today, err := kuro.GetSignForum(gid, account)
if err != nil {
err = fmt.Errorf("get sign forum error: %w", err)
return
}

record.HasSigned = today.HasSignIn

if today.HasSignIn {
return
}

_, err = kuro.SignForum(gid, account)
if err != nil {
err = fmt.Errorf("sign forum error: %w", err)
return
}

// todo: daily task for forum posts

return
}
15 changes: 7 additions & 8 deletions job/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"cmp"
"fmt"
"slices"
"strconv"
"strings"

"github.com/starudream/go-lib/core/v2/slog"
Expand All @@ -13,12 +12,8 @@ import (
"github.com/starudream/kuro-task/config"
)

var SignGameNameById = map[string]string{
kuro.GameIdMC: "鸣潮",
}

type SignGameRecord struct {
GameId string
GameId int
GameName string
ServerName string
RoleId string
Expand All @@ -42,6 +37,10 @@ func (rs SignGameRecords) Success() string {
}

func SignGame(account config.Account) (SignGameRecords, error) {
_, err := kuro.GetUser(account)
if err != nil {
return nil, fmt.Errorf("get user error: %w", err)
}
roles, err := kuro.ListRole(kuro.GameIdMC, account)
if err != nil {
return nil, fmt.Errorf("list role error: %w", err)
Expand Down Expand Up @@ -70,12 +69,12 @@ func SignGameRoles(roles []*kuro.Role, account config.Account) (SignGameRecords,
}

func SignGameRole(role *kuro.Role, account config.Account) (record SignGameRecord, err error) {
record.GameId = strconv.Itoa(role.GameId)
record.GameId = role.GameId
record.ServerName = role.ServerName
record.RoleId = role.RoleId
record.RoleName = role.RoleName

gameName, ok := SignGameNameById[record.GameId]
gameName, ok := kuro.GameNames[record.GameId]
if !ok {
err = fmt.Errorf("game id %d not support", role.GameId)
return
Expand Down

0 comments on commit 844d046

Please sign in to comment.