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.

563 lines
21 KiB

1 year ago
1 year ago
2 years ago
1 year ago
1 year ago
  1. use futures::future::BoxFuture;
  2. use crate::ship::items::ClientItemId;
  3. use crate::entity::item::Meseta;
  4. use crate::ship::ship::SendShipPacket;
  5. use crate::ship::map::MapArea;
  6. use crate::entity::character::{CharacterEntity, CharacterEntityId};
  7. use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction};
  8. use crate::entity::item::ItemModifier;
  9. use crate::entity::room::RoomEntityId;
  10. use crate::ship::items::state::{ItemState, ItemStateProxy, IndividualItemDetail};
  11. use crate::ship::items::itemstateaction::{ItemStateAction, ItemAction};
  12. use crate::ship::items::inventory::InventoryItem;
  13. use crate::ship::items::floor::FloorItem;
  14. use crate::ship::shops::ShopItem;
  15. use crate::ship::trade::TradeItem;
  16. use crate::ship::location::AreaClient;
  17. use crate::ship::drops::ItemDrop;
  18. use crate::ship::monster::MonsterType;
  19. use crate::ship::items::actions;
  20. pub fn pick_up_item<'a, EG>(
  21. item_state: &'a mut ItemState,
  22. entity_gateway: &'a mut EG,
  23. character: &'a CharacterEntity,
  24. item_id: &'a ClientItemId,
  25. ) -> BoxFuture<'a, Result<actions::TriggerCreateItem, anyhow::Error>>
  26. where
  27. EG: EntityGateway + 'static,
  28. EG::Transaction<'a>: Clone,
  29. {
  30. entity_gateway.with_transaction(move |transaction| async move {
  31. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  32. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  33. .act(actions::take_item_from_floor(character.id, *item_id))
  34. .act(actions::add_floor_item_to_inventory(character))
  35. .commit((item_state_proxy, transaction))
  36. .await?;
  37. item_state_proxy.commit().await;
  38. Ok((transaction, result))
  39. })
  40. }
  41. pub fn drop_item<'a, EG>(
  42. item_state: &'a mut ItemState,
  43. entity_gateway: &'a mut EG,
  44. character: &'a CharacterEntity,
  45. item_id: &'a ClientItemId,
  46. map_area: MapArea,
  47. drop_position: (f32, f32, f32),
  48. )-> BoxFuture<'a, Result<FloorItem, anyhow::Error>>
  49. where
  50. EG: EntityGateway + 'static,
  51. EG::Transaction<'a>: Clone,
  52. {
  53. entity_gateway.with_transaction(move |transaction| async move {
  54. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  55. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  56. .act(actions::take_item_from_inventory(character.id, *item_id, 0))
  57. .act(actions::add_inventory_item_to_shared_floor(character.id, map_area, drop_position))
  58. .commit((item_state_proxy, transaction))
  59. .await?;
  60. item_state_proxy.commit().await;
  61. Ok((transaction, result))
  62. })
  63. }
  64. pub fn drop_partial_item<'a, EG>(
  65. item_state: &'a mut ItemState,
  66. entity_gateway: &'a mut EG,
  67. character: &'a CharacterEntity,
  68. item_id: &'a ClientItemId,
  69. map_area: MapArea,
  70. drop_position: (f32, f32),
  71. amount: u32
  72. ) -> BoxFuture<'a, Result<FloorItem, anyhow::Error>>
  73. where
  74. EG: EntityGateway + 'static,
  75. {
  76. entity_gateway.with_transaction(move |transaction| async move {
  77. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  78. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  79. .act(actions::take_item_from_inventory(character.id, *item_id, amount))
  80. .act(actions::add_inventory_item_to_shared_floor(character.id, map_area, (drop_position.0, 0.0, drop_position.1)))
  81. .commit((item_state_proxy, transaction))
  82. .await?;
  83. item_state_proxy.commit().await;
  84. Ok((transaction, result))
  85. })
  86. }
  87. pub fn drop_meseta<'a, EG>(
  88. item_state: &'a mut ItemState,
  89. entity_gateway: &'a mut EG,
  90. character: &'a CharacterEntity,
  91. map_area: MapArea,
  92. drop_position: (f32, f32),
  93. amount: u32,
  94. ) -> BoxFuture<'a, Result<FloorItem, anyhow::Error>>
  95. where
  96. EG: EntityGateway + 'static,
  97. {
  98. entity_gateway.with_transaction(move |transaction| async move {
  99. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  100. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  101. .act(actions::take_meseta_from_inventory(character.id, amount))
  102. .act(actions::add_meseta_to_shared_floor(character.id, amount, map_area, drop_position))
  103. .commit((item_state_proxy, transaction))
  104. .await?;
  105. item_state_proxy.commit().await;
  106. Ok((transaction, result))
  107. })
  108. }
  109. pub fn withdraw_meseta<'a, EG>(
  110. item_state: &'a mut ItemState,
  111. entity_gateway: &'a mut EG,
  112. character: &'a CharacterEntity,
  113. amount: u32,
  114. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  115. where
  116. EG: EntityGateway + 'static,
  117. {
  118. entity_gateway.with_transaction(move |transaction| async move {
  119. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  120. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  121. .act(actions::take_meseta_from_bank(character.id, amount))
  122. .act(actions::add_meseta_from_bank_to_inventory(character.id, amount))
  123. .commit((item_state_proxy, transaction))
  124. .await?;
  125. item_state_proxy.commit().await;
  126. Ok((transaction, result))
  127. })
  128. }
  129. pub fn deposit_meseta<'a, EG>(
  130. item_state: &'a mut ItemState,
  131. entity_gateway: &'a mut EG,
  132. character: &'a CharacterEntity,
  133. amount: u32,
  134. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  135. where
  136. EG: EntityGateway + 'static,
  137. {
  138. entity_gateway.with_transaction(move |transaction| async move {
  139. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  140. let ((item_state_proxy, transaction), _) = ItemStateAction::default()
  141. .act(actions::take_meseta_from_inventory(character.id, amount))
  142. .act(actions::add_meseta_to_bank(character.id, amount))
  143. .commit((item_state_proxy, transaction))
  144. .await?;
  145. item_state_proxy.commit().await;
  146. Ok((transaction, ()))
  147. })
  148. }
  149. pub fn withdraw_item<'a, EG>(
  150. item_state: &'a mut ItemState,
  151. entity_gateway: &'a mut EG,
  152. character: &'a CharacterEntity,
  153. item_id: &'a ClientItemId,
  154. amount: u32,
  155. ) -> BoxFuture<'a, Result<InventoryItem, anyhow::Error>>
  156. where
  157. EG: EntityGateway + 'static,
  158. {
  159. entity_gateway.with_transaction(move |transaction| async move {
  160. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  161. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  162. .act(actions::take_item_from_bank(character.id, *item_id, amount))
  163. //.act(bank_item_to_inventory_item)
  164. //.act(add_item_to_inventory)
  165. .act(actions::add_bank_item_to_inventory(character))
  166. .commit((item_state_proxy, transaction))
  167. .await?;
  168. item_state_proxy.commit().await;
  169. Ok((transaction, result))
  170. })
  171. }
  172. pub fn deposit_item<'a, EG> (
  173. item_state: &'a mut ItemState,
  174. entity_gateway: &'a mut EG,
  175. character: &'a CharacterEntity,
  176. item_id: &'a ClientItemId,
  177. amount: u32,
  178. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  179. where
  180. EG: EntityGateway + 'static,
  181. {
  182. entity_gateway.with_transaction(move |transaction| async move {
  183. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  184. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  185. .act(actions::take_item_from_inventory(character.id, *item_id, amount))
  186. .act(actions::add_inventory_item_to_bank(character.id))
  187. .commit((item_state_proxy, transaction))
  188. .await?;
  189. item_state_proxy.commit().await;
  190. Ok((transaction, result))
  191. })
  192. }
  193. pub fn equip_item<'a, EG> (
  194. item_state: &'a mut ItemState,
  195. entity_gateway: &'a mut EG,
  196. character: &'a CharacterEntity,
  197. item_id: &'a ClientItemId,
  198. equip_slot: u8,
  199. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  200. where
  201. EG: EntityGateway + 'static,
  202. {
  203. entity_gateway.with_transaction(move |transaction| async move {
  204. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  205. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  206. .act(actions::equip_inventory_item(character.id, *item_id, equip_slot))
  207. .commit((item_state_proxy, transaction))
  208. .await?;
  209. item_state_proxy.commit().await;
  210. Ok((transaction, result))
  211. })
  212. }
  213. pub fn unequip_item<'a, EG> (
  214. item_state: &'a mut ItemState,
  215. entity_gateway: &'a mut EG,
  216. character: &'a CharacterEntity,
  217. item_id: &'a ClientItemId,
  218. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  219. where
  220. EG: EntityGateway + 'static,
  221. {
  222. entity_gateway.with_transaction(move |transaction| async move {
  223. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  224. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  225. .act(actions::unequip_inventory_item(character.id, *item_id))
  226. .commit((item_state_proxy, transaction))
  227. .await?;
  228. item_state_proxy.commit().await;
  229. Ok((transaction, result))
  230. })
  231. }
  232. pub fn sort_inventory<'a, EG> (
  233. item_state: &'a mut ItemState,
  234. entity_gateway: &'a mut EG,
  235. character: &'a CharacterEntity,
  236. item_ids: Vec<ClientItemId>,
  237. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  238. where
  239. EG: EntityGateway + 'static,
  240. {
  241. entity_gateway.with_transaction(move |transaction| async move {
  242. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  243. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  244. .act(actions::sort_inventory_items(character.id, item_ids))
  245. .commit((item_state_proxy, transaction))
  246. .await?;
  247. item_state_proxy.commit().await;
  248. Ok((transaction, result))
  249. })
  250. }
  251. pub fn use_item<'a, EG> (
  252. item_state: &'a mut ItemState,
  253. entity_gateway: &'a mut EG,
  254. character: &'a mut CharacterEntity,
  255. area_client: AreaClient,
  256. item_id: &'a ClientItemId,
  257. amount: u32,
  258. ) -> BoxFuture<'a, Result<Vec<SendShipPacket>, anyhow::Error>>
  259. where
  260. EG: EntityGateway + 'static,
  261. {
  262. entity_gateway.with_transaction(move |transaction| async move {
  263. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  264. let ((item_state_proxy, transaction), (pkts, new_character)) = ItemStateAction::default()
  265. .act(actions::remove_item_from_inventory(character.id, *item_id, amount))
  266. .act(actions::use_consumed_item(character))
  267. .act(actions::fork(
  268. actions::foreach(actions::apply_item_action_packets(character.id, area_client)),
  269. actions::apply_item_action_character(character)
  270. ))
  271. .commit((item_state_proxy, transaction))
  272. .await?;
  273. item_state_proxy.commit().await;
  274. *character = new_character;
  275. Ok((transaction, pkts.into_iter().flatten().collect()))
  276. })
  277. }
  278. pub fn feed_mag<'a, EG> (
  279. item_state: &'a mut ItemState,
  280. entity_gateway: &'a mut EG,
  281. character: &'a CharacterEntity,
  282. mag_item_id: &'a ClientItemId,
  283. tool_item_id: &'a ClientItemId,
  284. ) -> BoxFuture<'a, Result<(), anyhow::Error>>
  285. where
  286. EG: EntityGateway + 'static,
  287. {
  288. entity_gateway.with_transaction(move |transaction| async move {
  289. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  290. let ((item_state_proxy, transaction), _) = ItemStateAction::default()
  291. .act(actions::take_item_from_inventory(character.id, *tool_item_id, 1))
  292. .act(actions::feed_mag_item(character.clone(), *mag_item_id))
  293. .commit((item_state_proxy, transaction))
  294. .await?;
  295. item_state_proxy.commit().await;
  296. Ok((transaction, ()))
  297. })
  298. }
  299. pub fn buy_shop_item<'a, EG> (
  300. item_state: &'a mut ItemState,
  301. entity_gateway: &'a mut EG,
  302. character: &'a CharacterEntity,
  303. shop_item: &'a (dyn ShopItem + Send + Sync),
  304. item_id: ClientItemId,
  305. amount: u32,
  306. ) -> BoxFuture<'a, Result<InventoryItem, anyhow::Error>>
  307. where
  308. EG: EntityGateway + 'static,
  309. {
  310. let item_price = shop_item.price() as u32 * amount;
  311. entity_gateway.with_transaction(move |transaction| async move {
  312. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  313. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  314. .act(actions::take_meseta_from_inventory(character.id, item_price))
  315. //.act(bought_item_to_inventory_item)
  316. //.act(add_item_to_inventory)
  317. .act(actions::add_bought_item_to_inventory(character.id, shop_item, item_id, amount))
  318. .commit((item_state_proxy, transaction))
  319. .await?;
  320. item_state_proxy.commit().await;
  321. Ok((transaction, result))
  322. })
  323. }
  324. pub fn sell_item<'a, EG> (
  325. item_state: &'a mut ItemState,
  326. entity_gateway: &'a mut EG,
  327. character: &'a CharacterEntity,
  328. item_id: ClientItemId,
  329. amount: u32,
  330. ) -> BoxFuture<'a, Result<InventoryItem, anyhow::Error>>
  331. where
  332. EG: EntityGateway + 'static,
  333. {
  334. entity_gateway.with_transaction(move |transaction| async move {
  335. let item_state_proxy = ItemStateProxy::new(item_state.clone());
  336. let ((item_state_proxy, transaction), result) = ItemStateAction::default()
  337. .act(actions::take_item_from_inventory(character.id, item_id, amount))
  338. .act(actions::sell_inventory_item(character.id))
  339. .commit((item_state_proxy, transaction))
  340. .await?;
  341. item_state_proxy.commit().await;
  342. Ok((transaction, result))
  343. })
  344. }
  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. }