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.

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