Compare commits

...

6 Commits

4 changed files with 1341 additions and 6 deletions

1274
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,3 +7,6 @@ edition = "2021"
[dependencies]
rand = "0.8.5"
reqwest = { version = "0.12.4", features = ["json", "multipart"] }
serde_json = "1.0.117"
tokio = { version = "1.37.0", features = ["full"] }

View File

@ -6,6 +6,9 @@ use crate::K2;
use crate::Q;
use crate::WIDTH;
use reqwest::Error;
use std::collections::HashMap;
// Class to store information about each cell
#[derive(PartialEq)]
pub struct Cell {
@ -100,7 +103,11 @@ fn map_num(n: usize, s: usize, e: usize, a: usize, b: usize) -> usize {
}
// Display grid in console
pub fn display_grid(grid: &Vec<Vec<Cell>>) {
pub async fn display_grid(client: &reqwest::Client, grid: &Vec<Vec<Cell>>) -> Result<(), Error> {
let w = 192 / WIDTH;
let h = 192 / HEIGHT;
let mut map_list = Vec::new();
for j in 0..HEIGHT {
for i in 0..WIDTH {
// Get cell in grid
@ -111,9 +118,55 @@ pub fn display_grid(grid: &Vec<Vec<Cell>>) {
// Print cell to console
let index = map_num(state, 0, Q as usize, 0, BRIGHTNESS.len());
print!("{}", BRIGHTNESS.chars().nth(index).unwrap());
// Add object to json
let scale = map_num(state, 0, Q as usize, 0, 256);
let xw = ((x as u32) * w).to_string();
let yh = ((y as u32) * h).to_string();
// Additional string copies
let ss = scale.to_string();
let ws = w.to_string();
let hs = h.to_string();
// Insert data into hashmap
let mut color = HashMap::new();
color.insert("endpoint", "color".to_string());
color.insert("r", ss.clone());
color.insert("g", ss.clone());
color.insert("b", ss);
let mut rect = HashMap::new();
rect.insert("endpoint", "rectangle".to_string());
rect.insert("x", xw);
rect.insert("y", yh);
rect.insert("w", ws);
rect.insert("h", hs);
// Add data to list
map_list.push(color);
map_list.push(rect);
}
println!();
}
// Create UPDATE instruction
let mut update = HashMap::new();
update.insert("endpoint", "update".to_string());
map_list.push(update);
// Create formdata serializable object
let mut fdata = HashMap::new();
fdata.insert("instructions", serde_json::to_string(&map_list).unwrap());
// Send POST request
let response = client
.post("http://10.42.0.1:8080/instructions")
.form(&fdata)
.send()
.await?;
println!("Status Code: {}", response.status());
Ok(())
}
// Calculate next generation

View File

@ -1,8 +1,8 @@
// Display settings
pub const BRIGHTNESS: &str =
"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
pub const HEIGHT: u32 = 25; // Max: 35
pub const WIDTH: u32 = 25; // Max: 145
" .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$";
pub const HEIGHT: u32 = 96;
pub const WIDTH: u32 = 96;
pub const FPS: u32 = 10;
// Game settings
pub const K1: u32 = 2;
@ -13,18 +13,23 @@ pub const Q: u32 = 100;
mod cell;
use cell::*;
use reqwest::Error;
use std::{thread, time::Duration};
fn main() {
#[tokio::main]
async fn main() -> Result<(), Error> {
// Initialize grid randomly
let mut grid = init_grid(None);
let client = reqwest::Client::new();
loop {
// Display current generation
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
display_grid(&grid);
display_grid(&client, &grid).await?;
// Calculate next generation and wait
update_grid(&mut grid);
thread::sleep(Duration::from_millis(1000 / (FPS as u64)));
}
// Ok(())
}