Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: send mapspace id after inventory upgrade #80

Open
wants to merge 1 commit into
base: socket
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/api/inventory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
use actix_web::{
error::ErrorBadRequest,
web::{self, Json},
HttpResponse, Responder, Result,
Responder, Result,
};
use serde::{Deserialize, Serialize};
pub mod util;
Expand Down Expand Up @@ -52,14 +52,22 @@ async fn upgrade(
return Err(ErrorBadRequest("You are under attack. Cannot upgrade now"));
}

let mut map_space_id_if_valid = 0;

match item_type.as_str() {
"attacker" => upgrade_attacker(user_id, &mut conn, item_id),
"building" => upgrade_building(user_id, &mut conn, item_id),
"defender" => upgrade_defender(user_id, &mut conn, item_id),
"emp" => upgrade_emp(user_id, &mut conn, item_id),
"mine" => upgrade_mine(user_id, &mut conn, item_id),
"attacker" => upgrade_attacker(user_id, &mut conn, item_id)
.map_err(|err| ErrorBadRequest(err.to_string()))?,
"building" => {
map_space_id_if_valid = upgrade_building(user_id, &mut conn, item_id)
.map_err(|err| ErrorBadRequest(err.to_string()))?;
}
"defender" => upgrade_defender(user_id, &mut conn, item_id)
.map_err(|err| ErrorBadRequest(err.to_string()))?,
"emp" => upgrade_emp(user_id, &mut conn, item_id)
.map_err(|err| ErrorBadRequest(err.to_string()))?,
"mine" => upgrade_mine(user_id, &mut conn, item_id)
.map_err(|err| ErrorBadRequest(err.to_string()))?,
_ => return Err(ErrorBadRequest("Invalid item type")),
}
.map_err(|err| ErrorBadRequest(err.to_string()))?;
Ok(HttpResponse::Ok().finish())
Ok(Json(map_space_id_if_valid))
}
27 changes: 24 additions & 3 deletions src/api/inventory/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ pub(crate) fn upgrade_building(
player_id: i32,
conn: &mut PgConnection,
block_id: i32,
) -> Result<()> {
) -> Result<i32> {
let user_artifacts = get_user_artifacts(player_id, conn)?;

//check if the given block id is a building
Expand Down Expand Up @@ -681,15 +681,18 @@ pub(crate) fn upgrade_building(
if artifacts_in_bank < cost {
return Err(anyhow::anyhow!("Not enough artifacts in bank"));
}
run_transaction(
let _ = run_transaction(
conn,
block_id,
next_level_block_id,
player_id,
cost,
user_artifacts,
bank_map_space_id,
)
);
let building_map_space_id = get_building_map_space_id(conn, &id_of_map, &next_level_block_id)?;
println!("building_map_space_id: {:?}", building_map_space_id);
Ok(building_map_space_id)
}

pub(crate) fn upgrade_defender(
Expand Down Expand Up @@ -1178,3 +1181,21 @@ pub fn get_bank_map_space_id(
})?;
Ok(fetched_bank_map_space_id)
}

pub fn get_building_map_space_id(
conn: &mut PgConnection,
filtered_layout_id: &i32,
block_id: &i32,
) -> Result<i32> {
let fetched_building_map_space_id = map_spaces::table
.filter(map_spaces::map_id.eq(filtered_layout_id))
.filter(map_spaces::block_type_id.eq(block_id))
.select(map_spaces::id)
.first::<i32>(conn)
.map_err(|err| DieselError {
table: "map_spaces",
function: function!(),
error: err,
})?;
Ok(fetched_building_map_space_id)
}
Loading