Skip to content

Instantly share code, notes, and snippets.

@fogti
Created December 27, 2020 22:55
Show Gist options
  • Save fogti/2b8ede625d734b3c3d4dd9ad8ef458ed to your computer and use it in GitHub Desktop.
Save fogti/2b8ede625d734b3c3d4dd9ad8ef458ed to your computer and use it in GitHub Desktop.
a simple pixelflut client, draws circles...
[package]
name = "pxlf-client"
version = "0.1.0"
authors = ["Erik Zscheile <zseri.devel@ytrizja.de>"]
edition = "2018"
[dependencies]
fastrand = "1.4"
crossbeam-channel = "0.5"
use std::io::Write;
use std::net::TcpStream;
const PXLF_SERVER: &str = "rc3.schenklklopfer.de:1234";
const PXLF_MAX_X: u16 = 1920;
const PXLF_MAX_Y: u16 = 1080;
pub fn kreis(r: u16, mx: u16, my: u16) -> impl Iterator<Item = (u16, u16)> {
use std::f64::consts::FRAC_PI_2;
let steps: u16 = r * r;
let steps64 = f64::from(steps);
let (r, mx, my): (f64, f64, f64) = (r.into(), mx.into(), my.into());
(0..=steps)
.flat_map(move |i| {
let i = (f64::from(i) * FRAC_PI_2) / steps64;
let (s, c) = (i.sin() * r, i.cos() * r);
vec![(c, s), (c, -s), (-c, s), (-c, -s)].into_iter()
})
.flat_map(|(x, y)| vec![(x, y), (y, x)].into_iter())
.map(move |(x, y)| ((x + mx) as u16, (y + my) as u16))
}
fn main() {
/*
let lines = {
use std::io::{BufRead, BufReader};
BufReader::new(std::fs::File::open("pixels.dat").expect("unable to open pixels.dat"))
.lines()
.map(|i| i.map(|j| format!("PX {}\n", j)))
.collect::<Result<Vec<_>, _>>()
.expect("file I/O error")
};
*/
let mut stream = TcpStream::connect(PXLF_SERVER).expect("unable to connect to server");
let (s, r) = crossbeam_channel::bounded(10);
let gt = std::thread::spawn(move || {
let rng = fastrand::Rng::new();
loop {
let lines: Vec<String> = {
let mut ret = Vec::new();
let rd = rng.u16(5..500);
let mx = rng.u16(20..(PXLF_MAX_X - 20));
let my = rng.u16(20..(PXLF_MAX_Y - 20));
for i in 0..10 {
ret.extend(
kreis(rd + i, mx, my)
.filter(|&(x, y)| x < PXLF_MAX_X && y < PXLF_MAX_Y)
.map(|(x, y)| {
format!(
"PX {} {} {}\n",
x,
y,
std::iter::repeat_with(|| rng.lowercase())
.filter(|x| x.is_digit(16))
.take(8)
.collect::<String>()
)
}),
);
}
ret.sort();
ret.dedup();
rng.shuffle(&mut ret[..]);
ret
};
let coll: Vec<u8> = lines.iter().flat_map(|i| i.as_bytes()).copied().collect();
if !coll.is_empty() {
s.send(coll).unwrap();
}
}
});
while let Ok(coll) = r.recv() {
stream.write_all(&coll[..]).expect("unable to write line");
stream.flush().expect("unable to clear output buffer");
}
let _ = gt.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment