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.

776 lines
28 KiB

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