Skip to main content

fishpi_sdk/client/
finger.rs

1use crate::client::core::FishPi;
2use crate::domain::finger::{BagItem, MetalGift, UserBag, UserIp};
3use crate::utils::error::Error;
4use crate::wire::finger::{WireUserBag, WireUserIP};
5use serde_json::{Value, json};
6
7/// 摸金手指域访问器,持有金手指密钥。
8pub struct FingerApi {
9    client: FishPi,
10    gold_key: String,
11}
12
13impl FishPi {
14    /// 摸金手指域(管理员),`gold_key` 为金手指密钥。
15    pub fn finger(&self, gold_key: impl Into<String>) -> FingerApi {
16        FingerApi {
17            client: self.clone(),
18            gold_key: gold_key.into(),
19        }
20    }
21}
22
23impl FingerApi {
24    /// 上传摸鱼大闯关关卡数据。`time` 为 `None` 时用当前毫秒时间戳。
25    pub async fn add_mofish_score(
26        &self,
27        user_name: impl Into<String>,
28        stage: impl Into<String>,
29        time: Option<u64>,
30    ) -> Result<(), Error> {
31        let time = time.unwrap_or_else(|| {
32            std::time::SystemTime::now()
33                .duration_since(std::time::UNIX_EPOCH)
34                .map(|d| d.as_millis() as u64)
35                .unwrap_or(0)
36        });
37        self.client
38            .post_raw(
39                "api/games/mofish/score",
40                &json!({
41                    "goldFingerKey": self.gold_key,
42                    "userName": user_name.into(),
43                    "stage": stage.into(),
44                    "time": time,
45                }),
46            )
47            .await?;
48        Ok(())
49    }
50
51    /// 查询用户最近登录 IP。
52    pub async fn latest_login_ip(&self, user_name: impl Into<String>) -> Result<UserIp, Error> {
53        let wire: WireUserIP = self
54            .client
55            .post_data(
56                "user/query/latest-login-iP",
57                &json!({ "goldFingerKey": self.gold_key, "userName": user_name.into() }),
58            )
59            .await?;
60        Ok(wire.into())
61    }
62
63    /// 发放勋章。
64    pub async fn add_metal(
65        &self,
66        user_name: impl Into<String>,
67        metal: &MetalGift,
68    ) -> Result<(), Error> {
69        self.client
70            .post_raw(
71                "user/edit/give-metal",
72                &json!({
73                    "goldFingerKey": self.gold_key,
74                    "userName": user_name.into(),
75                    "attr": metal.attr,
76                    "name": metal.name,
77                    "description": metal.description,
78                    "data": metal.data,
79                }),
80            )
81            .await?;
82        Ok(())
83    }
84
85    /// 删除勋章。
86    pub async fn delete_metal(
87        &self,
88        user_name: impl Into<String>,
89        name: impl Into<String>,
90    ) -> Result<(), Error> {
91        self.client
92            .post_raw(
93                "user/edit/remove-metal",
94                &json!({
95                    "goldFingerKey": self.gold_key,
96                    "userName": user_name.into(),
97                    "name": name.into(),
98                }),
99            )
100            .await?;
101        Ok(())
102    }
103
104    /// 通过用户 ID 删除勋章。
105    pub async fn delete_metal_by_user_id(
106        &self,
107        user_id: impl Into<String>,
108        name: impl Into<String>,
109    ) -> Result<(), Error> {
110        self.client
111            .post_raw(
112                "user/edit/remove-metal-by-user-id",
113                &json!({
114                    "goldFingerKey": self.gold_key,
115                    "userId": user_id.into(),
116                    "name": name.into(),
117                }),
118            )
119            .await?;
120        Ok(())
121    }
122
123    /// 查询用户背包。
124    pub async fn user_bag(&self, user_name: impl Into<String>) -> Result<UserBag, Error> {
125        let wire: WireUserBag = self
126            .client
127            .post_data(
128                "user/query/items",
129                &json!({ "goldFingerKey": self.gold_key, "userName": user_name.into() }),
130            )
131            .await?;
132        Ok(wire.into())
133    }
134
135    /// 调整用户背包。
136    pub async fn edit_user_bag(
137        &self,
138        user_name: impl Into<String>,
139        item: BagItem,
140        sum: i32,
141    ) -> Result<(), Error> {
142        self.client
143            .post_raw(
144                "user/edit/items",
145                &json!({
146                    "goldFingerKey": self.gold_key,
147                    "userName": user_name.into(),
148                    "item": item.as_str(),
149                    "sum": sum,
150                }),
151            )
152            .await?;
153        Ok(())
154    }
155
156    /// 调整用户积分。
157    pub async fn edit_user_points(
158        &self,
159        user_name: impl Into<String>,
160        point: i32,
161        memo: impl Into<String>,
162    ) -> Result<(), Error> {
163        self.client
164            .post_raw(
165                "user/edit/points",
166                &json!({
167                    "goldFingerKey": self.gold_key,
168                    "userName": user_name.into(),
169                    "point": point,
170                    "memo": memo.into(),
171                }),
172            )
173            .await?;
174        Ok(())
175    }
176
177    /// 查询用户当前活跃度。
178    pub async fn liveness(&self, user_name: impl Into<String>) -> Result<f64, Error> {
179        let rsp: Value = self
180            .client
181            .post_raw(
182                "user/liveness",
183                &json!({ "goldFingerKey": self.gold_key, "userName": user_name.into() }),
184            )
185            .await?;
186        Ok(rsp["liveness"].as_f64().unwrap_or(0.0))
187    }
188
189    /// 查询用户昨日活跃度奖励。
190    pub async fn yesterday_liveness_reward(
191        &self,
192        user_name: impl Into<String>,
193    ) -> Result<f64, Error> {
194        let rsp: Value = self
195            .client
196            .post_raw(
197                "activity/yesterday-liveness-reward-api",
198                &json!({ "goldFingerKey": self.gold_key, "userName": user_name.into() }),
199            )
200            .await?;
201        Ok(rsp["sum"].as_f64().unwrap_or(0.0))
202    }
203}