fish_lib/models/
fishing_history_entry.rs

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
use crate::get_config;
use crate::traits::model::Model;
use crate::utils::math::float_interpolate;
use chrono::{DateTime, Utc};
use diesel::{AsChangeset, Insertable, Queryable, Selectable};

#[derive(Debug, Clone, PartialEq, Queryable, Selectable, AsChangeset)]
#[diesel(table_name = crate::schema::fish_fishing_history_entries)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct FishingHistoryEntry {
    pub id: i64,
    pub user_id: i64,
    pub species_id: i32,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub caught_count: i32,
    pub sold_count: i32,
    pub smallest_catch_size_ratio: f32,
    pub largest_catch_size_ratio: f32,
    pub last_catch: DateTime<Utc>,
    pub first_sell: Option<DateTime<Utc>>,
    pub last_sell: Option<DateTime<Utc>>,
}

impl FishingHistoryEntry {
    pub fn get_first_catch(&self) -> DateTime<Utc> {
        self.created_at
    }

    pub fn register_catch(&mut self, total_size_ratio: f32, catch_time: DateTime<Utc>) {
        if total_size_ratio < self.smallest_catch_size_ratio {
            self.smallest_catch_size_ratio = total_size_ratio;
        } else if total_size_ratio > self.largest_catch_size_ratio {
            self.largest_catch_size_ratio = total_size_ratio;
        }
        self.last_catch = catch_time;
        self.caught_count = self.caught_count.saturating_add(1);
    }

    pub fn register_sell(&mut self, sell_time: DateTime<Utc>) {
        if self.sold_count == 0 {
            self.first_sell = Some(sell_time);
        }
        self.last_sell = Some(sell_time);
        self.sold_count = self.sold_count.saturating_add(1);
    }

    pub fn get_smallest_size_mm(&self) -> f32 {
        let data = get_config()
            .get_species_data(self.species_id)
            .unwrap_or_else(|| panic!("Missing fish data for species id '{}'", self.species_id));
        float_interpolate(
            data.min_size_baby_mm as f32,
            data.max_size_adult_mm as f32,
            self.smallest_catch_size_ratio,
        )
    }

    pub fn get_largest_size_mm(&self) -> f32 {
        let data = get_config()
            .get_species_data(self.species_id)
            .unwrap_or_else(|| panic!("Missing fish data for species id '{}'", self.species_id));
        float_interpolate(
            data.min_size_baby_mm as f32,
            data.max_size_adult_mm as f32,
            self.largest_catch_size_ratio,
        )
    }

    pub fn get_smallest_weight_g(&self) -> f32 {
        let data = get_config()
            .get_species_data(self.species_id)
            .unwrap_or_else(|| panic!("Missing fish data for species id '{}'", self.species_id));
        float_interpolate(
            data.min_weight_baby_g as f32,
            data.max_weight_adult_g as f32,
            self.smallest_catch_size_ratio,
        )
    }

    pub fn get_largest_weight_g(&self) -> f32 {
        let data = get_config()
            .get_species_data(self.species_id)
            .unwrap_or_else(|| panic!("Missing fish data for species id '{}'", self.species_id));
        float_interpolate(
            data.min_weight_baby_g as f32,
            data.max_weight_adult_g as f32,
            self.largest_catch_size_ratio,
        )
    }
}

impl Model for FishingHistoryEntry {
    type Table = crate::schema::fish_fishing_history_entries::table;
    type PrimaryKeyType = i64;
    type InsertType = NewFishingHistoryEntry;

    fn table() -> Self::Table {
        crate::schema::fish_fishing_history_entries::table
    }

    fn id(&self) -> Self::PrimaryKeyType {
        self.id
    }
}

#[derive(Insertable)]
#[diesel(table_name = crate::schema::fish_fishing_history_entries)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct NewFishingHistoryEntry {
    pub user_id: i64,
    pub species_id: i32,
    pub caught_count: i32,
    pub sold_count: i32,
    pub smallest_catch_size_ratio: f32,
    pub largest_catch_size_ratio: f32,
}