Skip to content

Commit

Permalink
Improve Project Workflows (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
No-bodyq authored Dec 28, 2024
1 parent dd681db commit d8ecd39
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 207 deletions.
21 changes: 16 additions & 5 deletions .github/workflows/contracts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@ jobs:
matrix:
os: [ubuntu-22.04]
node: [lts/*]
env:
DOJO_VERSION: v1.0.2
steps:
- uses: actions/checkout@v3
- run: curl -L https://install.dojoengine.org | bash
- run: /home/runner/.config/.dojo/bin/dojoup -v ${{ env.DOJO_VERSION }}
- name: Install Dojo
run: |
DOJO_VERSION=$(grep '^dojo ' ./dojo-starter/.tool-versions | awk '{print $2}')
echo "DOJO_VERSION=$DOJO_VERSION" >> $GITHUB_ENV
curl -L https://install.dojoengine.org | bash
/home/runner/.config/.dojo/bin/dojoup -v $DOJO_VERSION
- run: |
cd dojo-starter && /home/runner/.config/.dojo/bin/sozo build
cd dojo-starter && /home/runner/.config/.dojo/bin/sozo test
/home/runner/.config/.dojo/bin/sozo test
if [[ `git status --porcelain` ]]; then
echo The git repo is dirty
echo "Make sure to run \"sozo build\" after changing Scarb.toml"
exit 1
fi
- name: Install Scarb
run: |
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh
export PATH=$PATH:/home/runner/.config/.dojo/bin
scarb --version
- name: Check Code Formatting
run: |
cd dojo-starter
scarb fmt --check
2 changes: 1 addition & 1 deletion dojo-starter/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dojo 1.0.2
dojo 1.0.2
7 changes: 4 additions & 3 deletions dojo-starter/src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub struct Session {
pub player_2: ContractAddress,
pub turn: u8, // 0 for Up (Player 1) and 1 for Down (Player 2)
pub winner: ContractAddress,
pub state: u8, // 0 for open, 1 for ongoing, 2 for finished
// 0 for open, 1 for ongoing, 2 for finished
pub state: u8,
}

#[derive(Copy, Drop, Serde, Debug)]
Expand All @@ -41,7 +42,7 @@ pub struct Player {
pub struct Counter {
#[key]
pub global_key: felt252,
value: u64
value: u64,
}

#[generate_trait]
Expand Down Expand Up @@ -83,7 +84,7 @@ impl PositionIntoFelt252 of Into<Position, felt252> {
#[derive(Copy, Drop, Serde, Introspect, Debug)]
pub struct Coordinates {
pub row: u8,
pub col: u8
pub col: u8,
}

#[generate_trait]
Expand Down
83 changes: 47 additions & 36 deletions dojo-starter/src/systems/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait IActions<T> {
fn create_lobby(ref self: T) -> u64;
fn join_lobby(ref self: T, session_id: u64);
fn can_choose_piece(
ref self: T, position: Position, coordinates_position: Coordinates, session_id: u64
ref self: T, position: Position, coordinates_position: Coordinates, session_id: u64,
) -> bool;
fn move_piece(ref self: T, current_piece: Piece, new_coordinates_position: Coordinates);

Expand All @@ -23,7 +23,7 @@ pub mod actions {
use super::IActions;
use starknet::{ContractAddress, get_caller_address};
use dojo_starter::models::{
Piece, Coordinates, Position, Session, Player, Counter, CounterTrait
Piece, Coordinates, Position, Session, Player, Counter, CounterTrait,
};

use dojo::model::{ModelStorage, ModelValueStorage};
Expand Down Expand Up @@ -116,7 +116,7 @@ pub mod actions {
ref self: ContractState,
position: Position,
coordinates_position: Coordinates,
session_id: u64
session_id: u64,
) -> bool {
let mut world = self.world_default();

Expand Down Expand Up @@ -155,7 +155,7 @@ pub mod actions {

// Implementation of the move function for the ContractState struct.
fn move_piece(
ref self: ContractState, current_piece: Piece, new_coordinates_position: Coordinates
ref self: ContractState, current_piece: Piece, new_coordinates_position: Coordinates,
) {
// Get the address of the current caller, possibly the player's address.
let mut world = self.world_default();
Expand All @@ -177,11 +177,11 @@ pub mod actions {

// Get updated position & target square
let updated_pieces_keys: Array<(u64, u8, u8)> = array![
(session_id, current_piece.row, current_piece.col),
(session_id, current_piece.row, current_piece.col),
(session_id, square.row, square.col),
];
let updated_pieces: Array<Piece> = world.read_models(updated_pieces_keys.span());

// Conditions to check for new jump:
// - Move caused a piece to be eaten (jump done)
// - Move did not cause a promotion to king
Expand All @@ -190,10 +190,10 @@ pub mod actions {
// Get jump landing position
let land_position = self.calculate_jump_position(current_piece, square);
let land_piece: Piece = world.read_model((session_id, land_position));

// Check for jumps if status didn't change
if current_piece.is_king == land_piece.is_king {
self.is_consecutive_jump_possible(land_piece)
self.is_consecutive_jump_possible(land_piece)
} else {
false
}
Expand Down Expand Up @@ -228,7 +228,7 @@ pub mod actions {
}

fn spawn(
ref self: ContractState, player: ContractAddress, position: Position, session_id: u64
ref self: ContractState, player: ContractAddress, position: Position, session_id: u64,
) {
let mut world = self.world_default();
if position == Position::Up {
Expand All @@ -237,7 +237,7 @@ pub mod actions {
self.initialize_player_pieces(player, 5, 7, Position::Down, session_id);
}
// Assign remaining pieces to player
let player_model = Player { player: player, remaining_pieces: 12, };
let player_model = Player { player: player, remaining_pieces: 12 };
world.write_model(@player_model);
}

Expand All @@ -247,7 +247,7 @@ pub mod actions {
start_col: u8,
row_step: u8,
col_step: u8,
session_id: u64
session_id: u64,
) -> bool {
let mut row = start_row;
let mut col = start_col;
Expand Down Expand Up @@ -325,7 +325,7 @@ pub mod actions {
}

fn change_is_alive(
self: @ContractState, mut current_piece: Piece, new_coordinates_position: Coordinates
self: @ContractState, mut current_piece: Piece, new_coordinates_position: Coordinates,
) {
let mut world = self.world_default();
let session_id = current_piece.session_id;
Expand Down Expand Up @@ -417,7 +417,7 @@ pub mod actions {
// Check down-left diagonal
if piece_col > 0 {
let target_down_left = Coordinates {
row: piece_row + 1, col: piece_col - 1
row: piece_row + 1, col: piece_col - 1,
};
let target_square: Piece = world.read_model((session_id, target_down_left));
return !target_square.is_alive;
Expand All @@ -426,7 +426,7 @@ pub mod actions {
// Check down-right diagonal
if piece_col + 1 < 8 {
let target_down_right = Coordinates {
row: piece_row + 1, col: piece_col + 1
row: piece_row + 1, col: piece_col + 1,
};
let target_square: Piece = world
.read_model((session_id, target_down_right));
Expand All @@ -450,26 +450,28 @@ pub mod actions {
// Check up-right diagonal
if piece_col + 1 < 8 {
let target_up_right = Coordinates {
row: piece_row - 1, col: piece_col + 1
row: piece_row - 1, col: piece_col + 1,
};
let target_square: Piece = world.read_model((session_id, target_up_right));
return !target_square.is_alive;
}
false
},
_ => false
_ => false,
}
}

fn is_jump_possible(self: @ContractState, piece: Piece, square: Piece) -> bool {
let world = self.world_default();
let session_id = piece.session_id;
// Cannot jump over square if piece in square is not alive
if !square.is_alive { return false; }
if !square.is_alive {
return false;
}

let land_pos = self.calculate_jump_position(piece, square);
let land_piece: Piece = world.read_model((session_id, land_pos));

!land_piece.is_alive
}

Expand All @@ -483,36 +485,42 @@ pub mod actions {

// Ensure we don't check outbound squares
if piece.row > 0 && piece.col < 7 {
possible_jumps_up_keys.append(
(session_id, Coordinates { row: piece.row - 1, col: piece.col + 1 })
); // UR
possible_jumps_up_keys
.append(
(session_id, Coordinates { row: piece.row - 1, col: piece.col + 1 }),
); // UR
}
if piece.row > 0 && piece.col > 0 {
possible_jumps_up_keys.append(
(session_id, Coordinates { row: piece.row - 1, col: piece.col - 1 })
); // UL
possible_jumps_up_keys
.append(
(session_id, Coordinates { row: piece.row - 1, col: piece.col - 1 }),
); // UL
}
if piece.row < 7 && piece.col < 7 {
possible_jumps_down_keys.append(
(session_id, Coordinates { row: piece.row + 1, col: piece.col + 1 })
); // DR
possible_jumps_down_keys
.append(
(session_id, Coordinates { row: piece.row + 1, col: piece.col + 1 }),
); // DR
}
if piece.row < 7 && piece.col > 0 {
possible_jumps_down_keys.append(
(session_id, Coordinates { row: piece.row + 1, col: piece.col - 1 })
); // DL
possible_jumps_down_keys
.append(
(session_id, Coordinates { row: piece.row + 1, col: piece.col - 1 }),
); // DL
}

let possible_jumps_up: Array<Piece> = world.read_models(possible_jumps_up_keys.span());
let possible_jumps_down: Array<Piece> = world.read_models(possible_jumps_down_keys.span());
let possible_jumps_down: Array<Piece> = world
.read_models(possible_jumps_down_keys.span());

// Check for valid jumps for corresponding direction
// Check for valid jumps for corresponding direction
// King can jump in both directions
let mut can_jump_again = false;
if (piece.position == Position::Down || piece.is_king) {
for jump_up in possible_jumps_up {
// Check for possible jump. Pieces must be in diferent teams
if (self.is_jump_possible(piece, jump_up) && piece.position != jump_up.position) {
if (self.is_jump_possible(piece, jump_up)
&& piece.position != jump_up.position) {
can_jump_again = true;
}
};
Expand All @@ -521,7 +529,8 @@ pub mod actions {
if (piece.position == Position::Up || piece.is_king) {
for jump_down in possible_jumps_down {
// Check for possible jump. Pieces must be in diferent teams
if (self.is_jump_possible(piece, jump_down) && piece.position != jump_down.position) {
if (self.is_jump_possible(piece, jump_down)
&& piece.position != jump_down.position) {
can_jump_again = true;
}
};
Expand All @@ -532,7 +541,9 @@ pub mod actions {

/// Calculate jump position for given start position and eaten piece position.
/// Takes into account all types of pieces (man/king) and all positions.
fn calculate_jump_position(self: @ContractState, original: Piece, eaten: Piece) -> Coordinates {
fn calculate_jump_position(
self: @ContractState, original: Piece, eaten: Piece,
) -> Coordinates {
// Use signed integers to handle negative values when calculating landing position
let signed_original_row: i8 = original.row.try_into().unwrap();
let signed_original_col: i8 = original.col.try_into().unwrap();
Expand Down
Loading

0 comments on commit d8ecd39

Please sign in to comment.