155 lines
3.5 KiB
Go
155 lines
3.5 KiB
Go
package tribally
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func BindTribally(apiKey, userId string) (string, error) {
|
|
url := "https://api.tribally.games/authenticate"
|
|
payload := strings.NewReader(fmt.Sprintf(`{"playerId": "%s"}`, userId))
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, payload)
|
|
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)
|
|
}
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
result := struct {
|
|
Error string `json:"error"`
|
|
Code string `json:"code"`
|
|
AuthURL string `json:"authUrl"`
|
|
}{}
|
|
err = json.Unmarshal(body, &result)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
logx.Errorw("============", logx.Field("body", string(body)))
|
|
if result.Error != "" {
|
|
return "", fmt.Errorf("error: %s, code: %s", result.Error, result.Code)
|
|
}
|
|
return result.AuthURL, nil
|
|
}
|
|
|
|
func VerifyTribally(token string) error {
|
|
url := "https://api.tribally.games/member/me"
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Accept", "application/json")
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result := struct {
|
|
Error string `json:"error"`
|
|
Code string `json:"code"`
|
|
Data struct {
|
|
UserID string `json:"userId"`
|
|
ScreenName string `json:"screenName"`
|
|
Slug string `json:"slug"`
|
|
HasBattlePass bool `json:"hasBattlePass"`
|
|
ProfileImage string `json:"profileImage"`
|
|
} `json:"data"`
|
|
}{}
|
|
err = json.Unmarshal(body, &result)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.Error != "" {
|
|
return fmt.Errorf("error: %s, code: %s", result.Error, result.Code)
|
|
}
|
|
|
|
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
|
|
}
|