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.

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