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.

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