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.

736 lines
25 KiB

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