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.

199 lines
7.2 KiB

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
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
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
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
  1. use elseware::common::serverstate::{ClientId, ServerState};
  2. use elseware::entity::gateway::{EntityGateway, InMemoryGateway};
  3. use elseware::common::leveltable::CharacterLevelTable;
  4. use elseware::ship::ship::{ShipServerState, SendShipPacket, RecvShipPacket};
  5. use elseware::ship::monster::MonsterType;
  6. use elseware::ship::location::RoomId;
  7. use libpso::packet::ship::*;
  8. use libpso::packet::messages::*;
  9. #[path = "common.rs"]
  10. mod common;
  11. use common::*;
  12. #[async_std::test]
  13. async fn test_character_gains_exp() {
  14. let mut entity_gateway = InMemoryGateway::default();
  15. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  16. let mut ship = Box::new(ShipServerState::builder()
  17. .gateway(entity_gateway.clone())
  18. .build());
  19. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  20. join_lobby(&mut ship, ClientId(1)).await;
  21. create_room(&mut ship, ClientId(1), "room", "").await;
  22. let (enemy_id, exp) = {
  23. //let room = ship.blocks.0[0].rooms.get(RoomId(0)).as_ref().unwrap();
  24. ship.blocks.0[0].rooms.with(RoomId(0), |room| Box::pin(async move {
  25. let (enemy_id, map_enemy) = (0..).filter_map(|i| {
  26. room.maps.enemy_by_id(i).map(|enemy| {
  27. (i, enemy)
  28. }).ok()
  29. }).next().unwrap();
  30. let map_enemy_stats = room.monster_stats.get(&map_enemy.monster).unwrap();
  31. (enemy_id, map_enemy_stats.exp)
  32. })).await.unwrap()
  33. };
  34. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp {
  35. client: enemy_id as u8,
  36. target: 16,
  37. enemy_id: enemy_id as u16,
  38. client_id: 0,
  39. unused: 0,
  40. last_hitter: 1,
  41. })))).await.unwrap();
  42. ship.clients.with(ClientId(1), |client| Box::pin(async move {
  43. assert!(exp == client.character.exp);
  44. })).await.unwrap();
  45. }
  46. #[async_std::test]
  47. async fn test_character_levels_up() {
  48. let mut entity_gateway = InMemoryGateway::default();
  49. let (_user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  50. char1.exp = 49;
  51. entity_gateway.save_character(&char1).await.unwrap();
  52. let mut ship = Box::new(ShipServerState::builder()
  53. .gateway(entity_gateway.clone())
  54. .build());
  55. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  56. join_lobby(&mut ship, ClientId(1)).await;
  57. create_room(&mut ship, ClientId(1), "room", "").await;
  58. let enemy_id = ship.blocks.0[0].rooms.with(RoomId(0), |room| Box::pin(async move {
  59. (0..).filter_map(|i| {
  60. room.maps.enemy_by_id(i).map(|_| {
  61. i
  62. }).ok()
  63. }).next().unwrap()
  64. })).await.unwrap();
  65. let levelup_pkt = ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp {
  66. client: enemy_id as u8,
  67. target: 16,
  68. enemy_id: enemy_id as u16,
  69. client_id: 0,
  70. unused: 0,
  71. last_hitter: 1,
  72. })))).await.unwrap();
  73. assert!(matches!(levelup_pkt[1].1, SendShipPacket::Message(Message {msg: GameMessage::PlayerLevelUp(PlayerLevelUp {lvl: 2, ..})})));
  74. let leveltable = CharacterLevelTable::default();
  75. ship.clients.with(ClientId(1), |client| Box::pin(async move {
  76. assert!(leveltable.get_level_from_exp(client.character.char_class, client.character.exp) == 2)
  77. })).await.unwrap();
  78. }
  79. #[async_std::test]
  80. async fn test_character_levels_up_multiple_times() {
  81. let mut entity_gateway = InMemoryGateway::default();
  82. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  83. let mut ship = Box::new(ShipServerState::builder()
  84. .gateway(entity_gateway.clone())
  85. .build());
  86. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  87. join_lobby(&mut ship, ClientId(1)).await;
  88. create_room(&mut ship, ClientId(1), "room", "").await;
  89. let (enemy_id, exp) = {
  90. ship.blocks.0[0].rooms.with(RoomId(0), |room| Box::pin(async move {
  91. let (enemy_id, map_enemy) = (0..).filter_map(|i| {
  92. room.maps.enemy_by_id(i).ok().and_then(|enemy| {
  93. if enemy.monster == MonsterType::DarkFalz2 {
  94. Some((i, enemy))
  95. }
  96. else {
  97. None
  98. }
  99. })
  100. }).next().unwrap();
  101. let map_enemy_stats = room.monster_stats.get(&map_enemy.monster).unwrap();
  102. (enemy_id, map_enemy_stats.exp)
  103. })).await.unwrap()
  104. };
  105. let levelup_pkt = ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp {
  106. client: enemy_id as u8,
  107. target: 16,
  108. enemy_id: enemy_id as u16,
  109. client_id: 0,
  110. unused: 0,
  111. last_hitter: 1,
  112. })))).await.unwrap();
  113. assert!(matches!(levelup_pkt[1].1, SendShipPacket::Message(Message {msg: GameMessage::PlayerLevelUp(PlayerLevelUp {lvl: 8, ..})})));
  114. ship.clients.with(ClientId(1), |client| Box::pin(async move {
  115. assert!(exp == client.character.exp);
  116. })).await.unwrap();
  117. }
  118. #[async_std::test]
  119. async fn test_one_character_gets_full_exp_and_other_attacker_gets_partial() {
  120. let mut entity_gateway = InMemoryGateway::default();
  121. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  122. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  123. let mut ship = Box::new(ShipServerState::builder()
  124. .gateway(entity_gateway.clone())
  125. .build());
  126. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  127. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  128. join_lobby(&mut ship, ClientId(1)).await;
  129. join_lobby(&mut ship, ClientId(2)).await;
  130. create_room(&mut ship, ClientId(1), "room", "").await;
  131. join_room(&mut ship, ClientId(2), 0).await;
  132. let (enemy_id, exp) = ship.blocks.0[0].rooms.with(RoomId(0), |room| Box::pin(async move {
  133. let (enemy_id, map_enemy) = (0..).filter_map(|i| {
  134. room.maps.enemy_by_id(i).ok().and_then(|enemy| {
  135. if enemy.monster == MonsterType::DarkFalz2 {
  136. Some((i, enemy))
  137. }
  138. else {
  139. None
  140. }
  141. })
  142. }).next().unwrap();
  143. let map_enemy_stats = room.monster_stats.get(&map_enemy.monster).unwrap();
  144. (enemy_id, map_enemy_stats.exp)
  145. })).await.unwrap();
  146. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp {
  147. client: enemy_id as u8,
  148. target: 16,
  149. enemy_id: enemy_id as u16,
  150. client_id: 0,
  151. unused: 0,
  152. last_hitter: 1,
  153. })))).await.unwrap();
  154. ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp {
  155. client: enemy_id as u8,
  156. target: 16,
  157. enemy_id: enemy_id as u16,
  158. client_id: 0,
  159. unused: 0,
  160. last_hitter: 0,
  161. })))).await.unwrap();
  162. ship.clients.with(ClientId(1), |client| Box::pin(async move {
  163. assert!(client.character.exp == exp);
  164. })).await.unwrap();
  165. ship.clients.with(ClientId(2), |client| Box::pin(async move {
  166. assert!(client.character.exp == (exp as f32 * 0.8) as u32);
  167. })).await.unwrap();
  168. }