Skip to content

Commit

Permalink
uniform interfaces for Get, Set, SetDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaonanln committed Oct 24, 2017
1 parent c081b0f commit cc15506
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 79 deletions.
68 changes: 49 additions & 19 deletions engine/entity/ListAttr.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (a *ListAttr) setOwner(owner *Entity, flag attrFlag) {
}

// Set sets item value
func (a *ListAttr) Set(index int, val interface{}) {
func (a *ListAttr) set(index int, val interface{}) {
a.items[index] = val
if sa, ok := val.(*MapAttr); ok {
// val is ListAttr, set parent and owner accordingly
Expand Down Expand Up @@ -156,6 +156,36 @@ func (a *ListAttr) GetMapAttr(index int) *MapAttr {
return val.(*MapAttr)
}

// AppendInt puts int value to the end of list
func (a *ListAttr) AppendInt(v int64) {
a.append(v)
}

// AppendFloat puts float value to the end of list
func (a *ListAttr) AppendFloat(v float64) {
a.append(v)
}

// AppendBool puts bool value to the end of list
func (a *ListAttr) AppendBool(v bool) {
a.append(v)
}

// AppendStr puts string value to the end of list
func (a *ListAttr) AppendStr(v string) {
a.append(v)
}

// AppendMapAttr puts MapAttr value to the end of list
func (a *ListAttr) AppendMapAttr(attr *MapAttr) {
a.append(attr)
}

// AppendListAttr puts ListAttr value to the end of list
func (a *ListAttr) AppendListAttr(attr *ListAttr) {
a.append(attr)
}

// Pop removes the last item from the end
func (a *ListAttr) pop() interface{} {
size := len(a.items)
Expand Down Expand Up @@ -227,34 +257,34 @@ func (a *ListAttr) append(val interface{}) {
}
}

// AppendInt puts int value to the end of list
func (a *ListAttr) AppendInt(v int64) {
a.append(v)
// SetInt sets int value at the index
func (a *ListAttr) SetInt(index int, v int64) {
a.set(index, v)
}

// AppendFloat puts float value to the end of list
func (a *ListAttr) AppendFloat(v float64) {
a.append(v)
// SetFloat sets float value at the index
func (a *ListAttr) SetFloat(index int, v float64) {
a.set(index, v)
}

// AppendBool puts bool value to the end of list
func (a *ListAttr) AppendBool(v bool) {
a.append(v)
// SetBool sets bool value at the index
func (a *ListAttr) SetBool(index int, v bool) {
a.set(index, v)
}

// AppendStr puts string value to the end of list
func (a *ListAttr) AppendStr(v string) {
a.append(v)
// SetStr sets string value at the index
func (a *ListAttr) SetStr(index int, v string) {
a.set(index, v)
}

// AppendMapAttr puts MapAttr value to the end of list
func (a *ListAttr) AppendMapAttr(attr *MapAttr) {
a.append(attr)
// SetMapAttr sets MapAttr value at the index
func (a *ListAttr) SetMapAttr(index int, attr *MapAttr) {
a.set(index, attr)
}

// AppendListAttr puts ListAttr value to the end of list
func (a *ListAttr) AppendListAttr(attr *ListAttr) {
a.append(attr)
// SetListAttr sets ListAttr value at the index
func (a *ListAttr) SetListAttr(index int, attr *ListAttr) {
a.set(index, attr)
}

// ToList converts ListAttr to slice, recursively
Expand Down
94 changes: 83 additions & 11 deletions engine/entity/MapAttr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@ func (a *MapAttr) Keys() []string {
return keys
}

// ForEachKey calls f on all keys
func (a *MapAttr) ForEachKey(f func(key string)) {
for k, _ := range a.attrs {
f(k)
}
}

// ForEach calls f on all items
// Be careful about the type of val
func (a *MapAttr) ForEach(f func(key string, val interface{})) {
for k, v := range a.attrs {
f(k, v)
}
}

// Set sets the key-attribute pair in MapAttr
func (a *MapAttr) Set(key string, val interface{}) {
func (a *MapAttr) set(key string, val interface{}) {
var flag attrFlag
a.attrs[key] = val
if sa, ok := val.(*MapAttr); ok {
Expand Down Expand Up @@ -77,10 +85,75 @@ func (a *MapAttr) Set(key string, val interface{}) {
}
}

// SetDefault sets the key-attribute pair in MapAttr if key not exists
func (a *MapAttr) SetDefault(key string, val interface{}) {
// SetInt sets int value at the key
func (a *MapAttr) SetInt(key string, v int64) {
a.set(key, v)
}

// SetFloat sets float value at the key
func (a *MapAttr) SetFloat(key string, v float64) {
a.set(key, v)
}

// SetBool sets bool value at the key
func (a *MapAttr) SetBool(key string, v bool) {
a.set(key, v)
}

// SetStr sets string value at the key
func (a *MapAttr) SetStr(key string, v string) {
a.set(key, v)
}

// SetMapAttr sets MapAttr value at the key
func (a *MapAttr) SetMapAttr(key string, attr *MapAttr) {
a.set(key, attr)
}

// SetListAttr sets ListAttr value at the key
func (a *MapAttr) SetListAttr(key string, attr *ListAttr) {
a.set(key, attr)
}

// SetDefaultInt sets default int value at the key
func (a *MapAttr) SetDefaultInt(key string, v int64) {
if _, ok := a.attrs[key]; !ok {
a.Set(key, val)
a.set(key, v)
}
}

// SetDefaultFloat sets default float value at the key
func (a *MapAttr) SetDefaultFloat(key string, v float64) {
if _, ok := a.attrs[key]; !ok {
a.set(key, v)
}
}

// SetDefaultBool sets default bool value at the key
func (a *MapAttr) SetDefaultBool(key string, v bool) {
if _, ok := a.attrs[key]; !ok {
a.set(key, v)
}
}

// SetDefaultStr sets default string value at the key
func (a *MapAttr) SetDefaultStr(key string, v string) {
if _, ok := a.attrs[key]; !ok {
a.set(key, v)
}
}

// SetDefaultMapAttr sets default MapAttr value at the key
func (a *MapAttr) SetDefaultMapAttr(key string, attr *MapAttr) {
if _, ok := a.attrs[key]; !ok {
a.set(key, attr)
}
}

// SetDefaultListAttr sets default ListAttr value at the key
func (a *MapAttr) SetDefaultListAttr(key string, attr *ListAttr) {
if _, ok := a.attrs[key]; !ok {
a.set(key, attr)
}
}

Expand Down Expand Up @@ -189,7 +262,6 @@ func (a *MapAttr) PopMapAttr(key string) *MapAttr {

//// Clear removes all key-values from the MapAttr
//func (a *MapAttr) Clear() {
//
// val, ok := a.attrs[key]
// if !ok {
// gwlog.Panicf("key not exists: %s", key)
Expand Down Expand Up @@ -265,13 +337,13 @@ func (a *MapAttr) AssignMap(doc map[string]interface{}) {
if iv, ok := v.(map[string]interface{}); ok {
ia := NewMapAttr()
ia.AssignMap(iv)
a.Set(k, ia)
a.set(k, ia)
} else if iv, ok := v.([]interface{}); ok {
ia := NewListAttr()
ia.AssignList(iv)
a.Set(k, ia)
a.set(k, ia)
} else {
a.Set(k, v)
a.set(k, v)
}
}
}
Expand All @@ -286,13 +358,13 @@ func (a *MapAttr) AssignMapWithFilter(doc map[string]interface{}, filter func(st
if iv, ok := v.(map[string]interface{}); ok {
ia := NewMapAttr()
ia.AssignMap(iv)
a.Set(k, ia)
a.set(k, ia)
} else if iv, ok := v.([]interface{}); ok {
ia := NewListAttr()
ia.AssignList(iv)
a.Set(k, ia)
a.set(k, ia)
} else {
a.Set(k, v)
a.set(k, v)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/chatroom_demo/Account.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (a *Account) Register_Client(username string, password string) {
goworld.PutKVDB("password$"+username, password, func(err error) {
avatarID := goworld.CreateEntityLocally("Avatar") // 创建一个Avatar对象然后立刻销毁,产生一次存盘
avatar := goworld.GetEntity(avatarID)
avatar.Attrs.Set("name", username)
avatar.Attrs.SetStr("name", username)
avatar.Destroy()
goworld.PutKVDB("avatarID$"+username, string(avatarID), func(err error) {
a.CallClient("ShowInfo", "注册成功,请点击登录")
Expand Down Expand Up @@ -89,7 +89,7 @@ func (a *Account) OnGetAvatarSpaceID(avatarID common.EntityID, spaceID common.En
return
}

a.Attrs.Set("loginAvatarID", avatarID)
a.Attrs.SetStr("loginAvatarID", string(avatarID))
a.EnterSpace(spaceID, entity.Vector3{})
}

Expand Down
6 changes: 3 additions & 3 deletions examples/chatroom_demo/Avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func (a *Avatar) OnCreated() {

// setDefaultAttrs 设置玩家的一些默认属性
func (a *Avatar) setDefaultAttrs() {
a.Attrs.SetDefault("name", "noname")
a.Attrs.SetDefaultStr("name", "noname")
a.SetFilterProp("chatroom", "1")
a.Attrs.Set("chatroom", "1")
a.Attrs.SetStr("chatroom", "1")
}

// GetSpaceID 获得玩家的场景ID并发给调用者
Expand Down Expand Up @@ -55,5 +55,5 @@ func (a *Avatar) SendChat_Client(text string) {
func (a *Avatar) enterRoom(name string) {
gwlog.Debugf("%s enter room %s", a, name)
a.SetFilterProp("chatroom", name)
a.Attrs.Set("chatroom", name)
a.Attrs.SetStr("chatroom", name)
}
2 changes: 1 addition & 1 deletion examples/test_game/Account.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (a *Account) OnGetAvatarSpaceID(avatarID common.EntityID, spaceID common.En
return
}

a.Attrs.Set("loginAvatarID", avatarID)
a.Attrs.SetStr("loginAvatarID", string(avatarID))
a.EnterSpace(spaceID, entity.Vector3{})
}

Expand Down
22 changes: 11 additions & 11 deletions examples/test_game/Avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (a *Avatar) PerSecondTick(arg1 int, arg2 string) {
}

func (a *Avatar) setDefaultAttrs() {
a.Attrs.SetDefault("name", "无名")
a.Attrs.SetDefault("level", 1)
a.Attrs.SetDefault("exp", 0)
a.Attrs.SetDefault("prof", 1+rand.Intn(4))
a.Attrs.SetDefault("spaceKind", 1+rand.Intn(100))
a.Attrs.SetDefault("lastMailID", 0)
a.Attrs.SetDefault("mails", goworld.MapAttr())
a.Attrs.SetDefault("testListField", goworld.ListAttr())
a.Attrs.SetDefaultStr("name", "无名")
a.Attrs.SetDefaultInt("level", 1)
a.Attrs.SetDefaultInt("exp", 0)
a.Attrs.SetDefaultInt("prof", int64(1+rand.Intn(4)))
a.Attrs.SetDefaultInt("spaceKind", int64(1+rand.Intn(100)))
a.Attrs.SetDefaultInt("lastMailID", 0)
a.Attrs.SetDefaultMapAttr("mails", goworld.MapAttr())
a.Attrs.SetDefaultListAttr("testListField", goworld.ListAttr())
}

// TestListField_Client is a test RPC for client
Expand All @@ -64,7 +64,7 @@ func (a *Avatar) TestListField_Client() {
if testListField.Size() > 0 && rand.Float32() < 0.3333333333 {
testListField.PopInt()
} else if testListField.Size() > 0 && rand.Float32() < 0.5 {
testListField.Set(rand.Intn(testListField.Size()), rand.Intn(100))
testListField.SetInt(rand.Intn(testListField.Size()), int64(rand.Intn(100)))
} else {
testListField.AppendInt(int64(rand.Intn(100)))
}
Expand Down Expand Up @@ -187,8 +187,8 @@ func (a *Avatar) OnGetMails(lastMailID int, mails []interface{}) {
continue
}
mail := typeconv.String(item[1])
mailsAttr.Set(strconv.Itoa(mailId), mail)
a.Attrs.Set("lastMailID", mailId)
mailsAttr.SetStr(strconv.Itoa(mailId), mail)
a.Attrs.SetInt("lastMailID", int64(mailId))
}

a.CallClient("OnGetMails", true)
Expand Down
4 changes: 2 additions & 2 deletions examples/test_game/MailService.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *MailService) OnInit() {
// OnCreated is called when MailService is created
func (s *MailService) OnCreated() {
gwlog.Infof("Registering MailService ...")
s.Attrs.SetDefault("lastMailID", 0)
s.Attrs.SetDefaultInt("lastMailID", 0)
s.DeclareService("MailService")
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func (s *MailService) GetMails(avatarID common.EntityID, lastMailID int) {

func (s *MailService) genMailID() int {
lastMailID := int(s.Attrs.GetInt("lastMailID")) + 1
s.Attrs.Set("lastMailID", lastMailID)
s.Attrs.SetInt("lastMailID", int64(lastMailID))
return lastMailID
}

Expand Down
4 changes: 2 additions & 2 deletions examples/unity_demo/Account.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (a *Account) Register_Client(username string, password string) {

playerID := goworld.CreateEntityLocally("Player") // 创建一个Player对象然后立刻销毁,产生一次存盘
player := goworld.GetEntity(playerID)
player.Attrs.Set("name", username)
player.Attrs.SetStr("name", username)
player.Destroy()

goworld.PutKVDB("playerID$"+username, string(playerID), func(err error) {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (a *Account) OnGetPlayerSpaceID(playerID common.EntityID, spaceID common.En
return
}

a.Attrs.Set("loginPlayerID", playerID)
a.Attrs.SetStr("loginPlayerID", string(playerID))
a.EnterSpace(spaceID, entity.Vector3{})
}

Expand Down
Loading

0 comments on commit cc15506

Please sign in to comment.