-
Notifications
You must be signed in to change notification settings - Fork 351
/
game.go
84 lines (70 loc) · 1.96 KB
/
game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package game
import (
"math/rand"
"strconv"
"strings"
"time"
"github.com/lonnng/nano"
"github.com/lonnng/nano/serialize/json"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var (
version = "" // 游戏版本
consume = map[int]int{} // 房卡消耗配置
forceUpdate = false
logger = log.WithField("component", "game")
)
// SetCardConsume 设置房卡消耗数量
func SetCardConsume(cfg string) {
for _, c := range strings.Split(cfg, ",") {
parts := strings.Split(c, "/")
if len(parts) < 2 {
logger.Warnf("无效的房卡配置: %s", c)
continue
}
round, card := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
rd, err := strconv.Atoi(round)
if err != nil {
continue
}
cd, err := strconv.Atoi(card)
if err != nil {
continue
}
consume[rd] = cd
}
logger.Infof("当前游戏房卡消耗配置: %+v", consume)
}
// Startup 初始化游戏服务器
func Startup() {
// set nano logger
nano.SetLogger(log.WithField("component", "nano"))
rand.Seed(time.Now().Unix())
version = viper.GetString("update.version")
heartbeat := viper.GetInt("core.heartbeat")
if heartbeat < 5 {
heartbeat = 5
}
nano.SetHeartbeatInterval(time.Duration(heartbeat) * time.Second)
// 房卡消耗配置
csm := viper.GetString("core.consume")
SetCardConsume(csm)
forceUpdate = viper.GetBool("update.force")
logger.Infof("当前游戏服务器版本: %s, 是否强制更新: %t, 当前心跳时间间隔: %d秒", version, forceUpdate, heartbeat)
logger.Info("game service starup")
// register game handler
nano.Register(defaultManager)
nano.Register(defaultDeskManager)
nano.Register(new(ClubManager))
// 加密管道
c := newCrypto()
pipeline := nano.NewPipeline()
pipeline.Inbound().PushBack(c.inbound)
pipeline.Outbound().PushBack(c.outbound)
// starx settings
nano.SetSerializer(json.NewSerializer())
addr := viper.GetString("core.addr")
// run it!
nano.Listen(addr, nano.WithPipeline(pipeline))
}