Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend unit testing for elo-ranking #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
frame_benchmarking::account("Bob", 0, 1),
asset_min_balance * 100,
),
(
asset_id,
frame_benchmarking::account("Charlie", 0, 2),
asset_min_balance * 100,
),
(
asset_id,
frame_benchmarking::account("Dave", 0, 3),
asset_min_balance * 100,
),
],
}
.assimilate_storage(&mut storage)
Expand All @@ -156,6 +166,8 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
elo: vec![
(frame_benchmarking::account("Alice", 0, 0), 2000),
(frame_benchmarking::account("Bob", 0, 1), 2400),
(frame_benchmarking::account("Charlie", 0, 2), 1000),
(frame_benchmarking::account("Dave", 0, 3), 1000),
],
}
.assimilate_storage(&mut storage)
Expand Down
75 changes: 66 additions & 9 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,51 @@ fn check_elo_stronger_looses() {
}

#[test]
fn check_elo_player_aborts() {
fn check_elo_player_draw() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in English it's "draws", in plural.

new_test_ext().execute_with(|| {
System::set_block_number(1);
let charlie: u64 = account("Charlie", 0, 2);
let dave:u64 = account("Dave", 0, 3);

let bet_asset_id = AssetId::get();
let bet_amount = AssetMinBalance::get() * 5;

assert_ok!(Chess::create_match(
RuntimeOrigin::signed(charlie),
dave,
MatchStyle::Bullet,
bet_asset_id,
bet_amount
));

let match_id = Chess::chess_match_id_from_nonce(0).unwrap();

assert_ok!(Chess::join_match(RuntimeOrigin::signed(dave), match_id));

// check elo before the match finishes
assert_eq!(Chess::player_elo(charlie), 1000);
assert_eq!(Chess::player_elo(dave), 1000);
Copy link
Collaborator

@Moliholy Moliholy Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both players have the same ELO and make draws then they maintain the same ELO, that's correct. However, in the previous example it checked what happens if they have different ELO, which I think it's interesting to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are totally right, it makes more sense your approach to see is changing. Revert it in this commit 7d2c849


assert_ok!(Chess::force_board_state(
match_id,
"8/8/8/8/8/5K2/Q7/7k w - - 1 68".into()
));
// this move forces draws
assert_ok!(Chess::make_move(
RuntimeOrigin::signed(charlie),
match_id,
"a2f2".into()
));
assert_eq!(Chess::chess_matches(match_id), None);

// check the elo after the match is complete
assert_eq!(Chess::player_elo(charlie), 1000);
assert_eq!(Chess::player_elo(dave), 1000);
});
}

#[test]
fn check_elo_stronger_abandon() {
new_test_ext().execute_with(|| {
System::set_block_number(1);

Expand All @@ -560,24 +604,37 @@ fn check_elo_player_aborts() {
assert_eq!(Chess::player_elo(alice), 2000);
assert_eq!(Chess::player_elo(bob), 2400);

assert_ok!(Chess::force_board_state(
assert_ok!(Chess::make_move(
RuntimeOrigin::signed(bob),
match_id,
"8/8/8/8/8/5K2/Q7/7k w - - 1 68".into()
"f2f3".into()
));
// this move forces draws
assert_ok!(Chess::make_move(
RuntimeOrigin::signed(bob),
RuntimeOrigin::signed(alice),
match_id,
"a2f2".into()
"e7e5".into()
));

// advance the block number to the point where bob's time-to-move is expired
System::set_block_number(
System::block_number() + <Test as Config>::BulletPeriod::get() + 1,
);

// Bob abandom, alice claims victory
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: abandoned.

assert_ok!(Chess::clear_abandoned_match(
RuntimeOrigin::signed(alice),
match_id
));

assert_eq!(Chess::chess_matches(match_id), None);

// check the elo after the match is complete
assert_eq!(Chess::player_elo(alice), 2013);
assert_eq!(Chess::player_elo(bob), 2387);
// check the elo after match is complete
assert_eq!(Chess::player_elo(alice), 2029);
assert_eq!(Chess::player_elo(bob), 2371);
});
}


const BOARD_STATE: &str = "Q7/5Q2/8/8/3k4/6P1/6BP/7K b - - 0 67";

#[test]
Expand Down