forked from Argus-Labs/world-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld_persona.go
75 lines (70 loc) · 2.21 KB
/
world_persona.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cardinal
import (
"errors"
"github.com/rotisserie/eris"
"pkg.world.dev/world-engine/cardinal/persona"
"pkg.world.dev/world-engine/cardinal/persona/component"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/types"
)
// GetSignerForPersonaTag returns the signer address that has been registered for the given persona tag after the
// given tick. If the engine's tick is less than or equal to the given tick, ErrorCreatePersonaTXsNotProcessed is
// returned. If the given personaTag has no signer address, ErrPersonaTagHasNoSigner is returned.
func (w *World) GetSignerForPersonaTag(personaTag string, tick uint64) (addr string, err error) {
if tick >= w.CurrentTick() {
return "", persona.ErrCreatePersonaTxsNotProcessed
}
var errs []error
wCtx := NewReadOnlyWorldContext(w)
s := NewSearch().Entity(filter.Exact(filter.Component[component.SignerComponent]()))
err = s.Each(wCtx,
func(id types.EntityID) bool {
sc, err := GetComponent[component.SignerComponent](wCtx, id)
if err != nil {
errs = append(errs, err)
}
if sc != nil && sc.PersonaTag == personaTag {
addr = sc.SignerAddress
return false
}
return true
},
)
errs = append(errs, err)
if addr == "" {
return "", persona.ErrPersonaTagHasNoSigner
}
return addr, errors.Join(errs...)
}
func (w *World) GetSignerComponentForPersona(personaTag string) (*component.SignerComponent, error) {
var sc *component.SignerComponent
wCtx := NewReadOnlyWorldContext(w)
q := NewSearch().Entity(filter.Exact(filter.Component[component.SignerComponent]()))
var getComponentErr error
searchIterationErr := eris.Wrap(
q.Each(wCtx,
func(id types.EntityID) bool {
var signerComp *component.SignerComponent
signerComp, getComponentErr = GetComponent[component.SignerComponent](wCtx, id)
if getComponentErr != nil {
return false
}
if signerComp.PersonaTag == personaTag {
sc = signerComp
return false
}
return true
},
), "",
)
if getComponentErr != nil {
return nil, getComponentErr
}
if searchIterationErr != nil {
return nil, searchIterationErr
}
if sc == nil {
return nil, eris.Errorf("persona tag %q not found", personaTag)
}
return sc, nil
}