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.

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