From de0546049794445e01859e5fc14d18b411ca45d3 Mon Sep 17 00:00:00 2001 From: mux Date: Sat, 17 Aug 2024 02:36:32 +0200 Subject: [PATCH] added uno protocol --- README.md | 2 +- src/lib.rs | 3 +- src/uno_logic.rs | 39 ++++++++++++++-- src/uno_protocol.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 src/uno_protocol.rs diff --git a/README.md b/README.md index 9f69400..f1bd45c 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -Making a Uno server and client, so I can make a Uno bot. \ No newline at end of file +I hate uno \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 438f5d6..6f26d91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ -pub mod uno_logic; \ No newline at end of file +pub mod uno_logic; +pub mod uno_protocol; \ No newline at end of file diff --git a/src/uno_logic.rs b/src/uno_logic.rs index 0f6ad2d..272127a 100644 --- a/src/uno_logic.rs +++ b/src/uno_logic.rs @@ -61,8 +61,8 @@ pub struct PlayerConnection<'a>{ } pub struct Card { - value: CardValue, - color: CardColors, + pub value: CardValue, + pub color: CardColors, } pub struct GameState<'a>{ @@ -122,6 +122,40 @@ impl Copy for CardValue {} //struct implementations +impl CardValue { + pub fn to_string<'a>(self) -> &'a str{ + match self { + CardValue::PLUS_FOUR => "PLUS_FOUR", + CardValue::CHANGE_COLOR => "CHANGE_COLOR", + CardValue::PLUS_TWO => "PLUS_TWO", + CardValue::SKIP => "SKIP", + CardValue::REVERSE => "REVERSE", + CardValue::ZERO => "ZERO", + CardValue::ONE => "ONE", + CardValue::TWO => "TWO", + CardValue::THREE => "THREE", + CardValue::FOUR => "FOUR", + CardValue::FIVE => "FIVE", + CardValue::SIX => "SIX", + CardValue::SEVEN => "SEVEN", + CardValue::EIGHT => "EIGHT", + CardValue::NINE => "NINE" + } + } +} + +impl CardColors { + pub fn to_string<'a>(self) -> &'a str{ + match self { + CardColors::BLACK => "BLACK", + CardColors::BLUE => "BLUE", + CardColors::GREEN => "GREEN", + CardColors::YELLOW => "YELLOW", + CardColors::RED => "RED" + } + } +} + impl PlayerState<'_> { pub fn new(player_name : &str, cards : Vec) -> PlayerState{ PlayerState { @@ -132,7 +166,6 @@ impl PlayerState<'_> { } } - impl PlayerConnection<'_> { pub fn new(player_name : &str, reader : BufReader, writer : BufWriter) -> PlayerConnection{ PlayerConnection { diff --git a/src/uno_protocol.rs b/src/uno_protocol.rs new file mode 100644 index 0000000..f2d9720 --- /dev/null +++ b/src/uno_protocol.rs @@ -0,0 +1,108 @@ +use crate::uno_logic::{Card, CardValue, CardColors}; +const PROTOCOL_ERROR : &'static str = "protocol error"; +// enums, structs, types +struct Turn{ + card_to_be_played : Card, + said_uno : bool, +} + +/* uno protocol +card_value: +card_color : +said_uno : + + +*/ +impl Turn{ + + fn get_value(card_value_line : &str) -> Result { + match &card_value_line.split(":").collect::>().get(1).unwrap()[..]{ + "PLUS_FOUR" => { Ok(CardValue::PLUS_FOUR) } + "CHANGE_COLOR" => {Ok(CardValue::CHANGE_COLOR)} + "PLUS_TWO" => {Ok(CardValue::PLUS_TWO)} + "SKIP" => {Ok(CardValue::SKIP)} + "REVERSE" => {Ok(CardValue::REVERSE)} + "ZERO" => {Ok(CardValue::ZERO)} + "ONE" => {Ok(CardValue::ONE)} + "TWO" => {Ok(CardValue::TWO)} + "THREE" => {Ok(CardValue::THREE)} + "FOUR" => {Ok(CardValue::FOUR)} + "FIVE" => {Ok(CardValue::FIVE)} + "SIX" => {Ok(CardValue::SIX)} + "SEVEN" => {Ok(CardValue::SEVEN)} + "EIGHT" => {Ok(CardValue::EIGHT)} + "NINE" => {Ok(CardValue::NINE)} + _ => {Err(String::from("protocol error"))} + } + } + + fn get_color(card_color_line : &str) -> Result { + match &card_color_line.split(":").collect::>().get(1).unwrap()[..] { + "BLACK" => Ok(CardColors::BLACK), + "BLUE" => Ok(CardColors::BLUE), + "GREEN" => Ok(CardColors::GREEN), + "YELLOW" => Ok(CardColors::YELLOW), + "RED" => Ok(CardColors::RED), + _ => Err(String::from(PROTOCOL_ERROR)) + } + } + + fn said_uno (said_uno_line : &str) -> Result { + match &said_uno_line.split(":").collect::>().get(1).unwrap()[..] { + "true" => Ok(true), + "false" => Ok(false), + _ => Err(String::from(PROTOCOL_ERROR)) + } + } + + pub fn parse_turn(protocol_input : String) -> Result{ + let protocol_lines : Vec<&str> = protocol_input.lines().collect(); + let card_value_line = match protocol_lines.get(0) { + Some(card_value) => card_value.to_owned(), + None => return Err(String::from(PROTOCOL_ERROR)) + }; + let card_value = match Turn::get_value(card_value_line) { + Ok(card_value) => card_value, + Err(error_msg) => return Err(error_msg) + }; + + let card_color_line = match protocol_lines.get(1) { + Some(card_color) => card_color.to_owned(), + None => return Err(String::from(PROTOCOL_ERROR)) + }; + let card_color = match Turn::get_color(card_color_line) { + Ok(card_color) => card_color, + Err(error_msg) => return Err(error_msg) + }; + + let said_uno_line = match protocol_lines.get(2) { + Some(said_uno) => said_uno.to_owned(), + None => return Err(String::from(PROTOCOL_ERROR)) + }; + let said_uno = match Turn::said_uno(said_uno_line) { + Ok(said_uno) => said_uno, + Err(error_msg) => return Err(error_msg) + }; + + return Ok(Turn{card_to_be_played : Card{value : card_value, color : card_color}, said_uno}); + } + + pub fn encode_turn (turn : Turn) -> String{ + let mut encoded_turn = String::new(); + encoded_turn.push_str("card_value:"); + encoded_turn.push_str(turn.card_to_be_played.value.to_string()); + encoded_turn.push('\n'); + encoded_turn.push_str("card_color:"); + encoded_turn.push_str(&turn.card_to_be_played.color.to_string()); + encoded_turn.push('\n'); + encoded_turn.push_str("said_uno:"); + if turn.said_uno { + encoded_turn.push_str("true"); + } + else { + encoded_turn.push_str("false"); + } + encoded_turn.push('\n'); + return encoded_turn; + } +} \ No newline at end of file