-
Notifications
You must be signed in to change notification settings - Fork 8
/
query.ex
1312 lines (1089 loc) · 39.9 KB
/
query.ex
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Ecspanse.Query do
@moduledoc """
The `Ecspanse.Query` module provides a set of functions for querying entities, components and resources.
The queries are read-only operations they do not modify the state of the components or resources.
Queries can be run both from within the Ecspanse systems and from outside of the framework.
"""
use Memoize
require Ex2ms
alias __MODULE__
alias Ecspanse.Component
alias Ecspanse.Entity
alias Ecspanse.Util
@typedoc "The query preparation struct."
@type t :: %Query{
return_entity: boolean(),
select: list(component_module :: module()),
select_optional: list(component_module :: module()),
or:
list(
with_components: list(component_module :: module()),
without_components: list(component_module :: module())
),
for_entities: list(Ecspanse.Entity.t()),
not_for_entities: list(Ecspanse.Entity.t()),
for_children_of: list(Ecspanse.Entity.t()),
for_descendants_of: list(Ecspanse.Entity.t()),
for_parents_of: list(Ecspanse.Entity.t()),
for_ancestors_of: list(Ecspanse.Entity.t())
}
@enforce_keys [:select]
defstruct [
:return_entity,
:select,
:select_optional,
:or,
:for_entities,
:not_for_entities,
:for_children_of,
:for_descendants_of,
:for_parents_of,
:for_ancestors_of
]
defmodule Error do
@moduledoc false
defexception [:message]
@impl true
def exception(message) do
msg = """
Message: #{message}
"""
msg = maybe_add_system_info(msg)
%Error{message: msg}
end
# if the query is called from a system, add the system module to the error message
defp maybe_add_system_info(msg) do
system_module = Process.get(:system_module)
system_msg = """
Calling System Module: #{Kernel.inspect(system_module)}
"""
if system_module do
msg <> system_msg
else
msg
end
end
end
@doc """
`select/2` is the most versatile function for querying entities and components.
On its own, it will return an `Ecspanse.Query` struct that holds the query details.
The struct needs to be passed to `Ecspanse.Query.stream/1` or `Ecspanse.Query.one/1` to get the results.
## Arguments
### 1. component_modules
The first argument is a tuple of components to be selected. The query will return the components
only for the entities that have **all** the components in the tuple.
The entity can be queried as well by adding `Ecspanse.Entity` as the first element in the tuple.
Also, optional components can be queries, by adding the `:opt` key.
The optional components should be placed at the end of the tuple.
The restults will be returned in the same order as the components in the tuple. This makes it easy to use pattern matching on the result.
### 2. filters
The filters are optional. They can be used to further narrow down the results.
- `:with` - a list of components that the entity must have in addition to the ones specified in the `component_modules` tuple.
But those components will not be returned in the result. `:with` filter has one option: `:without` - a list of components that the entity must not have.
- `:or_with` - similar to `:with`. It allows to specify multiple filters for the same query. Multiple `or_with` filters can be used in the same query.
The results will be returned if the entity components match any of the filters.
- `:for` - a list of `t:Ecspanse.Entity.t/0` that the query should be run for. The components will be returned only for those entities.
- `:not_for` - a list of `t:Ecspanse.Entity.t/0` that the query should not be run for. The components will be returned for all entities except those.
- `:for_children_of` - a list of `t:Ecspanse.Entity.t/0`. The components will be returned only for the children of those entities.
- `:for_descendants_of` - a list of `t:Ecspanse.Entity.t/0`. The components will be returned only for all descendants of those entities.
- `:for_parents_of` - a list of `t:Ecspanse.Entity.t/0`. The components will be returned only for the parents of those entities.
- `:for_ancestors_of` - a list of `t:Ecspanse.Entity.t/0`. The components will be returned only for all ancestors of those entities.
> #### Info {: .error}
> Combining the following filters is not supported: `:for, :not_for, :for_children_of, :for_descendants_of, :for_parents_of, :for_ancestors_of`.
> Only one of them can be used in a query. Otherwise it will rise an error.
## Examples
```elixir
Ecspanse.Query.select({Ecspanse.Entity, Demo.Components.Health, opt: Demo.Components.Mana},
with: [Demo.Components.Orc],
or_with: [[Demo.Components.Wizard], without: [Demo.Components.WhiteMagic]],
for_descendants_of: [enemy_clan_entity]
)
|> Ecspanse.Query.stream()
|> Enum.to_list()
```
a potential result may be:
```elixir
[
{orc_entity, %Demo.Components.Health{value: 100}, nil},
{wizard_entity, %Demo.Components.Health{value: 60}, %Demo.Components.Mana{value: 200}}
]
```
"""
@doc group: :generic
@spec select(component_modules :: tuple(), keyword()) :: t()
def select(component_modules_tuple, filters \\ []) do
comp = Tuple.to_list(component_modules_tuple) |> List.flatten()
# The order is essential here, because the result will be pattern_matched on the initial tuple
{select_comp, select_opt_comp} =
Enum.reduce(comp, {[], []}, fn
{:opt, opt_comp}, {select_comp, select_opt_comp} when is_atom(opt_comp) ->
{select_comp, select_opt_comp ++ [opt_comp]}
comp, {select_comp, select_opt_comp} when is_atom(comp) ->
{select_comp ++ [comp], select_opt_comp}
error, _acc ->
raise Error,
"Expected to be a Component or [opt: Component], got: `#{Kernel.inspect(error)}`"
end)
{return_entity, select_comp} =
case List.first(select_comp) do
Entity ->
{true, List.delete_at(select_comp, 0)}
_ ->
{false, select_comp}
end
:ok = validate_components(select_comp)
:ok = validate_components(select_opt_comp)
# composing component queries from mandatory select components, plus filters
# there can be multiple `or` filters for the same query (with: [:comp1, :comp2], or_with: [:comp3, :comp4])
or_component_filters =
compose_component_filters(
select_comp,
[Keyword.get(filters, :with, []) | Keyword.get_values(filters, :or_with)],
[]
)
:ok = validate_filters(filters)
for_entities = Keyword.get(filters, :for, []) |> Enum.uniq()
not_for_entities = Keyword.get(filters, :not_for, []) |> Enum.uniq()
for_children_of = Keyword.get(filters, :for_children_of, []) |> Enum.uniq()
for_descendants_of = Keyword.get(filters, :for_descendants_of, []) |> Enum.uniq()
for_parents_of = Keyword.get(filters, :for_parents_of, []) |> Enum.uniq()
for_ancestors_of = Keyword.get(filters, :for_ancestors_of, []) |> Enum.uniq()
:ok = validate_entities(for_entities)
%Query{
return_entity: return_entity,
select: select_comp,
select_optional: select_opt_comp,
or: or_component_filters,
for_entities: for_entities,
not_for_entities: not_for_entities,
for_children_of: for_children_of,
for_descendants_of: for_descendants_of,
for_parents_of: for_parents_of,
for_ancestors_of: for_ancestors_of
}
end
@doc """
Returns a stream of components tuples for a `t:t/0` query.
See the `select/2` function for more info.
"""
@doc group: :generic
@spec stream(t()) :: Enumerable.t()
def stream(query) do
components_state_ets_table =
Util.components_state_ets_table()
# filter by entity ids, if any. Retruns a stream
entities_with_components_stream =
entities_with_components_stream(query)
# filters by with/without components. Returns the entity ids
entity_ids = filter_by_components(query.or, entities_with_components_stream, [])
# retrieve the queried components for each entity
map_components(
query.return_entity,
query.select,
query.select_optional,
entity_ids,
components_state_ets_table
)
end
@doc """
Returns a single tuple wiht components for a `t:t/0` query. Returns `nil` if no result was found. Raises if more than one entry.
See the `select/2` function for more info.
"""
@doc group: :generic
@spec one(t()) :: components_state :: tuple() | nil
def one(query) do
case stream(query) |> Enum.to_list() do
[result_tuple] -> result_tuple
[] -> nil
results -> raise Error, "Expected to return one result, got: `#{Kernel.inspect(results)}`"
end
end
@doc """
Fetches an `t:Ecspanse.Entity.t/0` by its ID.
An entity exists only if it has at least one component.
## Examples
```elixir
{:ok, %Ecspanse.Entity{}} = Ecspanse.Query.fetch_entity(hero_entity_id)
```
"""
@doc group: :entities
@spec fetch_entity(Ecspanse.Entity.id()) :: {:ok, Ecspanse.Entity.t()} | {:error, :not_found}
def fetch_entity(entity_id) do
f =
Ex2ms.fun do
{{^entity_id, _component_module}, _component_tags, _component_state} -> ^entity_id
end
result =
try do
:ets.select(Util.components_state_ets_table(), f, 1)
rescue
e ->
case :ets.info(Util.components_state_ets_table()) do
:undefined -> reraise Util.server_not_started_error(), __STACKTRACE__
_ -> reraise e, __STACKTRACE__
end
end
case result do
{[^entity_id], _} ->
{:ok, Ecspanse.Util.build_entity(entity_id)}
_ ->
{:error, :not_found}
end
end
@doc """
Checks if an entity exists by its ID, or by the entity struct.
"""
@doc group: :entities
@spec entity_exists?(Ecspanse.Entity.id() | Ecspanse.Entity.t()) :: boolean()
def entity_exists?(entity_id) when is_binary(entity_id) do
case fetch_entity(entity_id) do
{:ok, %Ecspanse.Entity{}} -> true
_ -> false
end
end
def entity_exists?(%Ecspanse.Entity{id: entity_id}) do
entity_exists?(entity_id)
end
@doc """
Returns a component's entity.
## Examples
```elixir
{:ok, %Ecspanse.Entity{}} = Ecspanse.Query.get_component_entity(hero_component)
```
"""
@doc group: :entities
@spec get_component_entity(component_state :: struct()) ::
Ecspanse.Entity.t()
def get_component_entity(component) do
:ok = validate_components([component])
component.__meta__.entity
end
@doc """
Returns the list of child entities for the given entity.
## Examples
```elixir
[sword_item_entity, magic_potion_entity] = Ecspanse.Query.list_children(hero_entity)
```
"""
@doc group: :relationships
@spec list_children(Ecspanse.Entity.t()) :: list(Ecspanse.Entity.t())
def list_children(%Entity{} = entity) do
memo_list_children(entity)
end
@doc false
defmemo memo_list_children(%Entity{id: entity_id}), max_waiter: 1000, waiter_sleep_ms: 0 do
result =
try do
:ets.lookup(Util.components_state_ets_table(), {entity_id, Component.Children})
rescue
e ->
case :ets.info(Util.components_state_ets_table()) do
:undefined -> reraise Util.server_not_started_error(), __STACKTRACE__
_ -> reraise e, __STACKTRACE__
end
end
case result do
[{_key, _tags, %Component.Children{entities: children_entities}}] -> children_entities
[] -> []
end
end
@doc """
Returns the list of descendant entities for the given entity.
That means the children of the entity and their children and so on.
## Examples
```elixir
[inventory_entity, map_entity] = Ecspanse.Query.list_descendants(hero_entity)
```
"""
@doc group: :relationships
@spec list_descendants(Ecspanse.Entity.t()) :: list(Ecspanse.Entity.t())
def list_descendants(%Entity{} = entity) do
memo_list_descendants(entity)
end
@doc false
defmemo memo_list_descendants(%Entity{} = entity), max_waiter: 1000, waiter_sleep_ms: 0 do
list_descendants_entities([entity], [])
end
@doc """
Returns the list of parent entities for the given entity.
## Examples
```elixir
[hero_entity] = Ecspanse.Query.list_parents(inventory_entity)
```
"""
@doc group: :relationships
@spec list_parents(Ecspanse.Entity.t()) :: list(Ecspanse.Entity.t())
def list_parents(%Entity{} = entity) do
memo_list_parents(entity)
end
@doc false
defmemo memo_list_parents(%Entity{id: entity_id}), max_waiter: 1000, waiter_sleep_ms: 0 do
result =
try do
:ets.lookup(Util.components_state_ets_table(), {entity_id, Component.Parents})
rescue
e ->
case :ets.info(Util.components_state_ets_table()) do
:undefined -> reraise Util.server_not_started_error(), __STACKTRACE__
_ -> reraise e, __STACKTRACE__
end
end
case result do
[{_key, _tags, %Component.Parents{entities: parents_entities}}] -> parents_entities
[] -> []
end
end
@doc """
Returns the list of ancestor entities for the given entity.
That means the parents of the entity and their parents and so on.
## Examples
```elixir
[hero_entity, level_entity] = Ecspanse.Query.list_ancestors(compass_entity)
```
"""
@doc group: :relationships
@spec list_ancestors(Ecspanse.Entity.t()) :: list(Ecspanse.Entity.t())
def list_ancestors(%Entity{} = entity) do
memo_list_ancestors(entity)
end
@doc false
defmemo memo_list_ancestors(%Entity{} = entity), max_waiter: 1000, waiter_sleep_ms: 0 do
list_ancestors_entities([entity], [])
end
@doc """
Fetches an entity's component by a list of tags.
Raises if more than one entry is found.
> #### Note {: .info}
>
> The project logic must ensure that only one component per entity is tagged with the given tags.
## Examples
```elixir
{:ok, %Demo.Components.Paladin{} = hero_class_component} =
Ecspanse.Query.fetch_tagged_component(hero_entity, [:class])
```
"""
@doc group: :tags
@spec fetch_tagged_component(Ecspanse.Entity.t(), list(tag :: atom())) ::
{:ok, components_state :: struct()} | {:error, :not_found}
def fetch_tagged_component(entity, tags) do
components = list_tagged_components_for_entity(entity, tags)
case components do
[component] -> {:ok, component}
[] -> {:error, :not_found}
results -> raise Error, "Expected to return one result, got: `#{Kernel.inspect(results)}`"
end
end
@doc """
Lists a component's tags.
## Examples
```elixir
[:resource, :available] = Ecspanse.Query.list_tags(gold_component)
```
"""
@doc group: :tags
@spec list_tags(components_state :: struct()) :: list(tag :: atom())
def list_tags(component) do
:ok = validate_components([component])
# tags are stored as MapSet in the component Meta
MapSet.to_list(component.__meta__.tags)
end
@doc """
Returns a list of components tagged with a list of tags for all entities.
The components need to be tagged with all the given tags to return.
## Examples
```elixir
[gold_component, gems_component] = Ecspanse.Query.list_tagged_components([:resource, :available])
```
"""
@doc group: :tags
@spec list_tagged_components(list(tag :: atom())) ::
list(components_state :: struct())
def list_tagged_components(tags) do
:ok = validate_tags(tags)
table = Util.components_state_ets_table()
timer_component_tag = Ecspanse.Template.Component.Timer.timer_component_tag()
# optimized function for timer components
# the filter_timer_entities_components_tags function is called when the timer system runs
filtered_entities_components_tags =
case tags do
[^timer_component_tag] ->
Ecspanse.Util.filter_timer_entities_components_tags()
_ ->
Ecspanse.Util.filter_entities_components_tags(tags)
end
filtered_entities_components_tags
|> Stream.map(fn {entity_id, comp_module, _tags_set} ->
result =
try do
:ets.lookup(table, {entity_id, comp_module})
rescue
e ->
case :ets.info(table) do
:undefined -> reraise Util.server_not_started_error(), __STACKTRACE__
_ -> reraise e, __STACKTRACE__
end
end
case result do
[{_key, _tags, comp_state}] -> comp_state
# checking for race conditions when a required component is removed during the query
# the whole entity should be filtered out
[] -> :reject
end
end)
|> Stream.reject(fn state -> state == :reject end)
|> Enum.to_list()
end
@doc """
Returns a list of components tagged with a list of tags for a given entity.
The components need to be tagged with all the given tags to return.
## Examples
```elixir
[gold_component, gems_component] = Ecspanse.Query.list_tagged_components_for_entity(hero_entity, [:resource, :available])
```
"""
@doc group: :tags
@spec list_tagged_components_for_entity(Ecspanse.Entity.t(), list(tag :: atom())) ::
list(components_state :: struct())
def list_tagged_components_for_entity(entity, tags) do
:ok = validate_entities([entity])
:ok = validate_tags(tags)
tags_set = MapSet.new(tags)
Ecspanse.Util.list_entities_tags_state(entity)
|> Stream.filter(fn {_component_entity_id, component_tags_set, _state} ->
MapSet.subset?(tags_set, component_tags_set)
end)
|> Enum.map(fn {_entity_id, _tags, state} -> state end)
end
@doc """
Returns a list of components tagged with a list of tags for a given list of entities.
The components are not grouped by entity, but returned as a flat list.
The components need to be tagged with all the given tags to return.
## Examples
```elixir
[gold_component, gems_component, gems_component] = Ecspanse.Query.list_tagged_components_for_entities([hero_entity, enemy_entity], [:resource, :available])
```
"""
@doc group: :tags
@spec list_tagged_components_for_entities(list(Ecspanse.Entity.t()), list(tag :: atom())) ::
list(components_state :: struct())
def list_tagged_components_for_entities(entities, tags) do
:ok = validate_entities(entities)
:ok = validate_tags(tags)
entity_ids = Enum.map(entities, & &1.id)
table = Util.components_state_ets_table()
Ecspanse.Util.filter_entities_components_tags(tags)
|> Stream.filter(fn {entity_id, _comp_module, _tags_set} ->
entity_id in entity_ids
end)
|> Stream.map(fn {entity_id, comp_module, _tags_set} ->
result =
try do
:ets.lookup(table, {entity_id, comp_module})
rescue
e ->
case :ets.info(table) do
:undefined -> reraise Util.server_not_started_error(), __STACKTRACE__
_ -> reraise e, __STACKTRACE__
end
end
case result do
[{_key, _tags, comp_state}] -> comp_state
# checking for race conditions when a required component is removed during the query
# the whole entity should be filtered out
[] -> :reject
end
end)
|> Stream.reject(fn state -> state == :reject end)
|> Enum.to_list()
end
@doc """
Returns a list of components tagged with a list of tags for the children of a given entity.
The components need to be tagged with all the given tags to return.
## Examples
```elixir
[boots_component, compass_component] = Ecspanse.Query.list_tagged_components_for_children(hero_entity, [:inventory])
```
"""
@doc group: :tags
@spec list_tagged_components_for_children(Ecspanse.Entity.t(), list(tag :: atom())) ::
list(components_state :: struct())
def list_tagged_components_for_children(entity, tags) do
case list_children(entity) do
[] -> []
[child] -> list_tagged_components_for_entity(child, tags)
children -> list_tagged_components_for_entities(children, tags)
end
end
@doc """
Returns a list of components tagged with a list of tags for the descendants of a given entity.
The components need to be tagged with all the given tags to return.
## Examples
```elixir
[orc_component, orc_component, wizard_component] = Ecspanse.Query.list_tagged_components_for_descendants(dungeon_entity, [:enemy])
```
"""
@doc group: :tags
@spec list_tagged_components_for_descendants(Ecspanse.Entity.t(), list(tag :: atom())) ::
list(components_state :: struct())
def list_tagged_components_for_descendants(entity, tags) do
case list_descendants(entity) do
[] -> []
[descendant] -> list_tagged_components_for_entity(descendant, tags)
descendants -> list_tagged_components_for_entities(descendants, tags)
end
end
@doc """
Returns a list of components tagged with a list of tags for the parents of a given entity.
The components need to be tagged with all the given tags to return.
## Examples
```elixir
[hero_component] = Ecspanse.Query.list_tagged_components_for_parents(boots_entity, [:hero])
```
"""
@doc group: :tags
@spec list_tagged_components_for_parents(Ecspanse.Entity.t(), list(tag :: atom())) ::
list(components_state :: struct())
def list_tagged_components_for_parents(entity, tags) do
case list_parents(entity) do
[] -> []
[parent] -> list_tagged_components_for_entity(parent, tags)
parents -> list_tagged_components_for_entities(parents, tags)
end
end
@doc """
Returns a list of components tagged with a list of tags for the ancestors of a given entity.
The components need to be tagged with all the given tags to return.
## Examples
```elixir
[dungeon_component] = Ecspanse.Query.list_tagged_components_for_ancestors(hero_entity, [:dungeon])
```
"""
@doc group: :tags
@spec list_tagged_components_for_ancestors(Ecspanse.Entity.t(), list(tag :: atom())) ::
list(components_state :: struct())
def list_tagged_components_for_ancestors(entity, tags) do
case list_ancestors(entity) do
[] -> []
[ancestor] -> list_tagged_components_for_entity(ancestor, tags)
ancestors -> list_tagged_components_for_entities(ancestors, tags)
end
end
@doc """
Fetches the component by its module for a given entity.
## Examples
```elixir
{:ok, gold_component} = Ecspanse.Query.fetch_component(hero_entity, Demo.Components.Gold)
```
"""
@doc group: :components
@spec fetch_component(Ecspanse.Entity.t(), module()) ::
{:ok, component_state :: struct()} | {:error, :not_found}
def fetch_component(%Entity{id: entity_id}, component_module) do
result =
try do
:ets.lookup(Util.components_state_ets_table(), {entity_id, component_module})
rescue
e ->
case :ets.info(Util.components_state_ets_table()) do
:undefined -> reraise Util.server_not_started_error(), __STACKTRACE__
_ -> reraise e, __STACKTRACE__
end
end
case result do
[{_key, _tags, component}] -> {:ok, component}
[] -> {:error, :not_found}
end
end
@doc """
Fetches a tuple of components by their modules for a given entity.
The entity must have all the components for the query to succeed.
## Examples
```elixir
{:ok, {gold_component, gems_component}} = Ecspanse.Query.fetch_components(hero_entity, {Demo.Components.Gold, Demo.Components.Gems})
```
"""
@doc group: :components
@spec fetch_components(Ecspanse.Entity.t(), component_modules :: tuple()) ::
{:ok, components_state :: tuple()} | {:error, :not_found}
def fetch_components(%Entity{} = entity, component_modules_tuple) do
query = select(component_modules_tuple, for: [entity])
case one(query) do
result when is_tuple(result) -> {:ok, result}
nil -> {:error, :not_found}
end
end
@doc """
Lists all the components for a given entity.
The output is an unordered list of all the entity's components.
> #### Note {: .info}
>
> The `Ecspanse.Component.Children` and `Ecspanse.Component.Parents`
> components are **excluded** from the output.
>
> Use the provided `list_children/1` and `list_parents/1` functions to
> query the entity's relations.
## Examples
```elixir
[gold_component, gems_component, position_component, energy_component] =
Ecspanse.Query.list_components(hero_entity)
```
"""
@doc group: :components
@spec list_components(Ecspanse.Entity.t()) :: list(components_state :: struct())
def list_components(%Entity{id: id}) do
table = Util.components_state_ets_table()
f =
Ex2ms.fun do
{{entity_id, component_module}, _component_tags, component_state}
when entity_id == ^id and
component_module != Ecspanse.Component.Children and
component_module != Ecspanse.Component.Parents ->
component_state
end
try do
:ets.select(table, f)
rescue
e ->
case :ets.info(table) do
:undefined -> reraise Util.server_not_started_error(), __STACKTRACE__
_ -> reraise e, __STACKTRACE__
end
end
end
@doc """
Returns `true` if the entity has a component with the given module.
## Examples
```elixir
true = Ecspanse.Query.has_component?(hero_entity, Demo.Components.Gold)
```
"""
@doc group: :components
@spec has_component?(Ecspanse.Entity.t(), module()) :: boolean()
def has_component?(entity, component_module) when is_atom(component_module) do
has_components?(entity, [component_module])
end
@doc """
Returns `true` if the entity has all the components with the given modules.
## Examples
```elixir
true = Ecspanse.Query.has_components?(hero_entity, [Demo.Components.Gold, Demo.Components.Gems])
```
"""
@doc group: :components
@spec has_components?(Ecspanse.Entity.t(), list(module())) :: boolean()
def has_components?(entity, component_module_list)
when is_list(component_module_list) do
entities_components = Ecspanse.Util.list_entities_components()
component_module_list -- Map.get(entities_components, entity.id, []) == []
end
@doc """
Reurns `true` if a given entity is a child of another entity.
## Examples
```elixir
true = Ecspanse.Query.is_child_of?(parent: hero_entity, child: boots_entity)
```
"""
@doc group: :relationships
@spec is_child_of?(parent: Ecspanse.Entity.t(), child: Ecspanse.Entity.t()) :: boolean()
def is_child_of?(parent: %Entity{} = parent, child: %Entity{} = child) do
parents = list_parents(child)
parent in parents
end
@doc """
Reurns `true` if a given entity is a parent of another entity.
## Examples
```elixir
true = Ecspanse.Query.is_parent_of?(parent: hero_entity, child: boots_entity)
```
"""
@doc group: :relationships
@spec is_parent_of?(parent: Ecspanse.Entity.t(), child: Ecspanse.Entity.t()) :: boolean()
def is_parent_of?(parent: %Entity{} = parent, child: %Entity{} = child) do
children = list_children(parent)
child in children
end
@doc """
Returns true if the entity has at least a child with the given component module.
## Examples
```elixir
true = Ecspanse.Query.has_children_with_component?(hero_entity, Demo.Components.Boots)
```
"""
@doc group: :relationships
@spec has_children_with_component?(Ecspanse.Entity.t(), module()) :: boolean()
def has_children_with_component?(entity, component_module) do
has_children_with_components?(entity, [component_module])
end
@doc """
Returns true if the entity has at least a child with all the given component modules.
## Examples
```elixir
true = Ecspanse.Query.has_children_with_components?(hero_entity, [Demo.Components.Boots, Demo.Components.Sword])
```
"""
@doc group: :relationships
@spec has_children_with_components?(Ecspanse.Entity.t(), list(module())) :: boolean()
def has_children_with_components?(entity, component_module_list)
when is_list(component_module_list) do
memo_has_children_with_components?(entity, component_module_list)
end
@doc false
defmemo memo_has_children_with_components?(entity, component_module_list),
max_waiter: 1000,
waiter_sleep_ms: 0 do
components =
select(List.to_tuple(component_module_list), for_children_of: [entity])
|> stream()
|> Enum.to_list()
Enum.any?(components)
end
@doc """
Returns true if the entity has at least a parent with the given component module.
## Examples
```elixir
true = Ecspanse.Query.has_parents_with_component?(boots_entity, Demo.Components.Hero)
```
"""
@doc group: :relationships
@spec has_parents_with_component?(Ecspanse.Entity.t(), module()) ::
boolean()
def has_parents_with_component?(entity, component_module) do
has_parents_with_components?(entity, [component_module])
end
@doc """
Returns true if the entity has at least a parent with all the given component modules.
## Examples
```elixir
true = Ecspanse.Query.has_parents_with_components?(boots_entity, [Demo.Components.Hero, Demo.Components.Gold])
```
"""
@doc group: :relationships
@spec has_parents_with_components?(Ecspanse.Entity.t(), list(module())) :: boolean()
def has_parents_with_components?(entity, component_module_list)
when is_list(component_module_list) do
memo_has_parents_with_components?(entity, component_module_list)
end
@doc false
defmemo memo_has_parents_with_components?(entity, component_module_list),
max_waiter: 1000,
waiter_sleep_ms: 0 do
components =
select(List.to_tuple(component_module_list), for_parents_of: [entity])
|> stream()
|> Enum.to_list()
Enum.any?(components)
end
@doc """
Fetches a resource by its module.
## Examples
```elixir
{:ok, lobby_resource} = Ecspanse.Query.fetch_resource(Demo.Resources.Lobby)
```
"""
@doc group: :resources
@spec fetch_resource(resource_module :: module()) ::
{:ok, resource_state :: struct()} | {:error, :not_found}
def fetch_resource(resource_module) do
result =
try do
:ets.lookup(Util.resources_state_ets_table(), resource_module)
rescue
e ->
case :ets.info(Util.resources_state_ets_table()) do
:undefined -> reraise Util.server_not_started_error(), __STACKTRACE__
_ -> reraise e, __STACKTRACE__
end
end
case result do
[{_key, resource}] -> {:ok, resource}
[] -> {:error, :not_found}
end
end
# Helper
defp compose_component_filters(_select_components, [], acc), do: acc
defp compose_component_filters(select_components, [or_components | rest], acc) do
{with_components, without_components} =
case List.last(or_components) do
{:without, list_of_comp} when is_list(list_of_comp) ->
{List.delete_at(or_components, -1), list_of_comp}
{:without, comp} when is_atom(comp) ->
{List.delete_at(or_components, -1), [comp]}
_ ->
{or_components, []}
end
:ok = validate_components(with_components)
:ok = validate_components(without_components)
compose_component_filters(select_components, rest, [
[
with_components: Enum.uniq(select_components ++ with_components),
without_components: Enum.uniq(without_components)
]
| acc
])
end
defp entities_with_components_stream(query) do
cond do
not Enum.empty?(query.for_entities) ->