游戏节点配置嵌入在一个配置文件中
This commit is contained in:
22
cmd/root.go
22
cmd/root.go
@@ -4,8 +4,11 @@ Copyright © 2024 慕枫Go <mapleafgo@163.com>
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"game-driver/config"
|
"game-driver/config"
|
||||||
|
"game-driver/config/game"
|
||||||
"game-driver/internal"
|
"game-driver/internal"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -51,16 +54,29 @@ func init() {
|
|||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
|
||||||
// If a config file is found, read it in.
|
// If a config file is found, read it in.
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
if err := viper.ReadInConfig(); err == nil {
|
||||||
log.Printf("Using config file: %s", viper.ConfigFileUsed())
|
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)
|
err := viper.Unmarshal(&config.C)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln("unmarshal config failed: ", err)
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
config.yml
23
config.yml
@@ -1,6 +1,7 @@
|
|||||||
location: wushan
|
location: wushan
|
||||||
point: 0
|
point: 5
|
||||||
relay: COM7
|
relay: COM7
|
||||||
|
maxTimeout: 60 # 单位 s
|
||||||
log:
|
log:
|
||||||
level: debug
|
level: debug
|
||||||
file:
|
file:
|
||||||
@@ -18,13 +19,13 @@ aliyun:
|
|||||||
timeout: 10 # 单位 s
|
timeout: 10 # 单位 s
|
||||||
voice: zhifeng_emo
|
voice: zhifeng_emo
|
||||||
game:
|
game:
|
||||||
maxTimeout: 60 # 单位 s
|
url: rtu:///dev/ttyUSB0 # 点位 5 的串口地址
|
||||||
cardGroups:
|
# cardGroups: # 点位 4 的发卡器配置
|
||||||
- name: gpiochip0
|
# - name: gpiochip0
|
||||||
outOK: 6
|
# outOK: 6
|
||||||
lower: 13
|
# lower: 13
|
||||||
error: 19
|
# error: 19
|
||||||
empty: 26
|
# empty: 26
|
||||||
push: 11
|
# push: 11
|
||||||
reset: 22
|
# reset: 22
|
||||||
pull: 27
|
# pull: 27
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"game-driver/internal/routes/play/card_pusher"
|
|
||||||
"gopkg.in/natefinch/lumberjack.v2"
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,11 +16,6 @@ type AliyunConfig struct {
|
|||||||
Voice string
|
Voice string
|
||||||
}
|
}
|
||||||
|
|
||||||
type GameConfig struct {
|
|
||||||
MaxTimeout int
|
|
||||||
CardGroups []*card_pusher.LineGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
File *lumberjack.Logger
|
File *lumberjack.Logger
|
||||||
Level string
|
Level string
|
||||||
@@ -34,7 +28,7 @@ type config struct {
|
|||||||
Log Logger
|
Log Logger
|
||||||
Mqtt MqttConfig
|
Mqtt MqttConfig
|
||||||
Aliyun AliyunConfig
|
Aliyun AliyunConfig
|
||||||
Game GameConfig
|
MaxTimeout int
|
||||||
}
|
}
|
||||||
|
|
||||||
var C config
|
var C config
|
||||||
|
|||||||
16
config/game/game.go
Normal file
16
config/game/game.go
Normal 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
7
config/game/push_card.go
Normal 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
5
config/game/wait_card.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package game
|
||||||
|
|
||||||
|
type ConfigWait struct {
|
||||||
|
URL string
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ package play
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"game-driver/config"
|
"game-driver/config/game"
|
||||||
"game-driver/internal/middleware"
|
"game-driver/internal/middleware"
|
||||||
"game-driver/internal/routes/play/card_pusher"
|
"game-driver/internal/routes/play/card_pusher"
|
||||||
"game-driver/internal/schema"
|
"game-driver/internal/schema"
|
||||||
@@ -24,8 +24,10 @@ type ResponseBody struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PushCard(ctx context.Context) leaf.HandlerFunc {
|
func PushCard(ctx context.Context) leaf.HandlerFunc {
|
||||||
|
g := (game.G).(game.ConfigPush)
|
||||||
|
|
||||||
devices := make([]*card_pusher.Device, 0)
|
devices := make([]*card_pusher.Device, 0)
|
||||||
for _, group := range config.C.Game.CardGroups {
|
for _, group := range g.CardGroups {
|
||||||
gv, _ := json.Marshal(group)
|
gv, _ := json.Marshal(group)
|
||||||
zap.S().Info("发卡器指针:", string(gv))
|
zap.S().Info("发卡器指针:", string(gv))
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package play
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"game-driver/config/game"
|
||||||
"game-driver/internal/middleware"
|
"game-driver/internal/middleware"
|
||||||
"game-driver/internal/schema"
|
"game-driver/internal/schema"
|
||||||
"game-driver/leaf"
|
"game-driver/leaf"
|
||||||
@@ -9,14 +11,22 @@ import (
|
|||||||
"game-driver/pkg/channel"
|
"game-driver/pkg/channel"
|
||||||
"game-driver/pkg/tts"
|
"game-driver/pkg/tts"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WaitCard(ctx context.Context) leaf.HandlerFunc {
|
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 {
|
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() {
|
go func() {
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
@@ -99,7 +109,7 @@ func WaitCard(ctx context.Context) leaf.HandlerFunc {
|
|||||||
}
|
}
|
||||||
// 播报恭喜语音
|
// 播报恭喜语音
|
||||||
tts.DefaultTTS.Sound(cardOk)
|
tts.DefaultTTS.Sound(cardOk)
|
||||||
//TODO: 打开炫酷光效
|
//TODO: 打开炫酷光效,屏幕跳转恭喜页面
|
||||||
zap.S().Infof("读取到卡片数据%q,开始打开炫酷光效", id)
|
zap.S().Infof("读取到卡片数据%q,开始打开炫酷光效", id)
|
||||||
Default(c)
|
Default(c)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ func Run() {
|
|||||||
middleware.EmergencyStop(common.GlobalStopper),
|
middleware.EmergencyStop(common.GlobalStopper),
|
||||||
middleware.SoundStart(),
|
middleware.SoundStart(),
|
||||||
middleware.RelayMaster(nil),
|
middleware.RelayMaster(nil),
|
||||||
middleware.TimeoutOver(config.C.Game.MaxTimeout),
|
middleware.TimeoutOver(config.C.MaxTimeout),
|
||||||
middleware.TickerAction(),
|
middleware.TickerAction(),
|
||||||
middleware.PlayBgm(),
|
middleware.PlayBgm(),
|
||||||
routes.PlayRouter(ctx, config.C.Location, config.C.Point),
|
routes.PlayRouter(ctx, config.C.Location, config.C.Point),
|
||||||
|
|||||||
Reference in New Issue
Block a user