#game-client #async #tak #client #game #async-io

tokio-takconnect

Asynchronous Playtak client

6 releases

0.2.2 Jul 22, 2022
0.2.1 Jul 12, 2022
0.1.3 Apr 14, 2022
0.1.0 Mar 29, 2022

#1452 in Asynchronous

23 downloads per month

MIT license

35KB
824 lines

tokio-takconnect

A work-in-progress, asynchronous Playtak client.

Example project

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}"),
                    }
                }
            }
        }
    }
}

Dependencies

~6–18MB
~266K SLoC