优化代码

This commit is contained in:
flswld
2023-03-12 02:08:15 +08:00
parent 3bcdf75448
commit 8c1dedc472
20 changed files with 407 additions and 296 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"runtime"
"strings"
"time"
@@ -35,6 +36,8 @@ type GameDataConfig struct {
ScenePointMap map[int32]*ScenePoint // 场景传送点
SceneTagDataMap map[int32]*SceneTagData // 场景标签
SceneLuaConfigMap map[int32]*SceneLuaConfig // 场景LUA配置
GroupMap map[int32]*Group // 场景LUA区块group索引
LuaStateLruMap map[int32]*LuaStateLru // 场景LUA虚拟机LRU内存淘汰
WorldAreaDataMap map[int32]*WorldAreaData // 世界区域
GatherDataMap map[int32]*GatherData // 采集物
GatherDataPointTypeMap map[int32]*GatherData // 采集物场景节点索引
@@ -60,6 +63,7 @@ func InitGameDataConfig() {
startTime := time.Now().Unix()
CONF.loadAll()
endTime := time.Now().Unix()
runtime.GC()
logger.Info("load all game data config finish, cost: %v(s)", endTime-startTime)
}
@@ -68,6 +72,7 @@ func ReloadGameDataConfig() {
startTime := time.Now().Unix()
CONF_RELOAD.loadAll()
endTime := time.Now().Unix()
runtime.GC()
logger.Info("reload all game data config finish, cost: %v(s)", endTime-startTime)
}

View File

@@ -6,7 +6,6 @@ import (
"hk4e/pkg/logger"
"github.com/jszwec/csvutil"
"github.com/mroth/weightedrand"
)
// ReliquaryAffixData 圣遗物追加属性配置表
@@ -46,35 +45,6 @@ func GetReliquaryAffixDataByDepotIdAndPropId(appendPropDepotId int32, appendProp
return value[appendPropId]
}
func GetReliquaryAffixDataRandomByDepotId(appendPropDepotId int32, excludeTypeList ...uint32) *ReliquaryAffixData {
appendPropMap, exist := CONF.ReliquaryAffixDataMap[appendPropDepotId]
if !exist {
return nil
}
choices := make([]weightedrand.Choice, 0, len(appendPropMap))
for _, data := range appendPropMap {
isBoth := false
// 排除列表中的属性类型是否相同
for _, propType := range excludeTypeList {
if propType == uint32(data.PropType) {
isBoth = true
break
}
}
if isBoth {
continue
}
choices = append(choices, weightedrand.NewChoice(data, uint(data.RandomWeight)))
}
chooser, err := weightedrand.NewChooser(choices...)
if err != nil {
logger.Error("reliquary append random error: %v", err)
return nil
}
result := chooser.Pick()
return result.(*ReliquaryAffixData)
}
func GetReliquaryAffixDataMap() map[int32]map[int32]*ReliquaryAffixData {
return CONF.ReliquaryAffixDataMap
}

View File

@@ -6,7 +6,6 @@ import (
"hk4e/pkg/logger"
"github.com/jszwec/csvutil"
"github.com/mroth/weightedrand"
)
// ReliquaryMainData 圣遗物主属性配置表
@@ -46,24 +45,6 @@ func GetReliquaryMainDataByDepotIdAndPropId(mainPropDepotId int32, mainPropId in
return value[mainPropId]
}
func GetReliquaryMainDataRandomByDepotId(mainPropDepotId int32) *ReliquaryMainData {
mainPropMap, exist := CONF.ReliquaryMainDataMap[mainPropDepotId]
if !exist {
return nil
}
choices := make([]weightedrand.Choice, 0, len(mainPropMap))
for _, data := range mainPropMap {
choices = append(choices, weightedrand.NewChoice(data, uint(data.RandomWeight)))
}
chooser, err := weightedrand.NewChooser(choices...)
if err != nil {
logger.Error("reliquary main random error: %v", err)
return nil
}
result := chooser.Pick()
return result.(*ReliquaryMainData)
}
func GetReliquaryMainDataMap() map[int32]map[int32]*ReliquaryMainData {
return CONF.ReliquaryMainDataMap
}

View File

@@ -2,9 +2,11 @@ package gdconf
import (
"os"
"sort"
"strconv"
"sync"
"sync/atomic"
"time"
"hk4e/pkg/logger"
@@ -68,7 +70,53 @@ type Group struct {
LuaState *lua.LState `json:"-"`
}
const (
LuaStateLruKeepNum = 1000
)
type LuaStateLru struct {
GroupId int32
AccessTime int64
}
type LuaStateLruList []*LuaStateLru
func (l LuaStateLruList) Len() int {
return len(l)
}
func (l LuaStateLruList) Less(i, j int) bool {
return l[i].AccessTime < l[j].AccessTime
}
func (l LuaStateLruList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func LuaStateLruRemove() {
removeNum := len(CONF.LuaStateLruMap) - LuaStateLruKeepNum
if removeNum <= 0 {
return
}
luaStateLruList := make(LuaStateLruList, 0)
for _, luaStateLru := range CONF.LuaStateLruMap {
luaStateLruList = append(luaStateLruList, luaStateLru)
}
sort.Stable(luaStateLruList)
for i := 0; i < removeNum; i++ {
luaStateLru := luaStateLruList[i]
group := GetSceneGroup(luaStateLru.GroupId)
group.LuaState = nil
delete(CONF.LuaStateLruMap, luaStateLru.GroupId)
}
logger.Info("lua state lru remove finish, remove num: %v", removeNum)
}
func (g *Group) GetLuaState() *lua.LState {
CONF.LuaStateLruMap[g.Id] = &LuaStateLru{
GroupId: g.Id,
AccessTime: time.Now().UnixMilli(),
}
if g.LuaState == nil {
g.LuaState = newLuaState(g.LuaStr)
scriptLib := g.LuaState.NewTable()
@@ -207,6 +255,8 @@ func (g *GameDataConfig) loadGroup(group *Group, block *Block, sceneId int32, bl
func (g *GameDataConfig) loadSceneLuaConfig() {
OBJECT_ID_COUNTER = 0
g.SceneLuaConfigMap = make(map[int32]*SceneLuaConfig)
g.GroupMap = make(map[int32]*Group)
g.LuaStateLruMap = make(map[int32]*LuaStateLru)
sceneLuaPrefix := g.luaPrefix + "scene/"
for _, sceneData := range g.SceneDataMap {
sceneId := sceneData.SceneId
@@ -279,6 +329,7 @@ func (g *GameDataConfig) loadSceneLuaConfig() {
<-wc
wg.Done()
}()
g.GroupMap[group.Id] = group
}
wg.Wait()
sceneLuaConfig.BlockMap[block.Id] = block
@@ -315,29 +366,10 @@ func GetSceneLuaConfigMap() map[int32]*SceneLuaConfig {
return CONF.SceneLuaConfigMap
}
func GetSceneBlockConfig(sceneId int32, blockId int32) ([]*Monster, []*Npc, []*Gadget, bool) {
monsterList := make([]*Monster, 0)
npcList := make([]*Npc, 0)
gadgetList := make([]*Gadget, 0)
sceneConfig, exist := CONF.SceneLuaConfigMap[sceneId]
func GetSceneGroup(groupId int32) *Group {
groupConfig, exist := CONF.GroupMap[groupId]
if !exist {
return nil, nil, nil, false
return nil
}
blockConfig, exist := sceneConfig.BlockMap[blockId]
if !exist {
return nil, nil, nil, false
}
for _, groupConfig := range blockConfig.GroupMap {
for _, monsterConfig := range groupConfig.MonsterList {
monsterList = append(monsterList, monsterConfig)
}
for _, npcConfig := range groupConfig.NpcList {
npcList = append(npcList, npcConfig)
}
for _, gadgetConfig := range groupConfig.GadgetList {
gadgetList = append(gadgetList, gadgetConfig)
}
}
return monsterList, npcList, gadgetList, true
return groupConfig
}