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.

206 lines
9.3 KiB

1 year ago
  1. use elseware::common::serverstate::{ClientId, ServerState};
  2. use elseware::entity::gateway::InMemoryGateway;
  3. use elseware::ship::ship::{ShipServerState, SendShipPacket, RecvShipPacket};
  4. use elseware::ship::monster::MonsterType;
  5. use elseware::ship::drops::{DropTable, MonsterDropStats, MonsterDropType};
  6. use elseware::ship::drops::rare_drop_table::{RareDropTable, RareDropRate, RareDropItem};
  7. use elseware::ship::map::{Maps, MapVariant, MapArea, MapVariantMode, MapEnemy};
  8. use elseware::entity::item::weapon::WeaponType;
  9. use libpso::packet::ship::*;
  10. use libpso::packet::messages::*;
  11. #[path = "common.rs"]
  12. mod common;
  13. use common::*;
  14. #[async_std::test]
  15. async fn test_enemy_drops_item() {
  16. let mut entity_gateway = InMemoryGateway::default();
  17. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  18. let mut ship = Box::new(ShipServerState::builder()
  19. .gateway(entity_gateway.clone())
  20. .map_builder(Box::new(|_room_mode, _event| {
  21. Maps::new(
  22. vec![MapVariant::new(MapArea::Forest2, MapVariantMode::Online)],
  23. vec![Some(MapEnemy::new(MonsterType::Hildebear, MapArea::Forest2))],
  24. Vec::new(),
  25. )
  26. }))
  27. .drop_table_builder(Box::new(|episode, difficulty, section_id| {
  28. DropTable::builder()
  29. .monster_stat(MonsterType::Hildebear, MonsterDropStats {
  30. dar: 100,
  31. drop_type: MonsterDropType::Weapon,
  32. min_meseta: 0,
  33. max_meseta: 0,
  34. })
  35. .rare_table(RareDropTable::builder()
  36. .rate(MonsterType::Hildebear, RareDropRate {
  37. rate: 1.0,
  38. item: RareDropItem::Weapon(WeaponType::DarkFlow)
  39. })
  40. .build(episode, difficulty, section_id))
  41. .build(episode, difficulty, section_id)
  42. }))
  43. .build());
  44. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  45. join_lobby(&mut ship, ClientId(1)).await;
  46. create_room(&mut ship, ClientId(1), "room", "").await;
  47. let pkt = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::RequestItem(RequestItem {
  48. client: 0,
  49. target: 0,
  50. map_area: 2,
  51. pt_index: 0,
  52. enemy_id: 0,
  53. x: 0.0,
  54. z: 0.0,
  55. y: 0.0,
  56. })))).await.unwrap();
  57. match &pkt[0].1 {
  58. SendShipPacket::Message(Message{msg: GameMessage::ItemDrop(item_drop)}) => {
  59. assert_eq!(item_drop.item_id, 0x810001);
  60. assert_eq!(item_drop.item_bytes[1], 0x9D);
  61. },
  62. _ => panic!(),
  63. }
  64. }
  65. #[async_std::test]
  66. async fn test_enemy_drops_item_for_two_players() {
  67. let mut entity_gateway = InMemoryGateway::default();
  68. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  69. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  70. let mut ship = Box::new(ShipServerState::builder()
  71. .gateway(entity_gateway.clone())
  72. .map_builder(Box::new(|_room_mode, _event| {
  73. Maps::new(
  74. vec![MapVariant::new(MapArea::Forest2, MapVariantMode::Online)],
  75. vec![Some(MapEnemy::new(MonsterType::Hildebear, MapArea::Forest2))],
  76. Vec::new(),
  77. )
  78. }))
  79. .drop_table_builder(Box::new(|episode, difficulty, section_id| {
  80. DropTable::builder()
  81. .monster_stat(MonsterType::Hildebear, MonsterDropStats {
  82. dar: 100,
  83. drop_type: MonsterDropType::Weapon,
  84. min_meseta: 0,
  85. max_meseta: 0,
  86. })
  87. .rare_table(RareDropTable::builder()
  88. .rate(MonsterType::Hildebear, RareDropRate {
  89. rate: 1.0,
  90. item: RareDropItem::Weapon(WeaponType::DarkFlow)
  91. })
  92. .build(episode, difficulty, section_id))
  93. .build(episode, difficulty, section_id)
  94. }))
  95. .build());
  96. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  97. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  98. join_lobby(&mut ship, ClientId(1)).await;
  99. join_lobby(&mut ship, ClientId(2)).await;
  100. create_room(&mut ship, ClientId(1), "room", "").await;
  101. join_room(&mut ship, ClientId(2), 0).await;
  102. let pkt = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::RequestItem(RequestItem {
  103. client: 0,
  104. target: 0,
  105. map_area: 2,
  106. pt_index: 0,
  107. enemy_id: 0,
  108. x: 0.0,
  109. z: 0.0,
  110. y: 0.0,
  111. })))).await.unwrap();
  112. assert_eq!(pkt[0].0, ClientId(1));
  113. match &pkt[0].1 {
  114. SendShipPacket::Message(Message{msg: GameMessage::ItemDrop(item_drop)}) => {
  115. assert_eq!(item_drop.item_id, 0x810001);
  116. assert_eq!(item_drop.item_bytes[1], 0x9D);
  117. },
  118. _ => panic!(),
  119. }
  120. assert_eq!(pkt[1].0, ClientId(2));
  121. match &pkt[1].1 {
  122. SendShipPacket::Message(Message{msg: GameMessage::ItemDrop(item_drop)}) => {
  123. assert_eq!(item_drop.item_id, 0x810002);
  124. assert_eq!(item_drop.item_bytes[1], 0x9D);
  125. },
  126. _ => panic!(),
  127. }
  128. }
  129. #[async_std::test]
  130. async fn test_enemy_drops_item_for_two_players_and_pick_up() {
  131. let mut entity_gateway = InMemoryGateway::default();
  132. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  133. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  134. let mut ship = Box::new(ShipServerState::builder()
  135. .gateway(entity_gateway.clone())
  136. .map_builder(Box::new(|_room_mode, _event| {
  137. Maps::new(
  138. vec![MapVariant::new(MapArea::Forest2, MapVariantMode::Online)],
  139. vec![Some(MapEnemy::new(MonsterType::Hildebear, MapArea::Forest2))],
  140. Vec::new(),
  141. )
  142. }))
  143. .drop_table_builder(Box::new(|episode, difficulty, section_id| {
  144. DropTable::builder()
  145. .monster_stat(MonsterType::Hildebear, MonsterDropStats {
  146. dar: 100,
  147. drop_type: MonsterDropType::Weapon,
  148. min_meseta: 0,
  149. max_meseta: 0,
  150. })
  151. .rare_table(RareDropTable::builder()
  152. .rate(MonsterType::Hildebear, RareDropRate {
  153. rate: 1.0,
  154. item: RareDropItem::Weapon(WeaponType::DarkFlow)
  155. })
  156. .build(episode, difficulty, section_id))
  157. .build(episode, difficulty, section_id)
  158. }))
  159. .build());
  160. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  161. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  162. join_lobby(&mut ship, ClientId(1)).await;
  163. join_lobby(&mut ship, ClientId(2)).await;
  164. create_room(&mut ship, ClientId(1), "room", "").await;
  165. join_room(&mut ship, ClientId(2), 0).await;
  166. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::RequestItem(RequestItem {
  167. client: 0,
  168. target: 0,
  169. map_area: 2,
  170. pt_index: 0,
  171. enemy_id: 0,
  172. x: 0.0,
  173. z: 0.0,
  174. y: 0.0,
  175. })))).await.unwrap();
  176. ship.handle(ClientId(2), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  177. client: 0,
  178. target: 0,
  179. item_id: 0x810002,
  180. map_area: 0,
  181. unknown: [0; 3]
  182. })))).await.unwrap();
  183. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  184. client: 0,
  185. target: 0,
  186. item_id: 0x810001,
  187. map_area: 0,
  188. unknown: [0; 3]
  189. })))).await.unwrap();
  190. }