Skip to content

Commit a3064e4

Browse files
committed
Finish refactoring fishing market
1 parent 1af937e commit a3064e4

File tree

4 files changed

+129
-51
lines changed

4 files changed

+129
-51
lines changed

karen/data/fishing.json

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,86 @@
33
"fish": {
44
"cod": {
55
"name": "Cod",
6-
"sell_price_range": [
7-
2,
8-
15
9-
],
10-
"rarity": 0
6+
"rarity": 0,
7+
"pricing": {
8+
"base": 2,
9+
"time_offset": 0,
10+
"periods": [
11+
{"unit": "day", "period": 4, "max": 15}
12+
]
13+
}
1114
},
1215
"salmon": {
1316
"name": "Salmon",
14-
"sell_price_range": [
15-
10,
16-
40
17-
],
18-
"rarity": 0
17+
"rarity": 0,
18+
"pricing": {
19+
"base": 10,
20+
"time_offset": 1,
21+
"periods": [
22+
{"unit": "day", "period": 4, "max": 35},
23+
{"unit": "hour", "period": 10, "max": 10}
24+
]
25+
}
1926
},
2027
"tropical fish": {
2128
"name": "Tropical Fish",
22-
"sell_price_range": [
23-
30,
24-
120
25-
],
26-
"rarity": 1
29+
"rarity": 1,
30+
"pricing": {
31+
"base": 30,
32+
"time_offset": 4,
33+
"periods": [
34+
{"unit": "day", "period": 20, "max": 115},
35+
{"unit": "hour", "period": 14, "max": 20}
36+
]
37+
}
2738
},
2839
"pufferfish": {
2940
"name": "Pufferfish",
30-
"sell_price_range": [
31-
60,
32-
200
33-
],
34-
"rarity": 3
41+
"rarity": 3,
42+
"pricing": {
43+
"base": 60,
44+
"time_offset": 1,
45+
"periods": [
46+
{"unit": "day", "period": 20, "max": 200},
47+
{"unit": "hour", "period": 3, "max": 10}
48+
]
49+
}
3550
},
3651
"rainbow trout": {
3752
"name": "Rainbow Trout",
38-
"sell_price_range": [
39-
180,
40-
290
41-
],
42-
"rarity": 4
53+
"rarity": 4,
54+
"pricing": {
55+
"base": 100,
56+
"time_offset": 3,
57+
"periods": [
58+
{"unit": "day", "period": 30, "max": 275},
59+
{"unit": "day", "period": 5, "max": 25}
60+
]
61+
}
4362
},
4463
"gold fish": {
4564
"name": "Gold Fish",
46-
"sell_price_range": [
47-
200,
48-
300
49-
],
50-
"rarity": 4
65+
"rarity": 4,
66+
"pricing": {
67+
"base": 280,
68+
"time_offset": 0,
69+
"periods": [
70+
{"unit": "day", "period": 3, "max": 20},
71+
{"unit": "hour", "period": 3, "max": 20}
72+
]
73+
}
5174
},
5275
"emerald fish": {
5376
"name": "Emerald Fish",
54-
"sell_price_range": [
55-
600,
56-
800
57-
],
58-
"rarity": 5
77+
"rarity": 5,
78+
"pricing": {
79+
"base": 700,
80+
"time_offset": 0,
81+
"periods": [
82+
{"unit": "day", "period": 2, "max": 100},
83+
{"unit": "hour", "period": 3, "max": 24}
84+
]
85+
}
5986
}
6087
}
6188
}

karen/src/logic/fishing.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,57 @@ use std::collections::HashMap;
33
use chrono::{Datelike, Timelike, Utc};
44
use sqlx::PgConnection;
55

6-
use crate::common::data::FISHING_DATA;
6+
use crate::{common::data::FISHING_DATA, models::data::fishing::FishPeriodUnit};
77

8-
pub fn get_current_fish_prices() -> HashMap<String, i64> {
9-
let duration_since_start_of_year = Utc::now() - Utc::now().with_month(1).unwrap().with_day(1).unwrap().with_minute(0).unwrap();
8+
pub fn get_current_fish_prices() -> HashMap<String, i32> {
9+
let mut fish_pricing = HashMap::with_capacity(7);
1010

11-
let elapsed_weeks = duration_since_start_of_year.num_weeks();
12-
let elapsed_days = duration_since_start_of_year.num_days();
13-
let elapsed_hours = duration_since_start_of_year.num_hours();
11+
let duration_since_start_of_year = Utc::now()
12+
- Utc::now()
13+
.with_month(1)
14+
.unwrap()
15+
.with_day(1)
16+
.unwrap()
17+
.with_minute(0)
18+
.unwrap();
1419

15-
20+
let days = duration_since_start_of_year.num_days();
21+
let hours = duration_since_start_of_year.num_hours();
22+
23+
for fish in FISHING_DATA.fish.values() {
24+
let mut fish_price = fish.pricing.base;
25+
26+
for period in fish.pricing.periods.iter() {
27+
let period_value = match period.unit {
28+
FishPeriodUnit::Day => days,
29+
FishPeriodUnit::Hour => hours,
30+
} as i32;
31+
let period_value = ((period_value + fish.pricing.time_offset) % period.period) + 1;
32+
33+
let rate = period_value as f32 / period.period as f32;
34+
let period_price = (rate * period.max as f32) as i32;
35+
36+
fish_price += period_price;
37+
}
38+
39+
fish_pricing.insert(fish.name.clone(), fish_price);
40+
}
41+
42+
fish_pricing
1643
}
1744

18-
pub async fn randomize_fish_prices(db: &mut PgConnection) -> Result<(), sqlx::Error> {
45+
pub async fn update_fishing_prices(db: &mut PgConnection) -> Result<(), sqlx::Error> {
46+
let fish_pricing = get_current_fish_prices();
47+
1948
for fish in FISHING_DATA.fish.values() {
2049
sqlx::query!(
2150
"UPDATE items SET sell_price = $2 WHERE name = $1",
2251
fish.name,
23-
fastrand::choice(fish.sell_price_range.0..fish.sell_price_range.1).unwrap()
52+
fish_pricing[&fish.name],
2453
)
2554
.execute(&mut *db)
2655
.await?;
2756
}
2857

2958
Ok(())
30-
}
59+
}

karen/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use poem::{
88
};
99
use thiserror::Error;
1010
use tokio_schedule::Job;
11-
use tracing::{event, span, Level};
11+
use tracing::{Level, event};
1212

1313
mod common;
1414
mod config;
@@ -53,12 +53,12 @@ async fn setup_recurring_tasks(db_pool: sqlx::Pool<sqlx::Postgres>) {
5353
.perform(move || {
5454
let db_pool = db_pool_c2.clone();
5555
async move {
56-
use logic::fishing::randomize_fish_prices;
57-
58-
event!(Level::INFO, "Randomizing fish market prices...");
56+
use logic::fishing::update_fishing_prices;
57+
58+
event!(Level::INFO, "Updating fish market prices...");
5959

6060
let mut db = db_pool.acquire().await.unwrap();
61-
randomize_fish_prices(&mut db).await.unwrap();
61+
update_fishing_prices(&mut db).await.unwrap();
6262
}
6363
}),
6464
);

karen/src/models/data/fishing.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@ use std::collections::HashMap;
22

33
use serde::Deserialize;
44

5+
#[derive(Debug, Deserialize)]
6+
pub enum FishPeriodUnit {
7+
#[serde(rename = "day")]
8+
Day,
9+
#[serde(rename = "hour")]
10+
Hour,
11+
}
12+
13+
#[derive(Debug, Deserialize)]
14+
pub struct FishPricingPeriod {
15+
pub unit: FishPeriodUnit,
16+
pub period: i32,
17+
pub max: i32,
18+
}
19+
20+
#[derive(Debug, Deserialize)]
21+
pub struct FishPricing {
22+
pub base: i32,
23+
pub time_offset: i32,
24+
pub periods: Vec<FishPricingPeriod>,
25+
}
26+
527
#[derive(Debug, Deserialize)]
628
pub struct FishEntry {
729
pub name: String,
8-
pub sell_price_range: (i32, i32),
930
pub rarity: f32,
31+
pub pricing: FishPricing,
1032
}
1133

1234
#[derive(Debug, Deserialize)]

0 commit comments

Comments
 (0)