tribally report logic
This commit is contained in:
@@ -2,9 +2,11 @@ package tribally_report
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"nova_task/internal/consts"
|
"nova_task/internal/consts"
|
||||||
|
"nova_task/internal/model"
|
||||||
"nova_task/internal/svc"
|
"nova_task/internal/svc"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -23,19 +25,48 @@ func (c *Cron) Spec() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cron) Run() {
|
func (c *Cron) Run() {
|
||||||
tr, err := c.svcCtx.GlobalDataModel.GetValue(c.ctx, consts.TriballyReportTime)
|
tr, err := c.svcCtx.GlobalDataModel.FindOneByKey(c.ctx, consts.TriballyReportTime)
|
||||||
if err != nil {
|
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||||
logx.Errorw("get tribally report time failed", logx.Field("err", err))
|
logx.Errorw("get tribally report time failed", logx.Field("err", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var updateTime time.Time
|
if tr == nil {
|
||||||
if tr != "" {
|
tr = &model.NhGlobalData{
|
||||||
updateTime, err = time.Parse(time.DateTime, tr)
|
Key: consts.TriballyReportTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var start time.Time
|
||||||
|
if tr.Value != "" {
|
||||||
|
start, err = time.Parse(time.DateTime, tr.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Errorw("parse tribally report time failed", logx.Field("err", err))
|
logx.Errorw("parse tribally report time failed", logx.Field("err", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
end := time.Now()
|
||||||
|
triballyUsers, err := c.svcCtx.TriballyUserModel.FindUpdateTriballyUsers(c.ctx, start, end, 100)
|
||||||
|
if err != nil {
|
||||||
|
logx.Errorw("find update tribally users failed", logx.Field("err", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.svcCtx.TriballyUserModel.FindUpdateTriballyUsers(c.ctx, updateTime, 100)
|
tr.Value = end.Format(time.DateTime)
|
||||||
|
if tr.Id > 0 {
|
||||||
|
err = c.svcCtx.GlobalDataModel.Update(c.ctx, tr)
|
||||||
|
} else {
|
||||||
|
_, err = c.svcCtx.GlobalDataModel.Insert(c.ctx, tr)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
logx.Errorw("insert or update tribally report time failed", logx.Field("err", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(triballyUsers) == 0 {
|
||||||
|
logx.Infow("no tribally users", logx.Field("start", start), logx.Field("end", end))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//for _, u := range triballyUsers {
|
||||||
|
//
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ type (
|
|||||||
NhTriballyUserModel interface {
|
NhTriballyUserModel interface {
|
||||||
nhTriballyUserModel
|
nhTriballyUserModel
|
||||||
withSession(session sqlx.Session) NhTriballyUserModel
|
withSession(session sqlx.Session) NhTriballyUserModel
|
||||||
FindUpdateTriballyUsers(ctx context.Context, t time.Time, count int) ([]*TriballyUserChapter, error)
|
FindUpdateTriballyUsers(ctx context.Context, start, end time.Time, count int) ([]*TriballyUserChapter, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
customNhTriballyUserModel struct {
|
customNhTriballyUserModel struct {
|
||||||
@@ -32,10 +32,10 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *customNhTriballyUserModel) FindUpdateTriballyUsers(ctx context.Context, t time.Time, count int) ([]*TriballyUserChapter, error) {
|
func (m *customNhTriballyUserModel) FindUpdateTriballyUsers(ctx context.Context, start, end time.Time, count int) ([]*TriballyUserChapter, error) {
|
||||||
query := fmt.Sprintf("SELECT t.uid, t.chapter, MAX(g.chapter) as max_chapter, g.updated_at FROM nh_tribally_user t JOIN nh_game_report g ON t.uid = g.uid WHERE g.updated_at >= ? GROUP BY uid LIMIT ?")
|
query := fmt.Sprintf("SELECT t.uid, t.chapter, MAX(g.chapter) as max_chapter, g.updated_at FROM nh_tribally_user t JOIN nh_game_report g ON t.uid = g.uid WHERE g.updated_at >= ? AND g.updated_at < ? AND g.chapter > t.chapter GROUP BY uid LIMIT ?")
|
||||||
var result []*TriballyUserChapter
|
var result []*TriballyUserChapter
|
||||||
err := m.conn.QueryRowsCtx(ctx, &result, query, t, count)
|
err := m.conn.QueryRowsCtx(ctx, &result, query, start, end, count)
|
||||||
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
|
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package tribally
|
package tribally
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -8,6 +9,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BindTribally(apiKey, userId string) (string, error) {
|
func BindTribally(apiKey, userId string) (string, error) {
|
||||||
@@ -92,3 +94,61 @@ func VerifyTribally(token string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserChapter struct {
|
||||||
|
Email string `json:"email"`
|
||||||
|
Chapter int `json:"chapter"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func PostUserChapter(apiKey string, chapters ...UserChapter) error {
|
||||||
|
var players []map[string]any
|
||||||
|
for _, c := range chapters {
|
||||||
|
players = append(players, map[string]any{
|
||||||
|
"playerId": c.Email,
|
||||||
|
"isWinner": true,
|
||||||
|
"chapter": c.Chapter,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
arg := map[string]any{
|
||||||
|
"activityId": "abc1234",
|
||||||
|
"gameStartedAt": time.Now().Format(time.DateTime),
|
||||||
|
"players": players,
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := json.Marshal(arg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
url := "https://api.tribally.games/activity/create"
|
||||||
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
req.Header.Add("Accept", "application/json")
|
||||||
|
req.Header.Add("x-api-key", apiKey)
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
return errors.New(res.Status)
|
||||||
|
}
|
||||||
|
result := struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
Code string `json:"code"`
|
||||||
|
}{}
|
||||||
|
data, _ := io.ReadAll(res.Body)
|
||||||
|
fmt.Println(string(data))
|
||||||
|
return nil
|
||||||
|
err = json.NewDecoder(res.Body).Decode(&result)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if result.Error != "" {
|
||||||
|
return fmt.Errorf("error: %s, code: %s", result.Error, result.Code)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
25
internal/pkg/tribally/tribally_test.go
Normal file
25
internal/pkg/tribally/tribally_test.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package tribally
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
apiKey = "088847b7172bdffa1eb564a581bcf903"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBindTribally(t *testing.T) {
|
||||||
|
url, err := BindTribally(apiKey, "lhj168os@gmail.com")
|
||||||
|
require.Nil(t, err)
|
||||||
|
fmt.Println(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostUserChapter(t *testing.T) {
|
||||||
|
err := PostUserChapter(apiKey, UserChapter{
|
||||||
|
Email: "lhj168os@gmail.com",
|
||||||
|
Chapter: 3,
|
||||||
|
})
|
||||||
|
require.Nil(t, err)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user