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
//! Scope graph for the Valence Command system.
//!
//! ## Breakdown
//! Each scope is a node in a graph. A path from one node to another indicates
//! that the first scope implies the second. A dot in the scope name indicates
//! a sub-scope. You can use this to create a hierarchy of scopes. For example,
//! the scope "valence.command" implies "valence.command.tp". this means that if
//! a player has the "valence.command" scope, they can use the "tp" command.
//!
//! You may also link scopes together in the registry. This is useful for admin
//! scope umbrellas. For example, if the scope "valence.admin" is linked to
//! "valence.command", It means that if a player has the "valence.admin" scope,
//! they can use all commands under the command scope.
//!
//! # Example
//! ```
//! use valence_command::scopes::CommandScopeRegistry;
//!
//! let mut registry = CommandScopeRegistry::new();
//!
//! // add a scope to the registry
//! registry.add_scope("valence.command.teleport");
//!
//! // we added 4 scopes to the registry. "valence", "valence.command", "valence.command.teleport",
//! // and the root scope.
//! assert_eq!(registry.scope_count(), 4);
//!
//! registry.add_scope("valence.admin");
//!
//! // add a scope to the registry with a link to another scope
//! registry.link("valence.admin", "valence.command.teleport");
//!
//! // the "valence.admin" scope implies the "valence.command.teleport" scope
//! assert_eq!(
//!     registry.grants("valence.admin", "valence.command.teleport"),
//!     true
//! );
//! ```

use std::collections::{BTreeSet, HashMap};
use std::fmt::{Debug, Formatter};

use bevy_app::{App, Plugin, Update};
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::prelude::{Component, ResMut};
use bevy_ecs::query::Changed;
use bevy_ecs::system::{Query, Resource};
use petgraph::dot;
use petgraph::dot::Dot;
use petgraph::prelude::*;

pub struct CommandScopePlugin;

impl Plugin for CommandScopePlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<CommandScopeRegistry>()
            .add_systems(Update, add_new_scopes);
    }
}

/// Command scope Component for players. This is a list of scopes that a player
/// has. If a player has a scope, they can use any command that requires
/// that scope.
#[derive(
    Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Component, Default, Deref, DerefMut,
)]
pub struct CommandScopes(pub BTreeSet<String>);

/// This system makes it a bit easier to add new scopes to the registry without
/// having to explicitly add them to the registry on app startup.
fn add_new_scopes(
    mut registry: ResMut<CommandScopeRegistry>,
    scopes: Query<&CommandScopes, Changed<CommandScopes>>,
) {
    for scopes in scopes.iter() {
        for scope in scopes.iter() {
            if !registry.string_to_node.contains_key(scope) {
                registry.add_scope(scope);
            }
        }
    }
}

impl CommandScopes {
    /// create a new scope component
    pub fn new() -> Self {
        Self::default()
    }

    /// add a scope to this component
    pub fn add(&mut self, scope: &str) {
        self.0.insert(scope.into());
    }
}

/// Store the scope graph and provide methods for querying it.
#[derive(Clone, Resource)]
pub struct CommandScopeRegistry {
    graph: Graph<String, ()>,
    string_to_node: HashMap<String, NodeIndex>,
    root: NodeIndex,
}

impl Default for CommandScopeRegistry {
    fn default() -> Self {
        let mut graph = Graph::new();
        let root = graph.add_node("root".to_owned());
        Self {
            graph,
            string_to_node: HashMap::from([("root".to_owned(), root)]),
            root,
        }
    }
}

impl Debug for CommandScopeRegistry {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{:?}",
            Dot::with_config(&self.graph, &[dot::Config::EdgeNoLabel])
        )?;
        Ok(())
    }
}

impl CommandScopeRegistry {
    /// Create a new scope registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a scope to the registry.
    ///
    /// # Example
    /// ```
    /// use valence_command::CommandScopeRegistry;
    ///
    /// let mut registry = CommandScopeRegistry::new();
    ///
    /// // creates two nodes. "valence" and "command" with an edge from "valence" to "command"
    /// registry.add_scope("valence.command");
    /// // creates one node. "valence.command.tp" with an edge from "valence.command" to
    /// // "valence.command.tp"
    /// registry.add_scope("valence.command.tp");
    ///
    /// // the root node is always present
    /// assert_eq!(registry.scope_count(), 4);
    /// ```
    pub fn add_scope<S: Into<String>>(&mut self, scope: S) {
        let scope = scope.into();
        if self.string_to_node.contains_key(&scope) {
            return;
        }
        let mut current_node = self.root;
        let mut prefix = String::new();
        for part in scope.split('.') {
            let node = self
                .string_to_node
                .entry(prefix.clone() + part)
                .or_insert_with(|| {
                    let node = self.graph.add_node(part.to_owned());
                    self.graph.add_edge(current_node, node, ());
                    node
                });
            current_node = *node;

            prefix = prefix + part + ".";
        }
    }

    /// Remove a scope from the registry.
    ///
    /// # Example
    /// ```
    /// use valence_command::CommandScopeRegistry;
    ///
    /// let mut registry = CommandScopeRegistry::new();
    ///
    /// registry.add_scope("valence.command");
    /// registry.add_scope("valence.command.tp");
    ///
    /// assert_eq!(registry.scope_count(), 4);
    ///
    /// registry.remove_scope("valence.command.tp");
    ///
    /// assert_eq!(registry.scope_count(), 3);
    /// ```
    pub fn remove_scope(&mut self, scope: &str) {
        if let Some(node) = self.string_to_node.remove(scope) {
            self.graph.remove_node(node);
        };
    }

    /// Check if a scope grants another scope.
    ///
    /// # Example
    /// ```
    /// use valence_command::CommandScopeRegistry;
    ///
    /// let mut registry = CommandScopeRegistry::new();
    ///
    /// registry.add_scope("valence.command");
    /// registry.add_scope("valence.command.tp");
    ///
    /// assert!(registry.grants("valence.command", "valence.command.tp")); // command implies tp
    /// assert!(!registry.grants("valence.command.tp", "valence.command")); // tp does not imply command
    /// ```
    pub fn grants(&self, scope: &str, other: &str) -> bool {
        if scope == other {
            return true;
        }

        let scope_idx = match self.string_to_node.get(scope) {
            None => {
                return false;
            }
            Some(idx) => *idx,
        };
        let other_idx = match self.string_to_node.get(other) {
            None => {
                return false;
            }
            Some(idx) => *idx,
        };

        if scope_idx == self.root {
            return true;
        }

        // if we can reach the other scope from the scope, then the scope
        // grants the other scope
        let mut dfs = Dfs::new(&self.graph, scope_idx);
        while let Some(node) = dfs.next(&self.graph) {
            if node == other_idx {
                return true;
            }
        }
        false
    }

    /// do any of the scopes in the list grant the other scope?
    ///
    /// # Example
    /// ```
    /// use valence_command::CommandScopeRegistry;
    ///
    /// let mut registry = CommandScopeRegistry::new();
    ///
    /// registry.add_scope("valence.command");
    /// registry.add_scope("valence.command.tp");
    /// registry.add_scope("valence.admin");
    ///
    /// assert!(registry.any_grants(
    ///     &vec!["valence.admin", "valence.command"],
    ///     "valence.command.tp"
    /// ));
    /// ```
    pub fn any_grants(&self, scopes: &Vec<&str>, other: &str) -> bool {
        for scope in scopes {
            if self.grants(scope, other) {
                return true;
            }
        }
        false
    }

    /// Create a link between two scopes so that one implies the other. It will
    /// add them if they don't exist.
    ///
    /// # Example
    /// ```
    /// use valence_command::CommandScopeRegistry;
    ///
    /// let mut registry = CommandScopeRegistry::new();
    ///
    /// registry.add_scope("valence.command.tp");
    ///
    /// registry.link("valence.admin", "valence.command");
    ///
    /// assert!(registry.grants("valence.admin", "valence.command"));
    /// assert!(registry.grants("valence.admin", "valence.command.tp"));
    /// ```
    pub fn link(&mut self, scope: &str, other: &str) {
        self.add_scope(scope);
        self.add_scope(other);

        let scope_idx = self.string_to_node[scope];
        let other_idx = self.string_to_node[other];

        self.graph.add_edge(scope_idx, other_idx, ());
    }

    /// Get the number of scopes in the registry.
    pub fn scope_count(&self) -> usize {
        self.graph.node_count()
    }
}