Skip to content

Commit

Permalink
✨ Create member, guild and alliance features
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Nov 22, 2024
1 parent e90d820 commit 2212235
Show file tree
Hide file tree
Showing 34 changed files with 1,446 additions and 290 deletions.
1 change: 1 addition & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ version = "0.0.0"
dependencies = [
"dojo",
"dojo_cairo_test",
"registry",
]
2 changes: 0 additions & 2 deletions packages/controller/src/components/controllable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ mod ControllableComponent {
use controller::store::{Store, StoreTrait};
use controller::models::account::{Account, AccountTrait, AccountAssert};
use controller::models::controller::{Controller, ControllerTrait, ControllerAssert};
use controller::models::member::{Member, MemberTrait, MemberAssert};
use controller::models::signer::{Signer, SignerTrait, SignerAssert};
use controller::models::team::{Team, TeamTrait, TeamAssert};

// Storage

Expand Down
3 changes: 0 additions & 3 deletions packages/controller/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ mod store;

mod types {
mod method;
mod role;
}

mod models {
mod index;
mod account;
mod controller;
mod member;
mod signer;
mod team;
}

mod components {
Expand Down
2 changes: 1 addition & 1 deletion packages/controller/src/models/account.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Intenral imports
// Internal imports

use controller::models::index::Account;

Expand Down
2 changes: 1 addition & 1 deletion packages/controller/src/models/controller.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Intenral imports
// Internal imports

use controller::models::index::Controller;

Expand Down
19 changes: 0 additions & 19 deletions packages/controller/src/models/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ pub struct Controller {
constructor_calldata: ByteArray,
}

#[derive(Copy, Drop, Serde)]
#[dojo::model]
pub struct Member {
#[key]
account_id: felt252,
#[key]
team_id: felt252,
role: u8,
}

#[derive(Clone, Drop, Serde)]
#[dojo::model]
pub struct Signer {
Expand All @@ -45,12 +35,3 @@ pub struct Signer {
method: u8,
metadata: ByteArray,
}

#[derive(Clone, Drop, Serde)]
#[dojo::model]
pub struct Team {
#[key]
id: felt252,
name: felt252,
description: ByteArray,
}
95 changes: 0 additions & 95 deletions packages/controller/src/models/member.cairo

This file was deleted.

2 changes: 1 addition & 1 deletion packages/controller/src/models/signer.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Intenral imports
// Internal imports

use controller::models::index::Signer;
use controller::types::method::Method;
Expand Down
22 changes: 0 additions & 22 deletions packages/controller/src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use dojo::model::ModelStorage;

use controller::models::account::Account;
use controller::models::controller::Controller;
use controller::models::member::Member;
use controller::models::signer::Signer;
use controller::models::team::Team;


// Structs
Expand Down Expand Up @@ -44,21 +42,11 @@ impl StoreImpl of StoreTrait {
self.world.read_model(controller_id)
}

#[inline]
fn get_member(self: Store, account_id: felt252, team_id: felt252) -> Member {
self.world.read_model((account_id, team_id))
}

#[inline]
fn get_signer(self: Store, signer_id: felt252) -> Signer {
self.world.read_model(signer_id)
}

#[inline]
fn get_team(self: Store, team_id: felt252) -> Team {
self.world.read_model(team_id)
}

#[inline]
fn set_account(ref self: Store, account: @Account) {
self.world.write_model(account);
Expand All @@ -68,14 +56,4 @@ impl StoreImpl of StoreTrait {
fn set_controller(ref self: Store, controller: @Controller) {
self.world.write_model(controller);
}

#[inline]
fn set_member(ref self: Store, member: @Member) {
self.world.write_model(member);
}

#[inline]
fn set_team(ref self: Store, team: @Team) {
self.world.write_model(team);
}
}
48 changes: 44 additions & 4 deletions packages/provider/src/components/deployable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ mod DeployableComponent {
use provider::models::deployment::{Deployment, DeploymentTrait, DeploymentAssert};
use provider::models::factory::{Factory, FactoryTrait, FactoryAssert};
use provider::types::service::{Service, ServiceTrait, SERVICE_COUNT};
use provider::models::team::{Team, TeamTrait, TeamAssert};
use provider::models::teammate::{Teammate, TeammateTrait, TeammateAssert};
use provider::types::status::Status;
use provider::types::tier::Tier;
use provider::types::role::Role;

// Storage

Expand Down Expand Up @@ -60,10 +63,30 @@ mod DeployableComponent {
let deployment = store.get_deployment(service.into(), project);
deployment.assert_does_not_exist();

// [Check] Caller permission
let account_id: felt252 = starknet::get_caller_address().into();
let mut team = store.get_team(project);
if team.exists() {
// [Check] Caller is at least an admin
let teammate = store.get_teammate(project, team.time, account_id);
teammate.assert_is_allowed(Role::Admin);
// [Effect] Increment deployment count
team.deploy();
store.set_team(@team);
} else {
// [Effect] Create team
let time = starknet::get_block_timestamp();
let mut team = TeamTrait::new(project, time, project, "");
team.deploy();
store.set_team(@team);
// [Effect] Create teammate
let teammate = TeammateTrait::new(project, time, account_id, Role::Owner);
store.set_teammate(@teammate);
}

// [Effect] Create deployment
let owner = starknet::get_caller_address().into();
let deployment = DeploymentTrait::new(
service: service, project: project, owner: owner, tier: tier, config: "",
service: service, project: project, tier: tier, config: "",
);
store.set_deployment(@deployment);
}
Expand All @@ -85,12 +108,29 @@ mod DeployableComponent {
let mut deployment = store.get_deployment(service.into(), project);
deployment.assert_does_exist();

// [Check] Caller is owner
deployment.assert_is_owner(starknet::get_caller_address().into());
// [Check] Team exists
let mut team = store.get_team(project);
team.assert_does_exist();

// [Check] Caller is at least admin
let account_id: felt252 = starknet::get_caller_address().into();
let teammate = store.get_teammate(project, team.time, account_id);
teammate.assert_is_allowed(Role::Admin);

// [Effect] Delete deployment
deployment.nullify();
store.delete_deployment(@deployment);

// [Effect] Decrement deployment count
team.remove();

// [Effect] Delete team if no deployments left
if team.deployment_count == 0 {
team.nullify();
store.delete_team(@team);
} else {
store.set_team(@team);
}
}
}
}
87 changes: 87 additions & 0 deletions packages/provider/src/components/groupable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#[starknet::component]
mod GroupableComponent {
// Dojo imports

use dojo::world::WorldStorage;

// Internal imports

use provider::store::{Store, StoreTrait};
use provider::models::team::{Team, TeamTrait, TeamAssert};
use provider::models::teammate::{Teammate, TeammateTrait, TeammateAssert};
use provider::types::role::Role;

// Storage

#[storage]
struct Storage {}

// Events

#[event]
#[derive(Drop, starknet::Event)]
enum Event {}

#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn add(
self: @ComponentState<TContractState>,
world: WorldStorage,
name: felt252,
account_id: felt252,
role: Role,
) {
// [Setup] Datastore
let mut store: Store = StoreTrait::new(world);

// [Check] Team exists
let team = store.get_team(name);
team.assert_does_exist();

// [Check] Caller is at least admin
let caller_id: felt252 = starknet::get_caller_address().into();
let callermate = store.get_teammate(name, team.time, caller_id);
callermate.assert_is_allowed(Role::Admin);

// [Check] Teammate does not exist
let teammate = store.get_teammate(name, team.time, account_id);
teammate.assert_does_not_exist();

// [Effect] Create teammate
let teammate = TeammateTrait::new(name, team.time, account_id, role);
store.set_teammate(@teammate);
}

fn remove(
self: @ComponentState<TContractState>,
world: WorldStorage,
name: felt252,
account_id: felt252,
) {
// [Setup] Datastore
let mut store: Store = StoreTrait::new(world);

// [Check] Team exists
let team = store.get_team(name);
team.assert_does_exist();

// [Check] Caller is at least admin
let caller_id: felt252 = starknet::get_caller_address().into();
let callermate = store.get_teammate(name, team.time, caller_id);
callermate.assert_is_allowed(Role::Admin);

// [Check] Teammate exists
let mut teammate = store.get_teammate(name, team.time, account_id);
teammate.assert_does_exist();

// [Check] Caller has greater role than teammate
callermate.assert_is_greater(teammate.role.into());

// [Effect] Delete teammate
teammate.nullify();
store.delete_teammate(@teammate);
}
}
}
Loading

0 comments on commit 2212235

Please sign in to comment.