package play import ( "context" "encoding/json" "game-driver/config/game" "game-driver/internal/middleware" "game-driver/internal/routes/play/card_pusher" "game-driver/internal/schema" "game-driver/leaf" "game-driver/pkg/utils" "github.com/eclipse/paho.golang/paho" "go.uber.org/zap" "sync" "time" ) type ResponseBody struct { CardId string `json:"card_id"` Empty int `json:"empty"` Error int `json:"error"` OutOk int `json:"out_ok"` num int } func PushCard(ctx context.Context) leaf.HandlerFunc { g := (game.G).(game.ConfigPush) devices := make([]*card_pusher.Device, 0) for _, group := range g.CardGroups { gv, _ := json.Marshal(group) zap.S().Info("发卡器指针:", string(gv)) device, err := card_pusher.New(group) if err != nil { zap.S().Panicln("初始化发卡器失败: ", err) } devices = append(devices, device) } go func() { <-ctx.Done() for _, device := range devices { device.Close() } }() return func(c *leaf.Context) { payload := leaf.Value[*schema.PlayModal](c, middleware.PayloadJSONKey) var action time.Duration if a, ok := payload.Game["action"]; ok { if v, ok := a.(float64); ok { action = time.Duration(v) } } // 等待组 var wait sync.WaitGroup defer wait.Wait() // 结束信号通道 a := make(chan struct{}) // 发送结束信号 defer close(a) wait.Add(1) go func() { defer wait.Done() select { case <-a: case <-time.After(action * time.Second): body := &ResponseBody{} for _, device := range devices { body.Empty += device.GetEmpty() body.Error += device.GetError() } for i, device := range devices { if device.GetEmpty() == 0 && device.GetError() == 0 && device.GetOutOk() == 0 { body.num = i + 1 device.PushCard() break } } // 延迟1秒获取结果并发送消息 time.AfterFunc(time.Second, func() { if body.num != 0 { body.OutOk += devices[body.num-1].GetOutOk() } publishBody(ctx, c.Properties.ResponseTopic, body) }) } }() // 执行等待信号 Default(c) } } // 发布消息 func publishBody(ctx context.Context, topic string, body *ResponseBody) { if topic != "" { bytes, _ := json.Marshal(body) utils.GlobalMqttClient.Publish(ctx, &paho.Publish{ Topic: topic, Payload: bytes, QoS: 1, }) } }