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
// use crate::create_new::CreateNew;
use crate::fillable::{Fillable, Filler};
use crate::DFWorld;
use crate::SchemaExample;
use df_st_derive::{Fillable, Filler};
use juniper::GraphQLObject;
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::hash::Hash;

/// An link between a `HistoricalEvent` and a `HistoricalFigure`.
#[derive(
    Serialize,
    Deserialize,
    Clone,
    Debug,
    Fillable,
    Filler,
    Default,
    JsonSchema,
    GraphQLObject,
    Hash,
    PartialEq,
    Eq,
)]
pub struct LinkHEHF {
    /// A reference to a HistoricalEvent.
    pub he_id: i32,
    /// A reference to a HistoricalFigure.
    pub hf_id: i32,
}

macro_rules! add_all_from_hf_vec(
    { $links:ident, $he_id:ident, $he:ident: [$($hf_ids:ident),* $(,)*] } => {
        {
            $(
                Self::add_hf_from_vec(&mut $links, $he_id, &$he.$hf_ids);
            )*
        }
     };
);

macro_rules! add_all_from_hf_option(
    { $links:ident, $he_id:ident, $he:ident: [$($hf_id:ident),* $(,)*] } => {
        {
            $(
                Self::add_from_option(&mut $links, $he_id, $he.$hf_id);
            )*
        }
     };
);

impl LinkHEHF {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn create_links(world: &DFWorld) -> HashSet<LinkHEHF> {
        let mut links: HashSet<LinkHEHF> = HashSet::new();

        for (_, he) in &world.historical_events {
            let he_id = he.id;
            let he_id_o = Some(he.id);
            add_all_from_hf_vec!(links, he_id, he: [
                a_hf_ids,
                d_hf_ids,
                bodies_hf_id,
                competitor_hf_id,
                conspirator_hf_id,
                expelled_hf_id,
                groups_hf_id,
                implicated_hf_id,
            ]);
            add_all_from_hf_option!(links, he_id_o, he: [
                a_leader_hf_id,
                a_tactician_hf_id,
                acquirer_hf_id,
                actor_hf_id,
                appointer_hf_id,
                attacker_general_hf_id,
                attacker_hf_id,
                builder_hf_id,
                changee_hf_id,
                changer_hf_id,
                coconspirator_hf_id,
                contact_hf_id,
                convicted_hf_id,
                corrupt_convicter_hf_id,
                corruptor_hf_id,
                creator_hf_id,
                d_tactician_hf_id,
                defender_general_hf_id,
                doer_hf_id,
                enslaved_hf_id,
                eater_hf_id,
                fooled_hf_id,
                framer_hf_id,
                gambler_hf_id,
                giver_hf_id,
                group_1_hf_id,
                group_2_hf_id,
                group_hf_id,
                hf_id,
                hf_id1,
                hf_id2,
                hf_id_target,
                instigator_hf_id,
                interrogator_hf_id,
                identity_hf_id,
                last_owner_hf_id,
                leader_hf_id,
                lure_hf_id,
                modifier_hf_id,
                new_leader_hf_id,
                overthrown_hf_id,
                payer_hf_id,
                persecutor_hf_id,
                plotter_hf_id,
                pos_taker_hf_id,
                promise_to_hf_id,
                property_confiscated_from_hf_id,
                ransomed_hf_id,
                ransomer_hf_id,
                receiver_hf_id,
                saboteur_hf_id,
                seeker_hf_id,
                seller_hf_id,
                site_hf_id,
                slayer_hf_id,
                snatcher_hf_id,
                speaker_hf_id,
                spotter_hf_id,
                student_hf_id,
                sanctify_hf_id,
                target_hf_id,
                teacher_hf_id,
                trader_hf_id,
                trickster_hf_id,
                victim_hf_id,
                winner_hf_id,
                woundee_hf_id,
                wounder_hf_id,
            ]);
        }
        // check if relations correct
        // This is added because of https://github.com/DFHack/dfhack/issues/1629
        let last_hf_id = world.historical_figures.len() as i32;
        let last_he_id = world.historical_events.len() as i32;

        let mut remove_list: HashSet<LinkHEHF> = HashSet::new();
        for item in links.iter() {
            if item.hf_id > last_hf_id {
                warn!("Found LinkHEHF where hf_id is incorrect: {:#?}", item);
                remove_list.insert(item.clone());
            }
            if item.he_id > last_he_id {
                warn!("Found LinkHEHF where he_id is incorrect: {:#?}", item);
                remove_list.insert(item.clone());
            }
        }
        for item in remove_list.iter() {
            links.remove(item);
        }
        links
    }

    pub fn add_from_option(list: &mut HashSet<LinkHEHF>, he_id: Option<i32>, hf_id: Option<i32>) {
        if he_id.is_some() && hf_id.is_some() {
            let he_id = he_id.unwrap_or(0);
            let hf_id = hf_id.unwrap_or(0);
            // Skip values that are -1 or lower
            if he_id < 0 || hf_id < 0 {
                return;
            }
            list.insert(LinkHEHF { he_id, hf_id });
        }
    }

    pub fn add_hf_from_vec(list: &mut HashSet<LinkHEHF>, he_id: i32, hf_ids: &[i32]) {
        for hf_id in hf_ids {
            list.insert(LinkHEHF {
                he_id,
                hf_id: *hf_id,
            });
        }
    }
}

impl SchemaExample for LinkHEHF {
    fn example() -> Self {
        Self::default()
    }
}