|
@@ -2,11 +2,18 @@ package player
|
|
|
|
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/aws/aws-sdk-go/aws"
|
|
|
|
+ "github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
+ "github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
+ "github.com/aws/aws-sdk-go/service/s3/s3manager"
|
|
jsoniter "github.com/json-iterator/go"
|
|
jsoniter "github.com/json-iterator/go"
|
|
mhayaString "github.com/mhaya/extend/string"
|
|
mhayaString "github.com/mhaya/extend/string"
|
|
mhayaTime "github.com/mhaya/extend/time"
|
|
mhayaTime "github.com/mhaya/extend/time"
|
|
"github.com/mhaya/game/game_cluster/internal/code"
|
|
"github.com/mhaya/game/game_cluster/internal/code"
|
|
"github.com/mhaya/game/game_cluster/internal/constant"
|
|
"github.com/mhaya/game/game_cluster/internal/constant"
|
|
|
|
+ "github.com/mhaya/game/game_cluster/internal/data"
|
|
"github.com/mhaya/game/game_cluster/internal/event"
|
|
"github.com/mhaya/game/game_cluster/internal/event"
|
|
"github.com/mhaya/game/game_cluster/internal/mdb"
|
|
"github.com/mhaya/game/game_cluster/internal/mdb"
|
|
"github.com/mhaya/game/game_cluster/internal/mdb/models"
|
|
"github.com/mhaya/game/game_cluster/internal/mdb/models"
|
|
@@ -16,6 +23,9 @@ import (
|
|
clog "github.com/mhaya/logger"
|
|
clog "github.com/mhaya/logger"
|
|
"github.com/mhaya/net/parser/pomelo"
|
|
"github.com/mhaya/net/parser/pomelo"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
+ "io"
|
|
|
|
+ "net/http"
|
|
|
|
+ "net/url"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -76,6 +86,8 @@ func (p *actorPlayer) OnInit() {
|
|
|
|
|
|
p.Timer().Add(1*time.Second, p.addRoll)
|
|
p.Timer().Add(1*time.Second, p.addRoll)
|
|
p.Timer().Add(10*time.Second, p.sessionClose)
|
|
p.Timer().Add(10*time.Second, p.sessionClose)
|
|
|
|
+ //p.Timer().Add(30*time.Second, p.getAndUpdateAvatar) //排行榜头像
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
func (p *actorPlayer) addRoll() {
|
|
func (p *actorPlayer) addRoll() {
|
|
@@ -137,6 +149,175 @@ func (p *actorPlayer) setDb() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// 获取和更新头像
|
|
|
|
+func (p *actorPlayer) getAndUpdateAvatar() {
|
|
|
|
+ if p.isOnline {
|
|
|
|
+
|
|
|
|
+ openid, _ := mhayaString.ToInt64(p.Player.OpenId)
|
|
|
|
+
|
|
|
|
+ proxyURL, err := url.Parse("http://127.0.0.1:22307")
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Debugf("Failed to parse proxy URL: %v", err.Error())
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 创建代理配置
|
|
|
|
+ proxy := http.ProxyURL(proxyURL)
|
|
|
|
+
|
|
|
|
+ // 创建自定义的 Transport
|
|
|
|
+ tr := &http.Transport{
|
|
|
|
+ Proxy: proxy,
|
|
|
|
+ TLSHandshakeTimeout: 5 * time.Second,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 使用自定义 Transport 创建 HTTP 客户端
|
|
|
|
+ client := &http.Client{
|
|
|
|
+ Transport: tr,
|
|
|
|
+ Timeout: 10 * time.Second, // 设置超时时间
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ sdkConfig := data.SdkConfig.Get(3).Params
|
|
|
|
+
|
|
|
|
+ url := fmt.Sprintf("https://api.telegram.org/bot%s/getUserProfilePhotos?user_id=%d", sdkConfig.BotToken, openid)
|
|
|
|
+
|
|
|
|
+ // 发起 HTTP 请求
|
|
|
|
+ req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Failed to create request: %v", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resp, err := client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Failed to send request: %v", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ body, err := io.ReadAll(resp.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Error reading body: %v", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ var result struct {
|
|
|
|
+ Ok bool `json:"ok"`
|
|
|
|
+ Result struct {
|
|
|
|
+ Photos [][]struct {
|
|
|
|
+ FileID string `json:"file_id"`
|
|
|
|
+ } `json:"photos"`
|
|
|
|
+ } `json:"result"`
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err = json.Unmarshal(body, &result)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Error unmarshalling JSON: %v", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if !result.Ok {
|
|
|
|
+ clog.Error("Error: %s", string(body))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if len(result.Result.Photos) == 0 {
|
|
|
|
+ clog.Error("No photos found for user %d", openid)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(result.Result.Photos) > 0 {
|
|
|
|
+ // 获取第一张照片的最大尺寸
|
|
|
|
+ photo := result.Result.Photos[0][len(result.Result.Photos[0])-1]
|
|
|
|
+
|
|
|
|
+ //根据数据库记录判断是否需要更新图片库
|
|
|
|
+ if photo.FileID != p.Player.Avatar {
|
|
|
|
+
|
|
|
|
+ if p.uploadPhotoToS3(client, sdkConfig.BotToken, photo.FileID, openid) == true {
|
|
|
|
+ p.Player.Avatar = photo.FileID
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (p *actorPlayer) uploadPhotoToS3(client *http.Client, botToken, fileId string, openid int64) bool {
|
|
|
|
+
|
|
|
|
+ s3 := data.SdkConfig.Get(3).S3
|
|
|
|
+
|
|
|
|
+ url := fmt.Sprintf("https://api.telegram.org/bot%s/getFile?file_id=%s", botToken, fileId)
|
|
|
|
+
|
|
|
|
+ // 发起 HTTP 请求
|
|
|
|
+ req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Failed to create request: %v", err)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resp, err := client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Failed to send request: %v", err)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+
|
|
|
|
+ body, err := io.ReadAll(resp.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Error reading body: %v", err)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var fileInfo struct {
|
|
|
|
+ Ok bool `json:"ok"`
|
|
|
|
+ Result struct {
|
|
|
|
+ FilePath string `json:"file_path"`
|
|
|
|
+ } `json:"result"`
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err = json.Unmarshal(body, &fileInfo)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Error unmarshalling JSON: %v", err)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if !fileInfo.Ok {
|
|
|
|
+ clog.Error("Error: %s", string(body))
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ downloadUrl := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", botToken, fileInfo.Result.FilePath)
|
|
|
|
+
|
|
|
|
+ resp, err = client.Get(downloadUrl)
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Error: %v", err)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+
|
|
|
|
+ // 配置 AWS 凭证
|
|
|
|
+ sess, err := session.NewSession(&aws.Config{
|
|
|
|
+ Credentials: credentials.NewStaticCredentials(
|
|
|
|
+ s3.AccessKey,
|
|
|
|
+ s3.SecretKey,
|
|
|
|
+ "",
|
|
|
|
+ ),
|
|
|
|
+ Region: aws.String(s3.Region), // 替换为你的 S3 区域
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ uploader := s3manager.NewUploader(sess)
|
|
|
|
+
|
|
|
|
+ // 上传文件到 S3
|
|
|
|
+ _, err = uploader.Upload(&s3manager.UploadInput{
|
|
|
|
+ Bucket: aws.String(s3.BucketName), // S3 存储桶名称
|
|
|
|
+ Key: aws.String(fmt.Sprintf("%d.jpg", openid)), //当前目录存储文件
|
|
|
|
+ Body: resp.Body,
|
|
|
|
+ ACL: aws.String("private"), // 可以根据需要设置 ACL
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ if err != nil {
|
|
|
|
+ clog.Error("Error uploading file: %v", err)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ return true
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
func (p *actorPlayer) refreshRoll() {
|
|
func (p *actorPlayer) refreshRoll() {
|
|
if p.isOnline {
|
|
if p.isOnline {
|
|
if p.Player.RefreshRoll() {
|
|
if p.Player.RefreshRoll() {
|