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