Skip to content

Commit ee38e62

Browse files
committed
Some(release).unwrap()
1 parent a15b49b commit ee38e62

File tree

6 files changed

+88
-19
lines changed

6 files changed

+88
-19
lines changed

config.json

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
{
2-
"strategies": [
2+
"message": [
33
{
44
"strategy_text": {
5-
"key_word": ["привет", "здравствуй"],
5+
"key_word": [
6+
"привет",
7+
"здравствуйте"
8+
],
69
"equals": null
710
},
8-
"answer": "Привет! Как дела?"
11+
"answer": "Здравствуйте! Чем могу помочь?"
912
},
1013
{
1114
"strategy_text": {
12-
"key_word": null,
13-
"equals": "Как тебя зовут?"
15+
"key_word": [
16+
"пока",
17+
"до свидания"
18+
],
19+
"equals": null
1420
},
15-
"answer": "Меня зовут Бот."
21+
"answer": "До свидания! Хорошего дня!"
22+
}
23+
],
24+
"order": [
25+
{
26+
"unique_prefix": "Пустышка",
27+
"static_data": "Спасибо за покупку",
28+
"availability_data": [
29+
"dsa-11",
30+
"11-das"
31+
]
1632
}
1733
]
1834
}

src/args.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::process::exit;
2-
use crate::models::strategy::Strategy;
32

43
pub struct ArgsOption{
54
pub golden_key: Option<String>,

src/message.rs renamed to src/handlers/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use crate::models::{
99
pub async fn message_handler(message: Message, sender: &FunPaySender, me: &FPMe, strategies: &Strategies) {
1010
if me.id == message.author_id{return;}
1111
let text = match &message.text {
12-
Some(t) => t,
12+
Some(text) => text,
1313
None => return,
1414
};
15-
for i in strategies.strategies.iter() {
15+
for i in strategies.message.iter() {
1616
if i.strategy_text.check(text){sender.send_chat_message(&message.chat_id, &i.answer).await.unwrap();}
1717
}
1818

src/handlers/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod message;
2+
mod order;
3+
4+
pub use message::*;
5+
pub use order::*;

src/handlers/order.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use funpay_client::FunPaySender;
2+
use funpay_client::models::{OrderShortcut};
3+
use crate::models::FPMe;
4+
use crate::models::strategy::Strategies;
5+
6+
pub async fn order_handler(order: OrderShortcut, sender: &FunPaySender, me: &FPMe, strategies: &Strategies) {
7+
for i in &strategies.order{
8+
if order.description.starts_with(i.unique_prefix.as_str()){
9+
if let Some(static_data) = &i.static_data {
10+
sender.send_chat_message(&order.chat_id, static_data.as_str()).await.unwrap();
11+
}
12+
if let Some(availability) = &i.get_availability() {
13+
sender.send_chat_message(&order.chat_id, availability.as_str()).await.unwrap();
14+
}
15+
break
16+
}
17+
}
18+
strategies.save();
19+
}

src/models/strategy.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use serde::{Serialize, Deserialize};
1+
use serde::{Serialize, Deserialize, Serializer, Deserializer};
22
use std::path::{Path, PathBuf};
33
use std::fs;
4-
4+
use std::sync::Mutex;
5+
use parking_lot::RwLock;
56

67

78
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -33,17 +34,43 @@ impl StrategyText{
3334
}
3435

3536
#[derive(Serialize, Deserialize, Debug, Clone)]
36-
pub struct Strategy{
37+
pub struct StrategyMessage{
3738
pub strategy_text: StrategyText,
3839
pub answer: String,
39-
// event: Event,
4040

4141
}
4242

4343
#[derive(Serialize, Deserialize, Debug)]
44+
pub struct StrategyOrder{
45+
pub unique_prefix: String,
46+
pub static_data: Option<String>,
47+
pub availability_data: RwLock<Option<Vec<String>>>,
48+
}
49+
impl StrategyOrder {
50+
pub fn get_availability(&self) -> Option<String> {
51+
let mut data_guard = self.availability_data.write();
52+
if let Some(data) = data_guard.as_mut() {
53+
if !data.is_empty() {
54+
let response = data.remove(0);
55+
if data.is_empty() {
56+
*data_guard = None;
57+
}
58+
return Some(response);
59+
}
60+
}
61+
None
62+
}
63+
}
64+
#[derive(Serialize, Deserialize)]
4465
pub struct Strategies{
45-
pub strategies: Vec<Strategy>,
66+
pub message: Vec<StrategyMessage>,
67+
pub order: Vec<StrategyOrder>,
68+
#[serde(skip)]
69+
path_config: PathBuf,
4670
}
71+
72+
73+
4774
impl Strategies {
4875
pub fn new(path_config: Option<String>) -> Result<Self, Box<dyn std::error::Error>> {
4976
let path = match path_config {
@@ -54,13 +81,16 @@ impl Strategies {
5481
return Err(format!("File config no find {}", path.display()).into());
5582
}
5683
let text = fs::read_to_string(&path)?;
57-
58-
let config: Strategies = serde_json::from_str(&text)?;
59-
84+
let mut config: Strategies = serde_json::from_str(&text)?;
85+
config.path_config=path;
6086
Ok(config)
6187
}
62-
fn reload(){
63-
88+
fn reload(&self) {
89+
}
90+
91+
pub fn save(&self) {
92+
let config_json = serde_json::to_string_pretty(self).unwrap();
93+
fs::write(&self.path_config, config_json).unwrap();
6494
}
6595

6696
}

0 commit comments

Comments
 (0)