diff --git a/internal/config/config.go b/internal/config/config.go index d28c5b4..4a1cfe8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -7,54 +7,56 @@ import ( ) type Config struct { - ListenAddr string - PublicBaseURL string - MySQLDSN string - RedisAddr string - RedisPassword string - RedisDB int - AutoMigrate bool - JavaAuthBaseURL string - JavaDeviceBaseURL string - JavaAppBaseURL string - JavaBridgeBaseURL string - JavaOtherBaseURL string - JavaWalletBaseURL string - BaishunPlatformBaseURL string - BaishunAppID int64 - BaishunAppChannel string - BaishunAppKey string - BaishunGSP int + ListenAddr string + PublicBaseURL string + MySQLDSN string + RedisAddr string + RedisPassword string + RedisDB int + AutoMigrate bool + JavaAuthBaseURL string + JavaDeviceBaseURL string + JavaAppBaseURL string + JavaBridgeBaseURL string + JavaOtherBaseURL string + JavaWalletBaseURL string + BaishunPlatformBaseURL string + BaishunAppID int64 + BaishunAppName string + BaishunAppChannel string + BaishunAppKey string + BaishunGSP int BaishunLaunchCodeTTLSeconds int BaishunSSTokenTTLSeconds int InternalCallbackSecret string - Timeout time.Duration + Timeout time.Duration } func Load() Config { return Config{ - ListenAddr: getEnv("INVITE_HTTP_ADDR", ":2900"), - PublicBaseURL: getEnv("INVITE_PUBLIC_BASE_URL", "http://localhost:2900"), - MySQLDSN: getEnv("INVITE_MYSQL_DSN", "root:root@tcp(127.0.0.1:3306)/chatapp3?charset=utf8mb4&parseTime=True&loc=Local"), - RedisAddr: getEnv("INVITE_REDIS_ADDR", "127.0.0.1:6379"), - RedisPassword: getEnv("INVITE_REDIS_PASSWORD", ""), - RedisDB: getEnvInt("INVITE_REDIS_DB", 0), - AutoMigrate: getEnvBool("INVITE_AUTO_MIGRATE", false), - JavaAuthBaseURL: getEnv("INVITE_JAVA_AUTH_BASE_URL", "http://127.0.0.1:2100"), - JavaDeviceBaseURL: getEnv("INVITE_JAVA_DEVICE_BASE_URL", "http://127.0.0.1:2400"), - JavaAppBaseURL: getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400"), - JavaBridgeBaseURL: getEnv("INVITE_JAVA_BRIDGE_BASE_URL", "http://127.0.0.1:2200"), - JavaOtherBaseURL: getEnv("GAME_JAVA_OTHER_BASE_URL", getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400")), - JavaWalletBaseURL: getEnv("GAME_JAVA_WALLET_BASE_URL", "http://127.0.0.1:2300"), - BaishunPlatformBaseURL: getEnv("BAISHUN_PLATFORM_BASE_URL", ""), - BaishunAppID: getEnvInt64("BAISHUN_APP_ID", 0), - BaishunAppChannel: getEnv("BAISHUN_APP_CHANNEL", ""), - BaishunAppKey: getEnv("BAISHUN_APP_KEY", ""), - BaishunGSP: getEnvInt("BAISHUN_GSP", 101), + ListenAddr: getEnv("INVITE_HTTP_ADDR", ":2900"), + PublicBaseURL: getEnv("INVITE_PUBLIC_BASE_URL", "http://localhost:2900"), + MySQLDSN: getEnv("INVITE_MYSQL_DSN", "root:root@tcp(127.0.0.1:3306)/chatapp3?charset=utf8mb4&parseTime=True&loc=Local"), + RedisAddr: getEnv("INVITE_REDIS_ADDR", "127.0.0.1:6379"), + RedisPassword: getEnv("INVITE_REDIS_PASSWORD", ""), + RedisDB: getEnvInt("INVITE_REDIS_DB", 0), + AutoMigrate: getEnvBool("INVITE_AUTO_MIGRATE", false), + JavaAuthBaseURL: getEnv("INVITE_JAVA_AUTH_BASE_URL", "http://127.0.0.1:2100"), + JavaDeviceBaseURL: getEnv("INVITE_JAVA_DEVICE_BASE_URL", "http://127.0.0.1:2400"), + JavaAppBaseURL: getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400"), + JavaBridgeBaseURL: getEnv("INVITE_JAVA_BRIDGE_BASE_URL", "http://127.0.0.1:2200"), + JavaOtherBaseURL: getEnv("GAME_JAVA_OTHER_BASE_URL", getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400")), + JavaWalletBaseURL: getEnv("GAME_JAVA_WALLET_BASE_URL", "http://127.0.0.1:2300"), + BaishunPlatformBaseURL: getEnv("BAISHUN_PLATFORM_BASE_URL", ""), + BaishunAppID: getEnvInt64("BAISHUN_APP_ID", 0), + BaishunAppName: getEnv("BAISHUN_APP_NAME", ""), + BaishunAppChannel: getEnv("BAISHUN_APP_CHANNEL", ""), + BaishunAppKey: getEnv("BAISHUN_APP_KEY", ""), + BaishunGSP: getEnvInt("BAISHUN_GSP", 101), BaishunLaunchCodeTTLSeconds: getEnvInt("BAISHUN_LAUNCH_CODE_TTL_SECONDS", 300), - BaishunSSTokenTTLSeconds: getEnvInt("BAISHUN_SS_TOKEN_TTL_SECONDS", 86400), - InternalCallbackSecret: getEnv("GAME_INTERNAL_CALLBACK_SECRET", ""), - Timeout: time.Duration(getEnvInt("INVITE_HTTP_TIMEOUT_SECONDS", 8)) * time.Second, + BaishunSSTokenTTLSeconds: getEnvInt("BAISHUN_SS_TOKEN_TTL_SECONDS", 86400), + InternalCallbackSecret: getEnv("GAME_INTERNAL_CALLBACK_SECRET", ""), + Timeout: time.Duration(getEnvInt("INVITE_HTTP_TIMEOUT_SECONDS", 8)) * time.Second, } } diff --git a/internal/service/baishun.go b/internal/service/baishun.go index f9d8db8..7ff0ad2 100644 --- a/internal/service/baishun.go +++ b/internal/service/baishun.go @@ -89,14 +89,14 @@ type RoomGameListResponse struct { } type BaishunRoomStateResponse struct { - RoomID string `json:"roomId"` - State string `json:"state"` - GameSessionID string `json:"gameSessionId,omitempty"` - CurrentGameID string `json:"currentGameId,omitempty"` - CurrentVendorGameID int `json:"currentVendorGameId,omitempty"` - CurrentGameName string `json:"currentGameName,omitempty"` - CurrentGameCover string `json:"currentGameCover,omitempty"` - HostUserID int64 `json:"hostUserId,omitempty"` + RoomID string `json:"roomId"` + State string `json:"state"` + GameSessionID string `json:"gameSessionId,omitempty"` + CurrentGameID string `json:"currentGameId,omitempty"` + CurrentVendorGameID int `json:"currentVendorGameId,omitempty"` + CurrentGameName string `json:"currentGameName,omitempty"` + CurrentGameCover string `json:"currentGameCover,omitempty"` + HostUserID int64 `json:"hostUserId,omitempty"` } type BaishunLaunchAppRequest struct { @@ -118,15 +118,16 @@ type BaishunGameConfig struct { } type BaishunBridgeConfig struct { - AppChannel string `json:"appChannel"` - AppID int64 `json:"appId"` - UserID string `json:"userId"` - Code string `json:"code"` - RoomID string `json:"roomId"` - GameMode string `json:"gameMode"` - Language string `json:"language"` - GSP int `json:"gsp"` - GameConfig BaishunGameConfig `json:"gameConfig"` + AppName string `json:"appName"` + AppChannel string `json:"appChannel"` + AppID int64 `json:"appId"` + UserID string `json:"userId"` + Code string `json:"code"` + RoomID string `json:"roomId"` + GameMode string `json:"gameMode"` + Language string `json:"language"` + GSP int `json:"gsp"` + GameConfig BaishunGameConfig `json:"gameConfig"` } type BaishunLaunchEntry struct { @@ -192,22 +193,22 @@ type BaishunUpdateTokenRequest struct { } type BaishunChangeBalanceRequest struct { - AppID int64 `json:"app_id"` - UserID string `json:"user_id"` - SSToken string `json:"ss_token"` - CurrencyDiff int64 `json:"currency_diff"` - DiffMsg string `json:"diff_msg"` - GameID int `json:"game_id"` - GameRoundID string `json:"game_round_id"` - RoomID string `json:"room_id"` - ChangeTimeAt int64 `json:"change_time_at"` - OrderID string `json:"order_id"` - Extend string `json:"extend"` - MsgType string `json:"msg_type"` - CurrencyType *int `json:"currency_type"` - Signature string `json:"signature"` - SignatureNonce string `json:"signature_nonce"` - Timestamp int64 `json:"timestamp"` + AppID int64 `json:"app_id"` + UserID string `json:"user_id"` + SSToken string `json:"ss_token"` + CurrencyDiff int64 `json:"currency_diff"` + DiffMsg string `json:"diff_msg"` + GameID int `json:"game_id"` + GameRoundID string `json:"game_round_id"` + RoomID string `json:"room_id"` + ChangeTimeAt int64 `json:"change_time_at"` + OrderID string `json:"order_id"` + Extend string `json:"extend"` + MsgType string `json:"msg_type"` + CurrencyType *int `json:"currency_type"` + Signature string `json:"signature"` + SignatureNonce string `json:"signature_nonce"` + Timestamp int64 `json:"timestamp"` } type BaishunBalanceInfoRequest struct { @@ -234,39 +235,39 @@ type baishunBalanceInfoResponse struct { } type gameListRow struct { - ConfigID int64 - SysOrigin string - InternalGameID string - Name string - Category string - Cover string - Sort int64 - FullScreen bool - GameModeRaw string - VendorType string - VendorGameID string - LaunchMode string - ExtPackageVersion string - ExtPreviewURL string - PackageURL string - ExtOrientation *int - ExtSafeHeight int - CurrencyIcon string - ExtGSP string - CatalogName string - CatalogCover string - CatalogPreviewURL string - CatalogDownloadURL string + ConfigID int64 + SysOrigin string + InternalGameID string + Name string + Category string + Cover string + Sort int64 + FullScreen bool + GameModeRaw string + VendorType string + VendorGameID string + LaunchMode string + ExtPackageVersion string + ExtPreviewURL string + PackageURL string + ExtOrientation *int + ExtSafeHeight int + CurrencyIcon string + ExtGSP string + CatalogName string + CatalogCover string + CatalogPreviewURL string + CatalogDownloadURL string CatalogPackageVersion string - CatalogOrientation *int - CatalogSafeHeight int - CatalogStatus string + CatalogOrientation *int + CatalogSafeHeight int + CatalogStatus string } type baishunPlatformResponse struct { - Code int `json:"code"` - Message string `json:"message"` - Data []baishunPlatformGame `json:"data"` + Code int `json:"code"` + Message string `json:"message"` + Data []baishunPlatformGame `json:"data"` } type baishunPlatformOneGameResponse struct { @@ -276,7 +277,7 @@ type baishunPlatformOneGameResponse struct { } type baishunPlatformGame struct { - GameID int `json:"game_id"` + GameID int `json:"game_id"` Name string `json:"name"` PreviewURL string `json:"preview_url"` GameVersion string `json:"game_version"` @@ -387,19 +388,19 @@ func (s *BaishunService) LaunchGame(ctx context.Context, user AuthUser, req Bais } roomState := model.BaishunRoomState{ - ID: roomStateID, - SysOrigin: user.SysOrigin, - RoomID: req.RoomID, - HostUserID: user.UserID, - CurrentGameID: row.InternalGameID, - CurrentGameName: row.name(), - CurrentGameCover: row.cover(), - GameSessionID: session.GameSessionID, - LaunchUserID: &user.UserID, - State: baishunRoomStatePlaying, - StartTime: &now, - CreateTime: now, - UpdateTime: now, + ID: roomStateID, + SysOrigin: user.SysOrigin, + RoomID: req.RoomID, + HostUserID: user.UserID, + CurrentGameID: row.InternalGameID, + CurrentGameName: row.name(), + CurrentGameCover: row.cover(), + GameSessionID: session.GameSessionID, + LaunchUserID: &user.UserID, + State: baishunRoomStatePlaying, + StartTime: &now, + CreateTime: now, + UpdateTime: now, } vendorGameID := row.vendorGameIDInt() roomState.CurrentVendorGameID = &vendorGameID @@ -435,6 +436,7 @@ func (s *BaishunService) LaunchGame(ctx context.Context, user AuthUser, req Bais SafeHeight: row.safeHeight(), }, BridgeConfig: BaishunBridgeConfig{ + AppName: s.cfg.BaishunAppName, AppChannel: defaultIfBlank(s.cfg.BaishunAppChannel, "skychat"), AppID: s.cfg.BaishunAppID, UserID: strconv.FormatInt(user.UserID, 10), @@ -449,14 +451,14 @@ func (s *BaishunService) LaunchGame(ctx context.Context, user AuthUser, req Bais }, }, RoomState: BaishunRoomStateResponse{ - RoomID: req.RoomID, - State: baishunRoomStatePlaying, - GameSessionID: session.GameSessionID, - CurrentGameID: row.InternalGameID, + RoomID: req.RoomID, + State: baishunRoomStatePlaying, + GameSessionID: session.GameSessionID, + CurrentGameID: row.InternalGameID, CurrentVendorGameID: row.vendorGameIDInt(), - CurrentGameName: row.name(), - CurrentGameCover: row.cover(), - HostUserID: user.UserID, + CurrentGameName: row.name(), + CurrentGameCover: row.cover(), + HostUserID: user.UserID, }, }, nil } @@ -483,14 +485,14 @@ func (s *BaishunService) CloseGame(ctx context.Context, user AuthUser, req Baish Model(&model.BaishunRoomState{}). Where("sys_origin = ? AND room_id = ?", user.SysOrigin, req.RoomID). Updates(map[string]any{ - "state": baishunRoomStateIdle, - "end_time": now, - "game_session_id": "", - "current_game_id": "", + "state": baishunRoomStateIdle, + "end_time": now, + "game_session_id": "", + "current_game_id": "", "current_vendor_game_id": nil, - "current_game_name": "", - "current_game_cover": "", - "update_time": now, + "current_game_name": "", + "current_game_cover": "", + "update_time": now, }).Error; err != nil { return nil, err } @@ -1017,6 +1019,9 @@ func (s *BaishunService) fetchPlatformGames(ctx context.Context) ([]baishunPlatf "signature_nonce": nonce, "timestamp": timestamp, } + if strings.TrimSpace(s.cfg.BaishunAppName) != "" { + payload["app_name"] = s.cfg.BaishunAppName + } body, _ := json.Marshal(payload) req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(s.cfg.BaishunPlatformBaseURL, "/")+"/v1/api/gamelist", bytes.NewReader(body)) if err != nil { @@ -1143,24 +1148,24 @@ func (s *BaishunService) persistOrderRecord(ctx context.Context, sysOrigin strin return } record := model.BaishunOrderIdempotency{ - ID: id, - SysOrigin: sysOrigin, - OrderID: req.OrderID, - GameRoundID: req.GameRoundID, - RoomID: req.RoomID, - UserID: parseInt64Default(req.UserID), - VendorGameID: req.GameID, - CurrencyDiff: req.CurrencyDiff, - DiffMsg: req.DiffMsg, - MsgType: req.MsgType, - CurrencyType: req.CurrencyType, - WalletEventID: walletEventID, + ID: id, + SysOrigin: sysOrigin, + OrderID: req.OrderID, + GameRoundID: req.GameRoundID, + RoomID: req.RoomID, + UserID: parseInt64Default(req.UserID), + VendorGameID: req.GameID, + CurrencyDiff: req.CurrencyDiff, + DiffMsg: req.DiffMsg, + MsgType: req.MsgType, + CurrencyType: req.CurrencyType, + WalletEventID: walletEventID, WalletAssetRecordID: assetRecordID, - Status: status, - RequestJSON: requestJSON, - ResponseJSON: responseJSON, - CreateTime: time.Now(), - UpdateTime: time.Now(), + Status: status, + RequestJSON: requestJSON, + ResponseJSON: responseJSON, + CreateTime: time.Now(), + UpdateTime: time.Now(), } _ = s.repo.DB.WithContext(ctx).Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "sys_origin"}, {Name: "order_id"}},