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.

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