added uno protocol
This commit is contained in:
parent
d4045deef2
commit
de05460497
|
|
@ -1 +1 @@
|
|||
Making a Uno server and client, so I can make a Uno bot.
|
||||
I hate uno
|
||||
|
|
@ -1 +1,2 @@
|
|||
pub mod uno_logic;
|
||||
pub mod uno_protocol;
|
||||
|
|
@ -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<Card>) -> PlayerState{
|
||||
PlayerState {
|
||||
|
|
@ -132,7 +166,6 @@ impl PlayerState<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl PlayerConnection<'_> {
|
||||
pub fn new(player_name : &str, reader : BufReader<TcpStream>, writer : BufWriter<TcpStream>) -> PlayerConnection{
|
||||
PlayerConnection {
|
||||
|
|
|
|||
108
src/uno_protocol.rs
Normal file
108
src/uno_protocol.rs
Normal file
|
|
@ -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<CardValue, String> {
|
||||
match &card_value_line.split(":").collect::<Vec<&str>>().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<CardColors, String> {
|
||||
match &card_color_line.split(":").collect::<Vec<&str>>().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<bool, String> {
|
||||
match &said_uno_line.split(":").collect::<Vec<&str>>().get(1).unwrap()[..] {
|
||||
"true" => Ok(true),
|
||||
"false" => Ok(false),
|
||||
_ => Err(String::from(PROTOCOL_ERROR))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_turn(protocol_input : String) -> Result<Turn, String>{
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user