refactor(common): 重构 Conn 实体并优化地图进入逻辑
- 优化 Conn 实体的 SendPack 方法,提高代码复用性 - 添加 goja 模块到 go.work 文件 - 重构地图进入逻辑,增加玩家广播和刷怪功能 - 调整 OutInfo 结构中的 Vip 和 Viped 字段类型 - 简化 MonsterRefresh 结构体定义
This commit is contained in:
89
common/utils/goja/object_goslice_reflect.go
Normal file
89
common/utils/goja/object_goslice_reflect.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package goja
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/bits"
|
||||
"reflect"
|
||||
|
||||
"github.com/dop251/goja/unistring"
|
||||
)
|
||||
|
||||
type objectGoSliceReflect struct {
|
||||
objectGoArrayReflect
|
||||
}
|
||||
|
||||
func (o *objectGoSliceReflect) init() {
|
||||
o.objectGoArrayReflect._init()
|
||||
o.lengthProp.writable = true
|
||||
o.putIdx = o._putIdx
|
||||
}
|
||||
|
||||
func (o *objectGoSliceReflect) _putIdx(idx int, v Value, throw bool) bool {
|
||||
if idx >= o.fieldsValue.Len() {
|
||||
o.grow(idx + 1)
|
||||
}
|
||||
return o.objectGoArrayReflect._putIdx(idx, v, throw)
|
||||
}
|
||||
|
||||
func (o *objectGoSliceReflect) grow(size int) {
|
||||
oldcap := o.fieldsValue.Cap()
|
||||
if oldcap < size {
|
||||
n := reflect.MakeSlice(o.fieldsValue.Type(), size, growCap(size, o.fieldsValue.Len(), oldcap))
|
||||
reflect.Copy(n, o.fieldsValue)
|
||||
o.fieldsValue.Set(n)
|
||||
l := len(o.valueCache)
|
||||
if l > size {
|
||||
l = size
|
||||
}
|
||||
for i, w := range o.valueCache[:l] {
|
||||
if w != nil {
|
||||
w.setReflectValue(o.fieldsValue.Index(i))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tail := o.fieldsValue.Slice(o.fieldsValue.Len(), size)
|
||||
zero := reflect.Zero(o.fieldsValue.Type().Elem())
|
||||
for i := 0; i < tail.Len(); i++ {
|
||||
tail.Index(i).Set(zero)
|
||||
}
|
||||
o.fieldsValue.SetLen(size)
|
||||
}
|
||||
}
|
||||
|
||||
func (o *objectGoSliceReflect) shrink(size int) {
|
||||
o.valueCache.shrink(size)
|
||||
tail := o.fieldsValue.Slice(size, o.fieldsValue.Len())
|
||||
zero := reflect.Zero(o.fieldsValue.Type().Elem())
|
||||
for i := 0; i < tail.Len(); i++ {
|
||||
tail.Index(i).Set(zero)
|
||||
}
|
||||
o.fieldsValue.SetLen(size)
|
||||
}
|
||||
|
||||
func (o *objectGoSliceReflect) putLength(v uint32, throw bool) bool {
|
||||
if bits.UintSize == 32 && v > math.MaxInt32 {
|
||||
panic(rangeError("Integer value overflows 32-bit int"))
|
||||
}
|
||||
newLen := int(v)
|
||||
curLen := o.fieldsValue.Len()
|
||||
if newLen > curLen {
|
||||
o.grow(newLen)
|
||||
} else if newLen < curLen {
|
||||
o.shrink(newLen)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (o *objectGoSliceReflect) setOwnStr(name unistring.String, val Value, throw bool) bool {
|
||||
if name == "length" {
|
||||
return o.putLength(o.val.runtime.toLengthUint32(val), throw)
|
||||
}
|
||||
return o.objectGoArrayReflect.setOwnStr(name, val, throw)
|
||||
}
|
||||
|
||||
func (o *objectGoSliceReflect) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
|
||||
if name == "length" {
|
||||
return o.val.runtime.defineArrayLength(&o.lengthProp, descr, o.putLength, throw)
|
||||
}
|
||||
return o.objectGoArrayReflect.defineOwnPropertyStr(name, descr, throw)
|
||||
}
|
||||
Reference in New Issue
Block a user