[feat] use eti game.db, commit not working, something is wrong with game.id in the client/frontend
This commit is contained in:
@ -16,14 +16,14 @@ pub enum ClientEvent {
|
||||
#[derive(Debug)]
|
||||
pub enum ClientCommand {
|
||||
ListGames,
|
||||
GetGame(u64),
|
||||
GetGame(String),
|
||||
ServerAddr(SocketAddr),
|
||||
}
|
||||
|
||||
/// # Errors
|
||||
pub async fn run(
|
||||
mut rx_control: UnboundedReceiver<ClientCommand>,
|
||||
tx_event: UnboundedSender<ClientEvent>,
|
||||
tx_notify_ui: UnboundedSender<ClientEvent>,
|
||||
) -> eyre::Result<()> {
|
||||
// blocking wait for remote address
|
||||
log::debug!("waiting for server address");
|
||||
@ -75,7 +75,7 @@ pub async fn run(
|
||||
};
|
||||
|
||||
let data = request.encode();
|
||||
log::error!("encoded data: {}", String::from_utf8_lossy(&data));
|
||||
log::debug!("encoded data: {}", String::from_utf8_lossy(&data));
|
||||
|
||||
let stream = match conn.open_bidirectional_stream().await {
|
||||
Ok(stream) => stream,
|
||||
@ -91,104 +91,52 @@ pub async fn run(
|
||||
log::error!("failed to send request to server {:?}", e);
|
||||
}
|
||||
|
||||
if let Ok(Some(data)) = rx.receive().await {
|
||||
log::trace!("server response (raw): {}", String::from_utf8_lossy(&data));
|
||||
let mut all_data: Vec<u8> = Vec::new();
|
||||
while let Ok(Some(data)) = rx.receive().await {
|
||||
log::trace!("msg from server (raw): {}", String::from_utf8_lossy(&data));
|
||||
all_data.extend_from_slice(&data);
|
||||
}
|
||||
log::debug!("{} bytes received from server", all_data.len());
|
||||
log::trace!(
|
||||
"server response (RAW): {}",
|
||||
String::from_utf8_lossy(&all_data)
|
||||
);
|
||||
|
||||
let response = Response::decode(&data);
|
||||
log::trace!(
|
||||
"server response (decoded): {}",
|
||||
String::from_utf8_lossy(&data)
|
||||
);
|
||||
match response {
|
||||
Response::Games(games) => {
|
||||
for game in &games {
|
||||
log::debug!("{game:?}");
|
||||
}
|
||||
let response = Response::decode(&all_data);
|
||||
log::trace!("server response (DECODED): {response:?}");
|
||||
|
||||
if let Err(e) = tx_event.send(ClientEvent::ListGames(games)) {
|
||||
log::error!("failed to send ClientEvent::ListGames to client {e:?}");
|
||||
}
|
||||
match response {
|
||||
Response::Games(games) => {
|
||||
for game in &games {
|
||||
log::debug!("{game:?}");
|
||||
}
|
||||
Response::Game(game) => log::debug!("game received: {game:?}"),
|
||||
Response::GameNotFound(id) => log::debug!("game not found {id}"),
|
||||
Response::InvalidRequest(request_bytes, err) => log::error!(
|
||||
"server says our request was invalid (error: {}): {}",
|
||||
|
||||
if let Err(e) = tx_notify_ui.send(ClientEvent::ListGames(games)) {
|
||||
log::debug!("failed to send ClientEvent::ListGames to client {e:?}");
|
||||
}
|
||||
}
|
||||
Response::Game(game) => log::debug!("game received: {game:?}"),
|
||||
Response::GameNotFound(id) => log::debug!("game not found {id}"),
|
||||
Response::InvalidRequest(request_bytes, err) => log::error!(
|
||||
"server says our request was invalid (error: {}): {}",
|
||||
err,
|
||||
String::from_utf8_lossy(&request_bytes)
|
||||
),
|
||||
Response::EncodingError(err) => {
|
||||
log::error!("server encoding error: {err}");
|
||||
}
|
||||
Response::DecodingError(data, err) => {
|
||||
log::error!(
|
||||
"response decoding error: {} (data: {})",
|
||||
err,
|
||||
String::from_utf8_lossy(&request_bytes)
|
||||
),
|
||||
Response::EncodingError(err) => {
|
||||
log::error!("server encoding error: {err}");
|
||||
}
|
||||
Response::DecodingError(data, err) => {
|
||||
log::error!(
|
||||
"response decoding error: {} (data: {})",
|
||||
err,
|
||||
String::from_utf8_lossy(&data)
|
||||
);
|
||||
}
|
||||
String::from_utf8_lossy(&data)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(err) = tx.close().await {
|
||||
log::error!("failed to close stream: {err}");
|
||||
}
|
||||
if let Err(err) = tx.close().await {
|
||||
log::error!("failed to close stream: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// log::info!("server closed connection");
|
||||
// Ok(())
|
||||
}
|
||||
|
||||
// #[derive(Debug, Parser)]
|
||||
// struct Cli {
|
||||
// /// Server IP address.
|
||||
// #[clap(long, default_value = "127.0.0.1")]
|
||||
// ip: IpAddr,
|
||||
// /// Server port.
|
||||
// #[clap(long, default_value = "13337")]
|
||||
// port: u16,
|
||||
// }
|
||||
|
||||
// #[tokio::main]
|
||||
// async fn main() -> eyre::Result<()> {
|
||||
// let cli = Cli::parse();
|
||||
|
||||
// let (tx_control, rx_control) = tokio::sync::mpsc::unbounded_channel::<ControlMessage>();
|
||||
|
||||
// // Spawn client in a separate task
|
||||
// let client_handle = tokio::spawn(async move {
|
||||
// let remote_addr = SocketAddr::from((cli.ip, cli.port));
|
||||
// Client::run(remote_addr, rx_control).await
|
||||
// });
|
||||
|
||||
// // Handle stdin commands in the main task
|
||||
// let mut stdin = BufReader::new(tokio::io::stdin());
|
||||
// let mut line = String::new();
|
||||
|
||||
// loop {
|
||||
// line.clear();
|
||||
// if stdin.read_line(&mut line).await? == 0 {
|
||||
// break; // EOF reached
|
||||
// }
|
||||
|
||||
// // Trim whitespace and handle commands
|
||||
// match line.trim() {
|
||||
// "list" => {
|
||||
// tx_control.send(ControlMessage::ListGames)?;
|
||||
// }
|
||||
// cmd if cmd.starts_with("get ") => {
|
||||
// if let Ok(id) = cmd[4..].trim().parse::<u64>() {
|
||||
// tx_control.send(ControlMessage::GetGame(id))?;
|
||||
// } else {
|
||||
// println!("Invalid game ID");
|
||||
// }
|
||||
// }
|
||||
// "quit" | "exit" => break,
|
||||
// "" => continue,
|
||||
// _ => println!("Unknown command. Available commands: list, get <id>, quit"),
|
||||
// }
|
||||
// }
|
||||
|
||||
// client_handle.await??;
|
||||
// Ok(())
|
||||
// }
|
||||
|
Reference in New Issue
Block a user