diff --git a/game/builtin/item.go b/game/builtin/item.go new file mode 100644 index 0000000..d3f0775 --- /dev/null +++ b/game/builtin/item.go @@ -0,0 +1,51 @@ +package builtin + +import "github.com/kercylan98/minotaur/utils/huge" + +func NewItem[ID comparable](id ID, options ...ItemOption[ID]) *Item[ID] { + item := &Item[ID]{ + id: id, + } + for _, option := range options { + option(item) + } + return item +} + +type Item[ID comparable] struct { + id ID + stackLimit *huge.Int + count *huge.Int +} + +func (slf *Item[ID]) GetID() ID { + return slf.id +} + +func (slf *Item[ID]) GetCount() *huge.Int { + return slf.count +} + +func (slf *Item[ID]) GetStackLimit() *huge.Int { + return slf.stackLimit +} + +func (slf *Item[ID]) SetCount(count *huge.Int) { + if count.LessThan(huge.IntZero) { + slf.count = huge.IntZero.Copy() + return + } + slf.count = count.Copy() +} + +func (slf *Item[ID]) ChangeCount(count *huge.Int) error { + newCount := slf.count.Copy().Add(count) + if newCount.LessThan(huge.IntZero) { + return ErrItemInsufficientQuantityDeduction + } + if newCount.GreaterThan(slf.stackLimit) { + return ErrItemStackLimit + } + slf.count = newCount + return nil +} diff --git a/game/builtin/item_errors.go b/game/builtin/item_errors.go index e036a3d..25e14bd 100644 --- a/game/builtin/item_errors.go +++ b/game/builtin/item_errors.go @@ -1,5 +1,8 @@ package builtin +import "errors" + var ( - ErrItemCountException = "the number of items cannot be empty and must be a positive integer" + ErrItemInsufficientQuantityDeduction = errors.New("insufficient quantity deduction") + ErrItemStackLimit = errors.New("the number of items reaches the stacking limit") ) diff --git a/game/builtin/item_options.go b/game/builtin/item_options.go new file mode 100644 index 0000000..55d43c7 --- /dev/null +++ b/game/builtin/item_options.go @@ -0,0 +1,16 @@ +package builtin + +import "github.com/kercylan98/minotaur/utils/huge" + +type ItemOption[ID comparable] func(item *Item[ID]) + +// WithItemStackLimit 通过特定堆叠上限创建物品 +// - 默认无限制 +func WithItemStackLimit[ID comparable](stackLimit *huge.Int) ItemOption[ID] { + return func(item *Item[ID]) { + if stackLimit == nil || stackLimit.LessThanOrEqualTo(huge.IntZero) { + return + } + item.stackLimit = stackLimit + } +} diff --git a/game/item.go b/game/item.go new file mode 100644 index 0000000..73742c3 --- /dev/null +++ b/game/item.go @@ -0,0 +1,17 @@ +package game + +import "github.com/kercylan98/minotaur/utils/huge" + +// Item 物品 +type Item[ID comparable] interface { + // GetID 获取物品id + GetID() ID + // GetCount 获取物品数量 + GetCount() *huge.Int + // GetStackLimit 获取堆叠上限 + GetStackLimit() *huge.Int + // SetCount 设置物品数量 + SetCount(count *huge.Int) + // ChangeCount 改变物品数量 + ChangeCount(count *huge.Int) error +} diff --git a/server/cross.go b/server/cross.go new file mode 100644 index 0000000..6e83318 --- /dev/null +++ b/server/cross.go @@ -0,0 +1,9 @@ +package server + +// Cross 跨服功能接口实现 +type Cross interface { + // PushPacket 推送数据包 + PushPacket(serverId int64, packet []byte) error + + // +}