Skip to content

Commit

Permalink
Update : upgrade building
Browse files Browse the repository at this point in the history
  • Loading branch information
Hujunjob committed Oct 23, 2023
1 parent c386679 commit 70d919d
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 142 deletions.
28 changes: 14 additions & 14 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ sozo auth writer Gold $(get_contract_address "claim_mining") --world $WORLD_ADDR
sleep 1
sozo auth writer Iron $(get_contract_address "claim_mining") --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 1
# sozo auth writer LandCost upgrade_building --world $WORLD_ADDRESS
# sleep 1
# sozo auth writer UpgradeCost upgrade_building --world $WORLD_ADDRESS
# sleep 1
# sozo auth writer Gold upgrade_building --world $WORLD_ADDRESS
# sleep 1
# sozo auth writer Food upgrade_building --world $WORLD_ADDRESS
# sleep 1
# sozo auth writer Iron upgrade_building --world $WORLD_ADDRESS
# sleep 1
sozo auth writer LandCost $(get_contract_address "upgrade_building") --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 1
sozo auth writer UpgradeCost $(get_contract_address "upgrade_building") --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 1
sozo auth writer Gold $(get_contract_address "upgrade_building") --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 1
sozo auth writer Food $(get_contract_address "upgrade_building") --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 1
sozo auth writer Iron $(get_contract_address "upgrade_building") --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 1
# sozo auth writer Warrior admin --world $WORLD_ADDRESS
# sleep 1
# sozo auth writer UserWarrior admin --world $WORLD_ADDRESS
Expand All @@ -127,10 +127,10 @@ sleep 1
# sleep 1
# sozo auth writer Iron admin --world $WORLD_ADDRESS
# sleep 1
# sozo auth writer Land upgrade_compleate --world $WORLD_ADDRESS
# sleep 1
# sozo auth writer UpgradeCost upgrade_compleate --world $WORLD_ADDRESS
# sleep 1
sozo auth writer Land $(get_contract_address "upgrade_compleate") --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 1
sozo auth writer UpgradeCost $(get_contract_address "upgrade_compleate") --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 1
# sozo auth writer Land admin_attack --world $WORLD_ADDRESS
# sleep 1

Expand Down
4 changes: 2 additions & 2 deletions src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ mod build_building;
mod start_mining;
mod claim_mining;
mod remove_build;
// mod upgrade_building;
// mod upgrade_compleate;
mod upgrade_building;
mod upgrade_compleate;
// mod admin;
// mod attack_monster;
// mod open_pack;
215 changes: 121 additions & 94 deletions src/actions/upgrade_building.cairo
Original file line number Diff line number Diff line change
@@ -1,101 +1,128 @@
#[system]
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use starknet::{ContractAddress, ClassHash};

// define the interface
#[starknet::interface]
trait IActions<TContractState> {
fn execute(self: @TContractState, map_id: u64, x: u64, y: u64);
}

// dojo decorator
#[dojo::contract]
mod upgrade_building {
use array::ArrayTrait;
use box::BoxTrait;
use traits::{Into, TryInto};
use option::OptionTrait;
use dojo::world::Context;

use stark_land::components::global_config::GlobalConfig;
use stark_land::components::build_config::BuildConfig;
use stark_land::components::build_price::BuildPrice;
use stark_land::components::player::Player;
use stark_land::components::food::Food;
use stark_land::components::iron::Iron;
use stark_land::components::gold::Gold;
use stark_land::components::base::Base;
use stark_land::components::land::Land;
use stark_land::components::land::LandTrait;
use stark_land::components::land_cost::LandCost;
use stark_land::components::upgrade_cost::UpgradeCost;

fn execute(ctx: Context, map_id: u64, x: u64, y: u64) {
// assert(check_can_build_base(ctx, map_id, x, y), 'can not build here');
let base = get!(ctx.world, (map_id, ctx.origin), Base);
assert(base.x != 0 && base.y != 0, 'you have no base');
let build_config = get!(ctx.world, map_id, BuildConfig);

let mut upgrade_x = x;
let mut upgrade_y = y;
let mut land = get!(ctx.world, (map_id, upgrade_x, upgrade_y), Land);

//如果传入的坐标是 Base, 就对坐标进行修正
if(land.building == build_config.Build_Type_Base){
upgrade_x = base.x;
upgrade_y = base.y;
}
land = get!(ctx.world, (map_id, upgrade_x, upgrade_y), Land);

assert(LandTrait::land_property(map_id, upgrade_x, upgrade_y) >= build_config.Land_None, 'can not build');
assert(land.building != 0, 'have no building');
assert(land.owner == ctx.origin, 'not yours');

// 判断建筑物是否正在升级
let time_now: u64 = starknet::get_block_timestamp();
let mut upgrade_cost = get!(ctx.world,(map_id,upgrade_x, upgrade_y),UpgradeCost);
assert(time_now >= upgrade_cost.end_time, 'building are upgrading');
assert(upgrade_cost.claimed || upgrade_cost.end_time==0,
'claim targrt_level first ');//如果上一次的级别已经claim,或是首次升级
upgrade_cost.start_time = time_now;
upgrade_cost.claimed = false;

// 建设当前地块的累计成本
let mut land_cost = get!(ctx.world,(map_id,upgrade_x, upgrade_y),LandCost);

let build_price = get!(ctx.world,(map_id, land.building),BuildPrice);
assert(
build_price.food != 0 || build_price.gold != 0 || build_price.iron != 0, 'illegal upgrade'
);

// 当前地块的等级
let current_level = land.level;

// 升级所需的时间为 单位时间 * 下一等级
let unit_time = 36;
let mut index = 0;
let mut total_pow = 1;
loop{
total_pow = total_pow * 2;
index+=1;
if(index==current_level){
break;
use starknet::{ContractAddress, get_caller_address};
use super::IActions;

use stark_land::models::global_config::GlobalConfig;
use stark_land::models::build_config::BuildConfig;
use stark_land::models::build_price::BuildPrice;
use stark_land::models::player::Player;
use stark_land::models::food::Food;
use stark_land::models::iron::Iron;
use stark_land::models::gold::Gold;
use stark_land::models::hbase::HBase;
use stark_land::models::land::Land;
use stark_land::models::land::LandTrait;
use stark_land::models::land_cost::LandCost;
use stark_land::models::upgrade_cost::UpgradeCost;

// impl: implement functions specified in trait
#[external(v0)]
impl ActionsImpl of IActions<ContractState> {
// ContractState is defined by system decorator expansion
fn execute(self: @ContractState, map_id: u64, x: u64, y: u64) {
let time_now: u64 = starknet::get_block_timestamp();
// Access the world dispatcher for reading.
let world = self.world_dispatcher.read();

// Get the address of the current caller, possibly the player's address.
let origin =
get_caller_address(); // assert(check_can_build_base(ctx, map_id, x, y), 'can not build here');
let base = get!(world, (map_id, origin), HBase);
assert(base.x != 0 && base.y != 0, 'you have no base');
let build_config = get!(world, map_id, BuildConfig);

let mut upgrade_x = x;
let mut upgrade_y = y;
let mut land = get!(world, (map_id, upgrade_x, upgrade_y), Land);

//如果传入的坐标是 Base, 就对坐标进行修正
if (land.building == build_config.Build_Type_Base) {
upgrade_x = base.x;
upgrade_y = base.y;
}
land = get!(world, (map_id, upgrade_x, upgrade_y), Land);

assert(
LandTrait::land_property(map_id, upgrade_x, upgrade_y) >= build_config.Land_None,
'can not build'
);
assert(land.building != 0, 'have no building');
assert(land.owner == origin, 'not yours');

// 判断建筑物是否正在升级
let time_now: u64 = starknet::get_block_timestamp();
let mut upgrade_cost = get!(world, (map_id, upgrade_x, upgrade_y), UpgradeCost);
assert(time_now >= upgrade_cost.end_time, 'building are upgrading');
assert(
upgrade_cost.claimed || upgrade_cost.end_time == 0, 'claim targrt_level first '
); //如果上一次的级别已经claim,或是首次升级
upgrade_cost.start_time = time_now;
upgrade_cost.claimed = false;

// 建设当前地块的累计成本
let mut land_cost = get!(world, (map_id, upgrade_x, upgrade_y), LandCost);

let build_price = get!(world, (map_id, land.building), BuildPrice);
assert(
build_price.food != 0 || build_price.gold != 0 || build_price.iron != 0,
'illegal upgrade'
);

// 当前地块的等级
let current_level = land.level;

// 升级所需的时间为 单位时间 * 下一等级
let unit_time = 36;
let mut index = 0;
let mut total_pow = 1;
loop {
total_pow = total_pow * 2;
index += 1;
if (index == current_level) {
break;
};
};
};
upgrade_cost.end_time = time_now + unit_time * total_pow/2;

// 升级所需的资源为 = 建设单价 * 下一等级
let food_need = build_price.food * total_pow;
let mut food = get!(ctx.world, (map_id, ctx.origin), Food);
assert(food.balance >= food_need, 'food not enough');
food.balance = food.balance - food_need;
land_cost.cost_food = land_cost.cost_food + food_need;

let iron_need = build_price.iron * total_pow;
let mut iron = get!(ctx.world, (map_id, ctx.origin), Iron);
assert(iron.balance >= iron_need, 'iron not enough');
iron.balance = iron.balance - iron_need;
land_cost.cost_iron = land_cost.cost_iron + iron_need;

let gold_need = build_price.gold * total_pow;
let mut gold = get!(ctx.world, (map_id, ctx.origin), Gold);
assert(gold.balance >= gold_need, 'gold not enough');
gold.balance = gold.balance - gold_need;
land_cost.cost_gold = land_cost.cost_gold + gold_need;

//land.level = current_level + 1;
// 将升级后的等级暂存于 upgrade_cost
upgrade_cost.target_level = current_level + 1;
set!(ctx.world, (upgrade_cost,land_cost,food,iron,gold));
return ();
upgrade_cost.end_time = time_now + unit_time * total_pow / 2;

// 升级所需的资源为 = 建设单价 * 下一等级
let food_need = build_price.food * total_pow;
let mut food = get!(world, (map_id, origin), Food);
assert(food.balance >= food_need, 'food not enough');
food.balance = food.balance - food_need;
land_cost.cost_food = land_cost.cost_food + food_need;

let iron_need = build_price.iron * total_pow;
let mut iron = get!(world, (map_id, origin), Iron);
assert(iron.balance >= iron_need, 'iron not enough');
iron.balance = iron.balance - iron_need;
land_cost.cost_iron = land_cost.cost_iron + iron_need;

let gold_need = build_price.gold * total_pow;
let mut gold = get!(world, (map_id, origin), Gold);
assert(gold.balance >= gold_need, 'gold not enough');
gold.balance = gold.balance - gold_need;
land_cost.cost_gold = land_cost.cost_gold + gold_need;

//land.level = current_level + 1;
// 将升级后的等级暂存于 upgrade_cost
upgrade_cost.target_level = current_level + 1;
set!(world, (upgrade_cost, land_cost, food, iron, gold));
return ();
}
}
}
}
86 changes: 54 additions & 32 deletions src/actions/upgrade_compleate.cairo
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
#[system]
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use starknet::{ContractAddress, ClassHash};

// define the interface
#[starknet::interface]
trait IActions<TContractState> {
fn execute(self: @TContractState, map_id: u64, x: u64, y: u64);
}

// dojo decorator
#[dojo::contract]
mod upgrade_compleate {
use array::ArrayTrait;
use box::BoxTrait;
use traits::{Into, TryInto};
use option::OptionTrait;
use dojo::world::Context;

use stark_land::components::global_config::GlobalConfig;
use stark_land::components::build_config::BuildConfig;
use stark_land::components::build_price::BuildPrice;
use stark_land::components::player::Player;

use stark_land::components::base::Base;
use stark_land::components::land::Land;
use stark_land::components::land::LandTrait;

use stark_land::components::upgrade_cost::UpgradeCost;

fn execute(ctx: Context, map_id: u64, x: u64, y: u64) {
let base = get!(ctx.world, (map_id, ctx.origin), Base);
assert(base.x != 0 && base.y != 0, 'you have no base');
let mut upgrade_cost = get!(ctx.world,(map_id,x, y),UpgradeCost);
assert(upgrade_cost.start_time != 0, 'you have not upgrade');

// 判断建筑物是否升级完毕
let time_now: u64 = starknet::get_block_timestamp();
assert(time_now >= upgrade_cost.end_time, 'building are upgrading');
assert( !upgrade_cost.claimed, 'targrt_level claimed'); // 如果上次升级的级别已经 claim ,则 X

let mut land = get!(ctx.world, (map_id, x, y), Land);
land.level = upgrade_cost.target_level;
upgrade_cost.claimed = true;

set!(ctx.world, (land, upgrade_cost));
return ();
use starknet::{ContractAddress, get_caller_address};
use super::IActions;

use stark_land::models::global_config::GlobalConfig;
use stark_land::models::build_config::BuildConfig;
use stark_land::models::build_price::BuildPrice;
use stark_land::models::player::Player;
use stark_land::models::hbase::HBase;
use stark_land::models::land::Land;
use stark_land::models::land::LandTrait;
use stark_land::models::upgrade_cost::UpgradeCost;

// impl: implement functions specified in trait
#[external(v0)]
impl ActionsImpl of IActions<ContractState> {
// ContractState is defined by system decorator expansion
fn execute(self: @ContractState, map_id: u64, x: u64, y: u64) {
let time_now: u64 = starknet::get_block_timestamp();
// Access the world dispatcher for reading.
let world = self.world_dispatcher.read();

// Get the address of the current caller, possibly the player's address.
let origin = get_caller_address();
let base = get!(world, (map_id, origin), HBase);
assert(base.x != 0 && base.y != 0, 'you have no base');
let mut upgrade_cost = get!(world, (map_id, x, y), UpgradeCost);
assert(upgrade_cost.start_time != 0, 'you have not upgrade');

// 判断建筑物是否升级完毕
let time_now: u64 = starknet::get_block_timestamp();
assert(time_now >= upgrade_cost.end_time, 'building are upgrading');
assert(
!upgrade_cost.claimed, 'targrt_level claimed'
); // 如果上次升级的级别已经 claim ,则 X

let mut land = get!(world, (map_id, x, y), Land);
land.level = upgrade_cost.target_level;
upgrade_cost.claimed = true;

set!(world, (land, upgrade_cost));
return ();
}
}
}
}

0 comments on commit 70d919d

Please sign in to comment.