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
use crate::*;
use boxcars::Attribute;
use std::backtrace::Backtrace;
use thiserror::Error;

/// [`SubtrActorErrorVariant`] is an enumeration of all the specific error
/// variants that can occur while processing game replays in the subtr-actor
/// domain. These include errors related to network frames, frame indexing,
/// player sets, actor states, object ids, team identities, and data types
/// amongst others.
#[derive(Error, Debug, Clone)]
pub enum SubtrActorErrorVariant {
    #[error("Replay has no network frames")]
    NoNetworkFrames,

    #[error("Frame index out of bounds")]
    FrameIndexOutOfBounds,

    #[error("Players found in frames that were not part of original set. Found: {found:?}, Original: {original:?}")]
    InconsistentPlayerSet {
        found: std::collections::HashSet<PlayerId>,
        original: std::collections::HashSet<PlayerId>,
    },

    #[error(
        "No update for ActorId {actor_id:?} of ObjectId {object_id:?} after frame {frame_index}"
    )]
    NoUpdateAfterFrame {
        actor_id: boxcars::ActorId,
        object_id: boxcars::ObjectId,
        frame_index: usize,
    },

    #[error("No boost amount value.")]
    NoBoostAmountValue,

    #[error("The attribute value that was found was not of the expected type {expected_type:?} {actual_type:?}")]
    UnexpectedAttributeType {
        expected_type: String,
        actual_type: String,
    },

    #[error("ActorId {actor_id:?} has no matching player id")]
    NoMatchingPlayerId { actor_id: boxcars::ActorId },

    #[error("No game actor")]
    NoGameActor,

    #[error("ActorId {actor_id:} already exists with object_id {object_id:}")]
    ActorIdAlreadyExists {
        actor_id: boxcars::ActorId,
        object_id: boxcars::ObjectId,
    },

    #[error("{name:?} actor for player {player_id:?} not found")]
    ActorNotFound {
        name: &'static str,
        player_id: PlayerId,
    },

    #[error("There was no actor state for actor_id: {actor_id:?}")]
    NoStateForActorId { actor_id: boxcars::ActorId },

    #[error("Couldn't find object id for {name}")]
    ObjectIdNotFound { name: &'static str },

    #[error("No value found for derived key {name:?}")]
    DerivedKeyValueNotFound { name: String },

    #[error("Ball actor not found")]
    BallActorNotFound,

    #[error("Player team unknown, {player_id:?}")]
    UnknownPlayerTeam { player_id: PlayerId },

    #[error("Team object id not known {object_id:?}, for player {player_id:?}")]
    UnknownTeamObjectId {
        object_id: boxcars::ObjectId,
        player_id: PlayerId,
    },

    #[error("Team name was empty for {player_id:?}")]
    EmptyTeamName { player_id: PlayerId },

    #[error("Error returned to deliberately end processing early")]
    FinishProcessingEarly,

    #[error("Player stats header not found")]
    PlayerStatsHeaderNotFound,

    #[error("Interpolation time order was incorrect start_time {start_time:} {time:} {end_time:}")]
    InterpolationTimeOrderError {
        start_time: f32,
        time: f32,
        end_time: f32,
    },

    #[error("The updated actor id does not exist {update:?}")]
    UpdatedActorIdDoesNotExist { update: boxcars::UpdatedAttribute },

    #[error("Could not find {property:} in state")]
    PropertyNotFoundInState { property: &'static str },

    #[error("Could not build replay meta")]
    CouldNotBuildReplayMeta,

    #[error("Error converting float")]
    FloatConversionError,

    #[error(transparent)]
    NDArrayShapeError(#[from] ::ndarray::ShapeError),

    #[error("{0:?} was not a recognized feature adder")]
    UnknownFeatureAdderName(String),
}

/// [`SubtrActorError`] struct provides an error variant
/// [`SubtrActorErrorVariant`] along with its backtrace.
#[derive(Debug)]
pub struct SubtrActorError {
    pub backtrace: Backtrace,
    pub variant: SubtrActorErrorVariant,
}

impl SubtrActorError {
    pub fn new(variant: SubtrActorErrorVariant) -> Self {
        Self {
            backtrace: Backtrace::capture(),
            variant,
        }
    }

    pub fn new_result<T>(variant: SubtrActorErrorVariant) -> Result<T, Self> {
        Err(Self::new(variant))
    }
}

pub type SubtrActorResult<T> = Result<T, SubtrActorError>;

pub fn attribute_to_tag(attribute: &Attribute) -> &str {
    match attribute {
        Attribute::Boolean(_) => "AttributeTag::Boolean",
        Attribute::Byte(_) => "AttributeTag::Byte",
        Attribute::AppliedDamage(_) => "AttributeTag::AppliedDamage",
        Attribute::DamageState(_) => "AttributeTag::DamageState",
        Attribute::CamSettings(_) => "AttributeTag::CamSettings",
        Attribute::ClubColors(_) => "AttributeTag::ClubColors",
        Attribute::Demolish(_) => "AttributeTag::Demolish",
        Attribute::DemolishFx(_) => "AttributeTag::DemolishFx",
        Attribute::Enum(_) => "AttributeTag::Enum",
        Attribute::Explosion(_) => "AttributeTag::Explosion",
        Attribute::ExtendedExplosion(_) => "AttributeTag::ExtendedExplosion",
        Attribute::FlaggedByte(_, _) => "AttributeTag::FlaggedByte",
        Attribute::ActiveActor(_) => "AttributeTag::ActiveActor",
        Attribute::Float(_) => "AttributeTag::Float",
        Attribute::GameMode(_, _) => "AttributeTag::GameMode",
        Attribute::Int(_) => "AttributeTag::Int",
        Attribute::Int64(_) => "AttributeTag::Int64",
        Attribute::Loadout(_) => "AttributeTag::Loadout",
        Attribute::TeamLoadout(_) => "AttributeTag::TeamLoadout",
        Attribute::Location(_) => "AttributeTag::Location",
        Attribute::MusicStinger(_) => "AttributeTag::MusicStinger",
        Attribute::Pickup(_) => "AttributeTag::Pickup",
        Attribute::PickupNew(_) => "AttributeTag::PickupNew",
        Attribute::PlayerHistoryKey(_) => "AttributeTag::PlayerHistoryKey",
        Attribute::Welded(_) => "AttributeTag::Welded",
        Attribute::RigidBody(_) => "AttributeTag::RigidBody",
        Attribute::Title(_, _, _, _, _, _, _, _) => "AttributeTag::Title",
        Attribute::TeamPaint(_) => "AttributeTag::TeamPaint",
        Attribute::String(_) => "AttributeTag::String",
        Attribute::UniqueId(_) => "AttributeTag::UniqueId",
        Attribute::Reservation(_) => "AttributeTag::Reservation",
        Attribute::PartyLeader(_) => "AttributeTag::PartyLeader",
        Attribute::LoadoutOnline(_) => "AttributeTag::LoadoutOnline",
        Attribute::LoadoutsOnline(_) => "AttributeTag::LoadoutsOnline",
        Attribute::StatEvent(_) => "AttributeTag::StatEvent",
        Attribute::RepStatTitle(_) => "AttributeTag::RepStatTitle",
        Attribute::PickupInfo(_) => "AttributeTag::PickupInfo",
        Attribute::Impulse(_) => "AttributeTag::Impulse",
        Attribute::QWord(_) => "AttributeTag::QWordString",
        Attribute::PrivateMatch(_) => "AttributeTag::PrivateMatchSettings",
        Attribute::Rotation(_) => "AttributeTag::RotationTag",
    }
}