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.

769 lines
28 KiB

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
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
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 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
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
4 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
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 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
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_individual_item() {
  12. let mut entity_gateway = InMemoryGateway::default();
  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_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").await;
  73. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").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").await;
  138. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").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").await;
  187. let (user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").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. }
  200. ),
  201. }).await.unwrap());
  202. }
  203. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  204. char2.meseta = 300;
  205. entity_gateway.save_character(&char2).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 characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  240. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  241. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  242. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  243. assert!(c1.meseta == 23);
  244. assert!(c2.meseta == 277);
  245. }
  246. #[async_std::test]
  247. async fn test_pick_up_partial_stacked_item_when_inventory_is_otherwise_full() {
  248. let mut entity_gateway = InMemoryGateway::default();
  249. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  250. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  251. let mut p1_inv = Vec::new();
  252. for slot in 0..29usize {
  253. p1_inv.push(entity_gateway.create_item(
  254. item::NewItemEntity {
  255. item: item::ItemDetail::Weapon(
  256. item::weapon::Weapon {
  257. weapon: item::weapon::WeaponType::Saber,
  258. grind: 0,
  259. special: None,
  260. attrs: [None, None, None],
  261. tekked: true,
  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").await;
  321. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").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. }
  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. }
  348. ),
  349. }).await.unwrap());
  350. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  351. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap();
  352. let mut ship = Box::new(ShipServerState::builder()
  353. .gateway(entity_gateway.clone())
  354. .build());
  355. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  356. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  357. join_lobby(&mut ship, ClientId(1)).await;
  358. join_lobby(&mut ship, ClientId(2)).await;
  359. create_room(&mut ship, ClientId(1), "room", "").await;
  360. join_room(&mut ship, ClientId(2), 0).await;
  361. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  362. client: 0,
  363. target: 0,
  364. unknown1: 0,
  365. map_area: 0,
  366. item_id: 0x210000,
  367. x: 0.0,
  368. y: 0.0,
  369. z: 0.0,
  370. })))).await.unwrap().for_each(drop);
  371. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  372. client: 0,
  373. target: 0,
  374. item_id: 0x210000,
  375. map_area: 0,
  376. unknown: [0; 3]
  377. })))).await.unwrap().for_each(drop);
  378. let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  379. assert_eq!(p1_items.items.len(), 30);
  380. let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  381. assert_eq!(p2_items.items.len(), 0);
  382. ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  383. client: 0,
  384. target: 0,
  385. item_id: 0x210000,
  386. map_area: 0,
  387. unknown: [0; 3]
  388. })))).await.unwrap().for_each(drop);
  389. let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  390. assert_eq!(p1_items.items.len(), 30);
  391. let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  392. assert_eq!(p2_items.items.len(), 1);
  393. }
  394. #[async_std::test]
  395. async fn test_can_not_drop_more_meseta_than_is_held() {
  396. let mut entity_gateway = InMemoryGateway::default();
  397. let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  398. char1.meseta = 300;
  399. entity_gateway.save_character(&char1).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 characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  423. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  424. assert!(c1.meseta == 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").await;
  430. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").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, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  494. let (user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  495. char1.meseta = 999999;
  496. entity_gateway.save_character(&char1).await.unwrap();
  497. char2.meseta = 300;
  498. entity_gateway.save_character(&char2).await.unwrap();
  499. let mut ship = Box::new(ShipServerState::builder()
  500. .gateway(entity_gateway.clone())
  501. .build());
  502. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  503. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  504. join_lobby(&mut ship, ClientId(1)).await;
  505. join_lobby(&mut ship, ClientId(2)).await;
  506. create_room(&mut ship, ClientId(1), "room", "").await;
  507. join_room(&mut ship, ClientId(2), 0).await;
  508. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  509. client: 0,
  510. target: 0,
  511. item_id: 0xFFFFFFFF,
  512. map_area: 0,
  513. room: 0,
  514. x: 0.0,
  515. z: 0.0,
  516. })))).await.unwrap().for_each(drop);
  517. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  518. client: 0,
  519. target: 0,
  520. item_id: 0xFFFFFFFF,
  521. amount: 23,
  522. })))).await.unwrap().for_each(drop);
  523. let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  524. client: 0,
  525. target: 0,
  526. item_id: 0x00810001,
  527. map_area: 0,
  528. unknown: [0; 3]
  529. })))).await.unwrap().collect::<Vec<_>>();
  530. assert!(packets.len() == 0);
  531. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  532. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  533. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  534. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  535. assert!(c1.meseta == 999999);
  536. assert!(c2.meseta == 277);
  537. }
  538. #[async_std::test]
  539. async fn test_meseta_caps_at_999999_when_trying_to_pick_up_more() {
  540. let mut entity_gateway = InMemoryGateway::default();
  541. let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  542. let (user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  543. char1.meseta = 999998;
  544. entity_gateway.save_character(&char1).await.unwrap();
  545. char2.meseta = 300;
  546. entity_gateway.save_character(&char2).await.unwrap();
  547. let mut ship = Box::new(ShipServerState::builder()
  548. .gateway(entity_gateway.clone())
  549. .build());
  550. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  551. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  552. join_lobby(&mut ship, ClientId(1)).await;
  553. join_lobby(&mut ship, ClientId(2)).await;
  554. create_room(&mut ship, ClientId(1), "room", "").await;
  555. join_room(&mut ship, ClientId(2), 0).await;
  556. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  557. client: 0,
  558. target: 0,
  559. item_id: 0xFFFFFFFF,
  560. map_area: 0,
  561. room: 0,
  562. x: 0.0,
  563. z: 0.0,
  564. })))).await.unwrap().for_each(drop);
  565. ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  566. client: 0,
  567. target: 0,
  568. item_id: 0xFFFFFFFF,
  569. amount: 23,
  570. })))).await.unwrap().for_each(drop);
  571. ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  572. client: 0,
  573. target: 0,
  574. item_id: 0x00810001,
  575. map_area: 0,
  576. unknown: [0; 3]
  577. })))).await.unwrap().for_each(drop);
  578. let characters1 = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  579. let c1 = characters1.get(0).as_ref().unwrap().as_ref().unwrap();
  580. let characters2 = entity_gateway.get_characters_by_user(&user2).await.unwrap();
  581. let c2 = characters2.get(0).as_ref().unwrap().as_ref().unwrap();
  582. assert!(c1.meseta == 999999);
  583. assert!(c2.meseta == 277);
  584. }
  585. #[async_std::test]
  586. async fn test_player_drops_partial_stack_and_other_player_picks_it_up() {
  587. let mut entity_gateway = InMemoryGateway::default();
  588. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  589. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  590. let mut monomates = Vec::new();
  591. for _ in 0..5usize {
  592. monomates.push(entity_gateway.create_item(
  593. item::NewItemEntity {
  594. item: item::ItemDetail::Tool(
  595. item::tool::Tool {
  596. tool: item::tool::ToolType::Monomate,
  597. }
  598. ),
  599. }).await.unwrap());
  600. }
  601. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
  602. let mut ship = Box::new(ShipServerState::builder()
  603. .gateway(entity_gateway.clone())
  604. .build());
  605. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  606. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  607. join_lobby(&mut ship, ClientId(1)).await;
  608. join_lobby(&mut ship, ClientId(2)).await;
  609. create_room(&mut ship, ClientId(1), "room", "").await;
  610. join_room(&mut ship, ClientId(2), 0).await;
  611. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
  612. client: 0,
  613. target: 0,
  614. item_id: 0x10000,
  615. map_area: 0,
  616. room: 0,
  617. x: 0.0,
  618. z: 0.0,
  619. })))).await.unwrap().for_each(drop);
  620. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
  621. client: 0,
  622. target: 0,
  623. item_id: 0x10000,
  624. amount: 2,
  625. })))).await.unwrap().for_each(drop);
  626. ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  627. client: 0,
  628. target: 0,
  629. item_id: 0x00810001,
  630. map_area: 0,
  631. unknown: [0; 3]
  632. })))).await.unwrap().for_each(drop);
  633. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  634. assert_eq!(inventory_items.items.len(), 1);
  635. inventory_items.items[0].with_stacked(|items| {
  636. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  637. vec![item::ItemEntityId(3), item::ItemEntityId(4), item::ItemEntityId(5)]);
  638. }).unwrap();
  639. let inventory_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  640. assert_eq!(inventory_items.items.len(), 1);
  641. inventory_items.items[0].with_stacked(|items| {
  642. assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
  643. vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
  644. }).unwrap();
  645. }