网络游戏初步接入
This commit is contained in:
+56
-13
@@ -115,29 +115,45 @@ func (c *ClientService) Download(online *models.Online, uuid string, progress bi
|
||||
}
|
||||
|
||||
func (c *ClientService) Chat(online *models.Online, text string) (string, error) {
|
||||
var user = repo.User.GetUserInfo(global.DB)
|
||||
ctx, carnal := context.WithTimeout(context.TODO(), time.Millisecond*500)
|
||||
defer carnal()
|
||||
b, err := json.Marshal(gin.H{"text": text, "uuid": user.ID})
|
||||
req := gin.H{
|
||||
"text": text,
|
||||
}
|
||||
msg, err := PostSend("/chat", online, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
res, err := http.NewRequestWithContext(ctx, "POST", online.Url("/chat"), bytes.NewBuffer(b))
|
||||
return msg["message"].(string), err
|
||||
}
|
||||
|
||||
func (c *ClientService) StartGame(online *models.Online, gameUuid string, gameType int) (string, error) {
|
||||
req := gin.H{
|
||||
"type": gameType,
|
||||
"game_uuid": gameUuid,
|
||||
}
|
||||
msg, err := PostSend("/start_game", online, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
res.Header.Set("Content-Type", "application/json")
|
||||
res.Header.Set("User-UserID", user.ID)
|
||||
resp, err := http.DefaultClient.Do(res)
|
||||
return msg["message"].(string), err
|
||||
}
|
||||
|
||||
func (c *ClientService) PlayGame(online *models.Online, gameUuid string, pos int) (string, error) {
|
||||
req := gin.H{
|
||||
"pos": pos,
|
||||
"game_uuid": gameUuid,
|
||||
}
|
||||
msg, err := PostSend("/play_game", online, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
var msg = make(map[string]any)
|
||||
if err = json.Unmarshal(data, &msg); err != nil {
|
||||
return "", err
|
||||
return msg["message"].(string), err
|
||||
}
|
||||
|
||||
func (c *ClientService) CloseGame(online *models.Online, gameUuid string) (string, error) {
|
||||
req := gin.H{
|
||||
"game_uuid": gameUuid,
|
||||
}
|
||||
msg, err := PostSend("/close_game", online, req)
|
||||
return msg["message"].(string), err
|
||||
}
|
||||
|
||||
@@ -204,3 +220,30 @@ func unzipFile(zipFile, destDir string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func PostSend(router string, online *models.Online, req gin.H) (gin.H, error) {
|
||||
var user = repo.User.GetUserInfo(global.DB)
|
||||
ctx, carnal := context.WithTimeout(context.TODO(), time.Millisecond*500)
|
||||
defer carnal()
|
||||
b, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := http.NewRequestWithContext(ctx, "POST", online.Url(router), bytes.NewBuffer(b))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res.Header.Set("Content-Type", "application/json")
|
||||
res.Header.Set("User-User", user.ID)
|
||||
resp, err := http.DefaultClient.Do(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
var msg = make(map[string]any)
|
||||
if err = json.Unmarshal(data, &msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return msg, err
|
||||
}
|
||||
|
||||
+53
-26
@@ -66,17 +66,14 @@ func (s *serverService) StartListenServer() error {
|
||||
router.GET("/download/card/:uuid", func(c *gin.Context) {
|
||||
uuid := c.Param("uuid")
|
||||
card := repo.BaseCard.Find(global.DB, uuid)
|
||||
fmt.Println("card:", card)
|
||||
if card.UUID == "" {
|
||||
c.JSON(404, nil)
|
||||
return
|
||||
}
|
||||
cardPath := filepath.Join(cfg.T.CardDir, card.UUID)
|
||||
|
||||
// 设置响应头
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip", uuid))
|
||||
c.Header("Content-Type", "application/zip")
|
||||
|
||||
err := zipFolder(cardPath, c.Writer)
|
||||
if err != nil {
|
||||
c.JSON(500, err)
|
||||
@@ -85,15 +82,8 @@ func (s *serverService) StartListenServer() error {
|
||||
})
|
||||
|
||||
router.POST("/chat", func(c *gin.Context) {
|
||||
uuid := c.GetHeader("User-UserID")
|
||||
user := repo.UserFollow.GetUser(global.DB, uuid)
|
||||
if user.Ip != c.ClientIP() {
|
||||
c.JSON(200, gin.H{"message": "对方未关注你"})
|
||||
return
|
||||
}
|
||||
var msg = make(map[string]interface{})
|
||||
if err := c.ShouldBind(&msg); err != nil {
|
||||
c.JSON(200, gin.H{"message": "输入异常"})
|
||||
user, msg, is := publicPostCheck(c)
|
||||
if !is {
|
||||
return
|
||||
}
|
||||
message := &models.Message{
|
||||
@@ -105,23 +95,45 @@ func (s *serverService) StartListenServer() error {
|
||||
c.JSON(200, gin.H{"message": "ok"})
|
||||
})
|
||||
|
||||
// 游戏接口
|
||||
router.POST("/game", func(c *gin.Context) {
|
||||
uuid := c.GetHeader("User-UserID")
|
||||
user := repo.UserFollow.GetUser(global.DB, uuid)
|
||||
if user.Ip != c.ClientIP() {
|
||||
c.JSON(200, gin.H{"message": "对方未关注你"})
|
||||
// 开始游戏
|
||||
router.POST("/start_game", func(c *gin.Context) {
|
||||
user, msg, is := publicPostCheck(c)
|
||||
if !is {
|
||||
return
|
||||
}
|
||||
var msg = make(map[string]interface{})
|
||||
if err := c.ShouldBind(&msg); err != nil {
|
||||
c.JSON(200, gin.H{"message": "输入异常"})
|
||||
return
|
||||
}
|
||||
|
||||
message := &models.GameMessage{
|
||||
UserID: user.ID,
|
||||
Pos: msg["text"].(int),
|
||||
Router: "/start_game",
|
||||
User: &user.Users,
|
||||
GameType: int(msg["type"].(float64)),
|
||||
GameUuid: msg["game_uuid"].(string),
|
||||
}
|
||||
global.Send.Game1Chan <- message
|
||||
c.JSON(200, gin.H{"message": "ok"})
|
||||
})
|
||||
router.POST("/play_game", func(c *gin.Context) {
|
||||
user, msg, is := publicPostCheck(c)
|
||||
if !is {
|
||||
return
|
||||
}
|
||||
message := &models.GameMessage{
|
||||
Router: "/play_game",
|
||||
User: &user.Users,
|
||||
Pos: int(msg["pos"].(float64)),
|
||||
GameUuid: msg["game_uuid"].(string),
|
||||
}
|
||||
global.Send.Game1Chan <- message
|
||||
c.JSON(200, gin.H{"message": "ok"})
|
||||
})
|
||||
|
||||
router.POST("/close_game", func(c *gin.Context) {
|
||||
user, msg, is := publicPostCheck(c)
|
||||
if !is {
|
||||
return
|
||||
}
|
||||
message := &models.GameMessage{
|
||||
Router: "/close_game",
|
||||
User: &user.Users,
|
||||
GameUuid: msg["game_uuid"].(string),
|
||||
}
|
||||
global.Send.Game1Chan <- message
|
||||
c.JSON(200, gin.H{"message": "ok"})
|
||||
@@ -190,3 +202,18 @@ func zipFolder(folderPath string, zipFile io.Writer) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func publicPostCheck(c *gin.Context) (*models.UserFollows, map[string]interface{}, bool) {
|
||||
uuid := c.GetHeader("User-User")
|
||||
user := repo.UserFollow.GetUser(global.DB, uuid)
|
||||
if user.Ip != c.ClientIP() {
|
||||
c.JSON(200, gin.H{"message": "对方未关注你"})
|
||||
return nil, nil, false
|
||||
}
|
||||
var msg = make(map[string]interface{})
|
||||
if err := c.ShouldBind(&msg); err != nil {
|
||||
c.JSON(200, gin.H{"message": "输入异常"})
|
||||
return nil, nil, false
|
||||
}
|
||||
return user, msg, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user