You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

564 lines
20 KiB

1 year ago
2 years ago
7 months ago
1 year ago
1 year ago
  1. use futures::future::BoxFuture;
  2. use crate::ClientItemId;
  3. use entity::item::Meseta;
  4. use maps::area::MapArea;
  5. use entity::character::{CharacterEntity, CharacterEntityId};
  6. use entity::gateway::{EntityGateway, EntityGatewayTransaction};
  7. use entity::item::ItemModifier;
  8. use entity::room::RoomEntityId;
  9. use crate::state::{ItemState, ItemStateProxy, IndividualItemDetail};
  10. use crate::itemstateaction::{ItemStateAction, ItemAction};
  11. use crate::inventory::InventoryItem;
  12. use crate::floor::FloorItem;
  13. use shops::ShopItem;
  14. use crate::trade::TradeItem;
  15. use location::AreaClient;
  16. use drops::ItemDrop;
  17. use maps::monster::MonsterType;
  18. use crate::actions;
  19. pub fn pick_up_item<'a, EG>(
  20. item_state: &'a mut ItemState,
  21. entity_gateway: &'a mut EG,
  22. character: &'a CharacterEntity,
  23. item_id: &'a ClientItemId,
  24. ) -> BoxFuture<'a, Result<actions::TriggerCreateItem, anyhow::Error>>
  25. where
  26. EG: EntityGateway + 'static,
  27. EG::Transaction<'a>: Clone,
  28. {
  29. entity_gateway.with_transaction(move |transaction| async move {
  30. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  31. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  32. .act(actions::take_item_from_floor(character.id, *item_id))
  33. .act(actions::add_floor_item_to_inventory(character))
  34. .commit((item_state_proxy, transaction))
  35. .await?;
  36. item_state_proxy.commit().await;
  37. Ok((transaction, result))
  38. })
  39. }
  40. pub fn drop_item<'a, EG>(
  41. item_state: &'a mut ItemState,
  42. entity_gateway: &'a mut EG,
  43. character: &'a CharacterEntity,
  44. item_id: &'a ClientItemId,
  45. map_area: MapArea,
  46. drop_position: (f32, f32, f32),
  47. )-> BoxFuture<'a, Result<FloorItem, anyhow::Error>>
  48. where
  49. EG: EntityGateway + 'static,
  50. EG::Transaction<'a>: Clone,
  51. {
  52. entity_gateway.with_transaction(move |transaction| async move {
  53. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  54. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  55. .act(actions::take_item_from_inventory(character.id, *item_id, 0))
  56. .act(actions::add_inventory_item_to_shared_floor(character.id, map_area, drop_position))
  57. .commit((item_state_proxy, transaction))
  58. .await?;
  59. item_state_proxy.commit().await;
  60. Ok((transaction, result))
  61. })
  62. }
  63. pub fn drop_partial_item<'a, EG>(
  64. item_state: &'a mut ItemState,
  65. entity_gateway: &'a mut EG,
  66. character: &'a CharacterEntity,
  67. item_id: &'a ClientItemId,
  68. map_area: MapArea,
  69. drop_position: (f32, f32),
  70. amount: u32
  71. ) -> BoxFuture<'a, Result<FloorItem, anyhow::Error>>
  72. where
  73. EG: EntityGateway + 'static,
  74. {
  75. entity_gateway.with_transaction(move |transaction| async move {
  76. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  77. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  78. .act(actions::take_item_from_inventory(character.id, *item_id, amount))
  79. .act(actions::add_inventory_item_to_shared_floor(character.id, map_area, (drop_position.0, 0.0, drop_position.1)))
  80. .commit((item_state_proxy, transaction))
  81. .await?;
  82. item_state_proxy.commit().await;
  83. Ok((transaction, result))
  84. })
  85. }
  86. pub fn drop_meseta<'a, EG>(
  87. item_state: &'a mut ItemState,
  88. entity_gateway: &'a mut EG,
  89. character: &'a CharacterEntity,
  90. map_area: MapArea,
  91. drop_position: (f32, f32),
  92. amount: u32,
  93. ) -> BoxFuture<'a, Result<FloorItem, anyhow::Error>>
  94. where
  95. EG: EntityGateway + 'static,
  96. {
  97. entity_gateway.with_transaction(move |transaction| async move {
  98. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  99. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  100. .act(actions::take_meseta_from_inventory(character.id, amount))
  101. .act(actions::add_meseta_to_shared_floor(character.id, amount, map_area, drop_position))
  102. .commit((item_state_proxy, transaction))
  103. .await?;
  104. item_state_proxy.commit().await;
  105. Ok((transaction, result))
  106. })
  107. }
  108. pub fn withdraw_meseta<'a, EG>(
  109. item_state: &'a mut ItemState,
  110. entity_gateway: &'a mut EG,
  111. character: &'a CharacterEntity,
  112. amount: u32,
  113. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  114. where
  115. EG: EntityGateway + 'static,
  116. {
  117. entity_gateway.with_transaction(move |transaction| async move {
  118. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  119. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  120. .act(actions::take_meseta_from_bank(character.id, amount))
  121. .act(actions::add_meseta_from_bank_to_inventory(character.id, amount))
  122. .commit((item_state_proxy, transaction))
  123. .await?;
  124. item_state_proxy.commit().await;
  125. Ok((transaction, result))
  126. })
  127. }
  128. pub fn deposit_meseta<'a, EG>(
  129. item_state: &'a mut ItemState,
  130. entity_gateway: &'a mut EG,
  131. character: &'a CharacterEntity,
  132. amount: u32,
  133. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  134. where
  135. EG: EntityGateway + 'static,
  136. {
  137. entity_gateway.with_transaction(move |transaction| async move {
  138. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  139. let ((item_state_proxy, transaction), _) = ItemStateAction::default()
  140. .act(actions::take_meseta_from_inventory(character.id, amount))
  141. .act(actions::add_meseta_to_bank(character.id, amount))
  142. .commit((item_state_proxy, transaction))
  143. .await?;
  144. item_state_proxy.commit().await;
  145. Ok((transaction, ()))
  146. })
  147. }
  148. pub fn withdraw_item<'a, EG>(
  149. item_state: &'a mut ItemState,
  150. entity_gateway: &'a mut EG,
  151. character: &'a CharacterEntity,
  152. item_id: &'a ClientItemId,
  153. amount: u32,
  154. ) -> BoxFuture<'a, Result<InventoryItem, anyhow::Error>>
  155. where
  156. EG: EntityGateway + 'static,
  157. {
  158. entity_gateway.with_transaction(move |transaction| async move {
  159. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  160. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  161. .act(actions::take_item_from_bank(character.id, *item_id, amount))
  162. //.act(bank_item_to_inventory_item)
  163. //.act(add_item_to_inventory)
  164. .act(actions::add_bank_item_to_inventory(character))
  165. .commit((item_state_proxy, transaction))
  166. .await?;
  167. item_state_proxy.commit().await;
  168. Ok((transaction, result))
  169. })
  170. }
  171. pub fn deposit_item<'a, EG> (
  172. item_state: &'a mut ItemState,
  173. entity_gateway: &'a mut EG,
  174. character: &'a CharacterEntity,
  175. item_id: &'a ClientItemId,
  176. amount: u32,
  177. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  178. where
  179. EG: EntityGateway + 'static,
  180. {
  181. entity_gateway.with_transaction(move |transaction| async move {
  182. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  183. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  184. .act(actions::take_item_from_inventory(character.id, *item_id, amount))
  185. .act(actions::add_inventory_item_to_bank(character.id))
  186. .commit((item_state_proxy, transaction))
  187. .await?;
  188. item_state_proxy.commit().await;
  189. Ok((transaction, result))
  190. })
  191. }
  192. pub fn equip_item<'a, EG> (
  193. item_state: &'a mut ItemState,
  194. entity_gateway: &'a mut EG,
  195. character: &'a CharacterEntity,
  196. item_id: &'a ClientItemId,
  197. equip_slot: u8,
  198. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  199. where
  200. EG: EntityGateway + 'static,
  201. {
  202. entity_gateway.with_transaction(move |transaction| async move {
  203. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  204. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  205. .act(actions::equip_inventory_item(character.id, *item_id, equip_slot))
  206. .commit((item_state_proxy, transaction))
  207. .await?;
  208. item_state_proxy.commit().await;
  209. Ok((transaction, result))
  210. })
  211. }
  212. pub fn unequip_item<'a, EG> (
  213. item_state: &'a mut ItemState,
  214. entity_gateway: &'a mut EG,
  215. character: &'a CharacterEntity,
  216. item_id: &'a ClientItemId,
  217. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  218. where
  219. EG: EntityGateway + 'static,
  220. {
  221. entity_gateway.with_transaction(move |transaction| async move {
  222. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  223. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  224. .act(actions::unequip_inventory_item(character.id, *item_id))
  225. .commit((item_state_proxy, transaction))
  226. .await?;
  227. item_state_proxy.commit().await;
  228. Ok((transaction, result))
  229. })
  230. }
  231. pub fn sort_inventory<'a, EG> (
  232. item_state: &'a mut ItemState,
  233. entity_gateway: &'a mut EG,
  234. character: &'a CharacterEntity,
  235. item_ids: Vec<ClientItemId>,
  236. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  237. where
  238. EG: EntityGateway + 'static,
  239. {
  240. entity_gateway.with_transaction(move |transaction| async move {
  241. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  242. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  243. .act(actions::sort_inventory_items(character.id, item_ids))
  244. .commit((item_state_proxy, transaction))
  245. .await?;
  246. item_state_proxy.commit().await;
  247. Ok((transaction, result))
  248. })
  249. }
  250. pub fn use_item<'a, EG> (
  251. item_state: &'a mut ItemState,
  252. entity_gateway: &'a mut EG,
  253. character: &'a mut CharacterEntity,
  254. area_client: AreaClient,
  255. item_id: &'a ClientItemId,
  256. amount: u32,
  257. ) -> BoxFuture<'a, Result<Vec<actions::CreateItem>, anyhow::Error>>
  258. where
  259. EG: EntityGateway + 'static,
  260. {
  261. entity_gateway.with_transaction(move |transaction| async move {
  262. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  263. let ((item_state_proxy, transaction), (pkts, new_character)) = ItemStateAction::default()
  264. .act(actions::remove_item_from_inventory(character.id, *item_id, amount))
  265. .act(actions::use_consumed_item(character))
  266. .act(actions::fork(
  267. actions::foreach(actions::apply_item_action_packets(character.id, area_client)),
  268. actions::apply_item_action_character(character)
  269. ))
  270. .commit((item_state_proxy, transaction))
  271. .await?;
  272. item_state_proxy.commit().await;
  273. *character = new_character;
  274. Ok((transaction, pkts.into_iter().flatten().collect()))
  275. })
  276. }
  277. pub fn feed_mag<'a, EG> (
  278. item_state: &'a mut ItemState,
  279. entity_gateway: &'a mut EG,
  280. character: &'a CharacterEntity,
  281. mag_item_id: &'a ClientItemId,
  282. tool_item_id: &'a ClientItemId,
  283. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  284. where
  285. EG: EntityGateway + 'static,
  286. {
  287. entity_gateway.with_transaction(move |transaction| async move {
  288. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  289. let ((item_state_proxy, transaction), _) = ItemStateAction::default()
  290. .act(actions::take_item_from_inventory(character.id, *tool_item_id, 1))
  291. .act(actions::feed_mag_item(character.clone(), *mag_item_id))
  292. .commit((item_state_proxy, transaction))
  293. .await?;
  294. item_state_proxy.commit().await;
  295. Ok((transaction, ()))
  296. })
  297. }
  298. pub fn buy_shop_item<'a, EG> (
  299. item_state: &'a mut ItemState,
  300. entity_gateway: &'a mut EG,
  301. character: &'a CharacterEntity,
  302. shop_item: &'a (dyn ShopItem + Send + Sync),
  303. item_id: ClientItemId,
  304. amount: u32,
  305. ) -> BoxFuture<'a, Result<InventoryItem, anyhow::Error>>
  306. where
  307. EG: EntityGateway + 'static,
  308. {
  309. let item_price = shop_item.price() as u32 * amount;
  310. entity_gateway.with_transaction(move |transaction| async move {
  311. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  312. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  313. .act(actions::take_meseta_from_inventory(character.id, item_price))
  314. //.act(bought_item_to_inventory_item)
  315. //.act(add_item_to_inventory)
  316. .act(actions::add_bought_item_to_inventory(character.id, shop_item, item_id, amount))
  317. .commit((item_state_proxy, transaction))
  318. .await?;
  319. item_state_proxy.commit().await;
  320. Ok((transaction, result))
  321. })
  322. }
  323. pub fn sell_item<'a, EG> (
  324. item_state: &'a mut ItemState,
  325. entity_gateway: &'a mut EG,
  326. character: &'a CharacterEntity,
  327. item_id: ClientItemId,
  328. amount: u32,
  329. ) -> BoxFuture<'a, Result<InventoryItem, anyhow::Error>>
  330. where
  331. EG: EntityGateway + 'static,
  332. {
  333. entity_gateway.with_transaction(move |transaction| async move {
  334. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  335. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  336. .act(actions::take_item_from_inventory(character.id, item_id, amount))
  337. .act(actions::sell_inventory_item(character.id))
  338. .commit((item_state_proxy, transaction))
  339. .await?;
  340. item_state_proxy.commit().await;
  341. Ok((transaction, result))
  342. })
  343. }
  344. #[allow(clippy::type_complexity)]
  345. pub fn trade_items<'a, EG> (
  346. item_state: &'a mut ItemState,
  347. entity_gateway: &'a mut EG,
  348. p1: (&'a AreaClient, &'a CharacterEntity, &'a Vec<TradeItem>, Meseta),
  349. p2: (&'a AreaClient, &'a CharacterEntity, &'a Vec<TradeItem>, Meseta))
  350. -> BoxFuture<'a, Result<(Vec<InventoryItem>, Vec<InventoryItem>), anyhow::Error>>
  351. where
  352. EG: EntityGateway + 'static,
  353. {
  354. let p1_trade_items = p1.2
  355. .iter()
  356. .map(|item| {
  357. match item {
  358. TradeItem::Individual(item_id) => (*item_id, 1),
  359. TradeItem::Stacked(item_id, amount) => (*item_id, *amount as u32),
  360. }
  361. })
  362. .collect();
  363. let p2_trade_items = p2.2
  364. .iter()
  365. .map(|item| {
  366. match item {
  367. TradeItem::Individual(item_id) => (*item_id, 1),
  368. TradeItem::Stacked(item_id, amount) => (*item_id, *amount as u32),
  369. }
  370. })
  371. .collect();
  372. entity_gateway.with_transaction(move |mut transaction| async move {
  373. let p1_id = p1.1.id;
  374. let p2_id = p2.1.id;
  375. let trade = transaction.gateway().create_trade(&p1_id, &p2_id).await?;
  376. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  377. let ((item_state_proxy, transaction), p1_removed_items) = ItemStateAction::default()
  378. .act(actions::iterate(p1_trade_items, move |p1_trade_item| actions::take_item_from_inventory(p1_id, p1_trade_item.0, p1_trade_item.1) ))
  379. .act(actions::foreach(actions::assign_new_item_id()))
  380. .commit((item_state_proxy, transaction))
  381. .await?;
  382. let ((item_state_proxy, transaction), p2_removed_items) = ItemStateAction::default()
  383. .act(actions::iterate(p2_trade_items, move |p2_trade_item| actions::take_item_from_inventory(p2_id, p2_trade_item.0, p2_trade_item.1) ))
  384. .act(actions::foreach(actions::assign_new_item_id()))
  385. .commit((item_state_proxy, transaction))
  386. .await?;
  387. let ((item_state_proxy, transaction), p2_new_items) = ItemStateAction::default()
  388. .act(actions::insert(p1_removed_items))
  389. .act(actions::foreach(actions::add_item_to_inventory(p2.1.clone())))
  390. .act(actions::record_trade(trade.id, p1_id, p2_id))
  391. .commit((item_state_proxy, transaction))
  392. .await?;
  393. let ((item_state_proxy, transaction), p1_new_items) = ItemStateAction::default()
  394. .act(actions::insert(p2_removed_items))
  395. .act(actions::foreach(actions::add_item_to_inventory(p1.1.clone())))
  396. .act(actions::record_trade(trade.id, p2_id, p1_id))
  397. .commit((item_state_proxy, transaction))
  398. .await?;
  399. let ((item_state_proxy, transaction), _) = ItemStateAction::default()
  400. .act(actions::take_meseta_from_inventory(p1_id, p1.3.0))
  401. .act(actions::take_meseta_from_inventory(p2_id, p2.3.0))
  402. .act(actions::add_meseta_to_inventory(p1_id, p2.3.0))
  403. .act(actions::add_meseta_to_inventory(p2_id, p1.3.0))
  404. .commit((item_state_proxy, transaction))
  405. .await?;
  406. item_state_proxy.commit().await;
  407. Ok((transaction, (p1_new_items, p2_new_items)))
  408. })
  409. }
  410. pub fn take_meseta<'a, EG> (
  411. item_state: &'a mut ItemState,
  412. entity_gateway: &'a mut EG,
  413. character_id: &'a CharacterEntityId,
  414. meseta: Meseta)
  415. -> BoxFuture<'a, Result<(), anyhow::Error>>
  416. where
  417. EG: EntityGateway + 'static,
  418. {
  419. entity_gateway.with_transaction(move |transaction| async move {
  420. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  421. let ((item_state_proxy, transaction), _) = ItemStateAction::default()
  422. .act(actions::take_meseta_from_inventory(*character_id, meseta.0))
  423. .commit((item_state_proxy, transaction))
  424. .await?;
  425. item_state_proxy.commit().await;
  426. Ok((transaction, ()))
  427. })
  428. }
  429. pub fn enemy_drops_item<'a, EG> (
  430. item_state: &'a mut ItemState,
  431. entity_gateway: &'a mut EG,
  432. character_id: CharacterEntityId,
  433. room_id: RoomEntityId,
  434. monster_type: MonsterType,
  435. item_drop: ItemDrop)
  436. -> BoxFuture<'a, Result<FloorItem, anyhow::Error>>
  437. where
  438. EG: EntityGateway + 'static,
  439. {
  440. entity_gateway.with_transaction(move |transaction| async move {
  441. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  442. let ((item_state_proxy, transaction), floor_item) = ItemStateAction::default()
  443. .act(actions::convert_item_drop_to_floor_item(item_drop))
  444. .act(actions::item_note_enemy_drop(character_id, room_id, monster_type))
  445. .act(actions::add_item_to_local_floor(character_id))
  446. .commit((item_state_proxy, transaction))
  447. .await?;
  448. item_state_proxy.commit().await;
  449. Ok((transaction, floor_item))
  450. })
  451. }
  452. pub fn box_drops_item<'a, EG> (
  453. item_state: &'a mut ItemState,
  454. entity_gateway: &'a mut EG,
  455. character_id: CharacterEntityId,
  456. room_id: RoomEntityId,
  457. item_drop: ItemDrop)
  458. -> BoxFuture<'a, Result<FloorItem, anyhow::Error>>
  459. where
  460. EG: EntityGateway + 'static,
  461. {
  462. entity_gateway.with_transaction(move |transaction| async move {
  463. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  464. let ((item_state_proxy, transaction), floor_item) = ItemStateAction::default()
  465. .act(actions::convert_item_drop_to_floor_item(item_drop))
  466. .act(actions::item_note_box_drop(character_id, room_id))
  467. .act(actions::add_item_to_local_floor(character_id))
  468. .commit((item_state_proxy, transaction))
  469. .await?;
  470. item_state_proxy.commit().await;
  471. Ok((transaction, floor_item))
  472. })
  473. }
  474. pub fn apply_modifier<'a, EG> (
  475. item_state: &'a mut ItemState,
  476. entity_gateway: &'a mut EG,
  477. character: &'a CharacterEntity,
  478. item_id: ClientItemId,
  479. modifier: ItemModifier)
  480. -> BoxFuture<'a, Result<IndividualItemDetail, anyhow::Error>>
  481. where
  482. EG: EntityGateway + 'static,
  483. {
  484. entity_gateway.with_transaction(move |transaction| async move {
  485. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  486. let ((item_state_proxy, transaction), item) = ItemStateAction::default()
  487. .act(actions::take_item_from_inventory(character.id, item_id, 1))
  488. .act(actions::apply_modifier_to_inventory_item(modifier))
  489. .act(actions::add_item_to_inventory(character.clone()))
  490. .act(actions::as_individual_item())
  491. .commit((item_state_proxy, transaction))
  492. .await?;
  493. item_state_proxy.commit().await;
  494. Ok((transaction, item))
  495. })
  496. }
  497. pub fn floor_item_limit_reached<'a, EG> (
  498. item_state: &'a ItemState,
  499. entity_gateway: &'a mut EG,
  500. character: &'a CharacterEntity,
  501. item_id: &'a ClientItemId,
  502. map_area: MapArea
  503. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  504. where
  505. EG: EntityGateway + 'static,
  506. EG::Transaction<'a>: Clone,
  507. {
  508. entity_gateway.with_transaction(move |transaction| async move {
  509. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  510. let((item_state_proxy, transaction), result) = ItemStateAction::default()
  511. .act(actions::take_item_from_floor(character.id, *item_id))
  512. .act(actions::delete_item_from_floor(map_area))
  513. .commit((item_state_proxy, transaction))
  514. .await?;
  515. item_state_proxy.commit().await;
  516. Ok((transaction, result))
  517. })
  518. }