rusty_uma_extractor 0.1.0

A set of useful utility modules for applications developed to be used with the game Umamusume: Pretty Derby.
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! # Database Module
//! The main objective of this module is to provide a simple interface to extract vector arrays of structured data objects for one or more of the available
//! data structures in this module.
//! 
//! Currently, the queries are set to return all available records in the database table(s) of the passed-in `Query` with no option to pass in additional SQL operations
//! in order to customize the query. There is no plan to add this functionality in the future, however I am not opposed to adding such functionality if there is a
//! great enough demand or requirement of it. My intention for this module is for it to be a simple one-hot pull of data required for the application in which afterwards
//! the data structures can be referenced in-memory of the application requiring it, therefore needing to query the database directly only on rare/infrequent occasion.

extern crate time;

use core::result;
use std::{ fmt, fs::read, io::{self, Read}, path };
use rusqlite::{ Connection, Error as SqliteError, OpenFlags, ffi::SQLITE_OPEN_READONLY };
use time::UtcDateTime;

use super::data_structures::{ 
    CharacterCard,
    StatGradeRating,
    RunningStyle,
    SupportCard,
    SupportCardRarity,
    SupportCardType,
    Factor,
    FactorGrade,
    FactorType,
    Skill,
    SkillCategory,
    SkillRarity,
    SkillCondition,
    SkillAbilityTarget,
    AbilityType,
    TargetType,
    TargetValue,
    Race,
    RaceGrade,
    RaceDistance,
    RaceGround
};


pub type Result<T> = result::Result<T, DBError>;
pub type QueryResult<T> = Option<Vec<T>>;

// const CHARA_CARD_DATA_SQL: &str = "
//             SELECT c.id, c.chara_id, n.text, c.talent_speed, c.talent_stamina, c.talent_pow, c.talent_guts, c.talent_wiz
//             FROM card_data c
//             JOIN text_data n ON n.\"index\" = c.id AND n.category = 4
//             ORDER BY c.default_rarity DESC, c.id
// ";

const CHARA_CARD_DATA_SQL: &str = "
            SELECT
            crd.id,
            crd.card_id,
            cd.chara_id,
            n.text,
            cd.default_rarity,
            crd.rarity,
            crd.skill_set, 
            cd.talent_speed,
            crd.speed,
            crd.max_speed,
            cd.talent_stamina,
            crd.stamina,
            crd.max_stamina,
            cd.talent_pow,
            crd.pow,
            crd.max_pow,
            cd.talent_guts,
            crd.guts,
            crd.max_guts,
            cd.talent_wiz,
            crd.wiz,
            crd.max_wiz,
            cd.running_style,
            crd.proper_ground_turf,
            crd.proper_ground_dirt,
            crd.proper_distance_short,
            crd.proper_distance_mile,
            crd.proper_distance_middle,
            crd.proper_distance_long 
            FROM card_rarity_data crd
            JOIN card_data cd ON cd.id = crd.card_id
            JOIN text_data n ON n.\"index\" = cd.id AND n.category = 4
            ORDER BY cd.id ASC, crd.rarity
";

const SUPPORT_CARD_DATA_SQL: &str = "
            SELECT c.id, c.chara_id, c.rarity, c.command_id, c.start_date, n.text
            FROM support_card_data c
            JOIN text_data n ON n.\"index\" = c.id AND n.category = 75
            ORDER BY c.rarity DESC, c.start_date, c.id
";

const FACTOR_DATA_SQL: &str = "
            SELECT f.factor_id, f.rarity, f.grade, f.factor_type, n.text, d.text
            FROM succession_factor f
            JOIN text_data n ON n.\"index\" = f.factor_id AND n.category = 147
            JOIN text_data d ON d.\"index\" = f.factor_id AND d.category = 172
";

const SKILL_DATA_SQL: &str = "
            SELECT 
            s.id, 
            s.rarity,
            s.skill_category,
            n.text, 
            d.text,
            s.grade_value,
            s.tag_id,
            s.precondition_1, 
            s.condition_1,
            s.ability_type_1_1,
            s.target_type_1_1,
            s.target_value_1_1,
            s.ability_type_1_2,
            s.target_type_1_2,
            s.target_value_1_2,
            s.ability_type_1_3,
            s.target_type_1_3,
            s.target_value_1_3,
            s.precondition_2, 
            s.condition_2,
            s.ability_type_2_1,
            s.target_type_2_1,
            s.target_value_2_1,
            s.icon_id,
            s.is_general_skill
            FROM skill_data s
            JOIN text_data n ON n.\"index\" = s.id AND n.category = 47
            JOIN text_data d ON d.\"index\" = s.id AND d.category = 48
            ORDER BY s.ability_type_1_1 ASC, s.skill_category
";

const RACE_DATA_SQL: &str = "
            SELECT smp.id, ri.id, r.id, n.text, rt.text, r.grade, rcs.distance, rcs.ground, smp.month, smp.half
            FROM single_mode_program smp
            JOIN race_instance ri ON ri.id = smp.race_instance_id
            JOIN race r ON r.id = ri.race_id
            JOIN race_course_set rcs ON rcs.id = r.course_set
            JOIN text_data n ON n.\"index\" = r.id AND n.category = 33
            JOIN text_data rt ON rt.\"index\" = rcs.race_track_id AND rt.category = 34 
            ORDER BY r.id ASC
";

#[derive(Debug)]
pub enum DBError {
    /// Wrapper for SqliteError
    SQLError(SqliteError),
    /// Raised when there is a data type mismatch between the Struct field data type and the returned data type of that value in the table column
    DataTypeError,
    /// Raised when the path provided for the root folder of the game data cannot be validated or is invalid
    FilePathValidationError(io::Result<bool>)
}

impl std::error::Error for DBError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            DBError::DataTypeError => None,
            DBError::FilePathValidationError(_) => None,
            DBError::SQLError(ref e) => Some(e)
        }
    }
}

impl From<SqliteError> for DBError {
    fn from(value: SqliteError) -> Self {
        Self::SQLError(value)
    }
}

impl fmt::Display for DBError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DBError::SQLError(sql_err) =>
                write!(f,"An error occured querying the database: {}",sql_err.to_string()),
            DBError::DataTypeError =>
                write!(f,"An error occured trying to convert a datatype from the database."),
            DBError::FilePathValidationError(res) => {
                match *res {
                    Ok(_) => write!(f,"Path provided for directory is invalid"),
                    Err(_) => write!(f,"Path provided for directory could not be validated")
                }
            },
        }
    }
}

/// Enumeration options for available data structures to query for

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Query {
    CharacterCard,
    SupportCard,
    Factor,
    Skill,
    Race
}

/// Response object containing results for queries passed into `DatabaseController`

#[derive(Debug)]
pub struct QueryResponse {
    pub character_cards: QueryResult<CharacterCard>,
    pub support_cards: QueryResult<SupportCard>,
    pub factors: QueryResult<Factor>,
    pub skills: QueryResult<Skill>,
    pub races: QueryResult<Race>
}

/// Used to hold and manage queries passed into `DatabaseController`
/// 
/// # Example
/// ```
/// use rusty_uma_extractor::database::{ QueryOptions, Query, QueryResponse, DatabaseController };
/// 
/// //NOTE: The root of this directory would be the SteamLibrary your instance is installed at
/// const APP_DATA_DIR: &str = "
///     /../steamapps/compatdata/{APPID}/pfx/drive_c/users/steamuser/AppData/LocalLow/Cygames/Umamusume";
/// 
/// fn main() {
///     let db_controller: DatabaseController = DatabaseController::new(APP_DATA_DIR).unwrap();
///     let mut q_options: QueryOptions = QueryOptions::new();
///     q_options.add(Query::SupportCard);
///     q_options.add(Query::CharacterCard);
/// 
///     let res: QueryResponse = db_controller.get_queries(q_options).unwrap();
/// 
///     let QueryResponse {
///         character_cards,
///         support_cards,
///         factors: _,
///         skills: _,
///         races: _,
///     } = res;
/// 
///     // Do something with the returned data
///     // ....
/// }
/// 
/// ```

#[derive(PartialEq)]
pub struct QueryOptions {
    query_buff: Vec<Query>
}

impl QueryOptions {
    /// Create and return a new `QueryOptions` instance
    pub fn new() -> QueryOptions {
        let query_buff: Vec<Query> = Vec::with_capacity(5);
        QueryOptions { query_buff }
    }
    /// Adds a new `Query` to the `QueryOptions` instance
    pub fn add(&mut self, query: Query) {
        self.query_buff.push(query);
    }

    /// Remove the specified `Query` from the `QueryOptions` instance
    pub fn remove(&mut self, query: Query) {
        let _ = self.query_buff.extract_if(.., |q| *q == query);
    }

    /// Check if `Query` currently in `QueryOptions` instance
    pub fn has_query(&mut self, query: Query) -> bool {
        self.query_buff.iter().any(|q| *q == query)
    }

    fn iter(&self) -> impl std::iter::Iterator<Item = &Query> {
        self.query_buff.iter()
    }

    /// Clears out `QueryOptions` instance of all `Queries` it contains
    pub fn clear_options(&mut self) {
        self.query_buff.clear();
    }
}

/// Main interface used to pass in `QueryOptions` and return `QueryResponse` containing the requested data structures.
/// 
/// # Example
/// ```
/// use rusty_uma_extractor::database::{ QueryOptions, Query, QueryResponse, DatabaseController };
/// 
/// //NOTE: The root of this directory would be the SteamLibrary your instance is installed at
/// const APP_DATA_DIR: &str = "
///     /../steamapps/compatdata/{APPID}/pfx/drive_c/users/steamuser/AppData/LocalLow/Cygames/Umamusume";
/// 
/// fn main() {
///     let db_controller: DatabaseController = DatabaseController::new(APP_DATA_DIR).unwrap();
///     let mut q_options: QueryOptions = QueryOptions::new();
///     q_options.add(Query::SupportCard);
///     q_options.add(Query::CharacterCard);
/// 
///     let res: QueryResponse = db_controller.get_queries(q_options).unwrap();
/// 
///     let QueryResponse {
///         character_cards,
///         support_cards,
///         factors: _,
///         skills: _,
///         races: _,
///     } = res;
/// 
///     // Do something with the returned data
///     // ....
/// }
/// 
/// ```

#[derive(Debug, Clone)]
pub struct DatabaseController {
    db_path: String
}

impl DatabaseController {
    /// Create new instance of DatabaseController
    /// 
    /// `dir_path` needs to be a full path to the `Umamusume` directory in the AppData folder
    pub fn new(dir_path: &str) -> Result<DatabaseController> {
        let root_path = path::Path::new(dir_path);

        let path = root_path.try_exists();
        if let Ok(is_valid) = path {
            if is_valid {
               match root_path.join("master/master.mdb").to_str().and_then(|v| Some(v.to_owned())) {
                Some(db_path) => return Ok(DatabaseController { db_path }),
                None => return Err(DBError::FilePathValidationError(path))
               }
               
            }
            return Err(DBError::FilePathValidationError(path))
        }
        Err(DBError::FilePathValidationError(path))
    }

    /// Returns a `QueryResponse` containing the `QueryResult` of available data structures
    pub fn get_queries(self,options: QueryOptions) -> Result<QueryResponse> {
        let mut q_res = QueryResponse {
            character_cards: None,
            support_cards: None,
            factors: None,
            skills: None,
            races: None
        };

        for q in options.iter() {
            let db: DatabaseController = self.clone();
            match q {
                Query::CharacterCard => match db.character_card_query() {
                    Ok(character_cards) => q_res.character_cards = Some(character_cards.to_owned()),
                    Err(e) => return Err(e)
                },
                Query::SupportCard => match db.support_card_query() {
                    Ok(support_cards) => q_res.support_cards = Some(support_cards.to_owned()),
                    Err(e) => return Err(e)
                },
                Query::Factor => match db.factor_query() {
                    Ok(factors) => q_res.factors = Some(factors.to_owned()),
                    Err(e) => return Err(e)
                },
                Query::Skill => match db.skill_query() {
                    Ok(skills) => q_res.skills = Some(skills.to_owned()),
                    Err(e) => return Err(e)
                },
                Query::Race => match db.race_query() {
                    Ok(races) => q_res.races = Some(races.to_owned()),
                    Err(e) => return Err(e)
                }
            }
        }
        Ok(q_res)
    }

    fn character_card_query(self) -> Result<Vec<CharacterCard>> {
        let filename = self.db_path;
        let open_flags = OpenFlags::from_bits(SQLITE_OPEN_READONLY).unwrap();

        let conn = Connection::open_with_flags(filename, open_flags)?;
        let mut request = conn.prepare(CHARA_CARD_DATA_SQL)?;

        let to_chara_card = |row: & rusqlite::Row| Ok(
            CharacterCard {
                id: row.get(0)?,
                card_id: row.get(1)?,
                chara_id: row.get(2)?,
                name: row.get(3)?,
                default_rarity: row.get(4)?,
                rarity: row.get(5)?,
                skill_set: row.get(6)?,
                speed_talent: row.get(7)?,
                speed: row.get(8)?,
                max_speed: row.get(9)?,
                stamina_talent: row.get(10)?,
                stamina: row.get(11)?,
                max_stamina: row.get(12)?,
                power_talent: row.get(13)?,
                power: row.get(14)?,
                max_power: row.get(15)?,
                guts_talent: row.get(16)?,
                guts: row.get(17)?,
                max_guts: row.get(18)?,
                wis_talent: row.get(19)?,
                wis: row.get(20)?,
                max_wis: row.get(21)?,
                default_running_style: RunningStyle::try_from(row.get::<usize,i32>(22)?).unwrap(),
                turf_grade: StatGradeRating::try_from(row.get::<usize,i32>(23)?).unwrap(),
                dirt_grade: StatGradeRating::try_from(row.get::<usize,i32>(24)?).unwrap(),
                sprint_grade: StatGradeRating::try_from(row.get::<usize,i32>(25)?).unwrap(),
                mile_grade: StatGradeRating::try_from(row.get::<usize,i32>(26)?).unwrap(),
                medium_grade: StatGradeRating::try_from(row.get::<usize,i32>(27)?).unwrap(),
                long_grade: StatGradeRating::try_from(row.get::<usize,i32>(28)?).unwrap(),
            }
        );

        let chara_cards_qry = request.query_map([], to_chara_card)?;
        chara_cards_qry.map(|val| -> Result<CharacterCard> {
            match val {
                Ok(card) => Ok(card),
                Err(e) => Err(DBError::SQLError(e))
            }
        }).collect()
    }

    fn support_card_query(self) -> Result<Vec<SupportCard>> {
        let filename = self.db_path;
        let open_flags = OpenFlags::from_bits(SQLITE_OPEN_READONLY).unwrap();

        let conn = Connection::open_with_flags(filename, open_flags)?;
        let mut request = conn.prepare(SUPPORT_CARD_DATA_SQL)?;

        let to_support_card = |row: & rusqlite::Row| Ok(
            SupportCard {
                id: row.get(0)?,
                chara_id: row.get(1)?,
                name: row.get(5)?,
                rarity: SupportCardRarity::try_from(row.get::<usize,i32>(2)?).unwrap(),
                card_type: SupportCardType::try_from(row.get::<usize,i32>(3)?).unwrap(),
                timestamp: UtcDateTime::from_unix_timestamp(row.get(4)?).unwrap()
            }
        );

        let support_cards_qry = request.query_map([], to_support_card)?;
        support_cards_qry.map(|val| -> Result<SupportCard> {
            match val {
                Ok(card) => Ok(card),
                Err(e) => Err(DBError::SQLError(e))
            }
        }).collect()
        
    }

    fn factor_query(self) -> Result<Vec<Factor>> {
        let filename = self.db_path;
        let open_flags = OpenFlags::from_bits(SQLITE_OPEN_READONLY).unwrap();

        let conn = Connection::open_with_flags(filename, open_flags)?;
        let mut request = conn.prepare(FACTOR_DATA_SQL)?;

        let to_factor = |row: & rusqlite::Row| Ok(
            Factor {
                id: row.get(0)?,
                name: row.get(4)?,
                desc: row.get(5)?,
                rarity: row.get(1)?,
                grade: FactorGrade::try_from(row.get::<usize,i32>(2)?).unwrap(),
                factor_type: FactorType::try_from(row.get::<usize,i32>(3)?).unwrap()
            }
        );

        let factor_qry = request.query_map([], to_factor)?;
        factor_qry.map(|val| -> Result<Factor> {
            match val {
                Ok(card) => Ok(card),
                Err(e) => Err(DBError::SQLError(e))
            }
        }).collect()
    }

    fn skill_query(self) -> Result<Vec<Skill>> {
        let filename = self.db_path;
        let open_flags = OpenFlags::from_bits(SQLITE_OPEN_READONLY).unwrap();

        let conn = Connection::open_with_flags(filename, open_flags)?;
        let mut request = conn.prepare(SKILL_DATA_SQL)?;

        let to_skill = |row:& rusqlite::Row| Ok({
            let id: i32 = row.get(0)?;
            let rarity: i32 = row.get(1)?;
            let skill_category: i32 = row.get(2)?;
            let name: String = row.get(3)?;
            let desc: String = row.get(4)?;
            let grade_val: i32 = row.get(5)?;
            let tag_id: String = row.get(6)?;
            let precond_1: String = row.get(7)?;
            let cond_1: String = row.get(8)?;
            let ability_type_1_1: i32 = row.get(9)?;
            let target_type_1_1: i32 = row.get(10)?;
            let target_value_1_1: i32 = row.get(11)?;
            let ability_type_1_2: i32 = row.get(12)?;
            let target_type_1_2: i32 = row.get(13)?;
            let target_value_1_2: i32 = row.get(14)?;
            let ability_type_1_3: i32 = row.get(15)?;
            let target_type_1_3: i32 = row.get(16)?;
            let target_value_1_3: i32 = row.get(17)?;
            let precond_2: String = row.get(18)?;
            let cond_2: String = row.get(19)?;
            let ability_type_2_1: i32 = row.get(20)?;
            let target_type_2_1: i32 = row.get(21)?;
            let target_value_2_1: i32 = row.get(22)?;
            let icon_id: i32 = row.get(23)?;
            let is_general_skill: i32 = row.get(24)?;

            let skill_ability_target_1_1 = SkillAbilityTarget {
                ability_type: AbilityType::try_from(ability_type_1_1).ok(),
                target_type: TargetType::try_from(target_type_1_1).ok(),
                target_value: TargetValue::try_from(target_value_1_1).ok(),
            };

            let skill_ability_target_1_2 = SkillAbilityTarget {
                ability_type: AbilityType::try_from(ability_type_1_2).ok(),
                target_type: TargetType::try_from(target_type_1_2).ok(),
                target_value: TargetValue::try_from(target_value_1_2).ok()
            };

            let skill_ability_target_1_3 = SkillAbilityTarget {
                ability_type: AbilityType::try_from(ability_type_1_3).ok(),
                target_type: TargetType::try_from(target_type_1_3).ok(),
                target_value: TargetValue::try_from(target_value_1_3).ok()
            };

            let skill_cond_1 = SkillCondition {
                precondition: precond_1,
                condition: cond_1,
                ability_targets: vec![skill_ability_target_1_1,skill_ability_target_1_2,skill_ability_target_1_3]
            };

            let mut skill_cond_2: Option<SkillCondition> = None;

            if !cond_2.is_empty() {
                let skill_ability_target_2_1 = SkillAbilityTarget {
                ability_type: AbilityType::try_from(ability_type_2_1).ok(),
                target_type: TargetType::try_from(target_type_2_1).ok(),
                target_value: TargetValue::try_from(target_value_2_1).ok()
                };

                skill_cond_2 = Some(SkillCondition {
                    precondition: precond_2,
                    condition: cond_2,
                    ability_targets: vec![skill_ability_target_2_1]
                });
            }

            Skill {
                id,
                category: SkillCategory::into_cat(skill_category, rarity).unwrap(),
                name,
                desc,
                grade_val,
                tag_id,
                skill_conditions: (skill_cond_1,skill_cond_2),
                icon_id,
                is_general_skill: bool::try_from(is_general_skill).unwrap()
            }
        });

        let skill_qry = request.query_map([], to_skill)?;

        skill_qry.map(|val| -> Result<Skill> {
            match val {
                Ok(card) => Ok(card),
                Err(e) => Err(DBError::SQLError(e))
            }
        }).collect()

    }

    fn race_query(self) -> Result<Vec<Race>> {
        let filename = self.db_path;
        let open_flags = OpenFlags::from_bits(SQLITE_OPEN_READONLY).unwrap();

        let conn = Connection::open_with_flags(filename, open_flags).unwrap();
        let mut request = conn.prepare(RACE_DATA_SQL).unwrap();

        let to_response = |row: & rusqlite::Row| Ok(
            Race {
                program_id: row.get_unwrap(0),
                race_inst_id: row.get_unwrap(1),
                race_id: row.get_unwrap(2),
                race_name: row.get_unwrap(3),
                track_name: row.get_unwrap(4),
                race_grade: RaceGrade::try_from(row.get_unwrap::<usize,i32>(5)).unwrap(),
                race_dist: RaceDistance::try_from(row.get_unwrap::<usize,i32>(6)).unwrap(),
                track_ground: RaceGround::try_from(row.get_unwrap::<usize,i32>(7)).unwrap(),
                month: row.get_unwrap(8),
                half: row.get_unwrap(9)          
            }
        );

        let race_qry = request.query_map([], to_response).unwrap();
        race_qry.map(|val| -> Result<Race> {
            match val {
                Ok(card) => Ok(card),
                Err(e) => Err(DBError::SQLError(e))
            }
        }).collect()
        
    }

}