Skip to content

Commit

Permalink
fix: rebase fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thatchinkumar2005 committed Feb 9, 2025
1 parent 6797d69 commit 4c634fd
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 86 deletions.
4 changes: 3 additions & 1 deletion src/api/attack/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ pub fn get_hut_defender_types(conn: &mut PgConnection) -> Result<Vec<DefenderDet
let mut hut_defender_array: Vec<DefenderDetails> = Vec::new();
for (i, (block_type, defender_type, prop)) in hut_defenders.enumerate() {
hut_defender_array.push(DefenderDetails {
mapSpaceId: (i + 1) as i32,
map_space_id: -1,
name: defender_type.name.clone(),
radius: prop.range,
speed: defender_type.speed,
Expand All @@ -797,6 +797,8 @@ pub fn get_hut_defender_types(conn: &mut PgConnection) -> Result<Vec<DefenderDet
path_in_current_frame: Vec::new(),
block_id: block_type.id,
level: defender_type.level,
current_health: defender_type.max_health,
max_health: defender_type.max_health,
});
log::info!("hut_defenders {:?}", i);
}
Expand Down
34 changes: 28 additions & 6 deletions src/api/challenges/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use util::{
use crate::{
api::{
attack::{
socket::{BuildingResponse, ResultType, SocketRequest, SocketResponse},
socket::{BaseItemsDamageResponse, ResultType, SocketRequest, SocketResponse},
util::{
get_attacker_types, get_bomb_types, get_hut_defender_types, GameLog, ResultResponse,
},
Expand Down Expand Up @@ -263,7 +263,7 @@ async fn challenge_socket_handler(
.iter()
.map(|defender| DefenderDetails {
block_id: defender.block_id,
mapSpaceId: defender.map_space_id,
map_space_id: defender.map_space_id,
name: defender.name.clone(),
radius: defender.radius,
speed: defender.speed,
Expand All @@ -277,6 +277,8 @@ async fn challenge_socket_handler(
target_id: None,
path_in_current_frame: Vec::new(),
level: defender.level,
current_health: defender.max_health,
max_health: defender.max_health,
})
.collect();

Expand Down Expand Up @@ -329,6 +331,7 @@ async fn challenge_socket_handler(
name: building.name.clone(),
range: building.range,
frequency: building.frequency,
level: building.level,
})
.collect();

Expand Down Expand Up @@ -426,6 +429,8 @@ async fn challenge_socket_handler(
level: defender.level,
cost: defender.cost,
name: defender.name.clone(),
defender_id: defender.map_space_id,
max_health: defender.max_health,
})
.collect();

Expand Down Expand Up @@ -511,7 +516,10 @@ async fn challenge_socket_handler(
return Err(ErrorBadRequest("Attacker Does not exist"));
}

let mut damaged_buildings: Vec<BuildingResponse> = Vec::new();
let mut damaged_base_items: BaseItemsDamageResponse = BaseItemsDamageResponse {
buildings_damaged: Vec::new(),
defenders_damaged: Vec::new(),
};

let game_log = GameLog {
g: -1,
Expand Down Expand Up @@ -651,8 +659,20 @@ async fn challenge_socket_handler(
// }
// }
else if response.result_type == ResultType::BuildingsDamaged {
damaged_buildings
.extend(response.damaged_buildings.unwrap());
damaged_base_items.buildings_damaged.extend(
response
.damaged_base_items
.clone()
.unwrap()
.buildings_damaged,
);
damaged_base_items.defenders_damaged.extend(
response
.damaged_base_items
.clone()
.unwrap()
.defenders_damaged,
);
// if util::deduct_artifacts_from_building(
// response.damaged_buildings.unwrap(),
// &mut conn,
Expand Down Expand Up @@ -768,11 +788,13 @@ async fn challenge_socket_handler(
defender_damaged: None,
hut_triggered: false,
hut_defenders: None,
damaged_buildings: None,
damaged_base_items: None,
total_damage_percentage: None,
is_sync: false,
shoot_bullets: None,
is_game_over: true,
message: Some("Connection timed out".to_string()),
companion: None,
challenge: None,
})
.unwrap();
Expand Down
15 changes: 8 additions & 7 deletions src/api/defense/shortest_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn run_shortest_paths(

pub fn run_shortest_paths_challenges(
road_data: &Vec<RoadSave>,
) -> Result<HashMap<SourceDestXY, Coords>> {
) -> Result<HashMap<SourceDestXY, Path>> {
let roads_list: Vec<(i32, i32)> = road_data
.iter()
.map(|road_save| (road_save.pos_x, road_save.pos_y))
Expand Down Expand Up @@ -154,17 +154,17 @@ pub fn run_shortest_paths_challenges(
adjacency_list.insert((road_x, road_y), neighbors);
}

let mut shortest_paths: HashMap<SourceDestXY, Coords> = HashMap::new();
let mut shortest_paths: HashMap<SourceDestXY, Path> = HashMap::new();

for (start_x, start_y) in &roads_list {
let start_node = (*start_x, *start_y);
let mut visited: HashSet<(i32, i32)> = HashSet::new();
let mut queue: VecDeque<((i32, i32), (i32, i32))> = VecDeque::new();
let mut queue: VecDeque<((i32, i32), (i32, i32), i32)> = VecDeque::new();

visited.insert(start_node);
queue.push_back((start_node, start_node));
queue.push_back((start_node, start_node, 0));

while let Some((current_node, parent_node)) = queue.pop_front() {
while let Some((current_node, parent_node, current_length)) = queue.pop_front() {
for neighbor in &adjacency_list[&current_node] {
if visited.insert(*neighbor) {
let next_hop = if start_node == parent_node {
Expand All @@ -173,7 +173,7 @@ pub fn run_shortest_paths_challenges(
parent_node
};

queue.push_back((*neighbor, next_hop));
queue.push_back((*neighbor, next_hop, current_length + 1));

shortest_paths.insert(
SourceDestXY {
Expand All @@ -182,9 +182,10 @@ pub fn run_shortest_paths_challenges(
dest_x: neighbor.0,
dest_y: neighbor.1,
},
Coords {
Path {
x: next_hop.0,
y: next_hop.1,
l: current_length + 1,
},
);
}
Expand Down
9 changes: 5 additions & 4 deletions src/api/defense/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ pub struct DefenderSave {
pub pos_x: i32,
pub pos_y: i32,
pub is_placed: bool,
pub max_health: i32,
pub current_health: i32,
}

#[derive(Deserialize, Debug, Clone, Serialize)]
Expand Down Expand Up @@ -1073,9 +1075,9 @@ pub fn fetch_attacker_types(conn: &mut PgConnection, user_id: &i32) -> Result<Ve

pub fn fetch_emp_types(conn: &mut PgConnection, user_id: &i32) -> Result<Vec<EmpType>> {
let bomb_types = emp_type::table
.inner_join(available_blocks::table)
.filter(available_blocks::user_id.eq(user_id))
.load::<(EmpType, AvailableBlocks)>(conn)
.inner_join(available_emps::table.on(available_emps::emp_type_id.eq(emp_type::id)))
.filter(available_emps::user_id.eq(&user_id))
.load::<(EmpType, AvailableEmps)>(conn)
.map_err(|err| DieselError {
table: "emp_type",
function: function!(),
Expand All @@ -1084,7 +1086,6 @@ pub fn fetch_emp_types(conn: &mut PgConnection, user_id: &i32) -> Result<Vec<Emp
.into_iter()
.map(|(emp_type, _)| emp_type)
.collect();

Ok(bomb_types)
}

Expand Down
11 changes: 7 additions & 4 deletions src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use self::{
use crate::{
api::attack::{
socket::{
ActionType, BaseItemsDamageResponse, BuildingResponse, ChallengeResponse, ResultType,
SocketRequest, SocketResponse,
ActionType, BaseItemsDamageResponse, ChallengeResponse, ResultType, SocketRequest,
SocketResponse,
},
util::{EventResponse, GameLog},
},
Expand All @@ -17,7 +17,8 @@ use crate::{
validator::util::{Coords, SourceDestXY},
};
use anyhow::{Ok, Result};
use util::{maze_place_attacker_handle, Companion, CompanionResult, MineResponse, Path};
use challenges::maze_place_attacker_handle;
use util::{Companion, CompanionResult, MineResponse, Path};

pub mod challenges;
pub mod error;
Expand Down Expand Up @@ -198,6 +199,7 @@ pub fn game_handler(
message: Some(String::from("Placed companion")),
companion: None,
shoot_bullets: None,
challenge: None,
}));
}

Expand Down Expand Up @@ -571,7 +573,8 @@ pub fn game_handler(
is_game_over: false,
shoot_bullets: None,
message: Some(String::from("Self Destructed")),
comapnion,
challenge: None,
companion: None,
};

return Some(Ok(socket_response));
Expand Down
67 changes: 3 additions & 64 deletions src/validator/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ use serde::{Deserialize, Serialize};

use super::{
challenges::{attacker_movement_challenge_handle, bomb_blast_fallguys_handle},
util::{
select_side_hut_defender, BombType, Challenge, ChallengeType, Companion, CompanionResult,
DefenderTarget, HutDefenderDetails, MazeChallenge, Path,
},
util::{Challenge, Companion, CompanionResult, DefenderTarget, Path},
};

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -119,6 +116,8 @@ impl State {
is_invalidated: false,
},
challenge,
hut_defenders_released: 0,
sentries: Vec::new(),
}
}
pub fn get_sentries(&mut self) {
Expand Down Expand Up @@ -982,66 +981,6 @@ impl State {
}
}

pub fn activate_sentry(&mut self) {
for sentry in self.sentries.iter_mut() {
let mut current_sentry_data: BuildingDetails = BuildingDetails {
map_space_id: 0,
current_hp: 0,
total_hp: 0,
artifacts_obtained: 0,
tile: Coords { x: 0, y: 0 },
width: 0,
name: "".to_string(),
range: 0,
frequency: 0,
block_id: 0,
level: 0,
};
for building in self.buildings.iter() {
if building.map_space_id == sentry.building_data.map_space_id {
current_sentry_data = building.clone();
}
}

if current_sentry_data.current_hp > 0 {
//for attacker
let attacker_pos = self.attacker.as_ref().unwrap().attacker_pos;
let companion_pos = self.companion.as_ref().unwrap().companion_pos;
let companion_health = self.companion.as_ref().unwrap().companion_health;
let attacker_health = self.attacker.as_ref().unwrap().attacker_health;
let prev_state = sentry.is_sentry_activated;
let is_attacker_in_range = (sentry.building_data.tile.x - attacker_pos.x).abs()
+ (sentry.building_data.tile.y - attacker_pos.y).abs()
<= sentry.building_data.range
&& attacker_health > 0;
let is_companion_in_range = (sentry.building_data.tile.x - companion_pos.x).abs()
+ (sentry.building_data.tile.y - companion_pos.y).abs()
<= sentry.building_data.range
&& companion_health > 0;

sentry.is_sentry_activated = is_attacker_in_range || is_companion_in_range;
let new_state = sentry.is_sentry_activated;

if prev_state != new_state && new_state == true {
log::info!("sentry activated");
sentry.sentry_start_time = SystemTime::now();
if is_attacker_in_range {
sentry.target_id = 0;
} else if is_companion_in_range {
sentry.target_id = 1;
}
} else if prev_state != new_state && new_state == false {
log::info!("sentry deactivated");
sentry.current_bullet_shot_time = SystemTime::now() - Duration::new(2, 0);
sentry.target_id = -1;
}
} else {
sentry.is_sentry_activated = false;
sentry.target_id = -1;
}
}
}

pub fn cause_bullet_damage(&mut self) {
let attacker = self.attacker.as_mut().unwrap();
let companion = self.companion.as_mut().unwrap();
Expand Down

0 comments on commit 4c634fd

Please sign in to comment.