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.

738 lines
27 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
  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. modifiers: Vec::new(),
  150. }
  151. ),
  152. location: item::ItemLocation::Inventory {
  153. character_id: char1.id,
  154. }
  155. }).await.unwrap());
  156. }
  157. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  158. char2.meseta = 300;
  159. entity_gateway.save_character(&char2).await.unwrap();
  160. let mut ship = Box::new(ShipServerState::builder()
  161. .gateway(entity_gateway.clone())
  162. .build());
  163. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  164. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  165. join_lobby(&mut ship, ClientId(1)).await;
  166. join_lobby(&mut ship, ClientId(2)).await;
  167. create_room(&mut ship, ClientId(1), "room", "").await;
  168. join_room(&mut ship, ClientId(2), 0).await;
  169. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  170. client: 0,
  171. target: 0,
  172. item_id: 0xFFFFFFFF,
  173. map_area: 0,
  174. room: 0,
  175. x: 0.0,
  176. z: 0.0,
  177. })))).await.unwrap().for_each(drop);
  178. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  179. client: 0,
  180. target: 0,
  181. item_id: 0xFFFFFFFF,
  182. amount: 23,
  183. })))).await.unwrap().for_each(drop);
  184. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  185. client: 0,
  186. target: 0,
  187. item_id: 0xF0000001,
  188. map_area: 0,
  189. unknown: [0; 3]
  190. })))).await.unwrap().for_each(drop);
  191. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  192. assert_eq!(inventory_items.items.len(), 30);
  193. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  194. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  195. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  196. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  197. assert!(c1.meseta == 23);
  198. assert!(c2.meseta == 277);
  199. }
  200. #[async_std::test]
  201. async fn test_pick_up_partial_stacked_item_when_inventory_is_otherwise_full() {
  202. let mut entity_gateway = InMemoryGateway::new();
  203. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  204. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  205. let mut p1_inv = Vec::new();
  206. for slot in 0..29usize {
  207. p1_inv.push(entity_gateway.create_item(
  208. item::NewItemEntity {
  209. item: item::ItemDetail::Weapon(
  210. item::weapon::Weapon {
  211. weapon: item::weapon::WeaponType::Saber,
  212. grind: 0,
  213. special: None,
  214. attrs: [None, None, None],
  215. tekked: true,
  216. modifiers: Vec::new(),
  217. }
  218. ),
  219. location: item::ItemLocation::Inventory {
  220. character_id: char1.id,
  221. }
  222. }).await.unwrap().into());
  223. }
  224. p1_inv.push(item::InventoryItemEntity::Stacked(vec![entity_gateway.create_item(
  225. item::NewItemEntity {
  226. item: item::ItemDetail::Tool(
  227. item::tool::Tool {
  228. tool: item::tool::ToolType::Monomate,
  229. }
  230. ),
  231. location: item::ItemLocation::Inventory {
  232. character_id: char1.id,
  233. }
  234. }).await.unwrap()]));
  235. let mut p2_monomates = Vec::new();
  236. p2_monomates.push(entity_gateway.create_item(
  237. item::NewItemEntity {
  238. item: item::ItemDetail::Tool(
  239. item::tool::Tool {
  240. tool: item::tool::ToolType::Monomate,
  241. }
  242. ),
  243. location: item::ItemLocation::Inventory {
  244. character_id: char2.id,
  245. }
  246. }).await.unwrap());
  247. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  248. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(vec![p2_monomates])).await.unwrap();
  249. let mut ship = Box::new(ShipServerState::builder()
  250. .gateway(entity_gateway.clone())
  251. .build());
  252. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  253. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  254. join_lobby(&mut ship, ClientId(1)).await;
  255. join_lobby(&mut ship, ClientId(2)).await;
  256. create_room(&mut ship, ClientId(1), "room", "").await;
  257. join_room(&mut ship, ClientId(2), 0).await;
  258. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  259. client: 0,
  260. target: 0,
  261. unknown1: 0,
  262. map_area: 0,
  263. item_id: 0x210000,
  264. x: 0.0,
  265. y: 0.0,
  266. z: 0.0,
  267. })))).await.unwrap().for_each(drop);
  268. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  269. client: 0,
  270. target: 0,
  271. item_id: 0x210000,
  272. map_area: 0,
  273. unknown: [0; 3]
  274. })))).await.unwrap().for_each(drop);
  275. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  276. assert_eq!(inventory_items.items.len(), 30);
  277. inventory_items.items[29].with_stacked(|items| {
  278. assert_eq!(items.len(), 2);
  279. }).unwrap();
  280. }
  281. #[async_std::test]
  282. async fn test_can_not_pick_up_item_when_inventory_full() {
  283. let mut entity_gateway = InMemoryGateway::new();
  284. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  285. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  286. let mut p1_inv = Vec::new();
  287. for slot in 0..30usize {
  288. p1_inv.push(entity_gateway.create_item(
  289. item::NewItemEntity {
  290. item: item::ItemDetail::Weapon(
  291. item::weapon::Weapon {
  292. weapon: item::weapon::WeaponType::Saber,
  293. grind: 0,
  294. special: None,
  295. attrs: [None, None, None],
  296. tekked: true,
  297. modifiers: Vec::new(),
  298. }
  299. ),
  300. location: item::ItemLocation::Inventory {
  301. character_id: char1.id,
  302. }
  303. }).await.unwrap());
  304. }
  305. let mut p2_inv = Vec::new();
  306. p2_inv.push(entity_gateway.create_item(
  307. item::NewItemEntity {
  308. item: item::ItemDetail::Weapon(
  309. item::weapon::Weapon {
  310. weapon: item::weapon::WeaponType::Handgun,
  311. grind: 0,
  312. special: None,
  313. attrs: [None, None, None],
  314. tekked: true,
  315. modifiers: Vec::new(),
  316. }
  317. ),
  318. location: item::ItemLocation::Inventory {
  319. character_id: char2.id,
  320. }
  321. }).await.unwrap());
  322. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  323. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap();
  324. let mut ship = Box::new(ShipServerState::builder()
  325. .gateway(entity_gateway.clone())
  326. .build());
  327. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  328. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  329. join_lobby(&mut ship, ClientId(1)).await;
  330. join_lobby(&mut ship, ClientId(2)).await;
  331. create_room(&mut ship, ClientId(1), "room", "").await;
  332. join_room(&mut ship, ClientId(2), 0).await;
  333. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  334. client: 0,
  335. target: 0,
  336. unknown1: 0,
  337. map_area: 0,
  338. item_id: 0x210000,
  339. x: 0.0,
  340. y: 0.0,
  341. z: 0.0,
  342. })))).await.unwrap().for_each(drop);
  343. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  344. client: 0,
  345. target: 0,
  346. item_id: 0x210000,
  347. map_area: 0,
  348. unknown: [0; 3]
  349. })))).await.unwrap().for_each(drop);
  350. let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  351. assert_eq!(p1_items.items.len(), 30);
  352. let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  353. assert_eq!(p2_items.items.len(), 0);
  354. ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  355. client: 0,
  356. target: 0,
  357. item_id: 0x210000,
  358. map_area: 0,
  359. unknown: [0; 3]
  360. })))).await.unwrap().for_each(drop);
  361. let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  362. assert_eq!(p1_items.items.len(), 30);
  363. let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  364. assert_eq!(p2_items.items.len(), 1);
  365. }
  366. #[async_std::test]
  367. async fn test_can_not_drop_more_meseta_than_is_held() {
  368. let mut entity_gateway = InMemoryGateway::new();
  369. let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  370. char1.meseta = 300;
  371. entity_gateway.save_character(&char1).await.unwrap();
  372. let mut ship = Box::new(ShipServerState::builder()
  373. .gateway(entity_gateway.clone())
  374. .build());
  375. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  376. join_lobby(&mut ship, ClientId(1)).await;
  377. create_room(&mut ship, ClientId(1), "room", "").await;
  378. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  379. client: 0,
  380. target: 0,
  381. item_id: 0xFFFFFFFF,
  382. map_area: 0,
  383. room: 0,
  384. x: 0.0,
  385. z: 0.0,
  386. })))).await.unwrap().for_each(drop);
  387. let split_attempt = ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  388. client: 0,
  389. target: 0,
  390. item_id: 0xFFFFFFFF,
  391. amount: 301,
  392. })))).await;
  393. assert!(split_attempt.is_err());
  394. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  395. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  396. assert!(c1.meseta == 300);
  397. }
  398. #[async_std::test]
  399. async fn test_pick_up_stack_that_would_exceed_stack_limit() {
  400. let mut entity_gateway = InMemoryGateway::new();
  401. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  402. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  403. let mut p1_monomates = Vec::new();
  404. for _ in 0..6usize {
  405. p1_monomates.push(entity_gateway.create_item(
  406. item::NewItemEntity {
  407. item: item::ItemDetail::Tool(
  408. item::tool::Tool {
  409. tool: item::tool::ToolType::Monomate,
  410. }
  411. ),
  412. location: item::ItemLocation::Inventory {
  413. character_id: char1.id,
  414. }
  415. }).await.unwrap());
  416. }
  417. let mut p2_monomates = Vec::new();
  418. for _ in 0..6usize {
  419. p2_monomates.push(entity_gateway.create_item(
  420. item::NewItemEntity {
  421. item: item::ItemDetail::Tool(
  422. item::tool::Tool {
  423. tool: item::tool::ToolType::Monomate,
  424. }
  425. ),
  426. location: item::ItemLocation::Inventory {
  427. character_id: char2.id,
  428. }
  429. }).await.unwrap());
  430. }
  431. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![p1_monomates])).await.unwrap();
  432. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(vec![p2_monomates])).await.unwrap();
  433. let mut ship = Box::new(ShipServerState::builder()
  434. .gateway(entity_gateway.clone())
  435. .build());
  436. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  437. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  438. join_lobby(&mut ship, ClientId(1)).await;
  439. join_lobby(&mut ship, ClientId(2)).await;
  440. create_room(&mut ship, ClientId(1), "room", "").await;
  441. join_room(&mut ship, ClientId(2), 0).await;
  442. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  443. client: 0,
  444. target: 0,
  445. unknown1: 0,
  446. map_area: 0,
  447. item_id: 0x210000,
  448. x: 0.0,
  449. y: 0.0,
  450. z: 0.0,
  451. })))).await.unwrap().for_each(drop);
  452. let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  453. client: 0,
  454. target: 0,
  455. item_id: 0x210000,
  456. map_area: 0,
  457. unknown: [0; 3]
  458. })))).await.unwrap().collect::<Vec<_>>();
  459. assert!(packets.len() == 0);
  460. let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  461. assert_eq!(p1_items.items.len(), 1);
  462. p1_items.items[0].with_stacked(|items| {
  463. assert_eq!(items.len(), 6);
  464. }).unwrap();
  465. let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  466. assert_eq!(p2_items.items.len(), 0);
  467. }
  468. #[async_std::test]
  469. async fn test_can_not_pick_up_meseta_when_full() {
  470. let mut entity_gateway = InMemoryGateway::new();
  471. let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  472. let (user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  473. char1.meseta = 999999;
  474. entity_gateway.save_character(&char1).await.unwrap();
  475. char2.meseta = 300;
  476. entity_gateway.save_character(&char2).await.unwrap();
  477. let mut ship = Box::new(ShipServerState::builder()
  478. .gateway(entity_gateway.clone())
  479. .build());
  480. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  481. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  482. join_lobby(&mut ship, ClientId(1)).await;
  483. join_lobby(&mut ship, ClientId(2)).await;
  484. create_room(&mut ship, ClientId(1), "room", "").await;
  485. join_room(&mut ship, ClientId(2), 0).await;
  486. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  487. client: 0,
  488. target: 0,
  489. item_id: 0xFFFFFFFF,
  490. map_area: 0,
  491. room: 0,
  492. x: 0.0,
  493. z: 0.0,
  494. })))).await.unwrap().for_each(drop);
  495. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  496. client: 0,
  497. target: 0,
  498. item_id: 0xFFFFFFFF,
  499. amount: 23,
  500. })))).await.unwrap().for_each(drop);
  501. let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  502. client: 0,
  503. target: 0,
  504. item_id: 0xF0000001,
  505. map_area: 0,
  506. unknown: [0; 3]
  507. })))).await.unwrap().collect::<Vec<_>>();
  508. assert!(packets.len() == 0);
  509. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  510. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  511. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  512. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  513. assert!(c1.meseta == 999999);
  514. assert!(c2.meseta == 277);
  515. }
  516. #[async_std::test]
  517. async fn test_meseta_caps_at_999999_when_trying_to_pick_up_more() {
  518. let mut entity_gateway = InMemoryGateway::new();
  519. let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  520. let (user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  521. char1.meseta = 999998;
  522. entity_gateway.save_character(&char1).await.unwrap();
  523. char2.meseta = 300;
  524. entity_gateway.save_character(&char2).await.unwrap();
  525. let mut ship = Box::new(ShipServerState::builder()
  526. .gateway(entity_gateway.clone())
  527. .build());
  528. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  529. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  530. join_lobby(&mut ship, ClientId(1)).await;
  531. join_lobby(&mut ship, ClientId(2)).await;
  532. create_room(&mut ship, ClientId(1), "room", "").await;
  533. join_room(&mut ship, ClientId(2), 0).await;
  534. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  535. client: 0,
  536. target: 0,
  537. item_id: 0xFFFFFFFF,
  538. map_area: 0,
  539. room: 0,
  540. x: 0.0,
  541. z: 0.0,
  542. })))).await.unwrap().for_each(drop);
  543. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  544. client: 0,
  545. target: 0,
  546. item_id: 0xFFFFFFFF,
  547. amount: 23,
  548. })))).await.unwrap().for_each(drop);
  549. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  550. client: 0,
  551. target: 0,
  552. item_id: 0xF0000001,
  553. map_area: 0,
  554. unknown: [0; 3]
  555. })))).await.unwrap().for_each(drop);
  556. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  557. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  558. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  559. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  560. assert!(c1.meseta == 999999);
  561. assert!(c2.meseta == 277);
  562. }
  563. #[async_std::test]
  564. async fn test_player_drops_partial_stack_and_other_player_picks_it_up() {
  565. let mut entity_gateway = InMemoryGateway::new();
  566. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  567. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  568. let mut monomates = Vec::new();
  569. for _ in 0..5usize {
  570. monomates.push(entity_gateway.create_item(
  571. item::NewItemEntity {
  572. item: item::ItemDetail::Tool(
  573. item::tool::Tool {
  574. tool: item::tool::ToolType::Monomate,
  575. }
  576. ),
  577. location: item::ItemLocation::Inventory {
  578. character_id: char1.id,
  579. }
  580. }).await.unwrap());
  581. }
  582. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
  583. let mut ship = Box::new(ShipServerState::builder()
  584. .gateway(entity_gateway.clone())
  585. .build());
  586. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  587. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  588. join_lobby(&mut ship, ClientId(1)).await;
  589. join_lobby(&mut ship, ClientId(2)).await;
  590. create_room(&mut ship, ClientId(1), "room", "").await;
  591. join_room(&mut ship, ClientId(2), 0).await;
  592. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  593. client: 0,
  594. target: 0,
  595. item_id: 0x10000,
  596. map_area: 0,
  597. room: 0,
  598. x: 0.0,
  599. z: 0.0,
  600. })))).await.unwrap().for_each(drop);
  601. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  602. client: 0,
  603. target: 0,
  604. item_id: 0x10000,
  605. amount: 2,
  606. })))).await.unwrap().for_each(drop);
  607. ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  608. client: 0,
  609. target: 0,
  610. item_id: 0xF0000001,
  611. map_area: 0,
  612. unknown: [0; 3]
  613. })))).await.unwrap().for_each(drop);
  614. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  615. assert_eq!(inventory_items.items.len(), 1);
  616. inventory_items.items[0].with_stacked(|items| {
  617. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  618. vec![item::ItemEntityId(3), item::ItemEntityId(4), item::ItemEntityId(5)]);
  619. }).unwrap();
  620. let inventory_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  621. assert_eq!(inventory_items.items.len(), 1);
  622. inventory_items.items[0].with_stacked(|items| {
  623. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  624. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  625. }).unwrap();
  626. }