Skip to content

Commit

Permalink
score game
Browse files Browse the repository at this point in the history
amiyatulu committed Jun 6, 2024
1 parent fcd22f6 commit 86762bf
Showing 6 changed files with 68 additions and 8 deletions.
2 changes: 1 addition & 1 deletion custom-pallets/positive-externality/src/lib.rs
Original file line number Diff line number Diff line change
@@ -414,7 +414,7 @@ pub mod pallet {
RangePoint::ZeroToFive,
)?;

let score = T::SchellingGameSharedSource::get_mean_value_link(key.clone());
let score = T::SchellingGameSharedSource::get_mean_value_link(key.clone())?;
// println!("Score {:?}", score);
T::SharedStorageSource::set_positive_externality_link(user_to_calculate, score)?;

7 changes: 6 additions & 1 deletion custom-pallets/schelling-game-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -168,7 +168,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn new_mean_reveal_score)]
pub type IncentiveMeanRevealScore<T: Config> =
StorageMap<_, Blake2_128Concat, SumTreeNameType<T>, i64, ValueQuery>;
StorageMap<_, Blake2_128Concat, SumTreeNameType<T>, i64>;

/// Decision count for two choices after reveal vote: (count for 0, count for 1)
#[pallet::storage]
@@ -181,6 +181,10 @@ pub mod pallet {
pub type JurorsIncentiveDistributedAccounts<T: Config> =
StorageMap<_, Blake2_128Concat, SumTreeNameType<T>, Vec<T::AccountId>, ValueQuery>;


// #[pallet::storage]
// #[pallet::getter(fn )]

// Pallets use events to inform users when important changes are made.
// https://docs.substrate.io/main-docs/build/events-errors/
#[pallet::event]
@@ -223,6 +227,7 @@ pub mod pallet {
VoteNotRevealed,
TimeForStakingOver,
TimeForStakingNotOver,
NewMeanNotInserted,
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
60 changes: 57 additions & 3 deletions custom-pallets/schelling-game-shared/src/score_game.rs
Original file line number Diff line number Diff line change
@@ -163,9 +163,63 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(super) fn get_mean_value(key: SumTreeNameType<T>) -> i64 {
let value = <IncentiveMeanRevealScore<T>>::get(key.clone());
value

pub(super) fn get_score_value_schelling(
key: SumTreeNameType<T>,
who: AccountIdOf<T>,
range_point: RangePoint,
) -> Result<WonLost, DispatchError> {
match <PeriodName<T>>::get(&key) {
Some(period) => {
ensure!(period == Period::Execution, Error::<T>::PeriodDontMatch);
},
None => Err(Error::<T>::PeriodDoesNotExists)?,
}
let new_mean = Self::get_mean_value(key.clone())?;

let incentives_range = Self::get_incentives_range(range_point);
let reveal_votes = <ScoreVoteCommits<T>>::get(&key, &who);
match reveal_votes {
Some(commit_struct) => {
let vote_option = commit_struct.revealed_vote;
match vote_option {
Some(vote) => {
if vote * 1000 >= new_mean.checked_sub(incentives_range).unwrap()
&& vote * 1000 <= new_mean.checked_add(incentives_range).unwrap()
{
// get incentives
Ok(WonLost::Won)
} else {
Ok(WonLost::Lost)
}
},
None => Err(Error::<T>::VoteNotRevealed)?,
}

}
None => Err(Error::<T>::CommitDoesNotExists)?,
}

}

pub(super) fn set_new_mean_value(key: SumTreeNameType<T>) -> DispatchResult {
let reveal_values = <RevealScoreValues<T>>::get(&key);
let sd_and_mean = Self::std_deviation_interger(&reveal_values);
let new_mean = Self::calculate_new_mean(&reveal_values, sd_and_mean).unwrap();
// println!("new mean: {:?}", new_mean);
<IncentiveMeanRevealScore<T>>::insert(key.clone(), new_mean);

Ok(())
}

pub(super) fn get_mean_value(key: SumTreeNameType<T>) -> Result<i64, DispatchError> {
let mean_option = <IncentiveMeanRevealScore<T>>::get(key);
match mean_option {
Some(mean) => {
Ok(mean)
},
None => Err(Error::<T>::NewMeanNotInserted)?,
}
}

/// Calculate the mean of integer
2 changes: 1 addition & 1 deletion custom-pallets/schelling-game-shared/src/share_link.rs
Original file line number Diff line number Diff line change
@@ -286,7 +286,7 @@ impl<T: Config> SchellingGameSharedLink for Pallet<T> {
}

/// Get new mean in score schelling game
fn get_mean_value_link(key: Self::SumTreeName) -> i64 {
fn get_mean_value_link(key: Self::SumTreeName) -> Result<i64, DispatchError>{
Self::get_mean_value(key)
}

2 changes: 1 addition & 1 deletion custom-pallets/schelling-game-shared/src/tests.rs
Original file line number Diff line number Diff line change
@@ -816,7 +816,7 @@ fn score_schelling_game_test() {
RangePoint::ZeroToTen
));
let mean_values = TemplateModule::new_mean_reveal_score(key.clone());
assert_eq!(2000, mean_values);
assert_eq!(2000, mean_values.unwrap());
let balance = Balances::free_balance(4);
// println!("{:?}", balance);
assert_eq!(300033, balance);
3 changes: 2 additions & 1 deletion traits/trait-schelling-game-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::dispatch::DispatchResult;
use frame_support::pallet_prelude::DispatchError;
use sp_std::prelude::*;

pub trait SchellingGameSharedLink {
@@ -134,7 +135,7 @@ pub trait SchellingGameSharedLink {
range_point: Self::RangePoint,
) -> DispatchResult;

fn get_mean_value_link(key: Self::SumTreeName) -> i64;
fn get_mean_value_link(key: Self::SumTreeName) -> Result<i64, DispatchError>;

fn get_all_incentives_two_choice_helper(
key: Self::SumTreeName,

0 comments on commit 86762bf

Please sign in to comment.