Crates.io | tokio-takconnect |
lib.rs | tokio-takconnect |
version | 0.2.2 |
source | src |
created_at | 2022-03-29 00:45:07.727154 |
updated_at | 2022-07-22 17:12:06.542539 |
description | Asynchronous Playtak client |
homepage | |
repository | https://github.com/alion02/tokio-takconnect |
max_upload_size | |
id | 558222 |
size | 34,087 |
A work-in-progress, asynchronous Playtak client.
use std::{
io::{stdin, BufRead},
thread::spawn,
time::Duration,
};
use tokio::{select, sync::mpsc::unbounded_channel};
use tokio_takconnect::{connect_guest, Color, GameParameters, GameUpdate, SeekParameters};
#[tokio::main]
async fn main() {
let mut client = connect_guest().await.unwrap();
loop {
client
.seek(
SeekParameters::new(
None,
Color::Any,
GameParameters::new(
5,
Duration::from_secs(600),
Duration::from_secs(20),
0,
21,
1,
false,
false,
)
.unwrap(),
)
.unwrap(),
)
.await
.unwrap();
let mut game = client.game().await.unwrap();
let (tx, mut rx) = unbounded_channel();
spawn(move || {
for line in stdin().lock().lines() {
if tx.send(line.unwrap()).is_err() {
break;
}
}
});
println!("Game start.");
loop {
select! {
update = game.update() => {
match update.unwrap() {
GameUpdate::Played(m) => println!("Opponent plays: {m}"),
GameUpdate::Ended(_) => {
println!("Game over!");
break;
}
_ => (),
}
}
input = rx.recv() => {
match input.unwrap().parse() {
Ok(m) => game.play(m).await.unwrap(),
Err(e) => println!("Failed to parse move: {e}"),
}
}
}
}
}
}