游戏节点配置嵌入在一个配置文件中

This commit is contained in:
2024-12-10 16:41:16 +08:00
parent 37fb40672a
commit 193fa806c2
9 changed files with 85 additions and 34 deletions

View File

@@ -4,8 +4,11 @@ Copyright © 2024 慕枫Go <mapleafgo@163.com>
package cmd
import (
"errors"
"game-driver/config"
"game-driver/config/game"
"game-driver/internal"
"io/fs"
"log"
"os"
@@ -45,22 +48,35 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.yml", "默认当前目录下的config.yml")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.yml", "默认当前目录下的 config.yml")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetConfigFile(cfgFile)
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Printf("Using config file: %s", viper.ConfigFileUsed())
} else if errors.Is(err, fs.ErrNotExist) {
log.Printf("无法找到主配置文件: %s", viper.ConfigFileUsed())
os.Exit(1)
} else {
log.Panicln("read config file error: ", err)
}
err := viper.Unmarshal(&config.C)
if err != nil {
log.Panicln("unmarshal config failed: ", err)
}
// 初始化游戏节点配置
game.G = game.NewConfig(config.C.Point)
if game.G != nil { // 如果需要游戏配置
err = viper.UnmarshalKey("game", &game.G)
if err != nil {
log.Panicln("unmarshal game config failed: ", err)
}
} else {
log.Panicln("game config not found")
}
}

View File

@@ -1,6 +1,7 @@
location: wushan
point: 0
point: 5
relay: COM7
maxTimeout: 60 # 单位 s
log:
level: debug
file:
@@ -18,13 +19,13 @@ aliyun:
timeout: 10 # 单位 s
voice: zhifeng_emo
game:
maxTimeout: 60 # s
cardGroups:
- name: gpiochip0
outOK: 6
lower: 13
error: 19
empty: 26
push: 11
reset: 22
pull: 27
url: rtu:///dev/ttyUSB0 # 5 的串口地址
# cardGroups: # 点位 4 的发卡器配置
# - name: gpiochip0
# outOK: 6
# lower: 13
# error: 19
# empty: 26
# push: 11
# reset: 22
# pull: 27

View File

@@ -1,7 +1,6 @@
package config
import (
"game-driver/internal/routes/play/card_pusher"
"gopkg.in/natefinch/lumberjack.v2"
)
@@ -17,24 +16,19 @@ type AliyunConfig struct {
Voice string
}
type GameConfig struct {
MaxTimeout int
CardGroups []*card_pusher.LineGroup
}
type Logger struct {
File *lumberjack.Logger
Level string
}
type config struct {
Location string
Point int
Relay string
Log Logger
Mqtt MqttConfig
Aliyun AliyunConfig
Game GameConfig
Location string
Point int
Relay string
Log Logger
Mqtt MqttConfig
Aliyun AliyunConfig
MaxTimeout int
}
var C config

16
config/game/game.go Normal file
View File

@@ -0,0 +1,16 @@
package game
type Config any
func NewConfig(point int) Config {
switch point {
case 4:
return ConfigPush{}
case 5:
return ConfigWait{}
default:
return nil
}
}
var G Config

7
config/game/push_card.go Normal file
View File

@@ -0,0 +1,7 @@
package game
import "game-driver/internal/routes/play/card_pusher"
type ConfigPush struct {
CardGroups []*card_pusher.LineGroup
}

5
config/game/wait_card.go Normal file
View File

@@ -0,0 +1,5 @@
package game
type ConfigWait struct {
URL string
}

View File

@@ -3,7 +3,7 @@ package play
import (
"context"
"encoding/json"
"game-driver/config"
"game-driver/config/game"
"game-driver/internal/middleware"
"game-driver/internal/routes/play/card_pusher"
"game-driver/internal/schema"
@@ -24,8 +24,10 @@ type ResponseBody struct {
}
func PushCard(ctx context.Context) leaf.HandlerFunc {
g := (game.G).(game.ConfigPush)
devices := make([]*card_pusher.Device, 0)
for _, group := range config.C.Game.CardGroups {
for _, group := range g.CardGroups {
gv, _ := json.Marshal(group)
zap.S().Info("发卡器指针:", string(gv))

View File

@@ -2,6 +2,8 @@ package play
import (
"context"
"errors"
"game-driver/config/game"
"game-driver/internal/middleware"
"game-driver/internal/schema"
"game-driver/leaf"
@@ -9,14 +11,22 @@ import (
"game-driver/pkg/channel"
"game-driver/pkg/tts"
"go.uber.org/zap"
"io/fs"
"os"
"sync"
"time"
)
func WaitCard(ctx context.Context) leaf.HandlerFunc {
reader, err := card_reader.NewReader("rtu:///dev/ttyUSB0")
g := (game.G).(game.ConfigWait)
reader, err := card_reader.NewReader(g.URL)
if err != nil {
zap.S().Panicln("读卡器串口连接失败", err)
if errors.Is(err, fs.ErrNotExist) {
zap.S().Errorf("读卡器串口文件不存在: %s", g.URL)
os.Exit(1)
}
zap.S().Panicf("读卡器串口连接失败 [%T]: %q", err, err)
}
go func() {
<-ctx.Done()
@@ -99,7 +109,7 @@ func WaitCard(ctx context.Context) leaf.HandlerFunc {
}
// 播报恭喜语音
tts.DefaultTTS.Sound(cardOk)
//TODO: 打开炫酷光效
//TODO: 打开炫酷光效,屏幕跳转恭喜页面
zap.S().Infof("读取到卡片数据%q开始打开炫酷光效", id)
Default(c)
}

View File

@@ -129,7 +129,7 @@ func Run() {
middleware.EmergencyStop(common.GlobalStopper),
middleware.SoundStart(),
middleware.RelayMaster(nil),
middleware.TimeoutOver(config.C.Game.MaxTimeout),
middleware.TimeoutOver(config.C.MaxTimeout),
middleware.TickerAction(),
middleware.PlayBgm(),
routes.PlayRouter(ctx, config.C.Location, config.C.Point),