Skip to main content

workshop_rs/program/
shared.rs

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