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
use crate::inventory::{
    BarterItem, InventoryUpdate, Item, MoveItemRequest, RagfairResponseData, Upd,
};
use crate::{
    handle_error, handle_error2, Error, ErrorResponse, Result, Tarkov, PROD_ENDPOINT,
    TRADING_ENDPOINT,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Trading error
#[derive(Debug, err_derive::Error)]
pub enum TradingError {
    /// Transaction error
    #[error(display = "transaction error")]
    TransactionError,
    /// Loyalty level is not high enough to purchase this item.
    #[error(display = "bad loyalty level")]
    BadLoyaltyLevel,
}

/// Trader info
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct Trader {
    /// Trader ID
    #[serde(rename = "_id")]
    pub id: String,
    /// Trader is working
    pub working: bool,
    /// ?
    pub customization_seller: bool,
    /// Trader name
    pub name: String,
    /// Trader surname
    pub surname: String,
    /// Trader nickname
    pub nickname: String,
    /// Trader location
    pub location: String,
    /// Trader avatar
    pub avatar: String,
    /// Trader rouble balance
    pub balance_rub: u64,
    /// Trader dollar balance
    pub balance_dol: u64,
    /// Trader euro balance
    pub balance_eur: u64,
    /// ?
    pub display: bool,
    /// Trader discount
    pub discount: i64,
    /// Trader discount expiry
    pub discount_end: i64,
    /// ?
    pub buyer_up: bool,
    /// Trader currency
    pub currency: Currency,
    /// Resupply time
    pub supply_next_time: u64,
    /// Trader repair offer
    pub repair: Repair,
    /// Trader insurance offer
    pub insurance: Insurance,
    /// Trader grid height
    #[serde(rename = "gridHeight")]
    pub grid_height: u64,
    /// Trader loyalty
    pub loyalty: Loyalty,
    /// Unknown type
    pub sell_category: Vec<serde_json::Value>,
}

/// Trader's repair stats
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct Repair {
    /// Repair is available
    pub availability: bool,
    /// Repair quality
    pub quality: String,
    /// Item IDs excluded from repair.
    pub excluded_id_list: Vec<String>,
    /// Category IDs excluded from repair.
    pub excluded_category: Vec<String>,
    /// Currency
    pub currency: Option<String>,
    /// ?
    pub currency_coefficient: Option<u64>,
    /// Repair price rate
    pub price_rate: u64,
}

/// Trader currency
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub enum Currency {
    /// Rouble
    #[serde(rename = "RUB")]
    Rouble,
    /// US Dollar
    #[serde(rename = "USD")]
    Dollar,
    /// Euro
    #[serde(rename = "EUR")]
    Euro,
}

/// Trader's insurance offer
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct Insurance {
    /// Insurance is available
    pub availability: bool,
    /// Minimum cost to insure
    pub min_payment: u64,
    /// Minimum return time in hours.
    pub min_return_hour: u64,
    /// Maximum return time in hours.
    pub max_return_hour: u64,
    /// Maximum storage time in hours.
    pub max_storage_time: u64,
    /// Categories IDs excluded from insurance.
    pub excluded_category: Vec<String>,
}

/// Trader loyalty
#[derive(Debug, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Loyalty {
    /// Current loyalty level
    pub current_level: u64,
    /// Current loyalty standing
    pub current_standing: f64,
    /// Amount spent on trader
    pub current_sales_sum: u64,
    /// All loyalty levels
    pub loyalty_levels: HashMap<String, LoyaltyLevel>,
}

/// Trader loyalty level
#[derive(Debug, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct LoyaltyLevel {
    /// Minimum level
    pub min_level: u64,
    /// Minimum sales amount
    pub min_sales_sum: u64,
    /// Minimum standing
    pub min_standing: f64,
}

#[derive(Debug, Deserialize)]
struct TradersResponse {
    #[serde(flatten)]
    error: ErrorResponse,
    data: Option<Vec<Trader>>,
}

#[derive(Debug, Deserialize)]
struct TraderResponse {
    #[serde(flatten)]
    error: ErrorResponse,
    data: Option<Trader>,
}

#[derive(Debug, Deserialize)]
struct TraderItemsResponse {
    #[serde(flatten)]
    error: ErrorResponse,
    data: Option<TraderItems>,
}

#[derive(Debug, Deserialize)]
struct TraderItems {
    items: Vec<Item>,
    barter_scheme: HashMap<String, Vec<Vec<Price>>>,
    loyal_level_items: HashMap<String, u8>,
}

#[derive(Debug, Deserialize)]
struct TraderPricesResponse {
    #[serde(flatten)]
    error: ErrorResponse,
    data: Option<HashMap<String, Vec<Vec<Price>>>>,
}

/// Trader item price
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct Price {
    /// Item localization schema ID
    #[serde(rename = "_tpl")]
    pub schema_id: String,
    /// Item count
    pub count: f64,
}

/// Item for trade
#[derive(Debug, Clone, PartialEq)]
pub struct TraderItem {
    /// Item ID
    pub id: String,
    /// Item localization schema ID
    pub schema_id: String,
    /// Item attachments/options
    pub upd: Option<Upd>,
    /// Item price
    pub price: Vec<Price>,
    /// Loyalty level
    pub loyalty_level: u8,
}

#[derive(Debug, Serialize)]
struct TradeItemRequest<'a> {
    #[serde(rename = "Action")]
    action: &'a str,
    #[serde(rename = "type")]
    trade_type: &'a str,
    #[serde(rename = "tid")]
    trader_id: &'a str,
    item_id: &'a str,
    count: u64,
    scheme_id: u64,
    scheme_items: &'a [BarterItem],
}

#[derive(Debug, Deserialize)]
struct TradeResponse {
    #[serde(flatten)]
    error: ErrorResponse,
    data: serde_json::Value,
}

#[derive(Debug, Serialize)]
struct SellItemRequest<'a> {
    #[serde(rename = "Action")]
    action: &'a str,
    #[serde(rename = "type")]
    trade_type: &'a str,
    #[serde(rename = "tid")]
    trader_id: &'a str,
    items: &'a [SellItem],
}

#[derive(Debug, Serialize)]
struct SellItem {
    id: String,
    count: u64,
    scheme_id: u64,
}

#[derive(Debug, Deserialize)]
struct SellResponse {
    #[serde(flatten)]
    error: ErrorResponse,
}

impl Tarkov {
    /// Get a list of all traders.
    pub async fn get_traders(&self) -> Result<Vec<Trader>> {
        let url = format!("{}/client/trading/api/getTradersList", TRADING_ENDPOINT);
        let res: TradersResponse = self.post_json(&url, &{}).await?;

        handle_error(res.error, res.data)
    }

    /// Get a trader by ID.
    pub async fn get_trader(&self, trader_id: &str) -> Result<Trader> {
        if trader_id.is_empty() {
            return Err(Error::InvalidParameters);
        }

        let url = format!(
            "{}/client/trading/api/getTrader/{}",
            TRADING_ENDPOINT, trader_id
        );
        let res: TraderResponse = self.post_json(&url, &{}).await?;

        handle_error(res.error, res.data)
    }

    async fn get_trader_items_raw(&self, trader_id: &str) -> Result<TraderItems> {
        let url = format!(
            "{}/client/trading/api/getTraderAssort/{}",
            TRADING_ENDPOINT, trader_id
        );
        let res: TraderItemsResponse = self.post_json(&url, &{}).await?;

        handle_error(res.error, res.data)
    }

    async fn get_trader_prices_raw(
        &self,
        trader_id: &str,
    ) -> Result<HashMap<String, Vec<Vec<Price>>>> {
        let url = format!(
            "{}/client/trading/api/getUserAssortPrice/trader/{}",
            TRADING_ENDPOINT, trader_id
        );
        let res: TraderPricesResponse = self.post_json(&url, &{}).await?;

        handle_error(res.error, res.data)
    }

    /// Get a list of items for sale by trader ID.
    pub async fn get_trader_items(&self, trader_id: &str) -> Result<Vec<TraderItem>> {
        if trader_id.is_empty() {
            return Err(Error::InvalidParameters);
        }

        let mut result: Vec<TraderItem> = Vec::new();

        let items = self.get_trader_items_raw(trader_id).await?;
        let prices = self.get_trader_prices_raw(trader_id).await?;

        for item in items.items {
            // TODO: Properly deal with parent/children items
            if item.parent_id != Some("hideout".to_string()) {
                continue;
            }

            let loyalty_level = items
                .loyal_level_items
                .get(&item.id)
                .expect("Loyalty level could not be mapped.");
            let price = {
                let barter_or_price = match items.barter_scheme.get(&item.id) {
                    None => prices
                        .get(&item.id)
                        .expect("Item price could not be mapped."),
                    Some(barter) => barter,
                };

                barter_or_price.get(0)
            };

            let trader_item = TraderItem {
                id: item.id,
                schema_id: item.schema_id,
                upd: item.upd,
                price: price.expect("Item price could not be mapped.").clone(),
                loyalty_level: *loyalty_level,
            };

            result.push(trader_item);
        }

        Ok(result)
    }

    /// Trade items with traders.
    ///
    /// All trades, including cash trades, are considered bartering. `barter_items` expects a
    /// list of items from your inventory that matches the item price.
    pub async fn trade_item(
        &self,
        trader_id: &str,
        item_id: &str,
        quantity: u64,
        barter_items: &[BarterItem],
    ) -> Result<InventoryUpdate> {
        if trader_id.is_empty() || item_id.is_empty() || quantity == 0 || barter_items.is_empty() {
            return Err(Error::InvalidParameters);
        }

        let url = format!("{}/client/game/profile/items/moving", PROD_ENDPOINT);
        let body = MoveItemRequest {
            data: &[TradeItemRequest {
                action: "TradingConfirm",
                trade_type: "buy_from_trader",
                trader_id,
                item_id,
                count: quantity,
                scheme_id: 0,
                scheme_items: barter_items,
            }],
            tm: 0,
        };

        let res: TradeResponse = self.post_json(&url, &body).await?;
        handle_error2(res.error)?;

        let res: RagfairResponseData = Deserialize::deserialize(res.data)?;
        if !res.errors.is_empty() {
            let error = &res.errors[0];
            return Err(Error::UnknownAPIError(error.code));
        }

        let items: InventoryUpdate = Deserialize::deserialize(res.items)?;
        Ok(items)
    }

    /// Sell items to trader.
    pub async fn sell_item(
        &self,
        trader_id: &str,
        item_id: &str,
        quantity: u64,
    ) -> Result<InventoryUpdate> {
        if trader_id.is_empty() || item_id.is_empty() || quantity == 0 {
            return Err(Error::InvalidParameters);
        }

        let url = format!("{}/client/game/profile/items/moving", PROD_ENDPOINT);
        let body = MoveItemRequest {
            data: &[SellItemRequest {
                action: "TradingConfirm",
                trade_type: "sell_to_trader",
                trader_id,
                items: &[SellItem {
                    id: item_id.to_string(),
                    count: quantity,
                    scheme_id: 0,
                }],
            }],
            tm: 0,
        };

        let res: TradeResponse = self.post_json(&url, &body).await?;
        handle_error2(res.error)?;

        let res: RagfairResponseData = Deserialize::deserialize(res.data)?;
        if !res.errors.is_empty() {
            let error = &res.errors[0];
            return Err(Error::UnknownAPIError(error.code));
        }

        let items: InventoryUpdate = Deserialize::deserialize(res.items)?;
        Ok(items)
    }
}