added message_kind
This commit is contained in:
parent
82366bd923
commit
0d5c18ce09
6 changed files with 63 additions and 21 deletions
|
@ -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]
|
#[tokio::main]
|
||||||
pub async fn main() -> anyhow::Result<()> {
|
pub async fn main() -> anyhow::Result<()> {
|
||||||
let mut client = Client::connect("127.0.0.1", 8080).await?;
|
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 {
|
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.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use tokio::{io::AsyncWriteExt, net::TcpStream};
|
use tokio::{io::AsyncWriteExt, net::TcpStream};
|
||||||
|
|
||||||
use crate::message::Message;
|
use crate::{message::Message, player::Player};
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
pub server_host: String,
|
pub server_host: String,
|
||||||
|
@ -25,4 +25,8 @@ impl Client {
|
||||||
self.stream.write_all(&message.encode()).await?;
|
self.stream.write_all(&message.encode()).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_player() -> Option<Player> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_MOVE: u8 = 0;
|
||||||
pub const MESSAGE_KIND_PLAYER_SETUP: u8 = 1;
|
pub const MESSAGE_KIND_CREATE_PLAYER: u8 = 1;
|
||||||
pub const MESSAGE_KIND_LOBBY_SETUP: u8 = 2;
|
pub const MESSAGE_KIND_CREATE_LOBBY: u8 = 2;
|
||||||
|
|
||||||
|
|
|
@ -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
|
// 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
|
// idk i just want to layout something, really didn't think much
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum MessageKind {
|
pub enum MessageKind {
|
||||||
Move(u8),
|
Move,
|
||||||
PlayerSetup(u8),
|
CreatePlayer,
|
||||||
LobbySetup(u8),
|
CreateLobby,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
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 content = content.into();
|
||||||
let length = content.len() as u16;
|
let length = content.len() as u16;
|
||||||
|
|
||||||
// add message kind to constructor
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
message_kind,
|
||||||
length,
|
length,
|
||||||
content,
|
content,
|
||||||
})
|
})
|
||||||
|
@ -42,13 +42,27 @@ impl Message {
|
||||||
return Err(anyhow::anyhow!("Invalid message length"));
|
return Err(anyhow::anyhow!("Invalid message length"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let length = u16::from_be_bytes([buffer[0], buffer[1]]);
|
let message_kind = MessageKind::try_from(buffer[0]).expect("Error parsing the MessageKind");
|
||||||
let content = String::from_utf8(buffer[2..2 + length as usize].to_vec())?;
|
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 {
|
Ok(Self {
|
||||||
|
message_kind,
|
||||||
length,
|
length,
|
||||||
content,
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
use tokio::{io::AsyncReadExt, net::TcpStream};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sha256::digest;
|
use sha256::digest;
|
||||||
|
|
||||||
use crate::card::Card;
|
use crate::{card::Card, message_read::MessageReader};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Eq, PartialEq, Hash, Clone)]
|
#[derive(Serialize, Deserialize, Eq, PartialEq, Hash, Clone)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
|
@ -19,18 +20,18 @@ pub struct Player {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 hand_empty: Vec<Card> = Vec::new();
|
||||||
|
|
||||||
let to_digest: String = addr.to_string();
|
let to_digest: String = addr.to_string();
|
||||||
let id = digest(to_digest);
|
let id = digest(to_digest);
|
||||||
|
|
||||||
Player {
|
Ok(Some(Player {
|
||||||
addr,
|
addr,
|
||||||
id,
|
id,
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
hand: hand_empty,
|
hand: hand_empty,
|
||||||
}
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_addr(self) -> SocketAddr {
|
pub fn get_addr(self) -> SocketAddr {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use tokio::{io::AsyncReadExt, net::TcpListener};
|
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 struct Server {
|
||||||
pub host: String,
|
pub host: String,
|
||||||
|
@ -51,4 +52,8 @@ impl Server {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn send_message(addr: SocketAddr, message: Message) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue