196 lines
5.5 KiB
Go
196 lines
5.5 KiB
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"chatapp3-golang/internal/common"
|
|
"chatapp3-golang/internal/service/gameprovider"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// registerGameProviderRoutes 注册统一游戏厂商 app 路由。
|
|
func registerGameProviderRoutes(engine *gin.Engine, javaClient authGateway, registry *gameprovider.Registry) {
|
|
if registry == nil {
|
|
return
|
|
}
|
|
|
|
appGroup := engine.Group("/app/game")
|
|
appGroup.Use(authMiddleware(javaClient))
|
|
appGroup.GET("/room/shortcut", func(c *gin.Context) {
|
|
resp, err := registry.ListShortcutGames(c.Request.Context(), mustAuthUser(c), c.Query("roomId"))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, stripProvidersFromRoomGameItems(resp))
|
|
})
|
|
appGroup.GET("/room/list", func(c *gin.Context) {
|
|
resp, err := registry.ListRoomGames(c.Request.Context(), mustAuthUser(c), c.Query("roomId"), c.Query("category"))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, stripProvidersFromRoomGameListResponse(resp))
|
|
})
|
|
appGroup.GET("/state", func(c *gin.Context) {
|
|
resp, err := registry.GetRoomState(c.Request.Context(), mustAuthUser(c), c.Query("roomId"))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, stripProviderFromRoomState(resp))
|
|
})
|
|
appGroup.POST("/launch", func(c *gin.Context) {
|
|
var req gameprovider.LaunchRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
writeError(c, common.NewAppError(http.StatusBadRequest, "bad_request", err.Error()))
|
|
return
|
|
}
|
|
resp, err := registry.LaunchGame(c.Request.Context(), mustAuthUser(c), req, resolveClientIP(c))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, stripProviderFromLaunch(resp))
|
|
})
|
|
appGroup.POST("/close", func(c *gin.Context) {
|
|
var req gameprovider.CloseRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
writeError(c, common.NewAppError(http.StatusBadRequest, "bad_request", err.Error()))
|
|
return
|
|
}
|
|
resp, err := registry.CloseGame(c.Request.Context(), mustAuthUser(c), req)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, stripProviderFromRoomState(resp))
|
|
})
|
|
|
|
providerGroup := engine.Group("/app/game/providers")
|
|
providerGroup.Use(authMiddleware(javaClient))
|
|
providerGroup.GET("", func(c *gin.Context) {
|
|
writeOK(c, registry.List())
|
|
})
|
|
providerGroup.GET("/:provider/room/shortcut", func(c *gin.Context) {
|
|
provider, ok := resolveGameProvider(c, registry)
|
|
if !ok {
|
|
return
|
|
}
|
|
resp, err := provider.ListShortcutGames(c.Request.Context(), mustAuthUser(c), c.Query("roomId"))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, gin.H{"items": resp})
|
|
})
|
|
providerGroup.GET("/:provider/room/list", func(c *gin.Context) {
|
|
provider, ok := resolveGameProvider(c, registry)
|
|
if !ok {
|
|
return
|
|
}
|
|
resp, err := provider.ListRoomGames(c.Request.Context(), mustAuthUser(c), c.Query("roomId"), c.Query("category"))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, resp)
|
|
})
|
|
providerGroup.GET("/:provider/state", func(c *gin.Context) {
|
|
provider, ok := resolveGameProvider(c, registry)
|
|
if !ok {
|
|
return
|
|
}
|
|
resp, err := provider.GetRoomState(c.Request.Context(), mustAuthUser(c), c.Query("roomId"))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, resp)
|
|
})
|
|
providerGroup.POST("/:provider/launch", func(c *gin.Context) {
|
|
provider, ok := resolveGameProvider(c, registry)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req gameprovider.LaunchRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
writeError(c, common.NewAppError(http.StatusBadRequest, "bad_request", err.Error()))
|
|
return
|
|
}
|
|
resp, err := provider.LaunchGame(c.Request.Context(), mustAuthUser(c), req, resolveClientIP(c))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, resp)
|
|
})
|
|
providerGroup.POST("/:provider/close", func(c *gin.Context) {
|
|
provider, ok := resolveGameProvider(c, registry)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req gameprovider.CloseRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
writeError(c, common.NewAppError(http.StatusBadRequest, "bad_request", err.Error()))
|
|
return
|
|
}
|
|
resp, err := provider.CloseGame(c.Request.Context(), mustAuthUser(c), req)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
writeOK(c, resp)
|
|
})
|
|
}
|
|
|
|
func resolveGameProvider(c *gin.Context, registry *gameprovider.Registry) (gameprovider.Provider, bool) {
|
|
provider, err := registry.Resolve(c.Param("provider"))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return nil, false
|
|
}
|
|
return provider, true
|
|
}
|
|
|
|
func stripProvidersFromRoomGameItems(items []gameprovider.RoomGameListItem) []gameprovider.RoomGameListItem {
|
|
if len(items) == 0 {
|
|
return []gameprovider.RoomGameListItem{}
|
|
}
|
|
result := make([]gameprovider.RoomGameListItem, 0, len(items))
|
|
for _, item := range items {
|
|
item.Provider = ""
|
|
result = append(result, item)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func stripProvidersFromRoomGameListResponse(resp *gameprovider.RoomGameListResponse) *gameprovider.RoomGameListResponse {
|
|
if resp == nil {
|
|
return &gameprovider.RoomGameListResponse{Items: []gameprovider.RoomGameListItem{}}
|
|
}
|
|
return &gameprovider.RoomGameListResponse{
|
|
Items: stripProvidersFromRoomGameItems(resp.Items),
|
|
}
|
|
}
|
|
|
|
func stripProviderFromRoomState(resp *gameprovider.RoomStateResponse) *gameprovider.RoomStateResponse {
|
|
if resp == nil {
|
|
return nil
|
|
}
|
|
copied := *resp
|
|
copied.Provider = ""
|
|
return &copied
|
|
}
|
|
|
|
func stripProviderFromLaunch(resp *gameprovider.LaunchResponse) *gameprovider.LaunchResponse {
|
|
if resp == nil {
|
|
return nil
|
|
}
|
|
copied := *resp
|
|
copied.Provider = ""
|
|
copied.RoomState = *stripProviderFromRoomState(&resp.RoomState)
|
|
return &copied
|
|
}
|