mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-27 04:21:45 +00:00
Compare commits
No commits in common. "68cbd7fd6b5fab4ddb80816a349427e4111481c8" and "4808e7c78a83cc23371b6c91b83e8842b5b2a479" have entirely different histories.
68cbd7fd6b
...
4808e7c78a
1274
Cargo.lock
generated
1274
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -7,6 +7,3 @@ 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"] }
|
||||
|
55
src/cell.rs
55
src/cell.rs
@ -6,9 +6,6 @@ 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 {
|
||||
@ -103,11 +100,7 @@ fn map_num(n: usize, s: usize, e: usize, a: usize, b: usize) -> usize {
|
||||
}
|
||||
|
||||
// Display grid in console
|
||||
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();
|
||||
pub fn display_grid(grid: &Vec<Vec<Cell>>) {
|
||||
for j in 0..HEIGHT {
|
||||
for i in 0..WIDTH {
|
||||
// Get cell in grid
|
||||
@ -118,55 +111,9 @@ pub async fn display_grid(client: &reqwest::Client, grid: &Vec<Vec<Cell>>) -> Re
|
||||
// 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
|
||||
|
15
src/main.rs
15
src/main.rs
@ -1,8 +1,8 @@
|
||||
// Display settings
|
||||
pub const BRIGHTNESS: &str =
|
||||
" .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$";
|
||||
pub const HEIGHT: u32 = 96;
|
||||
pub const WIDTH: u32 = 96;
|
||||
"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
|
||||
pub const HEIGHT: u32 = 25; // Max: 35
|
||||
pub const WIDTH: u32 = 25; // Max: 145
|
||||
pub const FPS: u32 = 10;
|
||||
// Game settings
|
||||
pub const K1: u32 = 2;
|
||||
@ -13,23 +13,18 @@ pub const Q: u32 = 100;
|
||||
mod cell;
|
||||
use cell::*;
|
||||
|
||||
use reqwest::Error;
|
||||
use std::{thread, time::Duration};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
fn main() {
|
||||
// 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(&client, &grid).await?;
|
||||
display_grid(&grid);
|
||||
// Calculate next generation and wait
|
||||
update_grid(&mut grid);
|
||||
thread::sleep(Duration::from_millis(1000 / (FPS as u64)));
|
||||
}
|
||||
|
||||
// Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user