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.

184 lines
6.5 KiB

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