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.

777 lines
28 KiB

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