Skip to content

Commit

Permalink
feat: add battle rank
Browse files Browse the repository at this point in the history
  • Loading branch information
tiankonglan committed Dec 31, 2023
1 parent 4f485c1 commit 7c7726a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 130 deletions.
15 changes: 11 additions & 4 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ dojo = { git = "https://github.com/dojoengine/dojo", version = "0.4.1" }
initializer_class_hash = "0xbeef"

[tool.dojo.env]
rpc_url = "http://localhost:5050/"
account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
world_address="0x1374ef8311b490e1a3ae8e63f4cb1c602e6620d4a7bd87c66c44152b27770b4"
# rpc_url = "http://localhost:5050/"
# account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
# private_key = "0x1800000000300000180000000000030000000000003006001800006600"
# world_address="0x1374ef8311b490e1a3ae8e63f4cb1c602e6620d4a7bd87c66c44152b27770b4"

rpc_url = "https://api.cartridge.gg/x/mississippi/katana"
account_address = "0x7ef05a4d3ef7cdb6ec73c8efff4e9e592d967e98642819aa294df8f2b80436b"
private_key = "0x10689f03e44d450573704d279665292b1cbb259bf38dc5a2207e3913389a80e"
world_address="0x1374ef8311b490e1a3ae8e63f4cb1c602e6620d4a7bd87c66c44152b27770b4"


9 changes: 5 additions & 4 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
set -euo pipefail
pushd $(dirname "$0")/..

export RPC_URL="http://localhost:5050";
export RPC_URL="https://api.cartridge.gg/x/mississippi/katana";
# export RPC_URL="https://starknet-goerli.infura.io/v3/5ca372516740427e97512d4dfefd9c47";
export WORLD_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.world.address')

export GAME_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "mississippi_mini::game::game" ).address')
export GAME_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "mississippi_mini::game::game").address')

export CONFIG_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "mississippi_mini::config::config" ).address')
export CONFIG_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "mississippi_mini::config::config").address')


echo "---------------------------------------------------------------------------"
Expand All @@ -20,7 +20,7 @@ echo config : $CONFIG_ADDRESS
echo "---------------------------------------------------------------------------"

# enable system -> component authorizations
game_component=("Role" "Skill" "Global" "Player" "BattleInfo" "BattleResult")
game_component=("Role" "Skill" "Global" "Player" "BattleInfo" "BattleResult", "BattleRank")
config_component=("Role" "Skill" "Global")

for component in ${game_component[@]}; do
Expand All @@ -29,6 +29,7 @@ for component in ${game_component[@]}; do
done

for component in ${config_component[@]}; do
echo $component
sozo auth writer $component $CONFIG_ADDRESS --world $WORLD_ADDRESS --rpc-url $RPC_URL
done

Expand Down
3 changes: 2 additions & 1 deletion scripts/gen_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ set -euo pipefail
pushd $(dirname "$0")/..

# export RPC_URL="http://localhost:5050";
export RPC_URL="https://starknet-goerli.infura.io/v3/5ca372516740427e97512d4dfefd9c47";
# export RPC_URL="https://starknet-goerli.infura.io/v3/5ca372516740427e97512d4dfefd9c47";
export RPC_URL="https://api.cartridge.gg/x/mississippi/katana";
export WORLD_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.world.address')

export GAME_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "mississippi_mini::game::game" ).address')
Expand Down
11 changes: 9 additions & 2 deletions src/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ trait IGame<TContractState> {
#[dojo::contract]
mod game {
use starknet::{ContractAddress, get_caller_address};
use mississippi_mini::models::{Player, BattleInfo, BattleResult, Skill, Role, Global};
use mississippi_mini::utils::next_position;
use mississippi_mini::models::{Player, BattleInfo, BattleResult, Skill, Role, Global, BattleRank};
use mississippi_mini::constants;
use mississippi_mini::random::{RandomTrait};
// use mississippi_mini::utils::battle_property_settting;
Expand Down Expand Up @@ -177,6 +176,14 @@ mod game {
};
set!(world, (battleResult));

let mut rankInfo = get!(world, winner, (BattleRank));

let mut battleRank = BattleRank {
addr : winner,
score: (rankInfo.score + 1),
};
set!(world, (battleRank));

emit!(world, Win { battleId: attacker.battleId, winner: winner});
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// mod actions;
mod models;
mod game;
mod utils;
// mod utils;
mod constants;
mod config;
mod random;
93 changes: 3 additions & 90 deletions src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,96 +70,9 @@ struct BattleResult {
winner: ContractAddress,
}

// impl Default for MyStruct {
// fn default() -> Self {
// Self::new(0, String::default(), false)
// }
// }

#[derive(Model, Copy, Drop, Print, Serde)]
struct Room {
struct BattleRank {
#[key] // primary key
roomId: u32,
battleId: u32,
}

#[derive(Serde, Copy, Drop, Introspect)]
enum Direction {
None: (),
Left: (),
Right: (),
Up: (),
Down: (),
}

impl DirectionIntoFelt252 of Into<Direction, felt252> {
fn into(self: Direction) -> felt252 {
match self {
Direction::None(()) => 0,
Direction::Left(()) => 1,
Direction::Right(()) => 2,
Direction::Up(()) => 3,
Direction::Down(()) => 4,
}
}
}

#[derive(Model, Drop, Serde)]
struct Moves {
#[key]
player: ContractAddress,
remaining: u8,
last_direction: Direction
}

#[derive(Copy, Drop, Serde, Introspect)]
struct Vec2 {
x: u32,
y: u32
}

#[derive(Model, Copy, Drop, Serde)]
struct Position {
#[key]
player: ContractAddress,
vec: Vec2,
}

trait Vec2Trait {
fn is_zero(self: Vec2) -> bool;
fn is_equal(self: Vec2, b: Vec2) -> bool;
}

impl Vec2Impl of Vec2Trait {
fn is_zero(self: Vec2) -> bool {
if self.x - self.y == 0 {
return true;
}
false
}

fn is_equal(self: Vec2, b: Vec2) -> bool {
self.x == b.x && self.y == b.y
}
addr: ContractAddress,
score: u32,
}

// #[cfg(test)]
// mod tests {
// use super::{Position, Vec2, Vec2Trait};

// #[test]
// #[available_gas(100000)]
// fn test_vec_is_zero() {
// assert(Vec2Trait::is_zero(Vec2 { x: 0, y: 0 }), 'not zero');
// }

// #[test]
// #[available_gas(100000)]
// fn test_vec_is_equal() {
// let position = Vec2 { x: 420, y: 0 };
// assert(position.is_equal(Vec2 { x: 420, y: 0 }), 'not equal');
// }
// }



28 changes: 0 additions & 28 deletions src/utils.cairo

This file was deleted.

0 comments on commit 7c7726a

Please sign in to comment.