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.

272 lines
9.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
  1. /* TODO:
  2. 4. test unsealing item:
  3. - client item id does not change
  4. - unsealed item no longer has kill counter
  5. 5. test reject unsealing item if not enough kills (can this even happen?)
  6. */
  7. use elseware::common::serverstate::{ClientId, ServerState};
  8. use elseware::entity::gateway::{EntityGateway, InMemoryGateway};
  9. use elseware::ship::ship::{ShipServerState, RecvShipPacket, SendShipPacket};
  10. use elseware::entity::character::SectionID;
  11. use elseware::ship::room::Difficulty;
  12. use elseware::ship::monster::MonsterType;
  13. use elseware::entity::item;
  14. use libpso::packet::ship::*;
  15. use libpso::packet::messages::*;
  16. #[path = "common.rs"]
  17. mod common;
  18. use common::*;
  19. #[async_std::test]
  20. async fn test_sjs_drops_with_kill_counter() {
  21. let mut entity_gateway = InMemoryGateway::default();
  22. let (_user1, mut char1) = new_user_character_with_sid(&mut entity_gateway, "a1", "a", SectionID::Skyly).await;
  23. char1.exp = 80000000;
  24. entity_gateway.save_character(&char1).await.unwrap();
  25. let mut ship = Box::new(ShipServerState::builder()
  26. .gateway(entity_gateway.clone())
  27. .build());
  28. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  29. join_lobby(&mut ship, ClientId(1)).await;
  30. create_ep2_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Ultimate).await;
  31. let room = ship.blocks.0[0].rooms[0].as_mut().unwrap();
  32. room.toggle_redbox_mode(); // enable redbox mode
  33. let gigue_id = room.maps.get_enemy_id_by_monster_type(MonsterType::GiGue).unwrap();
  34. let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::RequestItem(RequestItem {
  35. client: 0,
  36. target: 0,
  37. map_area: 9, // seaside
  38. pt_index: 55, // gigue ? (taken from ingame logs)
  39. enemy_id: gigue_id,
  40. x: 0.0,
  41. y: 0.0,
  42. z: 0.0,
  43. })))).await.unwrap().collect::<Vec<_>>(); // this should return 1 packet (ItemDrop)?
  44. assert!(packets.len() == 1);
  45. match &packets[0].1 {
  46. SendShipPacket::Message(Message {msg: GameMessage::ItemDrop(item_drop)}) => {
  47. assert_eq!(item_drop.item_bytes[10], 0x80)
  48. }
  49. _ => panic!("SJS didn't drop with the expected value! attr[2] should be 0x80 (128) for 0 kills")
  50. }
  51. }
  52. #[async_std::test]
  53. async fn test_other_weapons_drop_without_kill_counter() {
  54. let mut entity_gateway = InMemoryGateway::default();
  55. let (_user1, mut char1) = new_user_character_with_sid(&mut entity_gateway, "a1", "a", SectionID::Skyly).await;
  56. char1.exp = 80000000;
  57. entity_gateway.save_character(&char1).await.unwrap();
  58. let mut ship = Box::new(ShipServerState::builder()
  59. .gateway(entity_gateway.clone())
  60. .build());
  61. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  62. join_lobby(&mut ship, ClientId(1)).await;
  63. create_ep2_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Ultimate).await;
  64. let room = ship.blocks.0[0].rooms[0].as_mut().unwrap();
  65. room.toggle_redbox_mode(); // enable redbox mode
  66. let enemy_id = room.maps.get_enemy_id_by_monster_type(MonsterType::Hildebear).unwrap();
  67. let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::RequestItem(RequestItem {
  68. client: 0,
  69. target: 0,
  70. map_area: 1, // temple alpha
  71. pt_index: 0, // TODO: this is going to break if pt_index ever gets properly used
  72. enemy_id: enemy_id,
  73. x: 0.0,
  74. y: 0.0,
  75. z: 0.0,
  76. })))).await.unwrap().collect::<Vec<_>>();
  77. assert!(packets.len() == 1);
  78. match &packets[0].1 {
  79. SendShipPacket::Message(Message {msg: GameMessage::ItemDrop(item_drop)}) => {
  80. assert_ne!(item_drop.item_bytes[10], 0x80)
  81. }
  82. _ => panic!("Weapon didn't drop with the expected value! attr[2] should be less than 0x80 (128) because it shouldn't have a kill counter!")
  83. }
  84. }
  85. #[async_std::test]
  86. async fn test_all_equipped_kill_counters_increase_per_kill() {
  87. let mut entity_gateway = InMemoryGateway::default();
  88. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  89. let mut ship = Box::new(ShipServerState::builder()
  90. .gateway(entity_gateway.clone())
  91. .build());
  92. let mut p1_inv = Vec::new();
  93. p1_inv.push(entity_gateway.create_item(
  94. item::NewItemEntity {
  95. item: item::ItemDetail::Weapon(
  96. item::weapon::Weapon {
  97. weapon: item::weapon::WeaponType::SealedJSword,
  98. grind: 0,
  99. special: None,
  100. attrs: [None,
  101. None,
  102. None,],
  103. tekked: true,
  104. kills: Some(0),
  105. }
  106. ),
  107. }).await.unwrap());
  108. p1_inv.push(entity_gateway.create_item(
  109. item::NewItemEntity {
  110. item: item::ItemDetail::Unit(
  111. item::unit::Unit {
  112. unit: item::unit::UnitType::Limiter,
  113. modifier: None,
  114. kills: Some(0),
  115. }
  116. ),
  117. }).await.unwrap());
  118. let equipped = item::EquippedEntity {
  119. weapon: Some(p1_inv[0].id),
  120. armor: None,
  121. shield: None,
  122. unit: [Some(p1_inv[1].id), None, None, None],
  123. mag: None,
  124. };
  125. entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
  126. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  127. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  128. join_lobby(&mut ship, ClientId(1)).await;
  129. create_room(&mut ship, ClientId(1), "room", "").await;
  130. let enemy_id = {
  131. let room = ship.blocks.0[0].rooms[0].as_ref().unwrap();
  132. let enemy_id = (0..).filter_map(|i| {
  133. room.maps.enemy_by_id(i).ok().and_then(|enemy| {
  134. if enemy.monster == MonsterType::Booma {
  135. Some(i)
  136. }
  137. else {
  138. None
  139. }
  140. })
  141. }).next().unwrap();
  142. enemy_id
  143. };
  144. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::KillMonster(KillMonster{
  145. client: enemy_id as u8,
  146. target: 16,
  147. map_area: 1,
  148. data: [8,0],
  149. })))).await.unwrap().for_each(drop);
  150. let equipped_items = entity_gateway.get_character_equips(&char1.id).await.unwrap();
  151. let inventory = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  152. let w = inventory.items.iter().find(|x| x.individual().unwrap().id == equipped_items.weapon.unwrap()).unwrap().individual().unwrap();
  153. let u = inventory.items.iter().find(|x| x.individual().unwrap().id == equipped_items.unit[0].unwrap()).unwrap().individual().unwrap();
  154. assert!(w.item.as_client_bytes()[11] == 1);
  155. assert!(u.item.as_client_bytes()[11] == 1);
  156. }
  157. #[async_std::test]
  158. async fn test_non_equipped_kill_counter_does_not_increase() {
  159. let mut entity_gateway = InMemoryGateway::default();
  160. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  161. let mut ship = Box::new(ShipServerState::builder()
  162. .gateway(entity_gateway.clone())
  163. .build());
  164. let mut p1_inv = Vec::new();
  165. p1_inv.push(entity_gateway.create_item(
  166. item::NewItemEntity {
  167. item: item::ItemDetail::Weapon(
  168. item::weapon::Weapon {
  169. weapon: item::weapon::WeaponType::SealedJSword,
  170. grind: 0,
  171. special: None,
  172. attrs: [None,
  173. None,
  174. None,],
  175. tekked: true,
  176. kills: Some(0),
  177. }
  178. ),
  179. }).await.unwrap());
  180. p1_inv.push(entity_gateway.create_item(
  181. item::NewItemEntity {
  182. item: item::ItemDetail::Unit(
  183. item::unit::Unit {
  184. unit: item::unit::UnitType::Limiter,
  185. modifier: None,
  186. kills: Some(0),
  187. }
  188. ),
  189. }).await.unwrap());
  190. let equipped = item::EquippedEntity {
  191. weapon: Some(p1_inv[0].id),
  192. armor: None,
  193. shield: None,
  194. unit: [None; 4],
  195. mag: None,
  196. };
  197. entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
  198. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  199. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  200. join_lobby(&mut ship, ClientId(1)).await;
  201. create_room(&mut ship, ClientId(1), "room", "").await;
  202. let enemy_id = {
  203. let room = ship.blocks.0[0].rooms[0].as_ref().unwrap();
  204. let enemy_id = (0..).filter_map(|i| {
  205. room.maps.enemy_by_id(i).ok().and_then(|enemy| {
  206. if enemy.monster == MonsterType::Booma {
  207. Some(i)
  208. }
  209. else {
  210. None
  211. }
  212. })
  213. }).next().unwrap();
  214. enemy_id
  215. };
  216. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::KillMonster(KillMonster{
  217. client: enemy_id as u8,
  218. target: 16,
  219. map_area: 1,
  220. data: [8,0],
  221. })))).await.unwrap().for_each(drop);
  222. let equipped_items = entity_gateway.get_character_equips(&char1.id).await.unwrap();
  223. let inventory = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  224. let w = inventory.items.iter().find(|x| x.individual().unwrap().id == equipped_items.weapon.unwrap()).unwrap().individual().unwrap();
  225. let u = inventory.items.iter().find(|x| x.individual().unwrap().id == item::ItemEntityId(2)).unwrap().individual().unwrap();
  226. assert!(w.item.as_client_bytes()[11] == 1);
  227. assert!(u.item.as_client_bytes()[11] == 0);
  228. }