Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cardinal): move context into cardinal #757

Closed
wants to merge 81 commits into from
Closed
Show file tree
Hide file tree
Changes from 78 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
d309164
move message and query into cardinal.
pyrofolium May 1, 2024
246023d
lint stuff.
pyrofolium May 1, 2024
0ebcb12
Merge branch 'main' of github.com:Argus-Labs/world-engine into brian/…
pyrofolium May 2, 2024
14322eb
place interface in front of message manager.
pyrofolium May 3, 2024
5665866
place interface in front of query manager.
pyrofolium May 3, 2024
48dfe33
use interface composition for query manager.
pyrofolium May 3, 2024
948a539
Merge branch 'main' of github.com:Argus-Labs/world-engine into brian/…
pyrofolium May 3, 2024
51d4dd6
Merge branch 'main' of github.com:Argus-Labs/world-engine into brian/…
pyrofolium May 6, 2024
d7847e8
changed. Issue with QUeries in world.go
pyrofolium May 7, 2024
2d5aaaa
changed. Issue with QUeries in world.go
pyrofolium May 7, 2024
bf065fa
changed. Issue with QUeries in world.go
pyrofolium May 7, 2024
cf8f6d8
not working.
pyrofolium May 7, 2024
e7781af
minor lint stuff.
pyrofolium May 7, 2024
5f84a9d
Merge branch 'main' of github.com:Argus-Labs/world-engine into newcon…
pyrofolium May 7, 2024
7e2db70
tests passing work on clean up later.
pyrofolium May 8, 2024
8fa31c6
refine error messages.
pyrofolium May 8, 2024
4831cf9
removed context requirement for queries.
pyrofolium May 9, 2024
41ab055
fixed the linter.
pyrofolium May 10, 2024
630b20a
moved search into cardinal.
pyrofolium May 10, 2024
799db3e
make 3 methods private.
pyrofolium May 10, 2024
5b35880
4 methods.
pyrofolium May 10, 2024
2252d9e
checkpoint.
pyrofolium May 10, 2024
7356fc5
checkpoint.
pyrofolium May 10, 2024
0492a2b
checkpoint.
pyrofolium May 10, 2024
8297b10
checkpoint.
pyrofolium May 10, 2024
6c98b6f
checkpoint.
pyrofolium May 10, 2024
1520543
checkpoint.
pyrofolium May 10, 2024
d5f9dcd
privatized. All other internal methods that are still public are used…
pyrofolium May 10, 2024
53a7e70
checkpoint.
pyrofolium May 10, 2024
a8836a3
checkpoint.
pyrofolium May 10, 2024
fcea4ce
checkpoint.
pyrofolium May 10, 2024
a3fb915
DONE.
pyrofolium May 10, 2024
258a4d8
lints
pyrofolium May 10, 2024
e5845b9
rename provider to world.
pyrofolium May 10, 2024
1e385ab
rename provider to world. lints and swagger fixes.
pyrofolium May 10, 2024
2dd20ad
remove old context file.
pyrofolium May 10, 2024
c3a6550
make new query type private.
pyrofolium May 10, 2024
62750f9
privatize handQuery and handleQueryRaw
pyrofolium May 10, 2024
206fa4e
review changes.
pyrofolium May 10, 2024
16635c0
change GetDebugState to GetState
pyrofolium May 10, 2024
4b0c967
rename context to WorldContext
pyrofolium May 10, 2024
ea77849
change RunCQLSearch to EvaluateCQL
pyrofolium May 10, 2024
140312d
get rid of the index.
pyrofolium May 10, 2024
728bd13
moved code block into EvaluateCQL
pyrofolium May 10, 2024
14d0460
move to query_manager
pyrofolium May 11, 2024
5be1575
lint stuff.
pyrofolium May 11, 2024
d3cc295
change GetDebugState to return single error.
pyrofolium May 13, 2024
ad20464
make test.mk original for swagger-check.
pyrofolium May 13, 2024
fb23e3b
better error handling.
pyrofolium May 13, 2024
3006832
add comment.
pyrofolium May 13, 2024
bdec3c2
mover some types out of engine.
pyrofolium May 13, 2024
5825ccd
Move GetWorldResponse to server.
pyrofolium May 13, 2024
00f5d83
change all eCtx to wCtx.
pyrofolium May 13, 2024
46afa0f
rename to a more general type.
pyrofolium May 13, 2024
5725224
get rid of debug.go
pyrofolium May 14, 2024
0f316bb
handle new entity state element type.
pyrofolium May 14, 2024
be09497
remove cql.go. Consolidate similar type.
pyrofolium May 14, 2024
634c588
update swagger.
pyrofolium May 14, 2024
bf5a799
remove empty file.
pyrofolium May 14, 2024
b2619cb
Remove GetComponentNameFromContext.
pyrofolium May 14, 2024
a76c6dd
remove queryhandler type.
pyrofolium May 14, 2024
9fd8900
remove unused structs in tests.
pyrofolium May 14, 2024
d46adb1
fix refactor error in comments.
pyrofolium May 14, 2024
56646e3
fix refactor error in comments.
pyrofolium May 14, 2024
aa19015
make error handling more simple.
pyrofolium May 14, 2024
aeb64d2
move entity requests and responses into server package.
pyrofolium May 14, 2024
94df133
make single error.
pyrofolium May 14, 2024
4d7fda1
move get world response into handler.
pyrofolium May 14, 2024
4a2a20b
lint issue.
pyrofolium May 15, 2024
d3f5da3
change cardinal.HandleQuery to cardinal.InternalHandleQuery.
pyrofolium May 15, 2024
12832f8
change to internal handle query.
pyrofolium May 15, 2024
2ef44ce
lint.
pyrofolium May 15, 2024
d73eeac
get rid of entityStateResponse.
pyrofolium May 15, 2024
1973562
clean up
smsunarto May 15, 2024
1421072
change HandleQuery to use groups and have that as the first parameter.
pyrofolium May 15, 2024
414446c
rename error to Errorf.
pyrofolium May 16, 2024
f5fdf04
ci: enable go pkg cache on swagger-check
heronimus May 16, 2024
f4e48ec
ci: add go cache dependency path
heronimus May 16, 2024
c5424ba
fix components comment refactor.
pyrofolium May 17, 2024
503b198
Merge branch 'newcontextmove' of github.com:Argus-Labs/world-engine i…
pyrofolium May 17, 2024
4de72df
fix: swaggo dependency error (#759)
heronimus May 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
## skip cache, use Namespace volume cache
cache: false
cache: true
cache-dependency-path: "**/*.sum"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Run swagger-check
Expand Down
3 changes: 1 addition & 2 deletions cardinal/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/testutils"
"pkg.world.dev/world-engine/cardinal/types"
"pkg.world.dev/world-engine/cardinal/types/engine"
)

type Health struct {
Expand All @@ -35,7 +34,7 @@ func setupWorld(t testing.TB, numOfEntities int, enableHealthSystem bool) *testu
if enableHealthSystem {
err := cardinal.RegisterSystems(
world,
func(wCtx engine.Context) error {
func(wCtx cardinal.WorldContext) error {
q := cardinal.NewSearch().Entity(filter.Contains(filter.Component[Health]()))
err := q.Each(wCtx,
func(id types.EntityID) bool {
Expand Down
94 changes: 36 additions & 58 deletions cardinal/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (

"pkg.world.dev/world-engine/cardinal/component"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/search"
"pkg.world.dev/world-engine/cardinal/types"
"pkg.world.dev/world-engine/cardinal/types/engine"
"pkg.world.dev/world-engine/cardinal/worldstage"
)

Expand All @@ -24,27 +22,6 @@ var (
ErrComponentAlreadyOnEntity = iterators.ErrComponentAlreadyOnEntity
)

// Imported
// This section aggregates function from other packages such that they are easily accessible
// via cardinal.<function_name>

// NewSearch is used to create a search object.
//
// Usage:
//
// cardinal.NewSearch().Entity(filter.Contains(filter.Component[EnergyComponent]()))
var NewSearch = search.NewSearch

// NewLegacySearch allows users to create a Search object with a filter already provided
// as a property.
//
// Example Usage:
//
// cardinal.NewLegacySearch().Entity(filter.Exact(Alpha{}, Beta{})).Count()
var NewLegacySearch = search.NewLegacySearch

type Search = search.Search

// FilterFunction wrap your component filter function of func(comp T) bool inside FilterFunction to use
// in search.
//
Expand All @@ -55,8 +32,8 @@ type Search = search.Search
// return true
// }))

func FilterFunction[T types.Component](f func(comp T) bool) func(ctx engine.Context, id types.EntityID) (bool, error) {
return search.ComponentFilter[T](f)
func FilterFunction[T types.Component](f func(comp T) bool) func(ctx WorldContext, id types.EntityID) (bool, error) {
return ComponentFilter[T](f)
}

func RegisterSystems(w *World, sys ...System) error {
Expand Down Expand Up @@ -110,10 +87,10 @@ func MustRegisterComponent[T types.Component](w *World) {
}
}

func EachMessage[In any, Out any](wCtx engine.Context, fn func(TxData[In]) (Out, error)) error {
func EachMessage[In any, Out any](wCtx WorldContext, fn func(TxData[In]) (Out, error)) error {
var msg MessageType[In, Out]
msgType := reflect.TypeOf(msg)
tempRes, ok := wCtx.GetMessageByType(msgType)
tempRes, ok := wCtx.getMessageByType(msgType)
if !ok {
return eris.Errorf("Could not find %s, Message may not be registered.", msg.Name())
}
Expand Down Expand Up @@ -153,7 +130,7 @@ func RegisterMessage[In any, Out any](world *World, name string, opts ...Message
func RegisterQuery[Request any, Reply any](
w *World,
name string,
handler func(wCtx engine.Context, req *Request) (*Reply, error),
handler func(wCtx WorldContext, req *Request) (*Reply, error),
opts ...QueryOption[Request, Reply],
) (err error) {
if w.worldStage.Current() != worldstage.Init {
Expand All @@ -164,17 +141,18 @@ func RegisterQuery[Request any, Reply any](
)
}

q, err := NewQueryType[Request, Reply](name, handler, opts...)
q, err := newQueryType[Request, Reply](name, handler, opts...)
if err != nil {
return err
}

return w.RegisterQuery(name, q)
res := w.RegisterQuery(name, q)
return res
}

// Create creates a single entity in the world, and returns the id of the newly created entity.
// At least 1 component must be provided.
func Create(wCtx engine.Context, components ...types.Component) (_ types.EntityID, err error) {
func Create(wCtx WorldContext, components ...types.Component) (_ types.EntityID, err error) {
// We don't handle panics here because we let CreateMany handle it for us
entityIDs, err := CreateMany(wCtx, 1, components...)
if err != nil {
Expand All @@ -185,30 +163,30 @@ func Create(wCtx engine.Context, components ...types.Component) (_ types.EntityI

// CreateMany creates multiple entities in the world, and returns the slice of ids for the newly created
// entities. At least 1 component must be provided.
func CreateMany(wCtx engine.Context, num int, components ...types.Component) (entityIDs []types.EntityID, err error) {
func CreateMany(wCtx WorldContext, num int, components ...types.Component) (entityIDs []types.EntityID, err error) {
defer func() { panicOnFatalError(wCtx, err) }()

// Error if the context is read only
if wCtx.IsReadOnly() {
if wCtx.isReadOnly() {
return nil, ErrEntityMutationOnReadOnly
}

if !wCtx.IsWorldReady() {
if !wCtx.isWorldReady() {
return nil, ErrEntitiesCreatedBeforeReady
}

// Get all component metadata for the given components
acc := make([]types.ComponentMetadata, 0, len(components))
for _, comp := range components {
c, err := wCtx.GetComponentByName(comp.Name())
c, err := wCtx.getComponentByName(comp.Name())
if err != nil {
return nil, eris.Wrap(err, "failed to create entity because component is not registered")
}
acc = append(acc, c)
}

// Create the entities
entityIDs, err = wCtx.StoreManager().CreateManyEntities(num, acc...)
entityIDs, err = wCtx.storeManager().CreateManyEntities(num, acc...)
if err != nil {
return nil, err
}
Expand All @@ -217,12 +195,12 @@ func CreateMany(wCtx engine.Context, num int, components ...types.Component) (en
for _, id := range entityIDs {
for _, comp := range components {
var c types.ComponentMetadata
c, err = wCtx.GetComponentByName(comp.Name())
c, err = wCtx.getComponentByName(comp.Name())
if err != nil {
return nil, eris.Wrap(err, "failed to create entity because component is not registered")
}

err = wCtx.StoreManager().SetComponentForEntity(c, id, comp)
err = wCtx.storeManager().SetComponentForEntity(c, id, comp)
if err != nil {
return nil, err
}
Expand All @@ -233,23 +211,23 @@ func CreateMany(wCtx engine.Context, num int, components ...types.Component) (en
}

// SetComponent sets component data to the entity.
func SetComponent[T types.Component](wCtx engine.Context, id types.EntityID, component *T) (err error) {
func SetComponent[T types.Component](wCtx WorldContext, id types.EntityID, component *T) (err error) {
defer func() { panicOnFatalError(wCtx, err) }()

// Error if the context is read only
if wCtx.IsReadOnly() {
if wCtx.isReadOnly() {
return ErrEntityMutationOnReadOnly
}

// Get the component metadata
var t T
c, err := wCtx.GetComponentByName(t.Name())
c, err := wCtx.getComponentByName(t.Name())
if err != nil {
return err
}

// Store the component
err = wCtx.StoreManager().SetComponentForEntity(c, id, component)
err = wCtx.storeManager().SetComponentForEntity(c, id, component)
if err != nil {
return err
}
Expand All @@ -265,18 +243,18 @@ func SetComponent[T types.Component](wCtx engine.Context, id types.EntityID, com
}

// GetComponent returns component data from the entity.
func GetComponent[T types.Component](wCtx engine.Context, id types.EntityID) (comp *T, err error) {
func GetComponent[T types.Component](wCtx WorldContext, id types.EntityID) (comp *T, err error) {
defer func() { panicOnFatalError(wCtx, err) }()

// Get the component metadata
var t T
c, err := wCtx.GetComponentByName(t.Name())
c, err := wCtx.getComponentByName(t.Name())
if err != nil {
return nil, err
}

// Get current component value
compValue, err := wCtx.StoreReader().GetComponentForEntity(c, id)
compValue, err := wCtx.storeReader().GetComponentForEntity(c, id)
if err != nil {
return nil, err
}
Expand All @@ -295,11 +273,11 @@ func GetComponent[T types.Component](wCtx engine.Context, id types.EntityID) (co
return comp, nil
}

func UpdateComponent[T types.Component](wCtx engine.Context, id types.EntityID, fn func(*T) *T) (err error) {
func UpdateComponent[T types.Component](wCtx WorldContext, id types.EntityID, fn func(*T) *T) (err error) {
defer func() { panicOnFatalError(wCtx, err) }()

// Error if the context is read only
if wCtx.IsReadOnly() {
if wCtx.isReadOnly() {
return err
}

Expand All @@ -321,23 +299,23 @@ func UpdateComponent[T types.Component](wCtx engine.Context, id types.EntityID,
return nil
}

func AddComponentTo[T types.Component](wCtx engine.Context, id types.EntityID) (err error) {
func AddComponentTo[T types.Component](wCtx WorldContext, id types.EntityID) (err error) {
defer func() { panicOnFatalError(wCtx, err) }()

// Error if the context is read only
if wCtx.IsReadOnly() {
if wCtx.isReadOnly() {
return ErrEntityMutationOnReadOnly
}

// Get the component metadata
var t T
c, err := wCtx.GetComponentByName(t.Name())
c, err := wCtx.getComponentByName(t.Name())
if err != nil {
return err
}

// Add the component to entity
err = wCtx.StoreManager().AddComponentToEntity(c, id)
err = wCtx.storeManager().AddComponentToEntity(c, id)
if err != nil {
return err
}
Expand All @@ -346,23 +324,23 @@ func AddComponentTo[T types.Component](wCtx engine.Context, id types.EntityID) (
}

// RemoveComponentFrom removes a component from an entity.
func RemoveComponentFrom[T types.Component](wCtx engine.Context, id types.EntityID) (err error) {
func RemoveComponentFrom[T types.Component](wCtx WorldContext, id types.EntityID) (err error) {
defer func() { panicOnFatalError(wCtx, err) }()

// Error if the context is read only
if wCtx.IsReadOnly() {
if wCtx.isReadOnly() {
return ErrEntityMutationOnReadOnly
}

// Get the component metadata
var t T
c, err := wCtx.GetComponentByName(t.Name())
c, err := wCtx.getComponentByName(t.Name())
if err != nil {
return err
}

// Remove the component from entity
err = wCtx.StoreManager().RemoveComponentFromEntity(c, id)
err = wCtx.storeManager().RemoveComponentFromEntity(c, id)
if err != nil {
return err
}
Expand All @@ -371,15 +349,15 @@ func RemoveComponentFrom[T types.Component](wCtx engine.Context, id types.Entity
}

// Remove removes the given Entity from the world.
func Remove(wCtx engine.Context, id types.EntityID) (err error) {
func Remove(wCtx WorldContext, id types.EntityID) (err error) {
defer func() { panicOnFatalError(wCtx, err) }()

// Error if the context is read only
if wCtx.IsReadOnly() {
if wCtx.isReadOnly() {
return ErrEntityMutationOnReadOnly
}

err = wCtx.StoreManager().RemoveEntity(id)
err = wCtx.storeManager().RemoveEntity(id)
if err != nil {
return err
}
Expand Down
Loading
Loading