Compare commits

...

2 Commits

Author SHA1 Message Date
e9bd89c8a5 Client index is public 2022-06-17 13:12:54 -05:00
9c55f69a91 Experimental game controllers 2022-06-17 11:46:04 -05:00
4 changed files with 102 additions and 2 deletions

2
Cargo.lock generated
View File

@ -756,7 +756,7 @@ checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868"
[[package]]
name = "gamenite"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"image",
"serde",

View File

@ -1,7 +1,7 @@
[package]
name = "gamenite"
description = "A graph library for board games"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "GPL-3.0-or-later"
homepage = "https://git.danejohnson.org/dane/gamenite"

99
src/controller.rs Normal file
View File

@ -0,0 +1,99 @@
use std::sync::mpsc;
use mpsc::{ Sender, Receiver, SendError, RecvError };
use std::collections::VecDeque;
pub struct Controller<M> {
server: Option<Server<M>>,
clients: VecDeque<Client<M>>,
}
pub struct Server<M> {
txs: Vec<Sender<M>>,
rxs: Vec<Receiver<M>>,
info: Receiver<(usize, M)>,
}
pub struct Client<M> {
pub idx: usize,
tx: Sender<M>,
rx: Receiver<M>,
info: Sender<(usize, M)>,
}
impl<M> Controller<M> {
pub fn new(n: usize) -> Self {
let (info_tx, info_rx) = mpsc::channel::<(usize, M)>();
let mut c = Controller {
server: Some(Server {
txs: Vec::new(),
rxs: Vec::new(),
info: info_rx,
}),
clients: VecDeque::new(),
};
for idx in 0..n {
let (server_tx, client_rx) = mpsc::channel();
let (client_tx, server_rx) = mpsc::channel();
match c.server.as_mut() {
Some(s) => {
s.txs.push(server_tx);
s.rxs.push(server_rx);
},
None => unreachable!(),
}
c.clients.push_back(Client {
idx,
tx: client_tx,
rx: client_rx,
info: info_tx.clone(),
});
}
c
}
pub fn server(&mut self) -> Option<Server<M>> {
self.server.take()
}
pub fn client(&mut self) -> Option<Client<M>> {
self.clients.pop_front()
}
}
impl<M> Server<M> {
pub fn send(&self, idx: usize, msg: M) -> Result<(), SendError<M>> {
self.txs[idx].send(msg)
}
pub fn recv(&self, idx: usize) -> Result<M, RecvError> {
self.rxs[idx].recv()
}
pub fn info(&self) -> Result<(usize, M), RecvError> {
self.info.recv()
}
}
impl <M> Server<M>
where M: Clone {
pub fn broadcast(&self, msg: M) -> Result<(), SendError<M>> {
for tx in &self.txs {
tx.send(M::clone(&msg))?;
}
Ok(())
}
}
impl<M> Client<M> {
pub fn send(&self, m: M) -> Result<(), SendError<M>> {
self.tx.send(m)
}
pub fn recv(&self) -> Result<M, RecvError> {
self.rx.recv()
}
pub fn info(&self, msg: M) -> Result<(), SendError<(usize, M)>> {
self.info.send((self.idx, msg))
}
}

View File

@ -25,6 +25,7 @@
//! Finally, we provide an [io] package to facilitate loading and saving these boards, and a related image to
//! a compressed file.
pub mod io;
pub mod controller;
use serde::{ Serialize, Deserialize };
use std::collections::{ HashMap, HashSet };