upkeep bug

This commit is contained in:
ntr 2019-09-13 22:11:02 +10:00
parent 7c7554dc8e
commit b33eb4d106
2 changed files with 118 additions and 12 deletions

View File

@ -1,12 +1,13 @@
use uuid::Uuid; use std::f64;
use rand::prelude::*;
use rand::distributions::{Normal, WeightedIndex};
use std::fs::copy; use std::fs::copy;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::char::from_u32; use std::char::from_u32;
use uuid::Uuid;
use rand::prelude::*;
use rand::distributions::{Normal, WeightedIndex};
use failure::Error; use failure::Error;
use failure::err_msg; use failure::err_msg;
@ -36,8 +37,8 @@ pub fn invader_write(id: Uuid) -> Result<Uuid, Error> {
// filters ? // filters ?
// distribution for lightness // distribution for lightness
// bellcurve around 50% // bellcurve around 75%
let l_dist = Normal::new(50.0, 10.0); let l_dist = Normal::new(75.0, 10.0);
let s_dist = Normal::new(25.0, 10.0); let s_dist = Normal::new(25.0, 10.0);
let mut colours = std::iter let mut colours = std::iter
@ -83,7 +84,104 @@ pub fn invader_write(id: Uuid) -> Result<Uuid, Error> {
Ok(id) Ok(id)
} }
fn hieroglyph() -> String { enum ConstructShape {
Square,
Triangle,
Circle,
Line,
V,
Tri,
Plus,
Blank,
}
// default ? shape
pub fn construct(id: Uuid) -> Result<Uuid, Error> {
let mut rng = thread_rng();
let mut svg = Vec::new();
// distribution for lightness
// bellcurve around 75%
let l_dist = Normal::new(50.0, 10.0);
let s_dist = Normal::new(25.0, 10.0);
// 8 6 or 4 points in shape
// 1 head point
// n / 2 shapes with a colour each
// random size/radius props for each
// add up to 100 for %
let shapes = [
(ConstructShape::Square, 100),
// (ConstructShape::Triangle, 10),
// (ConstructShape::Circle, 10),
// (ConstructShape::Line, 10),
// (ConstructShape::V, 10),
// (ConstructShape::Tri, 3),
// (ConstructShape::Plus, 5),
// (ConstructShape::Blank, 1),
];
let shape_dist = WeightedIndex::new(shapes.iter().map(|v| v.1))?;
let n_shapes = rng.gen_range(2, 10);
write!(&mut svg, "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='-500 -500 1000 1000' width='1000' height='1000'><g>")?;
for i in 0..n_shapes - 1 {
let h = rng.gen_range(0, 360);
let s = s_dist.sample(&mut rng) as usize;
let l = l_dist.sample(&mut rng) as usize;
let colour = format!("hsl({:}, {:}%, {:}%)", h, s, l);
let fraction: f64 = (1.0 / n_shapes as f64) * i as f64;
let angle: f64 = (fraction * 180.0) / f64::consts::PI;
match shapes[shape_dist.sample(&mut rng)].0 {
ConstructShape::Square => {
let size = rng.gen_range(5, 50);
let distance = rng.gen_range(50, 200);
let rotation = rng.gen_range(0, 180);
write!(&mut svg, "<rect fill=\"{}\" x=\"-{}\" y=\"{}\" width=\"{}\" height=\"{}\" transform=\"rotate({}) translate(0, {}) rotate({})\" />",
colour, size / 2, size / 2, size, size, rotation, distance, angle)?;
write!(&mut svg, "<rect fill=\"{}\" x=\"-{}\" y=\"{}\" width=\"{}\" height=\"{}\" transform=\"rotate({}) translate(0, {}) rotate({})\" />",
colour, size / 2, size / 2, size, size, rotation, distance, angle + 180.0)?;
},
ConstructShape::Triangle => {
},
ConstructShape::Circle => {
},
ConstructShape::Line => {
},
ConstructShape::V => {
},
ConstructShape::Tri => {
},
ConstructShape::Plus => {
},
ConstructShape::Blank => {
},
}
}
write!(&mut svg, "</g></svg>")?;
let dest = format!("/var/lib/mnml/public/imgs/{}.svg", id);
println!("default dest={:?}", dest);
let mut file = File::create(dest)?;
file.write_all(&svg)?;
Ok(id)
}
fn _hieroglyph() -> String {
let mut rng = thread_rng(); let mut rng = thread_rng();
let mut s = String::new(); let mut s = String::new();
@ -99,7 +197,6 @@ fn hieroglyph() -> String {
return s; return s;
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -111,9 +208,16 @@ mod tests {
// } // }
// } // }
// #[test]
// fn hieroglyph_test() {
// hieroglyph();
// }
#[test] #[test]
fn hieroglyph_test() { fn construct_img_test() {
hieroglyph(); for i in 0..100 {
construct(Uuid::new_v4()).unwrap();
}
} }
} }
@ -124,7 +228,7 @@ mod tests {
// var h = Math.floor(rand() * 360); // var h = Math.floor(rand() * 360);
// //saturation goes from 40 to 100, it avoids greyish colors // //saturation goes from 40 to 100, it avoids greyish colors
// var s = ((rand() * 60) + 40) + '%'; // var s = ((rand() * 60) + 40) + '%';
// //lightness can be anything from 0 to 100, but probabilities are a bell curve around 50% // //lightness can be anything from 0 to 100, but probabilities are a bell curve around 75%
// var l = ((rand()+rand()+rand()+rand()) * 25) + '%'; // var l = ((rand()+rand()+rand()+rand()) * 25) + '%';
// var color = 'hsl(' + h + ',' + s + ',' + l + ')'; // var color = 'hsl(' + h + ',' + s + ',' + l + ')';

View File

@ -271,6 +271,9 @@ impl Instance {
let mut game = Game::new(); let mut game = Game::new();
current_round.game_id = Some(game.id); current_round.game_id = Some(game.id);
// disable upkeep until players finish their game
self.phase_end = None;
game game
.set_player_num(2) .set_player_num(2)
.set_player_constructs(3) .set_player_constructs(3)
@ -303,7 +306,6 @@ impl Instance {
self.phase_start = Utc::now(); self.phase_start = Utc::now();
self.phase_end = self.time_control.vbox_phase_end(); self.phase_end = self.time_control.vbox_phase_end();
self.players.iter_mut().for_each(|p| { self.players.iter_mut().for_each(|p| {
p.set_ready(false); p.set_ready(false);
p.vbox.fill(); p.vbox.fill();