Skip to main content

workshop_rs/program/
shared.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum EventTeam {
3    All,
4    Team1,
5    Team2,
6}
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum EventTarget {
10    All,
11    Slot(u8),
12    Hero(String),
13}
14
15/// A non-ongoing player event identity.
16///
17/// Workshop can add player-scoped event identities independently of this
18/// crate. Consumers should use a wildcard arm when matching this type.
19#[non_exhaustive]
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum PlayerEventKind {
22    DealtDamage,
23    DealtFinalBlow,
24    DealtHealing,
25    DealtKnockback,
26    Died,
27    EarnedElimination,
28    Joined,
29    Left,
30    ReceivedHealing,
31    ReceivedKnockback,
32    TookDamage,
33}
34
35impl PlayerEventKind {
36    /// The inverse of [`catalog_id`](Self::catalog_id).
37    pub(crate) fn from_catalog_id(id: &str) -> Option<Self> {
38        Some(match id {
39            "playerDealtDamage" => Self::DealtDamage,
40            "playerDealtFinalBlow" => Self::DealtFinalBlow,
41            "playerDealtHealing" => Self::DealtHealing,
42            "playerDealtKnockback" => Self::DealtKnockback,
43            "playerDied" => Self::Died,
44            "playerEarnedElimination" => Self::EarnedElimination,
45            "playerJoined" => Self::Joined,
46            "playerLeft" => Self::Left,
47            "playerReceivedHealing" => Self::ReceivedHealing,
48            "playerReceivedKnockback" => Self::ReceivedKnockback,
49            "playerTookDamage" => Self::TookDamage,
50            _ => return None,
51        })
52    }
53
54    /// The canonical catalog event id (`playerDealtDamage`) the Workshop
55    /// event section resolves this kind from. Public since
56    /// `rule-content-v1` names events by this id.
57    pub fn catalog_id(self) -> &'static str {
58        match self {
59            Self::DealtDamage => "playerDealtDamage",
60            Self::DealtFinalBlow => "playerDealtFinalBlow",
61            Self::DealtHealing => "playerDealtHealing",
62            Self::DealtKnockback => "playerDealtKnockback",
63            Self::Died => "playerDied",
64            Self::EarnedElimination => "playerEarnedElimination",
65            Self::Joined => "playerJoined",
66            Self::Left => "playerLeft",
67            Self::ReceivedHealing => "playerReceivedHealing",
68            Self::ReceivedKnockback => "playerReceivedKnockback",
69            Self::TookDamage => "playerTookDamage",
70        }
71    }
72}
73
74/// The operation used by a Workshop variable modification action.
75///
76/// Workshop can add modification operations independently of this crate.
77/// Consumers should use a wildcard arm when matching this type.
78#[non_exhaustive]
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub enum ModifyOp {
81    Add,
82    Subtract,
83    Multiply,
84    Divide,
85    Modulo,
86    Min,
87    Max,
88    RaiseToPower,
89    AppendToArray,
90    RemoveFromArrayByValue,
91    RemoveFromArrayByIndex,
92}
93
94impl ModifyOp {
95    /// The inverse of [`catalog_id`](Self::catalog_id).
96    pub(crate) fn from_catalog_id(id: &str) -> Option<Self> {
97        Some(match id {
98            "add" => Self::Add,
99            "subtract" => Self::Subtract,
100            "multiply" => Self::Multiply,
101            "divide" => Self::Divide,
102            "modulo" => Self::Modulo,
103            "min" => Self::Min,
104            "max" => Self::Max,
105            "raiseToPower" => Self::RaiseToPower,
106            "appendToArray" => Self::AppendToArray,
107            "removeFromArrayByValue" => Self::RemoveFromArrayByValue,
108            "removeFromArrayByIndex" => Self::RemoveFromArrayByIndex,
109            _ => return None,
110        })
111    }
112
113    pub(crate) fn as_str(self) -> &'static str {
114        match self {
115            Self::Add => "Add",
116            Self::Subtract => "Subtract",
117            Self::Multiply => "Multiply",
118            Self::Divide => "Divide",
119            Self::Modulo => "Modulo",
120            Self::Min => "Min",
121            Self::Max => "Max",
122            Self::RaiseToPower => "RaiseToPower",
123            Self::AppendToArray => "AppendToArray",
124            Self::RemoveFromArrayByValue => "RemoveFromArrayByValue",
125            Self::RemoveFromArrayByIndex => "RemoveFromArrayByIndex",
126        }
127    }
128
129    /// The canonical catalog operator id (`add`, `removeFromArrayByIndex`)
130    /// the Workshop modify-operator position resolves this operation from.
131    /// Public since `rule-content-v1` names modify operands by this id.
132    pub fn catalog_id(self) -> &'static str {
133        match self {
134            Self::Add => "add",
135            Self::Subtract => "subtract",
136            Self::Multiply => "multiply",
137            Self::Divide => "divide",
138            Self::Modulo => "modulo",
139            Self::Min => "min",
140            Self::Max => "max",
141            Self::RaiseToPower => "raiseToPower",
142            Self::AppendToArray => "appendToArray",
143            Self::RemoveFromArrayByValue => "removeFromArrayByValue",
144            Self::RemoveFromArrayByIndex => "removeFromArrayByIndex",
145        }
146    }
147}