Init commit

This commit is contained in:
2021-10-19 19:38:15 +02:00
commit 655b7eacd1
11 changed files with 321 additions and 0 deletions

24
src/board.rs Normal file
View File

@@ -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;
}

63
src/main.rs Normal file
View File

@@ -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 <address>'")
}
fn start_game() {
println!("OK");
}
fn main() {
let args: Vec<String> = 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 ("");
}
}
}

0
src/models.rs Normal file
View File

5
src/models/board.rs Normal file
View File

@@ -0,0 +1,5 @@
// use crate::models::piece::Piece;
// #[derive(Display)]
pub type Board = Vec<Vec<crate::models::piece::Piece>>;

17
src/models/networking.rs Normal file
View File

@@ -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}
}

37
src/models/piece.rs Normal file
View File

@@ -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"),
}
}
}

20
src/netClient.rs Normal file
View File

@@ -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"));
// }

55
src/netServer.rs Normal file
View File

@@ -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);
}