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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use crate::db_object::{DBObject, OrderTypes};
use crate::df_world::{Coordinate, DBDFWorld};
use crate::schema::entities;
use crate::DbConnection;
use df_st_core::fillable::{Fillable, Filler};
use df_st_core::item_count::ItemCount;
use df_st_derive::{Fillable, HashAndPartialEqById};
use diesel::expression_methods::ExpressionMethods;
use diesel::prelude::*;
use diesel::query_dsl::RunQueryDsl;
use diesel::Queryable;
use failure::Error;
use std::collections::HashMap;

mod entity_child_en_id;
mod entity_hf_id;
mod entity_honor;
mod entity_link;
mod entity_occasion;
mod entity_occasion_schedule;
mod entity_occasion_schedule_feature;
mod entity_position;
mod entity_position_assignment;
mod entity_profession;
mod entity_weapon;
mod entity_worship_id;
pub use entity_child_en_id::EntityChildENID;
pub use entity_hf_id::EntityHFID;
pub use entity_honor::EntityHonor;
pub use entity_link::EntityLink;
pub use entity_occasion::EntityOccasion;
pub use entity_occasion_schedule::EntityOccasionSchedule;
pub use entity_occasion_schedule_feature::EntityOccasionScheduleFeature;
pub use entity_position::EntityPosition;
pub use entity_position_assignment::EntityPositionAssignment;
pub use entity_profession::EntityProfession;
pub use entity_weapon::EntityWeapon;
pub use entity_worship_id::EntityWorshipID;

#[derive(
    Clone,
    Debug,
    AsChangeset,
    Identifiable,
    HashAndPartialEqById,
    Queryable,
    Insertable,
    Fillable,
    Default,
)]
#[table_name = "entities"]
pub struct Entity {
    pub id: i32,
    pub world_id: i32,
    pub name: Option<String>,
    pub race: Option<String>,
    pub type_: Option<String>,
}

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

impl DBObject<df_st_core::Entity, Entity> for Entity {
    fn add_missing_data_advanced(core_world: &df_st_core::DFWorld, world: &mut DBDFWorld) {
        for entity in core_world.entities.values() {
            // Add honor list
            for honor in &entity.honor {
                let mut df_honor = EntityHonor::new();
                df_honor.add_missing_data(honor);
                df_honor.en_id = entity.id;
                world.entity_honors.push(df_honor);
            }
            // Add entity_link list
            for entity_link in &entity.entity_link {
                let mut df_entity_link = EntityLink::new();
                df_entity_link.add_missing_data(entity_link);
                df_entity_link.en_id = entity.id;
                world.entity_links.push(df_entity_link);
            }
            // Add entity_position list
            for entity_position in &entity.entity_position {
                let mut df_entity_position = EntityPosition::new();
                df_entity_position.add_missing_data(entity_position);
                df_entity_position.en_id = entity.id;
                world.entity_positions.push(df_entity_position);
            }
            // Add entity_position_assignment list
            for entity_position_assignment in &entity.entity_position_assignment {
                let mut df_entity_position_assignment = EntityPositionAssignment::new();
                df_entity_position_assignment.add_missing_data(entity_position_assignment);
                df_entity_position_assignment.en_id = entity.id;
                world
                    .entity_position_assignments
                    .push(df_entity_position_assignment);
            }
            // Add occasion list
            for occasion in &entity.occasion {
                let mut df_occasion = EntityOccasion::new();
                df_occasion.add_missing_data(occasion);
                df_occasion.en_id = entity.id;
                world.entity_occasions.push(df_occasion);
                // Add entity_occasion_schedules list
                for schedule in &occasion.schedule {
                    let mut df_entity_occasion_schedule = EntityOccasionSchedule::new();
                    df_entity_occasion_schedule.add_missing_data(schedule);
                    df_entity_occasion_schedule.en_id = entity.id;
                    df_entity_occasion_schedule.en_occ_id = occasion.local_id;
                    world
                        .entity_occasion_schedules
                        .push(df_entity_occasion_schedule);
                    // Add entity_occasion_schedule_features list
                    for feature in &schedule.feature {
                        let mut df_feature = EntityOccasionScheduleFeature::new();
                        df_feature.add_missing_data(feature);
                        df_feature.en_id = entity.id;
                        df_feature.en_occ_id = occasion.local_id;
                        df_feature.en_occ_sch_id = schedule.local_id;
                        world.entity_occasion_schedule_features.push(df_feature);
                    }
                }
            }
            // Add entity_worship_ids list
            for item in &entity.worship_id {
                let mut db_item = EntityWorshipID::new();
                db_item.en_id = entity.id;
                db_item.worship_id = *item;
                world.entity_worship_ids.push(db_item);
            }
            // Add entity_weapons list
            for item in &entity.weapon {
                let mut db_item = EntityWeapon::new();
                db_item.en_id = entity.id;
                db_item.weapon = item.clone();
                world.entity_weapons.push(db_item);
            }
            // Add entity_professions list
            for item in &entity.profession {
                let mut db_item = EntityProfession::new();
                db_item.en_id = entity.id;
                db_item.profession = item.clone();
                world.entity_professions.push(db_item);
            }
            // Add entity_hf_ids list
            for item in &entity.hf_ids {
                let mut db_item = EntityHFID::new();
                db_item.en_id = entity.id;
                db_item.hf_id = *item;
                world.entity_hf_ids.push(db_item);
            }
            // Add entity_child_en_ids list
            for item in &entity.child_en_ids {
                let mut db_item = EntityChildENID::new();
                db_item.en_id = entity.id;
                db_item.child_en_id = *item;
                world.entity_child_en_ids.push(db_item);
            }
            // Add claims list
            for coord in &entity.claims {
                let new_id: i32 = world.coordinates.len() as i32;
                world.coordinates.push(Coordinate {
                    id: new_id,
                    x: coord.x,
                    y: coord.y,
                    entity_id: Some(entity.id),
                    ..Default::default()
                });
            }
        }
    }

    #[cfg(feature = "postgres")]
    fn insert_into_db(conn: &DbConnection, entities: &[Entity]) {
        use diesel::pg::upsert::excluded;
        diesel::insert_into(entities::table)
            .values(entities)
            .on_conflict((entities::id, entities::world_id))
            .do_update()
            .set((
                entities::name.eq(excluded(entities::name)),
                entities::race.eq(excluded(entities::race)),
                entities::type_.eq(excluded(entities::type_)),
            ))
            .execute(conn)
            .expect("Error saving entities");
    }

    #[cfg(not(feature = "postgres"))]
    fn insert_into_db(conn: &DbConnection, entities: &[Entity]) {
        diesel::insert_into(entities::table)
            .values(entities)
            .execute(conn)
            .expect("Error saving entities");
    }

    fn find_db_item(
        conn: &DbConnection,
        id_filter: HashMap<String, i32>,
    ) -> Result<Option<Entity>, Error> {
        use crate::schema::entities::dsl::*;
        let query = entities;
        let query = query.filter(world_id.eq(id_filter.get("world_id").unwrap_or(&0)));
        let query = query.filter(id.eq(id_filter.get("id").unwrap_or(&0)));
        Ok(query.first::<Entity>(conn).optional()?)
    }

    fn find_db_list(
        conn: &DbConnection,
        id_filter: HashMap<String, i32>,
        _string_filter: HashMap<String, String>,
        offset: i64,
        limit: i64,
        order: Option<OrderTypes>,
        order_by: Option<String>,
        id_list: Option<Vec<i32>>,
    ) -> Result<Vec<Entity>, Error> {
        use crate::schema::entities::dsl::*;
        let (order_by, asc) = Self::get_order(order, order_by);
        let query = entities.limit(limit).offset(offset);
        let query = query.filter(world_id.eq(id_filter.get("world_id").unwrap_or(&0)));
        optional_filter! {
            query, id_filter,
            id_list => id,
            [
                "id" => id,
            ],
            {Ok(order_by!{
                order_by, asc, query, conn,
                "id" => id,
                "name" => name,
                "race" => race,
                "type" => type_,
            })},
        }
    }

    fn match_field_by(field: String) -> String {
        match field.as_ref() {
            "name" => "name",
            "race" => "race",
            "type" => "type",
            _ => "id",
        }
        .to_owned()
    }

    fn add_nested_items(
        conn: &DbConnection,
        db_list: &[Entity],
        _core_list: Vec<df_st_core::Entity>,
    ) -> Result<Vec<df_st_core::Entity>, Error> {
        let world_id = match db_list.first() {
            Some(x) => x.world_id,
            None => 0,
        };
        // Add EntityHonor
        let honor_list = EntityHonor::belonging_to(db_list)
            .filter(crate::schema::entity_honors::world_id.eq(world_id))
            .load::<EntityHonor>(conn)?
            .grouped_by(db_list);
        // Add EntityLink
        let link_list = EntityLink::belonging_to(db_list)
            .filter(crate::schema::entity_links::world_id.eq(world_id))
            .load::<EntityLink>(conn)?
            .grouped_by(db_list);
        // Add EntityPosition
        let pos_list = EntityPosition::belonging_to(db_list)
            .filter(crate::schema::entity_positions::world_id.eq(world_id))
            .load::<EntityPosition>(conn)?
            .grouped_by(db_list);
        // Add EntityPositionAssignment
        let pos_assignment_list = EntityPositionAssignment::belonging_to(db_list)
            .filter(crate::schema::entity_position_assignments::world_id.eq(world_id))
            .load::<EntityPositionAssignment>(conn)?
            .grouped_by(db_list);
        // Add EntityOccasion
        let occasion_list = EntityOccasion::belonging_to(db_list)
            .filter(crate::schema::entity_occasions::world_id.eq(world_id))
            .load::<EntityOccasion>(conn)?
            .grouped_by(db_list);
        // Add EntityOccasionSchedule
        let schedule_list = EntityOccasionSchedule::belonging_to(db_list)
            .filter(crate::schema::entity_occasion_schedules::world_id.eq(world_id))
            .load::<EntityOccasionSchedule>(conn)?
            .grouped_by(db_list);
        // Add EntityOccasionScheduleFeature
        let schedule_feature_list = EntityOccasionScheduleFeature::belonging_to(db_list)
            .filter(crate::schema::entity_occasion_schedule_features::world_id.eq(world_id))
            .load::<EntityOccasionScheduleFeature>(conn)?
            .grouped_by(db_list);
        // Add EntityWorshipID
        let worship_id_list = EntityWorshipID::belonging_to(db_list)
            .filter(crate::schema::entity_worship_ids::world_id.eq(world_id))
            .load::<EntityWorshipID>(conn)?
            .grouped_by(db_list);
        // Add EntityWeapon
        let weapon_list = EntityWeapon::belonging_to(db_list)
            .filter(crate::schema::entity_weapons::world_id.eq(world_id))
            .load::<EntityWeapon>(conn)?
            .grouped_by(db_list);
        // Add EntityProfession
        let profession_list = EntityProfession::belonging_to(db_list)
            .filter(crate::schema::entity_professions::world_id.eq(world_id))
            .load::<EntityProfession>(conn)?
            .grouped_by(db_list);
        // Add EntityHFID
        let hf_id_list = EntityHFID::belonging_to(db_list)
            .filter(crate::schema::entity_hf_ids::world_id.eq(world_id))
            .load::<EntityHFID>(conn)?
            .grouped_by(db_list);
        // Add EntityChildENID
        let child_en_id_list = EntityChildENID::belonging_to(db_list)
            .filter(crate::schema::entity_child_en_ids::world_id.eq(world_id))
            .load::<EntityChildENID>(conn)?
            .grouped_by(db_list);
        // Add EntityChildENID
        let coord_list = Coordinate::belonging_to(db_list)
            .filter(crate::schema::coordinates::world_id.eq(world_id))
            .load::<Coordinate>(conn)?
            .grouped_by(db_list);

        // Merge all
        let mut core_list: Vec<df_st_core::Entity> = Vec::new();
        for (index, en) in db_list.iter().enumerate() {
            let mut core_en = df_st_core::Entity::new();
            core_en.add_missing_data(en);

            for honor in honor_list.get(index).unwrap() {
                core_en.honor.add_missing_data(&vec![honor.clone()]);
            }
            for link in link_list.get(index).unwrap() {
                core_en.entity_link.add_missing_data(&vec![link.clone()]);
            }
            for pos in pos_list.get(index).unwrap() {
                core_en.entity_position.add_missing_data(&vec![pos.clone()]);
            }
            for pos_assignment in pos_assignment_list.get(index).unwrap() {
                core_en
                    .entity_position_assignment
                    .add_missing_data(&vec![pos_assignment.clone()]);
            }
            for occasion in occasion_list.get(index).unwrap() {
                let en_occ_id = occasion.local_id;
                let mut core_occasion = df_st_core::EntityOccasion::new();
                core_occasion.add_missing_data(occasion);

                for schedule in schedule_list.get(index).unwrap() {
                    if schedule.en_occ_id == en_occ_id {
                        let en_occ_sch_id = schedule.local_id;
                        let mut core_schedule = df_st_core::EntityOccasionSchedule::new();
                        core_schedule.add_missing_data(schedule);

                        for schedule_feature in schedule_feature_list.get(index).unwrap() {
                            if schedule_feature.en_occ_id == en_occ_id
                                && schedule_feature.en_occ_sch_id == en_occ_sch_id
                            {
                                core_schedule
                                    .feature
                                    .add_missing_data(&vec![schedule_feature.clone()]);
                            }
                        }
                        core_occasion
                            .schedule
                            .add_missing_data(&vec![core_schedule]);
                    }
                }
                core_en.occasion.push(core_occasion);
            }
            for item in worship_id_list.get(index).unwrap() {
                core_en.worship_id.push(item.worship_id);
            }
            for item in weapon_list.get(index).unwrap() {
                core_en.weapon.push(item.weapon.clone());
            }
            for item in profession_list.get(index).unwrap() {
                core_en.profession.push(item.profession.clone());
            }
            for item in hf_id_list.get(index).unwrap() {
                core_en.hf_ids.push(item.hf_id);
            }
            for item in child_en_id_list.get(index).unwrap() {
                core_en.child_en_ids.push(item.child_en_id);
            }
            core_en
                .claims
                .add_missing_data(coord_list.get(index).unwrap());
            core_list.push(core_en);
        }
        Ok(core_list)
    }

    fn get_count_from_db(
        conn: &DbConnection,
        id_filter: HashMap<String, i32>,
        _string_filter: HashMap<String, String>,
        offset: u32,
        limit: u32,
        group_by_opt: Option<String>,
        id_list: Option<Vec<i32>>,
    ) -> Result<Vec<ItemCount>, Error> {
        use crate::schema::entities::dsl::*;
        let query = entities.limit(limit as i64).offset(offset as i64);
        let query = query.filter(world_id.eq(id_filter.get("world_id").unwrap_or(&0)));
        optional_filter! {
            query, id_filter,
            id_list => id,
            [
                "id" => id,
            ],
            {group_by!{
                group_by_opt, query, conn,
                "id" => {id: i32},
                "name" => {name: Option<String>},
                "race" => {race: Option<String>},
                "type" => {type_: Option<String>},
            };},
        };
    }
}

/// From Core to DB
impl Filler<Entity, df_st_core::Entity> for Entity {
    fn add_missing_data(&mut self, source: &df_st_core::Entity) {
        self.id.add_missing_data(&source.id);
        self.name.add_missing_data(&source.name);
        self.race.add_missing_data(&source.race);
        self.type_.add_missing_data(&source.type_);
    }
}

/// From DB to Core
impl Filler<df_st_core::Entity, Entity> for df_st_core::Entity {
    fn add_missing_data(&mut self, source: &Entity) {
        self.id.add_missing_data(&source.id);
        self.name.add_missing_data(&source.name);
        self.race.add_missing_data(&source.race);
        self.type_.add_missing_data(&source.type_);
    }
}

impl PartialEq<Entity> for df_st_core::Entity {
    fn eq(&self, other: &Entity) -> bool {
        self.id == other.id
    }
}

impl PartialEq<df_st_core::Entity> for Entity {
    fn eq(&self, other: &df_st_core::Entity) -> bool {
        self.id == other.id
    }
}