1
+ local InternalZone = require ' internal.game.zone.InternalZone' ;
2
+
3
+ --- @type ZoneService
4
+ local ZoneService = Class .singleton (' ZoneService' , ' EventEmitter' , function (class )
5
+
6
+ --- @class ZoneService : BaseObject
7
+ --- @field public zones table<string , InternalZone>
8
+ --- @field private resources table<string , string[]>
9
+ local self = class ;
10
+
11
+ function self :Constructor ()
12
+ self :super ();
13
+ self .zones = {};
14
+ self .resources = {};
15
+ end
16
+
17
+ --- @param zone Zone
18
+ function self :Register (zone )
19
+ assert (not lib .is_server , ' ZoneService:Register is only available on client.' );
20
+ if (typeof (self .zones [zone .id ]) == ' InternalZone' ) then
21
+ console .warn ((" ^7[^6ZoneService^7] ^3Zone ^7[^5%s^7] ^3is already registered, overwriting..." ):format (zone .id ));
22
+ end
23
+ self .zones [zone .id ] = InternalZone (zone .id , zone .resource );
24
+ self .resources [zone .resource ] = type (self .resources [zone .resource ]) == ' table' and self .resources [zone .resource ] or {};
25
+ self .resources [zone .resource ][# self .resources [zone .resource ] + 1 ] = zone .id ;
26
+ console .success ((" ^7[^6ZoneService^7] ^3Zone ^7[^5%s^7] ^3has been registered." ):format (zone .id ));
27
+ end
28
+
29
+ --- @param id string
30
+ --- @return InternalZone
31
+ function self :Get (id )
32
+ assert (not lib .is_server , ' ZoneService:Get is only available on client.' );
33
+ return self .zones [id ];
34
+ end
35
+
36
+ --- @return table<string , InternalZone>
37
+ function self :GetAll ()
38
+ assert (not lib .is_server , ' ZoneService:GetAll is only available on client.' )
39
+ return self .zones ;
40
+ end
41
+
42
+ --- @param resource string
43
+ --- @param id string
44
+ --- @return table<string , InternalZone> , number
45
+ function self :GetByResource (resource , id )
46
+ assert (not lib .is_server , ' ZoneService:GetByResource is only available on client.' );
47
+ if (type (self .resources [resource ]) == ' table' ) then
48
+ for i = 1 , # self .resources [resource ] do
49
+ if (self .resources [resource ][i ] == id ) then
50
+ return self .zones [id ], i ;
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ --- @param resource string
57
+ --- @return table<string , InternalZone> | nil
58
+ function self :GetAllByResource (resource )
59
+ assert (not lib .is_server , ' ZoneService:GetAllByResource is only available on client.' );
60
+ if (type (self .resources [resource ]) == ' table' ) then
61
+ local zones = {};
62
+ for i = 1 , # self .resources [resource ] do
63
+ zones [self .resources [resource ][i ]] = self .zones [self .resources [resource ][i ]];
64
+ end
65
+ return zones ;
66
+ end
67
+ return nil ;
68
+ end
69
+
70
+ --- @param id string
71
+ function self :Remove (id )
72
+ if (typeof (self .zones [id ]) == ' InternalZone' ) then
73
+ local _ , index = self :GetByResource (self .zones [id ].resource , id );
74
+ self .zones [id ] = nil ;
75
+ self .resources [self .zones [id ].resource ][index ] = nil ;
76
+ console .warn ((" ^7[^6ZoneService^7] ^3Zone ^7[^5%s^7] ^3has been removed." ):format (id ));
77
+ end
78
+ end
79
+
80
+ --- @param resource string
81
+ function self :RemoveByResource (resource )
82
+ assert (not lib .is_server , ' ZoneService:RemoveByResource is only available on client.' );
83
+ if (type (self .resources [resource ]) == ' table' ) then
84
+ for i = 1 , # self .resources [resource ] do
85
+ self .zones [self .resources [resource ][i ]] = nil ;
86
+ console .warn ((" ^7[^6ZoneService^7] ^3Zone ^7[^5%s^7] ^3has been removed due to ^7[^5%s^7] ^3resource stop." ):format (self .resources [resource ][i ], resource ));
87
+ end
88
+ self .resources [resource ] = nil ;
89
+ end
90
+ end
91
+
92
+ return self ;
93
+
94
+ end );
95
+
96
+ return ZoneService ;
0 commit comments