示例#1
0
文件: db_msg.go 项目: mysll/flynet
func GetEntityData(ent datatype.Entityer, base bool, depth int) *SaveEntity {
	if !base && !ent.IsSave() {
		return nil
	}
	s := &SaveEntity{}
	ent.SyncToDb()
	s.Typ = ent.ObjTypeName()
	s.DBId = ent.GetDbId()
	s.Obj = ent.GetSaveLoader()
	s.Index = ent.GetIndex()
	if ent.Base() != nil {
		s.Base = GetEntityData(ent.Base(), true, depth)
	}
	if base {
		return s
	}
	if depth > 1 {
		return s
	}

	clds := ent.GetChilds()
	l := len(clds)
	if l > 0 {
		s.Childs = make([]*SaveEntity, 0, l)
		for _, e := range clds {
			if e != nil {
				if child := GetEntityData(e, false, depth+1); child != nil {
					s.Childs = append(s.Childs, child)
				}
			}
		}
	}

	return s
}
示例#2
0
文件: area_msg.go 项目: mysll/flynet
func GetItemInfo(obj datatype.Entityer, syncchild bool) (*datatype.EntityInfo, error) {
	item := &datatype.EntityInfo{}
	buffer := new(bytes.Buffer)
	enc := gob.NewEncoder(buffer)
	err := enc.Encode(obj)
	if err != nil {
		log.LogError("encode ", obj.ObjTypeName(), "error,", err)
		return nil, err
	}
	item.Type = obj.ObjTypeName()
	item.Caps = obj.GetCapacity()
	item.DbId = obj.GetDbId()
	item.ObjId = obj.GetObjId()
	item.Index = obj.GetIndex()
	item.Data = buffer.Bytes()

	if syncchild {
		ls := obj.GetChilds()
		if len(ls) > 0 {
			item.Childs = make([]*datatype.EntityInfo, 0, len(ls))
		}
		for _, c := range ls {
			if c != nil {
				child, err := GetItemInfo(c, syncchild)
				if err != nil {
					return nil, err
				}
				item.Childs = append(item.Childs, child)
			}
		}
	}

	return item, nil
}