betfair_stream_api/cache/primitives/
orderbook_runner_cache.rs1use std::collections::HashMap;
4
5use betfair_adapter::betfair_types::customer_strategy_ref::CustomerStrategyRef;
6use betfair_adapter::betfair_types::numeric::F64Ord;
7use betfair_adapter::betfair_types::types::sports_aping::{BetId, MarketId, SelectionId};
8use betfair_stream_types::response::UpdateSet2;
9use betfair_stream_types::response::order_change_message::{Order, StrategyMatchChange};
10use serde::{Deserialize, Serialize};
11
12use super::available_cache::Available;
13
14#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
16pub struct OrderBookRunner {
17 pub market_id: MarketId,
18 pub selection_id: SelectionId,
19 pub matched_lays: Available<UpdateSet2>,
20 pub matched_backs: Available<UpdateSet2>,
21 pub unmatched_orders: HashMap<BetId, Order>,
22 pub handicap: Option<F64Ord>,
23 pub strategy_matches: HashMap<CustomerStrategyRef, StrategyMatch>,
24}
25
26#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
27pub struct StrategyMatch {
28 pub matched_lays: Available<UpdateSet2>,
29 pub matched_backs: Available<UpdateSet2>,
30}
31
32impl OrderBookRunner {
33 pub(crate) fn new(market_id: MarketId, selection_id: SelectionId) -> Self {
34 Self {
35 market_id,
36 selection_id,
37 matched_lays: Available::new(&[]),
38 matched_backs: Available::new(&[]),
39 unmatched_orders: HashMap::new(),
40 handicap: None,
41 strategy_matches: HashMap::new(),
42 }
43 }
44
45 pub(crate) fn update_unmatched<'o>(
46 &mut self,
47 unmatched_orders: impl IntoIterator<Item = &'o Order>,
48 ) {
49 for order in unmatched_orders {
50 self.unmatched_orders
51 .insert(order.id.clone(), order.clone());
52 }
53 }
54
55 pub(crate) fn update_matched_lays(&mut self, ml: &Vec<UpdateSet2>) {
56 self.matched_lays.update(ml);
57 }
58
59 pub(crate) fn update_matched_backs(&mut self, mb: &Vec<UpdateSet2>) {
60 self.matched_backs.update(mb);
61 }
62
63 pub(crate) fn update_strategy_matches(
64 &mut self,
65 sm: &HashMap<CustomerStrategyRef, StrategyMatchChange>,
66 ) {
67 for (key, value) in sm {
68 let entry = self
69 .strategy_matches
70 .entry(key.clone())
71 .or_insert_with(|| StrategyMatch {
72 matched_lays: Available::new(&[]),
73 matched_backs: Available::new(&[]),
74 });
75
76 if let Some(ref ml) = value.ml {
77 entry.matched_lays.update(ml);
78 }
79
80 if let Some(ref mb) = value.mb {
81 entry.matched_backs.update(mb);
82 }
83 }
84 }
85}