added message_kind

This commit is contained in:
clizia 2025-01-28 16:33:46 +01:00
parent 82366bd923
commit 0d5c18ce09
6 changed files with 63 additions and 21 deletions

View file

@ -1,15 +1,33 @@
use lib::{client::Client, message::Message};
use lib::{client::Client, message::{Message, MessageKind}};
use crossterm::{
execute,
terminal::{
Clear,
ClearType,
enable_raw_mode,
disable_raw_mode
},
event::Event,
};
use std::io::{self, Write};
#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
let mut client = Client::connect("127.0.0.1", 8080).await?;
enable_raw_mode()?;
execute!(
io::stdout(),
Clear(ClearType::All),
)?;
for i in 1..11 {
let message = Message::new(format!("Hello toto x {}!!!", i));
let message = Message::new(lib::message::MessageKind::Move, format!("Hello toto x {}!!!", i));
client.send_message(message.unwrap().clone()).await?;
}
client.send_message(Message::new("EXIT").unwrap()).await?;
client.send_message(Message::new(MessageKind::Move, "EXIT").unwrap()).await?;
disable_raw_mode()?;
Ok(())
}

View file

@ -1,6 +1,6 @@
use tokio::{io::AsyncWriteExt, net::TcpStream};
use crate::message::Message;
use crate::{message::Message, player::Player};
pub struct Client {
pub server_host: String,
@ -25,4 +25,8 @@ impl Client {
self.stream.write_all(&message.encode()).await?;
Ok(())
}
pub async fn create_player() -> Option<Player> {
todo!()
}
}

View file

@ -1,5 +1,5 @@
pub const METADATA_SIZE: usize = 2;
pub const METADATA_SIZE: usize = 3;
pub const MESSAGE_KIND_MOVE: u8 = 0;
pub const MESSAGE_KIND_PLAYER_SETUP: u8 = 1;
pub const MESSAGE_KIND_LOBBY_SETUP: u8 = 2;
pub const MESSAGE_KIND_CREATE_PLAYER: u8 = 1;
pub const MESSAGE_KIND_CREATE_LOBBY: u8 = 2;

View file

@ -1,4 +1,4 @@
use crate::constant::METADATA_SIZE;
use crate::constant::{MESSAGE_KIND_CREATE_LOBBY, MESSAGE_KIND_CREATE_PLAYER, MESSAGE_KIND_MOVE, METADATA_SIZE};
// everything here needs to be adjusted for the deserialized json
//
@ -12,18 +12,18 @@ pub struct Message {
// idk i just want to layout something, really didn't think much
#[derive(Debug, Clone)]
pub enum MessageKind {
Move(u8),
PlayerSetup(u8),
LobbySetup(u8),
Move,
CreatePlayer,
CreateLobby,
}
impl Message {
pub fn new(content: impl Into<String>) -> anyhow::Result<Self> {
pub fn new(message_kind: MessageKind, content: impl Into<String>) -> anyhow::Result<Self> {
let content = content.into();
let length = content.len() as u16;
// add message kind to constructor
Ok(Self {
message_kind,
length,
content,
})
@ -42,13 +42,27 @@ impl Message {
return Err(anyhow::anyhow!("Invalid message length"));
}
let length = u16::from_be_bytes([buffer[0], buffer[1]]);
let content = String::from_utf8(buffer[2..2 + length as usize].to_vec())?;
let message_kind = MessageKind::try_from(buffer[0]).expect("Error parsing the MessageKind");
let length = u16::from_be_bytes([buffer[1], buffer[2]]);
let content = String::from_utf8(buffer[3..3 + length as usize].to_vec())?;
// add message kind to constructor
Ok(Self {
message_kind,
length,
content,
})
}
}
impl TryFrom<u8> for MessageKind {
type Error = &'static str;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
MESSAGE_KIND_MOVE => Ok(MessageKind::Move),
MESSAGE_KIND_CREATE_PLAYER => Ok(MessageKind::CreatePlayer),
MESSAGE_KIND_CREATE_LOBBY => Ok(MessageKind::CreateLobby),
_ => Err("invalid MessageKind byte")
}
}
}

View file

@ -1,8 +1,9 @@
use tokio::{io::AsyncReadExt, net::TcpStream};
use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
use sha256::digest;
use crate::card::Card;
use crate::{card::Card, message_read::MessageReader};
#[derive(Serialize, Deserialize, Eq, PartialEq, Hash, Clone)]
pub struct Player {
@ -19,18 +20,18 @@ pub struct Player {
}
impl Player {
pub fn new(addr: SocketAddr, name: &str) -> Self {
pub async fn new(addr: SocketAddr, name: &str) -> anyhow::Result<Option<Self>> {
let hand_empty: Vec<Card> = Vec::new();
let to_digest: String = addr.to_string();
let id = digest(to_digest);
Player {
Ok(Some(Player {
addr,
id,
name: name.to_string(),
hand: hand_empty,
}
}))
}
pub fn get_addr(self) -> SocketAddr {

View file

@ -1,5 +1,6 @@
use tokio::{io::AsyncReadExt, net::TcpListener};
use crate::message_read::MessageReader;
use std::net::SocketAddr;
use crate::{message::Message, message_read::MessageReader};
pub struct Server {
pub host: String,
@ -51,4 +52,8 @@ impl Server {
Ok(())
}
pub async fn send_message(addr: SocketAddr, message: Message) {
todo!()
}
}