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.

734 lines
26 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. use elseware::common::serverstate::{ClientId, ServerState};
  2. use elseware::entity::gateway::{EntityGateway, InMemoryGateway};
  3. use elseware::entity::item;
  4. use elseware::ship::ship::{ShipServerState, RecvShipPacket};
  5. use libpso::packet::ship::*;
  6. use libpso::packet::messages::*;
  7. #[path = "common.rs"]
  8. mod common;
  9. use common::*;
  10. #[async_std::test]
  11. async fn test_pick_up_item_stack_of_items_already_in_inventory() {
  12. let mut entity_gateway = InMemoryGateway::new();
  13. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  14. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  15. let mut p1_monomate = Vec::new();
  16. p1_monomate.push(entity_gateway.create_item(
  17. item::NewItemEntity {
  18. item: item::ItemDetail::Tool(
  19. item::tool::Tool {
  20. tool: item::tool::ToolType::Monomate
  21. }
  22. ),
  23. location: item::ItemLocation::Inventory {
  24. character_id: char1.id,
  25. }
  26. }).await.unwrap());
  27. let mut p2_items = Vec::new();
  28. for (slot, tool) in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter().enumerate() {
  29. let mut item = Vec::new();
  30. for _ in 0..5usize {
  31. item.push(entity_gateway.create_item(
  32. item::NewItemEntity {
  33. item: item::ItemDetail::Tool(
  34. item::tool::Tool {
  35. tool: tool
  36. }
  37. ),
  38. location: item::ItemLocation::Inventory {
  39. character_id: char2.id,
  40. }
  41. }).await.unwrap());
  42. }
  43. p2_items.push(item);
  44. }
  45. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![p1_monomate])).await.unwrap();
  46. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_items)).await.unwrap();
  47. let mut ship = Box::new(ShipServerState::builder()
  48. .gateway(entity_gateway.clone())
  49. .build());
  50. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  51. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  52. join_lobby(&mut ship, ClientId(1)).await;
  53. join_lobby(&mut ship, ClientId(2)).await;
  54. create_room(&mut ship, ClientId(1), "room", "").await;
  55. join_room(&mut ship, ClientId(2), 0).await;
  56. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  57. client: 0,
  58. target: 0,
  59. unknown1: 0,
  60. map_area: 0,
  61. item_id: 0x210000,
  62. x: 0.0,
  63. y: 0.0,
  64. z: 0.0,
  65. })))).await.unwrap().for_each(drop);
  66. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  67. client: 0,
  68. target: 0,
  69. item_id: 0x210000,
  70. map_area: 0,
  71. unknown: [0; 3]
  72. })))).await.unwrap().for_each(drop);
  73. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  74. assert_eq!(inventory_items.items.len(), 1);
  75. inventory_items.items[0].with_stacked(|items| {
  76. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  77. vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3), item::ItemEntityId(4), item::ItemEntityId(5), item::ItemEntityId(6)]);
  78. assert!(items.iter().all(|item| item.item.item_type() == item::ItemType::Tool(item::tool::ToolType::Monomate)));
  79. }).unwrap();
  80. }
  81. #[async_std::test]
  82. async fn test_pick_up_item_stack_of_items_not_already_held() {
  83. let mut entity_gateway = InMemoryGateway::new();
  84. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  85. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  86. let mut p2_monomate = Vec::new();
  87. p2_monomate.push(entity_gateway.create_item(
  88. item::NewItemEntity {
  89. item: item::ItemDetail::Tool(
  90. item::tool::Tool {
  91. tool: item::tool::ToolType::Monomate
  92. }
  93. ),
  94. location: item::ItemLocation::Inventory {
  95. character_id: char2.id,
  96. }
  97. }).await.unwrap());
  98. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(vec![p2_monomate])).await.unwrap();
  99. let mut ship = Box::new(ShipServerState::builder()
  100. .gateway(entity_gateway.clone())
  101. .build());
  102. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  103. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  104. join_lobby(&mut ship, ClientId(1)).await;
  105. join_lobby(&mut ship, ClientId(2)).await;
  106. create_room(&mut ship, ClientId(1), "room", "").await;
  107. join_room(&mut ship, ClientId(2), 0).await;
  108. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  109. client: 0,
  110. target: 0,
  111. unknown1: 0,
  112. map_area: 0,
  113. item_id: 0x210000,
  114. x: 0.0,
  115. y: 0.0,
  116. z: 0.0,
  117. })))).await.unwrap().for_each(drop);
  118. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  119. client: 0,
  120. target: 0,
  121. item_id: 0x210000,
  122. map_area: 0,
  123. unknown: [0; 3]
  124. })))).await.unwrap().for_each(drop);
  125. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  126. assert_eq!(inventory_items.items.len(), 1);
  127. inventory_items.items[0].with_stacked(|items| {
  128. assert_eq!(items.len(), 1);
  129. assert_eq!(items[0].id, item::ItemEntityId(1));
  130. assert_eq!(items[0].item.item_type(), item::ItemType::Tool(item::tool::ToolType::Monomate));
  131. }).unwrap();
  132. }
  133. #[async_std::test]
  134. async fn test_pick_up_meseta_when_inventory_full() {
  135. let mut entity_gateway = InMemoryGateway::new();
  136. let (user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  137. let (user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  138. let mut p1_items = Vec::new();
  139. for _ in 0..30usize {
  140. p1_items.push(entity_gateway.create_item(
  141. item::NewItemEntity {
  142. item: item::ItemDetail::Weapon(
  143. item::weapon::Weapon {
  144. weapon: item::weapon::WeaponType::Saber,
  145. grind: 0,
  146. special: None,
  147. attrs: [None, None, None],
  148. tekked: true,
  149. }
  150. ),
  151. location: item::ItemLocation::Inventory {
  152. character_id: char1.id,
  153. }
  154. }).await.unwrap());
  155. }
  156. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  157. char2.meseta = 300;
  158. entity_gateway.save_character(&char2).await.unwrap();
  159. let mut ship = Box::new(ShipServerState::builder()
  160. .gateway(entity_gateway.clone())
  161. .build());
  162. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  163. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  164. join_lobby(&mut ship, ClientId(1)).await;
  165. join_lobby(&mut ship, ClientId(2)).await;
  166. create_room(&mut ship, ClientId(1), "room", "").await;
  167. join_room(&mut ship, ClientId(2), 0).await;
  168. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  169. client: 0,
  170. target: 0,
  171. item_id: 0xFFFFFFFF,
  172. map_area: 0,
  173. room: 0,
  174. x: 0.0,
  175. z: 0.0,
  176. })))).await.unwrap().for_each(drop);
  177. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  178. client: 0,
  179. target: 0,
  180. item_id: 0xFFFFFFFF,
  181. amount: 23,
  182. })))).await.unwrap().for_each(drop);
  183. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  184. client: 0,
  185. target: 0,
  186. item_id: 0x00810001,
  187. map_area: 0,
  188. unknown: [0; 3]
  189. })))).await.unwrap().for_each(drop);
  190. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  191. assert_eq!(inventory_items.items.len(), 30);
  192. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  193. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  194. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  195. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  196. assert!(c1.meseta == 23);
  197. assert!(c2.meseta == 277);
  198. }
  199. #[async_std::test]
  200. async fn test_pick_up_partial_stacked_item_when_inventory_is_otherwise_full() {
  201. let mut entity_gateway = InMemoryGateway::new();
  202. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  203. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  204. let mut p1_inv = Vec::new();
  205. for slot in 0..29usize {
  206. p1_inv.push(entity_gateway.create_item(
  207. item::NewItemEntity {
  208. item: item::ItemDetail::Weapon(
  209. item::weapon::Weapon {
  210. weapon: item::weapon::WeaponType::Saber,
  211. grind: 0,
  212. special: None,
  213. attrs: [None, None, None],
  214. tekked: true,
  215. }
  216. ),
  217. location: item::ItemLocation::Inventory {
  218. character_id: char1.id,
  219. }
  220. }).await.unwrap().into());
  221. }
  222. p1_inv.push(item::InventoryItemEntity::Stacked(vec![entity_gateway.create_item(
  223. item::NewItemEntity {
  224. item: item::ItemDetail::Tool(
  225. item::tool::Tool {
  226. tool: item::tool::ToolType::Monomate,
  227. }
  228. ),
  229. location: item::ItemLocation::Inventory {
  230. character_id: char1.id,
  231. }
  232. }).await.unwrap()]));
  233. let mut p2_monomates = Vec::new();
  234. p2_monomates.push(entity_gateway.create_item(
  235. item::NewItemEntity {
  236. item: item::ItemDetail::Tool(
  237. item::tool::Tool {
  238. tool: item::tool::ToolType::Monomate,
  239. }
  240. ),
  241. location: item::ItemLocation::Inventory {
  242. character_id: char2.id,
  243. }
  244. }).await.unwrap());
  245. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  246. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(vec![p2_monomates])).await.unwrap();
  247. let mut ship = Box::new(ShipServerState::builder()
  248. .gateway(entity_gateway.clone())
  249. .build());
  250. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  251. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  252. join_lobby(&mut ship, ClientId(1)).await;
  253. join_lobby(&mut ship, ClientId(2)).await;
  254. create_room(&mut ship, ClientId(1), "room", "").await;
  255. join_room(&mut ship, ClientId(2), 0).await;
  256. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  257. client: 0,
  258. target: 0,
  259. unknown1: 0,
  260. map_area: 0,
  261. item_id: 0x210000,
  262. x: 0.0,
  263. y: 0.0,
  264. z: 0.0,
  265. })))).await.unwrap().for_each(drop);
  266. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  267. client: 0,
  268. target: 0,
  269. item_id: 0x210000,
  270. map_area: 0,
  271. unknown: [0; 3]
  272. })))).await.unwrap().for_each(drop);
  273. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  274. assert_eq!(inventory_items.items.len(), 30);
  275. inventory_items.items[29].with_stacked(|items| {
  276. assert_eq!(items.len(), 2);
  277. }).unwrap();
  278. }
  279. #[async_std::test]
  280. async fn test_can_not_pick_up_item_when_inventory_full() {
  281. let mut entity_gateway = InMemoryGateway::new();
  282. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  283. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  284. let mut p1_inv = Vec::new();
  285. for slot in 0..30usize {
  286. p1_inv.push(entity_gateway.create_item(
  287. item::NewItemEntity {
  288. item: item::ItemDetail::Weapon(
  289. item::weapon::Weapon {
  290. weapon: item::weapon::WeaponType::Saber,
  291. grind: 0,
  292. special: None,
  293. attrs: [None, None, None],
  294. tekked: true,
  295. }
  296. ),
  297. location: item::ItemLocation::Inventory {
  298. character_id: char1.id,
  299. }
  300. }).await.unwrap());
  301. }
  302. let mut p2_inv = Vec::new();
  303. p2_inv.push(entity_gateway.create_item(
  304. item::NewItemEntity {
  305. item: item::ItemDetail::Weapon(
  306. item::weapon::Weapon {
  307. weapon: item::weapon::WeaponType::Handgun,
  308. grind: 0,
  309. special: None,
  310. attrs: [None, None, None],
  311. tekked: true,
  312. }
  313. ),
  314. location: item::ItemLocation::Inventory {
  315. character_id: char2.id,
  316. }
  317. }).await.unwrap());
  318. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  319. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap();
  320. let mut ship = Box::new(ShipServerState::builder()
  321. .gateway(entity_gateway.clone())
  322. .build());
  323. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  324. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  325. join_lobby(&mut ship, ClientId(1)).await;
  326. join_lobby(&mut ship, ClientId(2)).await;
  327. create_room(&mut ship, ClientId(1), "room", "").await;
  328. join_room(&mut ship, ClientId(2), 0).await;
  329. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  330. client: 0,
  331. target: 0,
  332. unknown1: 0,
  333. map_area: 0,
  334. item_id: 0x210000,
  335. x: 0.0,
  336. y: 0.0,
  337. z: 0.0,
  338. })))).await.unwrap().for_each(drop);
  339. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  340. client: 0,
  341. target: 0,
  342. item_id: 0x210000,
  343. map_area: 0,
  344. unknown: [0; 3]
  345. })))).await.unwrap().for_each(drop);
  346. let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  347. assert_eq!(p1_items.items.len(), 30);
  348. let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  349. assert_eq!(p2_items.items.len(), 0);
  350. ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  351. client: 0,
  352. target: 0,
  353. item_id: 0x210000,
  354. map_area: 0,
  355. unknown: [0; 3]
  356. })))).await.unwrap().for_each(drop);
  357. let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  358. assert_eq!(p1_items.items.len(), 30);
  359. let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  360. assert_eq!(p2_items.items.len(), 1);
  361. }
  362. #[async_std::test]
  363. async fn test_can_not_drop_more_meseta_than_is_held() {
  364. let mut entity_gateway = InMemoryGateway::new();
  365. let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  366. char1.meseta = 300;
  367. entity_gateway.save_character(&char1).await.unwrap();
  368. let mut ship = Box::new(ShipServerState::builder()
  369. .gateway(entity_gateway.clone())
  370. .build());
  371. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  372. join_lobby(&mut ship, ClientId(1)).await;
  373. create_room(&mut ship, ClientId(1), "room", "").await;
  374. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  375. client: 0,
  376. target: 0,
  377. item_id: 0xFFFFFFFF,
  378. map_area: 0,
  379. room: 0,
  380. x: 0.0,
  381. z: 0.0,
  382. })))).await.unwrap().for_each(drop);
  383. let split_attempt = ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  384. client: 0,
  385. target: 0,
  386. item_id: 0xFFFFFFFF,
  387. amount: 301,
  388. })))).await;
  389. assert!(split_attempt.is_err());
  390. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  391. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  392. assert!(c1.meseta == 300);
  393. }
  394. #[async_std::test]
  395. async fn test_pick_up_stack_that_would_exceed_stack_limit() {
  396. let mut entity_gateway = InMemoryGateway::new();
  397. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  398. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  399. let mut p1_monomates = Vec::new();
  400. for _ in 0..6usize {
  401. p1_monomates.push(entity_gateway.create_item(
  402. item::NewItemEntity {
  403. item: item::ItemDetail::Tool(
  404. item::tool::Tool {
  405. tool: item::tool::ToolType::Monomate,
  406. }
  407. ),
  408. location: item::ItemLocation::Inventory {
  409. character_id: char1.id,
  410. }
  411. }).await.unwrap());
  412. }
  413. let mut p2_monomates = Vec::new();
  414. for _ in 0..6usize {
  415. p2_monomates.push(entity_gateway.create_item(
  416. item::NewItemEntity {
  417. item: item::ItemDetail::Tool(
  418. item::tool::Tool {
  419. tool: item::tool::ToolType::Monomate,
  420. }
  421. ),
  422. location: item::ItemLocation::Inventory {
  423. character_id: char2.id,
  424. }
  425. }).await.unwrap());
  426. }
  427. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![p1_monomates])).await.unwrap();
  428. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(vec![p2_monomates])).await.unwrap();
  429. let mut ship = Box::new(ShipServerState::builder()
  430. .gateway(entity_gateway.clone())
  431. .build());
  432. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  433. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  434. join_lobby(&mut ship, ClientId(1)).await;
  435. join_lobby(&mut ship, ClientId(2)).await;
  436. create_room(&mut ship, ClientId(1), "room", "").await;
  437. join_room(&mut ship, ClientId(2), 0).await;
  438. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  439. client: 0,
  440. target: 0,
  441. unknown1: 0,
  442. map_area: 0,
  443. item_id: 0x210000,
  444. x: 0.0,
  445. y: 0.0,
  446. z: 0.0,
  447. })))).await.unwrap().for_each(drop);
  448. let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  449. client: 0,
  450. target: 0,
  451. item_id: 0x210000,
  452. map_area: 0,
  453. unknown: [0; 3]
  454. })))).await.unwrap().collect::<Vec<_>>();
  455. assert!(packets.len() == 0);
  456. let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  457. assert_eq!(p1_items.items.len(), 1);
  458. p1_items.items[0].with_stacked(|items| {
  459. assert_eq!(items.len(), 6);
  460. }).unwrap();
  461. let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  462. assert_eq!(p2_items.items.len(), 0);
  463. }
  464. #[async_std::test]
  465. async fn test_can_not_pick_up_meseta_when_full() {
  466. let mut entity_gateway = InMemoryGateway::new();
  467. let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  468. let (user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  469. char1.meseta = 999999;
  470. entity_gateway.save_character(&char1).await.unwrap();
  471. char2.meseta = 300;
  472. entity_gateway.save_character(&char2).await.unwrap();
  473. let mut ship = Box::new(ShipServerState::builder()
  474. .gateway(entity_gateway.clone())
  475. .build());
  476. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  477. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  478. join_lobby(&mut ship, ClientId(1)).await;
  479. join_lobby(&mut ship, ClientId(2)).await;
  480. create_room(&mut ship, ClientId(1), "room", "").await;
  481. join_room(&mut ship, ClientId(2), 0).await;
  482. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  483. client: 0,
  484. target: 0,
  485. item_id: 0xFFFFFFFF,
  486. map_area: 0,
  487. room: 0,
  488. x: 0.0,
  489. z: 0.0,
  490. })))).await.unwrap().for_each(drop);
  491. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  492. client: 0,
  493. target: 0,
  494. item_id: 0xFFFFFFFF,
  495. amount: 23,
  496. })))).await.unwrap().for_each(drop);
  497. let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  498. client: 0,
  499. target: 0,
  500. item_id: 0x00810001,
  501. map_area: 0,
  502. unknown: [0; 3]
  503. })))).await.unwrap().collect::<Vec<_>>();
  504. assert!(packets.len() == 0);
  505. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  506. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  507. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  508. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  509. assert!(c1.meseta == 999999);
  510. assert!(c2.meseta == 277);
  511. }
  512. #[async_std::test]
  513. async fn test_meseta_caps_at_999999_when_trying_to_pick_up_more() {
  514. let mut entity_gateway = InMemoryGateway::new();
  515. let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  516. let (user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  517. char1.meseta = 999998;
  518. entity_gateway.save_character(&char1).await.unwrap();
  519. char2.meseta = 300;
  520. entity_gateway.save_character(&char2).await.unwrap();
  521. let mut ship = Box::new(ShipServerState::builder()
  522. .gateway(entity_gateway.clone())
  523. .build());
  524. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  525. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  526. join_lobby(&mut ship, ClientId(1)).await;
  527. join_lobby(&mut ship, ClientId(2)).await;
  528. create_room(&mut ship, ClientId(1), "room", "").await;
  529. join_room(&mut ship, ClientId(2), 0).await;
  530. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  531. client: 0,
  532. target: 0,
  533. item_id: 0xFFFFFFFF,
  534. map_area: 0,
  535. room: 0,
  536. x: 0.0,
  537. z: 0.0,
  538. })))).await.unwrap().for_each(drop);
  539. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  540. client: 0,
  541. target: 0,
  542. item_id: 0xFFFFFFFF,
  543. amount: 23,
  544. })))).await.unwrap().for_each(drop);
  545. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  546. client: 0,
  547. target: 0,
  548. item_id: 0x00810001,
  549. map_area: 0,
  550. unknown: [0; 3]
  551. })))).await.unwrap().for_each(drop);
  552. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  553. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  554. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  555. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  556. assert!(c1.meseta == 999999);
  557. assert!(c2.meseta == 277);
  558. }
  559. #[async_std::test]
  560. async fn test_player_drops_partial_stack_and_other_player_picks_it_up() {
  561. let mut entity_gateway = InMemoryGateway::new();
  562. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  563. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  564. let mut monomates = Vec::new();
  565. for _ in 0..5usize {
  566. monomates.push(entity_gateway.create_item(
  567. item::NewItemEntity {
  568. item: item::ItemDetail::Tool(
  569. item::tool::Tool {
  570. tool: item::tool::ToolType::Monomate,
  571. }
  572. ),
  573. location: item::ItemLocation::Inventory {
  574. character_id: char1.id,
  575. }
  576. }).await.unwrap());
  577. }
  578. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
  579. let mut ship = Box::new(ShipServerState::builder()
  580. .gateway(entity_gateway.clone())
  581. .build());
  582. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  583. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  584. join_lobby(&mut ship, ClientId(1)).await;
  585. join_lobby(&mut ship, ClientId(2)).await;
  586. create_room(&mut ship, ClientId(1), "room", "").await;
  587. join_room(&mut ship, ClientId(2), 0).await;
  588. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  589. client: 0,
  590. target: 0,
  591. item_id: 0x10000,
  592. map_area: 0,
  593. room: 0,
  594. x: 0.0,
  595. z: 0.0,
  596. })))).await.unwrap().for_each(drop);
  597. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  598. client: 0,
  599. target: 0,
  600. item_id: 0x10000,
  601. amount: 2,
  602. })))).await.unwrap().for_each(drop);
  603. ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  604. client: 0,
  605. target: 0,
  606. item_id: 0x00810001,
  607. map_area: 0,
  608. unknown: [0; 3]
  609. })))).await.unwrap().for_each(drop);
  610. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  611. assert_eq!(inventory_items.items.len(), 1);
  612. inventory_items.items[0].with_stacked(|items| {
  613. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  614. vec![item::ItemEntityId(3), item::ItemEntityId(4), item::ItemEntityId(5)]);
  615. }).unwrap();
  616. let inventory_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  617. assert_eq!(inventory_items.items.len(), 1);
  618. inventory_items.items[0].with_stacked(|items| {
  619. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  620. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  621. }).unwrap();
  622. }