Skip to content

Commit

Permalink
minor improvements to multiplayer example
Browse files Browse the repository at this point in the history
  • Loading branch information
Trisfald committed Jul 6, 2020
1 parent 9d2f2bb commit 3707a68
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions examples/king_of_the_hill/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use weasel::event::{EventKind, EventProcessor, EventQueue, EventTrigger, EventWr
use weasel::round::{EndRound, EndTurn, StartRound, TurnsCount};
use weasel::team::{CreateTeam, ResetObjectives, TeamId};
use weasel::util::Id;
use weasel::{Client, Server};
use weasel::Server;

mod rules;
mod tcp;
Expand Down Expand Up @@ -123,7 +123,17 @@ fn client_game_loop(mut client: TcpClient) {
key.to_digit(10).unwrap(),
client.id,
) {
client_end_turn(&mut client.game_client, turn);
// Wait for the turn completion.
wait(|| {
client
.game_client
.lock()
.unwrap()
.battle()
.rounds()
.completed_turns()
> turn
});
game_status(client.game_client.lock().unwrap().battle(), client.id);
}
}
Expand Down Expand Up @@ -180,6 +190,7 @@ where
return false;
}
};
println!("Waiting for all players to make their move...");
// Wait for our turn.
wait(|| {
// We have defined the rounds model to be equal to the id of the player who should act.
Expand Down Expand Up @@ -209,8 +220,6 @@ where
}

fn server_end_turn(server: &mut Arc<Mutex<Server<CustomRules>>>) {
// Wait for all players to have made their play.
println!("Waiting for all players to make their move...");
wait(|| server.lock().unwrap().battle().rounds().completed_rounds() % 3 == 0);
// Decide the winner.
let winner = winner(server);
Expand Down Expand Up @@ -244,14 +253,6 @@ fn server_end_turn(server: &mut Arc<Mutex<Server<CustomRules>>>) {
.unwrap();
}

fn client_end_turn(client: &mut Arc<Mutex<Client<CustomRules>>>, turn: TurnsCount) {
// Wait for the turn completion.
println!("Waiting for all players to make their move...");
wait(|| client.lock().unwrap().battle().rounds().completed_turns() > turn);
// // Print the outcome.
// turn_results(client);
}

/// Returns the number of completed turns.
fn completed_turns<T>(controller: &Arc<Mutex<T>>) -> TurnsCount
where
Expand Down

0 comments on commit 3707a68

Please sign in to comment.