Skip to content

Commit

Permalink
add recommendation system
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzn committed Dec 24, 2023
1 parent 44ba4c0 commit c1f3029
Showing 3 changed files with 49 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/models/data/starkane.cairo
Original file line number Diff line number Diff line change
@@ -43,9 +43,6 @@ impl CharacterPlayerProgressImpl of CharacterPlayerProgressTrait {
}
}

// calculate_attack(cahracetr, character_progress.level)

// relacion user -> score by match o total score
#[derive(Model, Copy, Drop, Serde)]
struct PlayerStadistics {
#[key]
@@ -54,6 +51,15 @@ struct PlayerStadistics {
matchs_lost: u128,
characters_owned: u32,
total_score: u128,
recommendation_score: u64
}

struct Recommendation {
#[key]
from: felt252,
#[key]
to: felt252,
recommended: bool
}

#[derive(Model, Copy, Drop, Serde)]
13 changes: 12 additions & 1 deletion src/store.cairo
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ use starkane::models::entities::character::Character;
use starkane::models::entities::skill::Skill;
use starkane::models::entities::map::{Map, Tile};
use starkane::models::data::starkane::{
CharacterPlayerProgress, MatchCount, PlayerStadistics, Ranking, RankingCount
CharacterPlayerProgress, MatchCount, PlayerStadistics, Ranking, RankingCount, Recommendation
};

/// Store struct.
@@ -76,6 +76,8 @@ trait StoreTrait {
fn set_ranking(ref self: Store, ranking: Ranking);
fn get_ranking_count(ref self: Store, id: felt252) -> RankingCount;
fn set_ranking_count(ref self: Store, ranking_count: RankingCount);
fn set_recommendation(ref self: Store, recommendation: Recommendation);
fn get_recommendation(ref self: Store, player: felt252) -> Recommendation;
}

/// Implementation of the `StoreTrait` trait for the `Store` struct.
@@ -294,4 +296,13 @@ impl StoreImpl of StoreTrait {
fn set_ranking_count(ref self: Store, ranking_count: RankingCount) {
set!(self.world, (ranking_count));
}

fn set_recommendation(ref self: Store, recommendation: Recommendation) {
set!(self.world, (recommendation));
}

fn get_recommendation(ref self: Store, from: felt252, to: felt252) -> Recommendation {
let recommendation_key = (player, to);
get!(self.world, recommendation_key.into(), (Recommendation))
}
}
28 changes: 28 additions & 0 deletions src/systems/recommend_system.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[starknet::interface]
trait IRecommendSystem<TContractState> {
fn recommend_player(self: @ContractState, from: felt252, to: felt252);
}

#[dojo::contract]
mod recommend_system {
use super::IRecommendSystem;
use starkane::store::{Store, StoreTrait};

#[storage]
struct Storage {}

#[external(v0)]
impl RecommendSystem of IRecommendSystem<ContractState> {
fn recommend_player(self: @ContractState, from: felt252, to: felt252) {
// [Setup] Datastore
let mut store: Store = StoreTrait::new(self.world_dispatcher.read());
assert(!store.get_recommendation(from, to), 'you already recommend this player');

set_recommendation(Recommendation { from, to, recommended: true });

let mut to_stadistics = get_player_stadistics(to);
to_stadistics.recommendation_score += 1;
set_player_stadistics(to_stadistics);
}
}
}

0 comments on commit c1f3029

Please sign in to comment.