完善小游戏逻辑

This commit is contained in:
2024-10-18 22:42:58 +08:00
parent 8a8a6e0bfa
commit 83dfff5aa4
5 changed files with 162 additions and 123 deletions
+9 -51
View File
@@ -16,9 +16,10 @@ import (
"work_cation/repo"
)
type serverService struct {
type ServerService struct {
Status string
server *http.Server
Router *gin.Engine
}
const (
@@ -26,9 +27,9 @@ const (
StatusOffline = "offline"
)
var Server = &serverService{Status: StatusOffline}
var Server = &ServerService{Status: StatusOffline}
func (s *serverService) Online() error {
func (s *ServerService) Online() error {
if s.Status == StatusOnline {
return nil
}
@@ -42,7 +43,7 @@ func (s *serverService) Online() error {
return nil
}
func (s *serverService) StatusOffline() error {
func (s *ServerService) StatusOffline() error {
//if err := s.server.Close(); err != nil {
// return err
//}
@@ -53,8 +54,9 @@ func (s *serverService) StatusOffline() error {
return nil
}
func (s *serverService) StartListenServer() error {
func (s *ServerService) StartListenServer() error {
router := gin.Default()
s.Router = router
router.GET("/user", func(c *gin.Context) {
user := repo.User.GetUserInfo(global.DB)
c.JSON(200, user)
@@ -82,7 +84,7 @@ func (s *serverService) StartListenServer() error {
})
router.POST("/chat", func(c *gin.Context) {
user, msg, is := publicPostCheck(c)
user, msg, is := PublicPostCheck(c)
if !is {
return
}
@@ -98,50 +100,6 @@ func (s *serverService) StartListenServer() error {
c.JSON(200, gin.H{"message": "ok"})
})
// 开始游戏
router.POST("/start_game", func(c *gin.Context) {
user, msg, is := publicPostCheck(c)
if !is {
return
}
message := &models.GameMessage{
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"})
})
srv := &http.Server{
Addr: cfg.T.ServerAddr,
Handler: router,
@@ -206,7 +164,7 @@ func zipFolder(folderPath string, zipFile io.Writer) error {
return nil
}
func publicPostCheck(c *gin.Context) (*models.UserFollows, map[string]interface{}, bool) {
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() {