prepare basic crate signature and API functionality

This commit is contained in:
Baipyrus 2024-05-13 15:22:28 +02:00
parent ef80491813
commit 4d15cefdbd
2 changed files with 24 additions and 2 deletions

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
@ -114,6 +121,20 @@ pub fn display_grid(grid: &Vec<Vec<Cell>>) {
}
println!();
}
// 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

@ -20,11 +20,12 @@ use std::{thread, time::Duration};
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)));