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.

1786 lines
69 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. use std::collections::BTreeSet;
  2. use networking::serverstate::{ClientId, ServerState};
  3. use entity::gateway::{EntityGateway, InMemoryGateway};
  4. use entity::item;
  5. use libpso::item::weapon;
  6. use ship_server::{RecvShipPacket, SendShipPacket};
  7. use shops::StandardItemShops;
  8. use libpso::packet::ship::*;
  9. use libpso::packet::messages::*;
  10. #[path = "common.rs"]
  11. mod common;
  12. use common::*;
  13. #[async_std::test]
  14. async fn test_bank_items_sent_in_character_login() {
  15. let mut entity_gateway = InMemoryGateway::default();
  16. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  17. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  18. let item = entity_gateway.create_item(
  19. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  20. .as_new()
  21. ).await.unwrap();
  22. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![item]), &item::BankIdentifier::Character).await.unwrap();
  23. let mut ship = standard_ship(entity_gateway.clone());
  24. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  25. let packets = ship.handle(ClientId(1), RecvShipPacket::MenuSelect(MenuSelect {
  26. menu: BLOCK_MENU_ID,
  27. item: 1,
  28. })).await.unwrap();
  29. assert!(matches!(&packets[0], (_, SendShipPacket::FullCharacter(fc)) if fc.character.bank.items[0].data1[0..3] == [0x00, 0x08, 0x04] ));
  30. }
  31. #[async_std::test]
  32. async fn test_request_bank_items() {
  33. let mut entity_gateway = InMemoryGateway::default();
  34. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  35. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  36. let mut bank = Vec::new();
  37. for _ in 0..3 {
  38. bank.push(entity_gateway.create_item(
  39. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  40. .as_new()
  41. ).await.unwrap());
  42. }
  43. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
  44. let mut ship = standard_ship(entity_gateway.clone());
  45. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  46. join_lobby(&mut ship, ClientId(1)).await;
  47. create_room(&mut ship, ClientId(1), "room", "").await;
  48. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  49. client: 0,
  50. target: 0,
  51. unknown: 0,
  52. })))).await.unwrap();
  53. assert!(matches!(&packets[0], (_, SendShipPacket::BankItemList (bank_item_list))
  54. if bank_item_list.item_count == 3
  55. && bank_item_list.size == 0x18 * 3 + 0x14
  56. && bank_item_list.items[0].data1[0..3] == [0x00, 0x08, 0x04]
  57. && bank_item_list.items[1].data1[0..3] == [0x00, 0x08, 0x04]
  58. && bank_item_list.items[2].data1[0..3] == [0x00, 0x08, 0x04]
  59. ));
  60. }
  61. #[async_std::test]
  62. async fn test_request_stacked_bank_items() {
  63. let mut entity_gateway = InMemoryGateway::default();
  64. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  65. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  66. let mut monomates = Vec::new();
  67. for _ in 0..3usize {
  68. monomates.push(entity_gateway.create_item(
  69. ItemBuilder::tool(item::tool::ToolType::Monomate)
  70. .as_new()
  71. ).await.unwrap());
  72. }
  73. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
  74. let mut ship = standard_ship(entity_gateway.clone());
  75. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  76. join_lobby(&mut ship, ClientId(1)).await;
  77. create_room(&mut ship, ClientId(1), "room", "").await;
  78. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  79. client: 0,
  80. target: 0,
  81. unknown: 0,
  82. })))).await.unwrap();
  83. assert!(matches!(&packets[0], (_, SendShipPacket::BankItemList (bank_item_list))
  84. if bank_item_list.item_count == 1
  85. && bank_item_list.size == 0x18 + 0x14
  86. && bank_item_list.items[0].data1[0..3] == [0x03, 0x00, 0x00]
  87. && bank_item_list.items[0].amount == 3
  88. ));
  89. }
  90. #[async_std::test]
  91. async fn test_request_bank_items_sorted() {
  92. let mut entity_gateway = InMemoryGateway::default();
  93. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  94. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  95. let item1 = entity_gateway.create_item(
  96. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  97. .as_new()
  98. ).await.unwrap();
  99. let monomate = entity_gateway.create_item(
  100. ItemBuilder::tool(item::tool::ToolType::Monomate)
  101. .as_new()
  102. ).await.unwrap();
  103. let item2 = entity_gateway.create_item(
  104. ItemBuilder::weapon(weapon::WeaponType::Calibur)
  105. .as_new()
  106. ).await.unwrap();
  107. let bank = vec![item::BankItemEntity::Individual(item1), vec![monomate].into(), item2.into()];
  108. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
  109. let mut ship = standard_ship(entity_gateway.clone());
  110. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  111. join_lobby(&mut ship, ClientId(1)).await;
  112. create_room(&mut ship, ClientId(1), "room", "").await;
  113. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  114. client: 0,
  115. target: 0,
  116. unknown: 0,
  117. })))).await.unwrap();
  118. assert!(matches!(&packets[0], (_, SendShipPacket::BankItemList (bank_item_list))
  119. if bank_item_list.item_count == 3
  120. && bank_item_list.size == 0x18 * 3 + 0x14
  121. && bank_item_list.items[0].data1[0..3] == [0x00, 0x02, 0x04]
  122. && bank_item_list.items[1].data1[0..3] == [0x00, 0x08, 0x04]
  123. && bank_item_list.items[2].data1[0..3] == [0x03, 0x00, 0x00]
  124. ));
  125. }
  126. #[async_std::test]
  127. async fn test_deposit_individual_item() {
  128. let mut entity_gateway = InMemoryGateway::default();
  129. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  130. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  131. let item0 = entity_gateway.create_item(
  132. ItemBuilder::weapon(weapon::WeaponType::Saber)
  133. .as_new()
  134. ).await.unwrap();
  135. let item1 = entity_gateway.create_item(
  136. ItemBuilder::weapon(weapon::WeaponType::Handgun)
  137. .as_new()
  138. ).await.unwrap();
  139. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![item0, item1])).await.unwrap();
  140. let mut ship = standard_ship(entity_gateway.clone());
  141. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  142. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  143. join_lobby(&mut ship, ClientId(1)).await;
  144. join_lobby(&mut ship, ClientId(2)).await;
  145. create_room(&mut ship, ClientId(1), "room", "").await;
  146. join_room(&mut ship, ClientId(2), 0).await;
  147. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  148. client: 0,
  149. target: 0,
  150. unknown: 0,
  151. })))).await.unwrap();
  152. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  153. client: 0,
  154. target: 0,
  155. item_id: 0x10001,
  156. action: 0,
  157. item_amount: 0,
  158. meseta_amount: 0,
  159. unknown: 0,
  160. })))).await.unwrap();
  161. assert!(packets.len() == 2);
  162. assert!(matches!(&packets[1], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::PlayerNoLongerHasItem(player_no_longer_has_item)}))
  163. if player_no_longer_has_item.item_id == 0x10001
  164. && player_no_longer_has_item.amount == 0
  165. ));
  166. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  167. assert_eq!(inventory_items.items.len(), 1);
  168. inventory_items.items[0].with_individual(|item| {
  169. assert_eq!(item.id, item::ItemEntityId(1));
  170. }).unwrap();
  171. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  172. assert_eq!(bank_items.items.len(), 1);
  173. bank_items.items[0].with_individual(|item| {
  174. assert_eq!(item.id, item::ItemEntityId(2));
  175. }).unwrap();
  176. }
  177. #[async_std::test]
  178. async fn test_deposit_stacked_item() {
  179. let mut entity_gateway = InMemoryGateway::default();
  180. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  181. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  182. let mut monomates = Vec::new();
  183. for _ in 0..3usize {
  184. monomates.push(entity_gateway.create_item(
  185. ItemBuilder::tool(item::tool::ToolType::Monomate)
  186. .as_new()
  187. ).await.unwrap());
  188. }
  189. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
  190. let mut ship = standard_ship(entity_gateway.clone());
  191. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  192. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  193. join_lobby(&mut ship, ClientId(1)).await;
  194. join_lobby(&mut ship, ClientId(2)).await;
  195. create_room(&mut ship, ClientId(1), "room", "").await;
  196. join_room(&mut ship, ClientId(2), 0).await;
  197. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  198. client: 0,
  199. target: 0,
  200. unknown: 0,
  201. })))).await.unwrap();
  202. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  203. client: 0,
  204. target: 0,
  205. item_id: 0x10000,
  206. action: 0,
  207. item_amount: 3,
  208. meseta_amount: 0,
  209. unknown: 0,
  210. })))).await.unwrap();
  211. assert!(packets.len() == 2);
  212. assert!(matches!(&packets[1], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::PlayerNoLongerHasItem(player_no_longer_has_item)}))
  213. if player_no_longer_has_item.item_id == 0x10000
  214. && player_no_longer_has_item.amount == 3
  215. ));
  216. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  217. assert_eq!(bank_items.items.len(), 1);
  218. bank_items.items[0].with_stacked(|items| {
  219. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  220. vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3)]);
  221. }).unwrap();
  222. }
  223. #[async_std::test]
  224. async fn test_deposit_partial_stacked_item() {
  225. let mut entity_gateway = InMemoryGateway::default();
  226. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  227. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  228. let mut monomates = Vec::new();
  229. for _ in 0..3usize {
  230. monomates.push(entity_gateway.create_item(
  231. ItemBuilder::tool(item::tool::ToolType::Monomate)
  232. .as_new()
  233. ).await.unwrap());
  234. }
  235. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
  236. let mut ship = standard_ship(entity_gateway.clone());
  237. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  238. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  239. join_lobby(&mut ship, ClientId(1)).await;
  240. join_lobby(&mut ship, ClientId(2)).await;
  241. create_room(&mut ship, ClientId(1), "room", "").await;
  242. join_room(&mut ship, ClientId(2), 0).await;
  243. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  244. client: 0,
  245. target: 0,
  246. unknown: 0,
  247. })))).await.unwrap();
  248. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  249. client: 0,
  250. target: 0,
  251. item_id: 0x10000,
  252. action: 0,
  253. item_amount: 2,
  254. meseta_amount: 0,
  255. unknown: 0,
  256. })))).await.unwrap();
  257. assert!(packets.len() == 2);
  258. assert!(matches!(&packets[1], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::PlayerNoLongerHasItem(player_no_longer_has_item)}))
  259. if player_no_longer_has_item.item_id == 0x10000
  260. && player_no_longer_has_item.amount == 2
  261. ));
  262. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  263. assert_eq!(bank_items.items.len(), 1);
  264. bank_items.items[0].with_stacked(|items| {
  265. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  266. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  267. }).unwrap();
  268. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  269. assert_eq!(inventory_items.items.len(), 1);
  270. inventory_items.items[0].with_stacked(|items| {
  271. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  272. vec![item::ItemEntityId(3)]);
  273. }).unwrap();
  274. }
  275. #[async_std::test]
  276. async fn test_deposit_stacked_item_with_stack_already_in_bank() {
  277. let mut entity_gateway = InMemoryGateway::default();
  278. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  279. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  280. let mut bank_monomates = Vec::new();
  281. let mut inventory_monomates = Vec::new();
  282. for _ in 0..2usize {
  283. inventory_monomates.push(entity_gateway.create_item(
  284. ItemBuilder::tool(item::tool::ToolType::Monomate)
  285. .as_new()
  286. ).await.unwrap());
  287. bank_monomates.push(entity_gateway.create_item(
  288. ItemBuilder::tool(item::tool::ToolType::Monomate)
  289. .as_new()
  290. ).await.unwrap());
  291. }
  292. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
  293. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankIdentifier::Character).await.unwrap();
  294. let mut ship = standard_ship(entity_gateway.clone());
  295. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  296. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  297. join_lobby(&mut ship, ClientId(1)).await;
  298. join_lobby(&mut ship, ClientId(2)).await;
  299. create_room(&mut ship, ClientId(1), "room", "").await;
  300. join_room(&mut ship, ClientId(2), 0).await;
  301. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  302. client: 0,
  303. target: 0,
  304. unknown: 0,
  305. })))).await.unwrap();
  306. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  307. client: 0,
  308. target: 0,
  309. item_id: 0x10000,
  310. action: 0,
  311. item_amount: 2,
  312. meseta_amount: 0,
  313. unknown: 0,
  314. })))).await.unwrap();
  315. assert!(packets.len() == 2);
  316. assert!(matches!(&packets[1], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::PlayerNoLongerHasItem(player_no_longer_has_item)}))
  317. if player_no_longer_has_item.item_id == 0x10000
  318. && player_no_longer_has_item.amount == 2
  319. ));
  320. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  321. assert_eq!(bank_items.items.len(), 1);
  322. bank_items.items[0].with_stacked(|items| {
  323. assert_eq!(items.iter().map(|i| i.id).collect::<BTreeSet<_>>(),
  324. vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3), item::ItemEntityId(4)].into_iter().collect::<BTreeSet<_>>() );
  325. }).unwrap();
  326. }
  327. #[async_std::test]
  328. async fn test_deposit_stacked_item_with_full_stack_in_bank() {
  329. let mut entity_gateway = InMemoryGateway::default();
  330. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  331. let mut inventory_monomates = Vec::new();
  332. for _ in 0..2usize {
  333. inventory_monomates.push(entity_gateway.create_item(
  334. ItemBuilder::tool(item::tool::ToolType::Monomate)
  335. .as_new()
  336. ).await.unwrap());
  337. }
  338. let mut bank_monomates = Vec::new();
  339. for _ in 0..10 {
  340. bank_monomates.push(entity_gateway.create_item(
  341. ItemBuilder::tool(item::tool::ToolType::Monomate)
  342. .as_new()
  343. ).await.unwrap());
  344. }
  345. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
  346. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankIdentifier::Character).await.unwrap();
  347. let mut ship = standard_ship(entity_gateway.clone());
  348. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  349. join_lobby(&mut ship, ClientId(1)).await;
  350. create_room(&mut ship, ClientId(1), "room", "").await;
  351. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  352. client: 0,
  353. target: 0,
  354. unknown: 0,
  355. })))).await.unwrap();
  356. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  357. client: 0,
  358. target: 0,
  359. item_id: 0x10000,
  360. action: 0,
  361. item_amount: 2,
  362. meseta_amount: 0,
  363. unknown: 0,
  364. })))).await;
  365. assert!(packets.is_err());
  366. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  367. assert_eq!(bank_items.items.len(), 1);
  368. bank_items.items[0].with_stacked(|items| {
  369. assert_eq!(items.len(), 10);
  370. }).unwrap();
  371. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  372. assert_eq!(inventory_items.items.len(), 1);
  373. inventory_items.items[0].with_stacked(|items| {
  374. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  375. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  376. }).unwrap();
  377. }
  378. #[async_std::test]
  379. async fn test_deposit_individual_item_in_full_bank() {
  380. let mut entity_gateway = InMemoryGateway::default();
  381. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  382. let mut inventory = Vec::new();
  383. inventory.push(entity_gateway.create_item(
  384. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  385. .as_new()
  386. ).await.unwrap());
  387. let mut bank = Vec::new();
  388. for _ in 0..200usize {
  389. bank.push(entity_gateway.create_item(
  390. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  391. .as_new()
  392. ).await.unwrap());
  393. }
  394. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
  395. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
  396. let mut ship = standard_ship(entity_gateway.clone());
  397. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  398. join_lobby(&mut ship, ClientId(1)).await;
  399. create_room(&mut ship, ClientId(1), "room", "").await;
  400. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  401. client: 0,
  402. target: 0,
  403. unknown: 0,
  404. })))).await.unwrap();
  405. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  406. client: 0,
  407. target: 0,
  408. item_id: 0x10000,
  409. action: 0,
  410. item_amount: 0,
  411. meseta_amount: 0,
  412. unknown: 0,
  413. })))).await;
  414. assert!(packets.is_err());
  415. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  416. assert_eq!(bank_items.items.len(), 200);
  417. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  418. assert_eq!(inventory_items.items.len(), 1);
  419. inventory_items.items[0].with_individual(|item| {
  420. assert_eq!(item.id, item::ItemEntityId(1));
  421. }).unwrap();
  422. }
  423. #[async_std::test]
  424. async fn test_deposit_stacked_item_in_full_bank() {
  425. let mut entity_gateway = InMemoryGateway::default();
  426. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  427. let mut monomates = Vec::new();
  428. for _ in 0..2usize {
  429. monomates.push(entity_gateway.create_item(
  430. ItemBuilder::tool(item::tool::ToolType::Monomate)
  431. .as_new()
  432. ).await.unwrap());
  433. }
  434. let mut full_bank = Vec::new();
  435. for _ in 0..200usize {
  436. full_bank.push(entity_gateway.create_item(
  437. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  438. .as_new()
  439. ).await.unwrap());
  440. }
  441. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
  442. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(full_bank), &item::BankIdentifier::Character).await.unwrap();
  443. let mut ship = standard_ship(entity_gateway.clone());
  444. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  445. join_lobby(&mut ship, ClientId(1)).await;
  446. create_room(&mut ship, ClientId(1), "room", "").await;
  447. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  448. client: 0,
  449. target: 0,
  450. unknown: 0,
  451. })))).await.unwrap();
  452. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  453. client: 0,
  454. target: 0,
  455. item_id: 0x10000,
  456. action: 0,
  457. item_amount: 2,
  458. meseta_amount: 0,
  459. unknown: 0,
  460. })))).await;
  461. assert!(packets.is_err());
  462. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  463. assert_eq!(bank_items.items.len(), 200);
  464. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  465. assert_eq!(inventory_items.items.len(), 1);
  466. inventory_items.items[0].with_stacked(|items| {
  467. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  468. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  469. }).unwrap();
  470. }
  471. #[async_std::test]
  472. async fn test_deposit_stacked_item_in_full_bank_with_partial_stack() {
  473. let mut entity_gateway = InMemoryGateway::default();
  474. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  475. let mut monomates = Vec::new();
  476. for _ in 0..2usize {
  477. monomates.push(entity_gateway.create_item(
  478. ItemBuilder::tool(item::tool::ToolType::Monomate)
  479. .as_new()
  480. ).await.unwrap());
  481. }
  482. let mut bank_monomates = Vec::new();
  483. for _ in 0..2usize {
  484. bank_monomates.push(entity_gateway.create_item(
  485. ItemBuilder::tool(item::tool::ToolType::Monomate)
  486. .as_new()
  487. ).await.unwrap());
  488. }
  489. let mut almost_full_bank: Vec<item::BankItemEntity> = Vec::new();
  490. for _ in 0..199usize {
  491. almost_full_bank.push(entity_gateway.create_item(
  492. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  493. .as_new()
  494. ).await.unwrap().into());
  495. }
  496. almost_full_bank.push(bank_monomates.into());
  497. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
  498. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(almost_full_bank), &item::BankIdentifier::Character).await.unwrap();
  499. let mut ship = standard_ship(entity_gateway.clone());
  500. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  501. join_lobby(&mut ship, ClientId(1)).await;
  502. create_room(&mut ship, ClientId(1), "room", "").await;
  503. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  504. client: 0,
  505. target: 0,
  506. unknown: 0,
  507. })))).await.unwrap();
  508. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  509. client: 0,
  510. target: 0,
  511. item_id: 0x10000,
  512. action: 0,
  513. item_amount: 2,
  514. meseta_amount: 0,
  515. unknown: 0,
  516. })))).await.unwrap();
  517. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  518. assert_eq!(bank_items.items.len(), 200);
  519. bank_items.items[199].with_stacked(|items| {
  520. assert_eq!(items.len(), 4);
  521. }).unwrap();
  522. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  523. assert_eq!(inventory_items.items.len(), 0);
  524. }
  525. #[async_std::test]
  526. async fn test_deposit_meseta() {
  527. let mut entity_gateway = InMemoryGateway::default();
  528. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  529. entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap();
  530. let mut ship = standard_ship(entity_gateway.clone());
  531. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  532. join_lobby(&mut ship, ClientId(1)).await;
  533. create_room(&mut ship, ClientId(1), "room", "").await;
  534. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  535. client: 0,
  536. target: 0,
  537. unknown: 0,
  538. })))).await.unwrap();
  539. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  540. client: 0,
  541. target: 0,
  542. item_id: 0xFFFFFFFF,
  543. action: 0,
  544. item_amount: 0,
  545. meseta_amount: 23,
  546. unknown: 0,
  547. })))).await.unwrap();
  548. let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  549. let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  550. assert!(c1_meseta.0 == 277);
  551. assert!(c1_bank_meseta.0 == 23);
  552. }
  553. #[async_std::test]
  554. async fn test_deposit_too_much_meseta() {
  555. let mut entity_gateway = InMemoryGateway::default();
  556. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  557. entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap();
  558. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(999980)).await.unwrap();
  559. let mut ship = standard_ship(entity_gateway.clone());
  560. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  561. join_lobby(&mut ship, ClientId(1)).await;
  562. create_room(&mut ship, ClientId(1), "room", "").await;
  563. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  564. client: 0,
  565. target: 0,
  566. unknown: 0,
  567. })))).await.unwrap();
  568. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  569. client: 0,
  570. target: 0,
  571. item_id: 0xFFFFFFFF,
  572. action: 0,
  573. item_amount: 0,
  574. meseta_amount: 23,
  575. unknown: 0,
  576. })))).await;
  577. assert!(packets.is_err());
  578. let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  579. let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  580. assert!(c1_meseta.0 == 300);
  581. assert!(c1_bank_meseta.0 == 999980);
  582. }
  583. #[async_std::test]
  584. async fn test_deposit_meseta_when_bank_is_maxed() {
  585. let mut entity_gateway = InMemoryGateway::default();
  586. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  587. entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap();
  588. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(999999)).await.unwrap();
  589. let mut ship = standard_ship(entity_gateway.clone());
  590. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  591. join_lobby(&mut ship, ClientId(1)).await;
  592. create_room(&mut ship, ClientId(1), "room", "").await;
  593. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  594. client: 0,
  595. target: 0,
  596. unknown: 0,
  597. })))).await.unwrap();
  598. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  599. client: 0,
  600. target: 0,
  601. item_id: 0xFFFFFFFF,
  602. action: 0,
  603. item_amount: 0,
  604. meseta_amount: 23,
  605. unknown: 0,
  606. })))).await;
  607. assert!(packets.is_err());
  608. let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  609. let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  610. assert!(c1_meseta.0 == 300);
  611. assert!(c1_bank_meseta.0 == 999999);
  612. }
  613. #[async_std::test]
  614. async fn test_withdraw_individual_item() {
  615. let mut entity_gateway = InMemoryGateway::default();
  616. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  617. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  618. let mut bank = Vec::new();
  619. bank.push(entity_gateway.create_item(
  620. ItemBuilder::weapon(weapon::WeaponType::Saber)
  621. .as_new()
  622. ).await.unwrap());
  623. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
  624. let mut ship = standard_ship(entity_gateway.clone());
  625. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  626. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  627. join_lobby(&mut ship, ClientId(1)).await;
  628. join_lobby(&mut ship, ClientId(2)).await;
  629. create_room(&mut ship, ClientId(1), "room", "").await;
  630. join_room(&mut ship, ClientId(2), 0).await;
  631. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  632. client: 0,
  633. target: 0,
  634. unknown: 0,
  635. })))).await.unwrap();
  636. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  637. client: 0,
  638. target: 0,
  639. item_id: 0x20000,
  640. action: 1,
  641. item_amount: 0,
  642. meseta_amount: 0,
  643. unknown: 0,
  644. })))).await.unwrap();
  645. assert!(packets.len() == 2);
  646. assert!(matches!(&packets[1], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::CreateItem(create_item)}))
  647. if create_item.item_id == 0x20000
  648. ));
  649. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  650. assert_eq!(inventory_items.items.len(), 1);
  651. inventory_items.items[0].with_individual(|item| {
  652. assert_eq!(item.id, item::ItemEntityId(1));
  653. }).unwrap();
  654. }
  655. #[async_std::test]
  656. async fn test_withdraw_stacked_item() {
  657. let mut entity_gateway = InMemoryGateway::default();
  658. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  659. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  660. let mut monomates = Vec::new();
  661. for _ in 0..3usize {
  662. monomates.push(entity_gateway.create_item(
  663. ItemBuilder::tool(item::tool::ToolType::Monomate)
  664. .as_new()
  665. ).await.unwrap());
  666. }
  667. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
  668. let mut ship = standard_ship(entity_gateway.clone());
  669. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  670. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  671. join_lobby(&mut ship, ClientId(1)).await;
  672. join_lobby(&mut ship, ClientId(2)).await;
  673. create_room(&mut ship, ClientId(1), "room", "").await;
  674. join_room(&mut ship, ClientId(2), 0).await;
  675. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  676. client: 0,
  677. target: 0,
  678. unknown: 0,
  679. })))).await.unwrap();
  680. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  681. client: 0,
  682. target: 0,
  683. item_id: 0x20000,
  684. action: 1,
  685. item_amount: 3,
  686. meseta_amount: 0,
  687. unknown: 0,
  688. })))).await.unwrap();
  689. assert!(packets.len() == 2);
  690. assert!(matches!(&packets[1], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::CreateItem(create_item)}))
  691. if create_item.item_id == 0x20000
  692. ));
  693. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  694. assert_eq!(inventory_items.items.len(), 1);
  695. inventory_items.items[0].with_stacked(|items| {
  696. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  697. vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3)]);
  698. }).unwrap();
  699. }
  700. #[async_std::test]
  701. async fn test_withdraw_partial_stacked_item() {
  702. let mut entity_gateway = InMemoryGateway::default();
  703. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  704. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  705. let mut monomates = Vec::new();
  706. for _ in 0..3usize {
  707. monomates.push(entity_gateway.create_item(
  708. ItemBuilder::tool(item::tool::ToolType::Monomate)
  709. .as_new()
  710. ).await.unwrap());
  711. }
  712. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
  713. let mut ship = standard_ship(entity_gateway.clone());
  714. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  715. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  716. join_lobby(&mut ship, ClientId(1)).await;
  717. join_lobby(&mut ship, ClientId(2)).await;
  718. create_room(&mut ship, ClientId(1), "room", "").await;
  719. join_room(&mut ship, ClientId(2), 0).await;
  720. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  721. client: 0,
  722. target: 0,
  723. unknown: 0,
  724. })))).await.unwrap();
  725. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  726. client: 0,
  727. target: 0,
  728. item_id: 0x20000,
  729. action: 1,
  730. item_amount: 2,
  731. meseta_amount: 0,
  732. unknown: 0,
  733. })))).await.unwrap();
  734. assert!(packets.len() == 2);
  735. assert!(matches!(&packets[1], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::CreateItem(create_item)}))
  736. if create_item.item_id == 0x20001
  737. ));
  738. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  739. assert_eq!(bank_items.items.len(), 1);
  740. bank_items.items[0].with_stacked(|items| {
  741. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  742. vec![item::ItemEntityId(3)]);
  743. }).unwrap();
  744. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  745. assert_eq!(inventory_items.items.len(), 1);
  746. inventory_items.items[0].with_stacked(|items| {
  747. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  748. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  749. }).unwrap();
  750. }
  751. #[async_std::test]
  752. async fn test_withdraw_stacked_item_with_stack_already_in_inventory() {
  753. let mut entity_gateway = InMemoryGateway::default();
  754. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  755. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  756. let mut inventory_monomates = Vec::new();
  757. let mut bank_monomates = Vec::new();
  758. for _ in 0..2usize {
  759. inventory_monomates.push(entity_gateway.create_item(
  760. ItemBuilder::tool(item::tool::ToolType::Monomate)
  761. .as_new()
  762. ).await.unwrap());
  763. bank_monomates.push(entity_gateway.create_item(
  764. ItemBuilder::tool(item::tool::ToolType::Monomate)
  765. .as_new()
  766. ).await.unwrap());
  767. }
  768. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
  769. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankIdentifier::Character).await.unwrap();
  770. let mut ship = standard_ship(entity_gateway.clone());
  771. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  772. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  773. join_lobby(&mut ship, ClientId(1)).await;
  774. join_lobby(&mut ship, ClientId(2)).await;
  775. create_room(&mut ship, ClientId(1), "room", "").await;
  776. join_room(&mut ship, ClientId(2), 0).await;
  777. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  778. client: 0,
  779. target: 0,
  780. unknown: 0,
  781. })))).await.unwrap();
  782. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  783. client: 0,
  784. target: 0,
  785. item_id: 0x20000,
  786. action: 1,
  787. item_amount: 2,
  788. meseta_amount: 0,
  789. unknown: 0,
  790. })))).await.unwrap();
  791. assert!(packets.len() == 2);
  792. assert!(matches!(&packets[1], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::CreateItem(create_item)}))
  793. if create_item.item_id == 0x20000
  794. ));
  795. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  796. assert_eq!(bank_items.items.len(), 0);
  797. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  798. assert_eq!(inventory_items.items.len(), 1);
  799. inventory_items.items[0].with_stacked(|items| {
  800. assert_eq!(items.iter().map(|i| i.id).collect::<BTreeSet<_>>(),
  801. vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3), item::ItemEntityId(4)].into_iter().collect::<BTreeSet<_>>());
  802. }).unwrap();
  803. }
  804. #[async_std::test]
  805. async fn test_withdraw_stacked_item_with_full_stack_in_inventory() {
  806. let mut entity_gateway = InMemoryGateway::default();
  807. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  808. let mut bank_monomates = Vec::new();
  809. for _ in 0..2usize {
  810. bank_monomates.push(entity_gateway.create_item(
  811. ItemBuilder::tool(item::tool::ToolType::Monomate)
  812. .as_new()
  813. ).await.unwrap());
  814. }
  815. let mut inventory_monomates = Vec::new();
  816. for _ in 0..10usize {
  817. inventory_monomates.push(entity_gateway.create_item(
  818. ItemBuilder::tool(item::tool::ToolType::Monomate)
  819. .as_new()
  820. ).await.unwrap());
  821. }
  822. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
  823. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), &item::BankIdentifier::Character).await.unwrap();
  824. let mut ship = standard_ship(entity_gateway.clone());
  825. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  826. join_lobby(&mut ship, ClientId(1)).await;
  827. create_room(&mut ship, ClientId(1), "room", "").await;
  828. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  829. client: 0,
  830. target: 0,
  831. unknown: 0,
  832. })))).await.unwrap();
  833. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  834. client: 0,
  835. target: 0,
  836. item_id: 0x20000,
  837. action: 1,
  838. item_amount: 2,
  839. meseta_amount: 0,
  840. unknown: 0,
  841. })))).await;
  842. assert!(packets.is_err());
  843. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  844. assert_eq!(bank_items.items.len(), 1);
  845. bank_items.items[0].with_stacked(|items| {
  846. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  847. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  848. }).unwrap();
  849. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  850. assert_eq!(inventory_items.items.len(), 1);
  851. inventory_items.items[0].with_stacked(|items| {
  852. assert_eq!(items.len(), 10);
  853. }).unwrap();
  854. }
  855. #[async_std::test]
  856. async fn test_withdraw_individual_item_in_full_inventory() {
  857. let mut entity_gateway = InMemoryGateway::default();
  858. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  859. let mut bank = Vec::new();
  860. bank.push(entity_gateway.create_item(
  861. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  862. .as_new()
  863. ).await.unwrap());
  864. let mut inventory = Vec::new();
  865. for _ in 0..30usize {
  866. inventory.push(entity_gateway.create_item(
  867. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  868. .as_new()
  869. ).await.unwrap());
  870. }
  871. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
  872. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), &item::BankIdentifier::Character).await.unwrap();
  873. let mut ship = standard_ship(entity_gateway.clone());
  874. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  875. join_lobby(&mut ship, ClientId(1)).await;
  876. create_room(&mut ship, ClientId(1), "room", "").await;
  877. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  878. client: 0,
  879. target: 0,
  880. unknown: 0,
  881. })))).await.unwrap();
  882. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  883. client: 0,
  884. target: 0,
  885. item_id: 0x20000,
  886. action: 1,
  887. item_amount: 0,
  888. meseta_amount: 0,
  889. unknown: 0,
  890. })))).await;
  891. assert!(packets.is_err());
  892. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  893. assert_eq!(bank_items.items.len(), 1);
  894. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  895. assert_eq!(inventory_items.items.len(), 30);
  896. }
  897. #[async_std::test]
  898. async fn test_withdraw_stacked_item_in_full_inventory() {
  899. let mut entity_gateway = InMemoryGateway::default();
  900. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  901. let mut monomates = Vec::new();
  902. for _ in 0..2usize {
  903. monomates.push(entity_gateway.create_item(
  904. ItemBuilder::tool(item::tool::ToolType::Monomate)
  905. .as_new()
  906. ).await.unwrap());
  907. }
  908. let mut inventory = Vec::new();
  909. for _ in 0..30usize {
  910. inventory.push(entity_gateway.create_item(
  911. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  912. .as_new()
  913. ).await.unwrap());
  914. }
  915. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
  916. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
  917. let mut ship = standard_ship(entity_gateway.clone());
  918. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  919. join_lobby(&mut ship, ClientId(1)).await;
  920. create_room(&mut ship, ClientId(1), "room", "").await;
  921. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  922. client: 0,
  923. target: 0,
  924. unknown: 0,
  925. })))).await.unwrap();
  926. let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  927. client: 0,
  928. target: 0,
  929. item_id: 0x20000,
  930. action: 1,
  931. item_amount: 2,
  932. meseta_amount: 0,
  933. unknown: 0,
  934. })))).await;
  935. assert!(packets.is_err());
  936. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  937. assert_eq!(bank_items.items.len(), 1);
  938. bank_items.items[0].with_stacked(|items| {
  939. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  940. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  941. }).unwrap();
  942. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  943. assert_eq!(inventory_items.items.len(), 30);
  944. }
  945. #[async_std::test]
  946. async fn test_withdraw_stacked_item_in_full_inventory_with_partial_stack() {
  947. let mut entity_gateway = InMemoryGateway::default();
  948. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  949. let mut bank_item = Vec::new();
  950. for _ in 0..2usize {
  951. bank_item.push(entity_gateway.create_item(
  952. ItemBuilder::tool(item::tool::ToolType::Monomate)
  953. .as_new()
  954. ).await.unwrap());
  955. }
  956. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_item]), &item::BankIdentifier::Character).await.unwrap();
  957. let mut items = Vec::new();
  958. for _i in 0..29usize {
  959. items.push(entity_gateway.create_item(
  960. ItemBuilder::weapon(weapon::WeaponType::Vulcan)
  961. .as_new()
  962. ).await.unwrap().into());
  963. }
  964. let mut item29 = Vec::new();
  965. for _ in 0..2usize {
  966. item29.push(entity_gateway.create_item(
  967. ItemBuilder::tool(item::tool::ToolType::Monomate)
  968. .as_new()
  969. ).await.unwrap());
  970. }
  971. items.push(item::InventoryItemEntity::Stacked(item29));
  972. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(items)).await.unwrap();
  973. let mut ship = standard_ship(entity_gateway.clone());
  974. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  975. join_lobby(&mut ship, ClientId(1)).await;
  976. create_room(&mut ship, ClientId(1), "room", "").await;
  977. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  978. client: 0,
  979. target: 0,
  980. unknown: 0,
  981. })))).await.unwrap();
  982. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  983. client: 0,
  984. target: 0,
  985. item_id: 0x20000,
  986. action: 1,
  987. item_amount: 2,
  988. meseta_amount: 0,
  989. unknown: 0,
  990. })))).await.unwrap();
  991. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  992. assert!(bank_items.items.len() == 0);
  993. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  994. assert!(inventory_items.items.len() == 30);
  995. match &inventory_items.items[29] {
  996. item::InventoryItemEntity::Stacked(items) => {
  997. assert_eq!(items.len(), 4);
  998. },
  999. _ => panic!(),
  1000. }
  1001. }
  1002. #[async_std::test]
  1003. async fn test_withdraw_meseta() {
  1004. let mut entity_gateway = InMemoryGateway::default();
  1005. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  1006. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
  1007. let mut ship = standard_ship(entity_gateway.clone());
  1008. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  1009. join_lobby(&mut ship, ClientId(1)).await;
  1010. create_room(&mut ship, ClientId(1), "room", "").await;
  1011. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1012. client: 0,
  1013. target: 0,
  1014. unknown: 0,
  1015. })))).await.unwrap();
  1016. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1017. client: 0,
  1018. target: 0,
  1019. item_id: 0xFFFFFFFF,
  1020. action: 1,
  1021. item_amount: 0,
  1022. meseta_amount: 23,
  1023. unknown: 0,
  1024. })))).await.unwrap();
  1025. let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  1026. let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  1027. assert!(c1_meseta.0 == 23);
  1028. assert!(c1_bank_meseta.0 == 277);
  1029. }
  1030. #[async_std::test]
  1031. async fn test_withdraw_too_much_meseta() {
  1032. let mut entity_gateway = InMemoryGateway::default();
  1033. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  1034. entity_gateway.set_character_meseta(&char1.id, item::Meseta(999980)).await.unwrap();
  1035. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
  1036. let mut ship = standard_ship(entity_gateway.clone());
  1037. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  1038. join_lobby(&mut ship, ClientId(1)).await;
  1039. create_room(&mut ship, ClientId(1), "room", "").await;
  1040. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1041. client: 0,
  1042. target: 0,
  1043. unknown: 0,
  1044. })))).await.unwrap();
  1045. let packet = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1046. client: 0,
  1047. target: 0,
  1048. item_id: 0xFFFFFFFF,
  1049. action: 1,
  1050. item_amount: 0,
  1051. meseta_amount: 23,
  1052. unknown: 0,
  1053. })))).await;
  1054. assert!(packet.is_err());
  1055. let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  1056. let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  1057. assert!(c1_meseta.0 == 999980);
  1058. assert!(c1_bank_meseta.0 == 300);
  1059. }
  1060. #[async_std::test]
  1061. async fn test_withdraw_meseta_inventory_is_maxed() {
  1062. let mut entity_gateway = InMemoryGateway::default();
  1063. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  1064. entity_gateway.set_character_meseta(&char1.id, item::Meseta(999999)).await.unwrap();
  1065. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
  1066. let mut ship = standard_ship(entity_gateway.clone());
  1067. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  1068. join_lobby(&mut ship, ClientId(1)).await;
  1069. create_room(&mut ship, ClientId(1), "room", "").await;
  1070. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1071. client: 0,
  1072. target: 0,
  1073. unknown: 0,
  1074. })))).await.unwrap();
  1075. let packet = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1076. client: 0,
  1077. target: 0,
  1078. item_id: 0xFFFFFFFF,
  1079. action: 1,
  1080. item_amount: 0,
  1081. meseta_amount: 23,
  1082. unknown: 0,
  1083. })))).await;
  1084. assert!(packet.is_err());
  1085. let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  1086. let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  1087. assert!(c1_meseta.0 == 999999);
  1088. assert!(c1_bank_meseta.0 == 300);
  1089. }
  1090. #[async_std::test]
  1091. async fn test_withdraw_meseta_and_buy_a_few_monomates_with_it() {
  1092. let mut entity_gateway = InMemoryGateway::default();
  1093. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  1094. entity_gateway.set_character_meseta(&char1.id, item::Meseta(100)).await.unwrap();
  1095. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
  1096. let mut ship = standard_ship_buildable(entity_gateway.clone())
  1097. .item_shops(StandardItemShops::default())
  1098. .build();
  1099. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  1100. join_lobby(&mut ship, ClientId(1)).await;
  1101. create_room(&mut ship, ClientId(1), "room", "").await;
  1102. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1103. client: 0,
  1104. target: 0,
  1105. unknown: 0,
  1106. })))).await.unwrap();
  1107. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1108. client: 0,
  1109. target: 0,
  1110. item_id: 0xFFFFFFFF,
  1111. action: 1,
  1112. item_amount: 0,
  1113. meseta_amount: 60,
  1114. unknown: 0,
  1115. })))).await.unwrap();
  1116. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::ShopRequest(ShopRequest {
  1117. client: 255,
  1118. target: 255,
  1119. shop_type: 0,
  1120. })))).await.unwrap();
  1121. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BuyItem(BuyItem {
  1122. client: 255,
  1123. target: 255,
  1124. item_id: 0x10000,
  1125. shop_type: 0,
  1126. shop_index: 0,
  1127. amount: 3,
  1128. unknown1: 0,
  1129. })))).await.unwrap();
  1130. //let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  1131. //let c1_bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  1132. //assert!(c1_meseta.0 == 23);
  1133. //assert!(c1_bank_meseta.0 == 277);
  1134. }
  1135. #[async_std::test]
  1136. async fn test_deposit_items_into_shared_banks() {
  1137. let mut entity_gateway = InMemoryGateway::default();
  1138. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  1139. let item0 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Saber).as_new()).await.unwrap();
  1140. let item1 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Buster).as_new()).await.unwrap();
  1141. let item2 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Rifle).as_new()).await.unwrap();
  1142. let item3 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Handgun).as_new()).await.unwrap();
  1143. let item4 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Autogun).as_new()).await.unwrap();
  1144. let item5 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Calibur).as_new()).await.unwrap();
  1145. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![item0, item1, item2, item3, item4, item5])).await.unwrap();
  1146. let mut ship = standard_ship(entity_gateway.clone());
  1147. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  1148. join_lobby(&mut ship, ClientId(1)).await;
  1149. create_room(&mut ship, ClientId(1), "room", "").await;
  1150. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1151. client: 0,
  1152. target: 0,
  1153. unknown: 0,
  1154. })))).await.unwrap();
  1155. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1156. client: 0,
  1157. target: 0,
  1158. item_id: 0x10000,
  1159. action: 0,
  1160. item_amount: 0,
  1161. meseta_amount: 0,
  1162. unknown: 0,
  1163. })))).await.unwrap();
  1164. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/sbank asdf".into()))).await.unwrap();
  1165. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1166. client: 0,
  1167. target: 0,
  1168. unknown: 0,
  1169. })))).await.unwrap();
  1170. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1171. client: 0,
  1172. target: 0,
  1173. item_id: 0x10001,
  1174. action: 0,
  1175. item_amount: 0,
  1176. meseta_amount: 0,
  1177. unknown: 0,
  1178. })))).await.unwrap();
  1179. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1180. client: 0,
  1181. target: 0,
  1182. item_id: 0x10002,
  1183. action: 0,
  1184. item_amount: 0,
  1185. meseta_amount: 0,
  1186. unknown: 0,
  1187. })))).await.unwrap();
  1188. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/sbank qwer".into()))).await.unwrap();
  1189. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1190. client: 0,
  1191. target: 0,
  1192. item_id: 0x10003,
  1193. action: 0,
  1194. item_amount: 0,
  1195. meseta_amount: 0,
  1196. unknown: 0,
  1197. })))).await.unwrap();
  1198. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/bank".into()))).await.unwrap();
  1199. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1200. client: 0,
  1201. target: 0,
  1202. item_id: 0x10004,
  1203. action: 0,
  1204. item_amount: 0,
  1205. meseta_amount: 0,
  1206. unknown: 0,
  1207. })))).await.unwrap();
  1208. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Shared(item::BankName("asdf".into()))).await.unwrap();
  1209. assert_eq!(bank_items.items.len(), 2);
  1210. bank_items.items[0].with_individual(|item| {
  1211. assert_eq!(item.id, item::ItemEntityId(2));
  1212. }).unwrap();
  1213. bank_items.items[1].with_individual(|item| {
  1214. assert_eq!(item.id, item::ItemEntityId(3));
  1215. }).unwrap();
  1216. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Shared(item::BankName("qwer".into()))).await.unwrap();
  1217. assert_eq!(bank_items.items.len(), 1);
  1218. bank_items.items[0].with_individual(|item| {
  1219. assert_eq!(item.id, item::ItemEntityId(4));
  1220. }).unwrap();
  1221. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  1222. assert_eq!(bank_items.items.len(), 2);
  1223. bank_items.items[0].with_individual(|item| {
  1224. assert_eq!(item.id, item::ItemEntityId(1));
  1225. }).unwrap();
  1226. bank_items.items[1].with_individual(|item| {
  1227. assert_eq!(item.id, item::ItemEntityId(5));
  1228. }).unwrap();
  1229. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  1230. assert_eq!(inventory_items.items.len(), 1);
  1231. inventory_items.items[0].with_individual(|item| {
  1232. assert_eq!(item.id, item::ItemEntityId(6));
  1233. }).unwrap();
  1234. }
  1235. #[async_std::test]
  1236. async fn test_deposit_meseta_into_shared_banks() {
  1237. let mut entity_gateway = InMemoryGateway::default();
  1238. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  1239. entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap();
  1240. let mut ship = standard_ship(entity_gateway.clone());
  1241. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  1242. join_lobby(&mut ship, ClientId(1)).await;
  1243. create_room(&mut ship, ClientId(1), "room", "").await;
  1244. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1245. client: 0,
  1246. target: 0,
  1247. unknown: 0,
  1248. })))).await.unwrap();
  1249. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1250. client: 0,
  1251. target: 0,
  1252. item_id: 0xFFFFFFFF,
  1253. action: 0,
  1254. item_amount: 0,
  1255. meseta_amount: 23,
  1256. unknown: 0,
  1257. })))).await.unwrap();
  1258. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/sbank asdf".into()))).await.unwrap();
  1259. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1260. client: 0,
  1261. target: 0,
  1262. unknown: 0,
  1263. })))).await.unwrap();
  1264. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1265. client: 0,
  1266. target: 0,
  1267. item_id: 0xFFFFFFFF,
  1268. action: 0,
  1269. item_amount: 0,
  1270. meseta_amount: 55,
  1271. unknown: 0,
  1272. })))).await.unwrap();
  1273. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/sbank qwer".into()))).await.unwrap();
  1274. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1275. client: 0,
  1276. target: 0,
  1277. unknown: 0,
  1278. })))).await.unwrap();
  1279. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1280. client: 0,
  1281. target: 0,
  1282. item_id: 0xFFFFFFFF,
  1283. action: 0,
  1284. item_amount: 0,
  1285. meseta_amount: 32,
  1286. unknown: 0,
  1287. })))).await.unwrap();
  1288. let meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  1289. assert_eq!(meseta.0, 300-23-55-32);
  1290. let bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  1291. assert_eq!(bank_meseta.0, 23);
  1292. let bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Shared(item::BankName("asdf".into()))).await.unwrap();
  1293. assert_eq!(bank_meseta.0, 55);
  1294. let bank_meseta = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Shared(item::BankName("qwer".into()))).await.unwrap();
  1295. assert_eq!(bank_meseta.0, 32);
  1296. }
  1297. #[async_std::test]
  1298. async fn test_withdraw_items_from_shared_banks() {
  1299. let mut entity_gateway = InMemoryGateway::default();
  1300. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  1301. let item0 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Saber).as_new()).await.unwrap();
  1302. let item1 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Buster).as_new()).await.unwrap();
  1303. let item2 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Rifle).as_new()).await.unwrap();
  1304. let item3 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Handgun).as_new()).await.unwrap();
  1305. let item4 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Autogun).as_new()).await.unwrap();
  1306. let item5 = entity_gateway.create_item(ItemBuilder::weapon(weapon::WeaponType::Calibur).as_new()).await.unwrap();
  1307. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![item0, item1]), &item::BankIdentifier::Character).await.unwrap();
  1308. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![item2, item3]), &item::BankIdentifier::Shared(item::BankName("asdf".into()))).await.unwrap();
  1309. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![item4, item5]), &item::BankIdentifier::Shared(item::BankName("qwer".into()))).await.unwrap();
  1310. let mut ship = standard_ship(entity_gateway.clone());
  1311. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  1312. join_lobby(&mut ship, ClientId(1)).await;
  1313. create_room(&mut ship, ClientId(1), "room", "").await;
  1314. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1315. client: 0,
  1316. target: 0,
  1317. unknown: 0,
  1318. })))).await.unwrap();
  1319. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1320. client: 0,
  1321. target: 0,
  1322. item_id: 0x20000, // saber (1)
  1323. action: 1,
  1324. item_amount: 0,
  1325. meseta_amount: 0,
  1326. unknown: 0,
  1327. })))).await.unwrap();
  1328. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/sbank asdf".into()))).await.unwrap();
  1329. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1330. client: 0,
  1331. target: 0,
  1332. unknown: 0,
  1333. })))).await.unwrap();
  1334. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1335. client: 0,
  1336. target: 0,
  1337. item_id: 0x810003, // handgun (4)
  1338. action: 1,
  1339. item_amount: 0,
  1340. meseta_amount: 0,
  1341. unknown: 0,
  1342. })))).await.unwrap();
  1343. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1344. client: 0,
  1345. target: 0,
  1346. item_id: 0x810004, // rifle (3)
  1347. action: 1,
  1348. item_amount: 0,
  1349. meseta_amount: 0,
  1350. unknown: 0,
  1351. })))).await.unwrap();
  1352. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/sbank qwer".into()))).await.unwrap();
  1353. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1354. client: 0,
  1355. target: 0,
  1356. item_id: 0x810006, // autogun (5)
  1357. action: 1,
  1358. item_amount: 0,
  1359. meseta_amount: 0,
  1360. unknown: 0,
  1361. })))).await.unwrap();
  1362. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Shared(item::BankName("asdf".into()))).await.unwrap();
  1363. assert_eq!(bank_items.items.len(), 0);
  1364. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Shared(item::BankName("qwer".into()))).await.unwrap();
  1365. assert_eq!(bank_items.items.len(), 1);
  1366. bank_items.items[0].with_individual(|item| {
  1367. assert_eq!(item.id, item::ItemEntityId(6));
  1368. }).unwrap();
  1369. let bank_items = entity_gateway.get_character_bank(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  1370. assert_eq!(bank_items.items.len(), 1);
  1371. bank_items.items[0].with_individual(|item| {
  1372. assert_eq!(item.id, item::ItemEntityId(2));
  1373. }).unwrap();
  1374. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  1375. assert_eq!(inventory_items.items.len(), 4);
  1376. inventory_items.items[0].with_individual(|item| {
  1377. assert_eq!(item.id, item::ItemEntityId(1));
  1378. }).unwrap();
  1379. inventory_items.items[1].with_individual(|item| {
  1380. assert_eq!(item.id, item::ItemEntityId(4));
  1381. }).unwrap();
  1382. inventory_items.items[2].with_individual(|item| {
  1383. assert_eq!(item.id, item::ItemEntityId(3));
  1384. }).unwrap();
  1385. inventory_items.items[3].with_individual(|item| {
  1386. assert_eq!(item.id, item::ItemEntityId(5));
  1387. }).unwrap();
  1388. }
  1389. #[async_std::test]
  1390. async fn test_withdraw_meseta_from_shared_banks() {
  1391. let mut entity_gateway = InMemoryGateway::default();
  1392. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  1393. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Character, item::Meseta(300)).await.unwrap();
  1394. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Shared(item::BankName("asdf".into())), item::Meseta(300)).await.unwrap();
  1395. entity_gateway.set_bank_meseta(&char1.id, &item::BankIdentifier::Shared(item::BankName("qwer".into())), item::Meseta(300)).await.unwrap();
  1396. let mut ship = standard_ship(entity_gateway.clone());
  1397. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  1398. join_lobby(&mut ship, ClientId(1)).await;
  1399. create_room(&mut ship, ClientId(1), "room", "").await;
  1400. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1401. client: 0,
  1402. target: 0,
  1403. unknown: 0,
  1404. })))).await.unwrap();
  1405. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1406. client: 0,
  1407. target: 0,
  1408. item_id: 0xFFFFFFFF,
  1409. action: 1,
  1410. item_amount: 0,
  1411. meseta_amount: 23,
  1412. unknown: 0,
  1413. })))).await.unwrap();
  1414. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/sbank asdf".into()))).await.unwrap();
  1415. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1416. client: 0,
  1417. target: 0,
  1418. unknown: 0,
  1419. })))).await.unwrap();
  1420. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1421. client: 0,
  1422. target: 0,
  1423. item_id: 0xFFFFFFFF,
  1424. action: 1,
  1425. item_amount: 0,
  1426. meseta_amount: 55,
  1427. unknown: 0,
  1428. })))).await.unwrap();
  1429. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/sbank qwer".into()))).await.unwrap();
  1430. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1431. client: 0,
  1432. target: 0,
  1433. unknown: 0,
  1434. })))).await.unwrap();
  1435. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1436. client: 0,
  1437. target: 0,
  1438. item_id: 0xFFFFFFFF,
  1439. action: 1,
  1440. item_amount: 0,
  1441. meseta_amount: 32,
  1442. unknown: 0,
  1443. })))).await.unwrap();
  1444. ship.handle(ClientId(1), RecvShipPacket::PlayerChat(PlayerChat::new(0, "\tE/bank".into()))).await.unwrap();
  1445. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  1446. client: 0,
  1447. target: 0,
  1448. unknown: 0,
  1449. })))).await.unwrap();
  1450. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  1451. client: 0,
  1452. target: 0,
  1453. item_id: 0xFFFFFFFF,
  1454. action: 1,
  1455. item_amount: 0,
  1456. meseta_amount: 5,
  1457. unknown: 0,
  1458. })))).await.unwrap();
  1459. let meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
  1460. let bank_meseta1 = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Character).await.unwrap();
  1461. let bank_meseta2 = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Shared(item::BankName("asdf".into()))).await.unwrap();
  1462. let bank_meseta3 = entity_gateway.get_bank_meseta(&char1.id, &item::BankIdentifier::Shared(item::BankName("qwer".into()))).await.unwrap();
  1463. assert_eq!(meseta.0, 23+55+32+5);
  1464. assert_eq!(bank_meseta1.0, 300-23-5);
  1465. assert_eq!(bank_meseta2.0, 300-55);
  1466. assert_eq!(bank_meseta3.0, 300-32);
  1467. }