From 655b7eacd1b6ceab16e842c1c0c7aeac2d01783d Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 19 Oct 2021 19:38:15 +0200 Subject: [PATCH] Init commit --- .gitignore | 1 + Cargo.lock | 89 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 +++++ src/board.rs | 24 +++++++++++ src/main.rs | 63 ++++++++++++++++++++++++++++ src/models.rs | 0 src/models/board.rs | 5 +++ src/models/networking.rs | 17 ++++++++ src/models/piece.rs | 37 +++++++++++++++++ src/netClient.rs | 20 +++++++++ src/netServer.rs | 55 +++++++++++++++++++++++++ 11 files changed, 321 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/board.rs create mode 100644 src/main.rs create mode 100644 src/models.rs create mode 100644 src/models/board.rs create mode 100644 src/models/networking.rs create mode 100644 src/models/piece.rs create mode 100644 src/netClient.rs create mode 100644 src/netServer.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..21b1d99 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,89 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "itoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" + +[[package]] +name = "khetamine" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "proc-macro2" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "serde" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..54803b6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "khetamine" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1.0.130", features = ["derive"] } +serde_json = "1.0.68" \ No newline at end of file diff --git a/src/board.rs b/src/board.rs new file mode 100644 index 0000000..c236eed --- /dev/null +++ b/src/board.rs @@ -0,0 +1,24 @@ +// bored +use crate::models::piece::Piece; + + +fn print_board(board: Board) { + println!("{}", board.iter().map(|row| row.iter().map(|p| p.to_string())..join(" ")).join("\n")); +} + +fn main() { + // let board = vec![ + // vec![Piece::Pharaoh], + // vec![], + // vec![], + // vec![], + // ]; + + // print_board(board); +} + + +fn is_legal_move() -> bool { + return true; +} + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..91daa5e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,63 @@ +use std::env; +mod models { + pub mod board; + pub mod piece; + pub mod networking; +} + +fn print_help() { + println!("Ey fam, hmu with dat '--serve' or '--join
'") +} + +fn start_game() { + println!("OK"); +} + +fn main() { + let args: Vec = env::args().collect(); + if (args.len() == 2) && ((args[1] == "--serve") | (args[1] == "serve")) { + + //Start game server + println!("Serving!"); + start_game(); + + } else if (args.len() == 3) && ((args[1] == "--join") | (args[1] == "join")) { + + //Join game server + println!("Joining!"); + start_game(); + + } else if (args.len() == 2) && (args[1] == "--help") { + + print_help(); + + } else { + + println!("What do you mean with {:?}\n", args); + print_help(); + + } + + println!("{}", serialize_move(models::networking::Action::Rotate { + from: (2, 2), + rot: models::networking::RotationDirection::Positive + })); + + +} + + + +fn serialize_move(action: models::networking::Action) -> str { + let serialized = serde_json::to_string(&action); + match serialized { + Ok(s) => { + println!("OK: {}", s); + return &s; + }, + Err(_e) => { + println!("Couldn't serialize..."); + return (""); + } + } + } \ No newline at end of file diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/models/board.rs b/src/models/board.rs new file mode 100644 index 0000000..ddf029b --- /dev/null +++ b/src/models/board.rs @@ -0,0 +1,5 @@ + +// use crate::models::piece::Piece; + +// #[derive(Display)] +pub type Board = Vec>; \ No newline at end of file diff --git a/src/models/networking.rs b/src/models/networking.rs new file mode 100644 index 0000000..03de6d6 --- /dev/null +++ b/src/models/networking.rs @@ -0,0 +1,17 @@ +use serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize)] +pub enum RotationDirection { + Positive, + Negative +} + +pub type Position = (u8, u8); + +#[derive(Serialize, Deserialize)] +#[serde(tag = "type")] +pub enum Action { + Move {from: Position, to: Position}, + Rotate {from: Position, rot: RotationDirection}, + Swap {from: Position, to: Position} +} \ No newline at end of file diff --git a/src/models/piece.rs b/src/models/piece.rs new file mode 100644 index 0000000..ebf52e5 --- /dev/null +++ b/src/models/piece.rs @@ -0,0 +1,37 @@ +use std::fmt; + +enum DjedDirection { + NorthWest, + NorthEast, +} + +enum PyramidDirection { + NorthWest, + NorthEast, + SouthEast, + SouthWest, +} + +enum Stack { + Single, + Double, +} + +// #[derive(Display)] +pub enum Piece { + Pharaoh, + Djed(DjedDirection), + Pyramid(PyramidDirection), + Obelisk(Stack) +} + +impl fmt::Display for Piece { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Piece::Pharaoh => write!(f, "Pharaoh"), + Piece::Djed(_) => write!(f, "Djed"), + Piece::Pyramid(_) => write!(f, "Pyramid"), + Piece::Obelisk(_) => write!(f, "Obelisk"), + } + } +} \ No newline at end of file diff --git a/src/netClient.rs b/src/netClient.rs new file mode 100644 index 0000000..d291c5d --- /dev/null +++ b/src/netClient.rs @@ -0,0 +1,20 @@ +//Go brooklyn! + +use std::io::Write; +use std::net::TcpStream; + +fn client_connect(addr: String) -> () { + const PORT: &str ="4446"; + let stream = TcpStream::connect(addr.to_owned() + ":" + PORT); + match stream { + Ok(mut s) => { + let _res = s.write(b"Testing tcp! wack...\n"); () + }, + Err(e) => println!("[ERR]: Did not connect to port:\n{}", e), + } +} + +// fn main() { +// client_connect(String::from("127.0.0.1")); +// } + diff --git a/src/netServer.rs b/src/netServer.rs new file mode 100644 index 0000000..1f0e0c0 --- /dev/null +++ b/src/netServer.rs @@ -0,0 +1,55 @@ +use std::net::{TcpListener, TcpStream}; +use std::io; +use std::io::Write; +use std::io::Read; + +fn handle_client(mut stream: TcpStream) { + let _res = stream.write(b"Hello from the other side.\n"); + let mut line; + loop { + line = [0; 512]; + let result = stream.read(&mut line); + match result { + Ok(s) => { + if s > 1 { + println!("{}",s); + } else { + println!("Empty msg!"); + break; + } + (); + }, + Err(_e) => { + println!("Oh no"); + break; + } + } + + }; + () +} + +fn start_listener(address: &str) { + println!("Listening on {}", address); + let listener = TcpListener::bind(address).unwrap(); + for stream in listener.incoming() { + match stream { + Ok(s) => { + handle_client(s); + println!("Lost connection, retrying.\n"); + }, + Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { + println!("What's going on here - Axel F"); + break; + }, + Err(e) => panic!("encountered IO error: {}", e), + } + } +} + +fn main() { + const PORT: &str ="4446"; + let full_address: String = "0.0.0.0:".to_owned() + PORT; + + start_listener(&full_address); +} \ No newline at end of file