mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-27 04:21:45 +00:00
Compare commits
6 Commits
4808e7c78a
...
68cbd7fd6b
Author | SHA1 | Date | |
---|---|---|---|
68cbd7fd6b | |||
4d15cefdbd | |||
ef80491813 | |||
0e893fd2c2 | |||
c61e9cc69e | |||
092884485e |
1274
Cargo.lock
generated
1274
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -7,3 +7,6 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
reqwest = { version = "0.12.4", features = ["json", "multipart"] }
|
||||||
|
serde_json = "1.0.117"
|
||||||
|
tokio = { version = "1.37.0", features = ["full"] }
|
||||||
|
55
src/cell.rs
55
src/cell.rs
@ -6,6 +6,9 @@ use crate::K2;
|
|||||||
use crate::Q;
|
use crate::Q;
|
||||||
use crate::WIDTH;
|
use crate::WIDTH;
|
||||||
|
|
||||||
|
use reqwest::Error;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// Class to store information about each cell
|
// Class to store information about each cell
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub struct Cell {
|
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
|
// 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 j in 0..HEIGHT {
|
||||||
for i in 0..WIDTH {
|
for i in 0..WIDTH {
|
||||||
// Get cell in grid
|
// Get cell in grid
|
||||||
@ -111,9 +118,55 @@ pub fn display_grid(grid: &Vec<Vec<Cell>>) {
|
|||||||
// Print cell to console
|
// Print cell to console
|
||||||
let index = map_num(state, 0, Q as usize, 0, BRIGHTNESS.len());
|
let index = map_num(state, 0, Q as usize, 0, BRIGHTNESS.len());
|
||||||
print!("{}", BRIGHTNESS.chars().nth(index).unwrap());
|
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!();
|
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
|
// Calculate next generation
|
||||||
|
15
src/main.rs
15
src/main.rs
@ -1,8 +1,8 @@
|
|||||||
// Display settings
|
// Display settings
|
||||||
pub const BRIGHTNESS: &str =
|
pub const BRIGHTNESS: &str =
|
||||||
"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
|
" .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$";
|
||||||
pub const HEIGHT: u32 = 25; // Max: 35
|
pub const HEIGHT: u32 = 96;
|
||||||
pub const WIDTH: u32 = 25; // Max: 145
|
pub const WIDTH: u32 = 96;
|
||||||
pub const FPS: u32 = 10;
|
pub const FPS: u32 = 10;
|
||||||
// Game settings
|
// Game settings
|
||||||
pub const K1: u32 = 2;
|
pub const K1: u32 = 2;
|
||||||
@ -13,18 +13,23 @@ pub const Q: u32 = 100;
|
|||||||
mod cell;
|
mod cell;
|
||||||
use cell::*;
|
use cell::*;
|
||||||
|
|
||||||
|
use reqwest::Error;
|
||||||
use std::{thread, time::Duration};
|
use std::{thread, time::Duration};
|
||||||
|
|
||||||
fn main() {
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Error> {
|
||||||
// Initialize grid randomly
|
// Initialize grid randomly
|
||||||
let mut grid = init_grid(None);
|
let mut grid = init_grid(None);
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Display current generation
|
// Display current generation
|
||||||
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
|
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
|
||||||
display_grid(&grid);
|
display_grid(&client, &grid).await?;
|
||||||
// Calculate next generation and wait
|
// Calculate next generation and wait
|
||||||
update_grid(&mut grid);
|
update_grid(&mut grid);
|
||||||
thread::sleep(Duration::from_millis(1000 / (FPS as u64)));
|
thread::sleep(Duration::from_millis(1000 / (FPS as u64)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user