From 52b1e145415da350bf78e1ed3997819deabfdaa9 Mon Sep 17 00:00:00 2001 From: ntr Date: Sat, 13 Jul 2019 16:40:14 +1000 Subject: [PATCH] params for invaders --- server/src/img.rs | 92 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 8 deletions(-) diff --git a/server/src/img.rs b/server/src/img.rs index 1d383704..755a2b13 100644 --- a/server/src/img.rs +++ b/server/src/img.rs @@ -1,5 +1,7 @@ use uuid::Uuid; use rand::prelude::*; +use rand::distributions::{Normal, WeightedIndex}; + use std::fs::copy; use std::fs::File; use std::io::prelude::*; @@ -31,23 +33,49 @@ fn invader_img_write(id: Uuid) -> Result { let mut rng = thread_rng(); let mut svg = Vec::new(); + // rounding ? + // filters ? + + // distribution for lightness + // bellcurve around 50% + let l_dist = Normal::new(50.0, 10.0); + let s_dist = Normal::new(25.0, 10.0); + + let mut colours = std::iter + ::repeat_with(|| { + let h = rng.gen_range(0, 360); + let s = s_dist.sample(&mut rng) as u16; + let l = l_dist.sample(&mut rng) as u16; + format!("hsl({:}, {:}%, {:}%)", h, s, l) + }) + .take(3) + .collect::>(); + + colours.push("none".to_string()); + + // add up to 100 for % + // [3] is the transparent colour + let weights = [ + 5, + 5, + 65, + 25, + ]; + let colour_dist = WeightedIndex::new(&weights)?; + write!(&mut svg, "")?; for i in 0..50 { let x = (i % 5) * 50; let y = (i / 5) * 50; - let colour = match rng.gen_bool(0.5) { - true => "whitesmoke", - false => "none", - }; + let colour = &colours[colour_dist.sample(&mut rng)]; + write!(&mut svg, "", colour, x, y)?; write!(&mut svg, "", colour, 450 - x, y)?; } write!(&mut svg, "")?; - // println!("{:?}", String::from_utf8(svg.clone())?); - let dest = format!("/var/lib/mnml/public/imgs/{}.svg", id); println!("molecule dest={:?}", dest); @@ -64,7 +92,9 @@ mod tests { #[test] fn invader_img_test() { - invader_img_write(Uuid::new_v4()).unwrap(); + for i in 0..100 { + invader_img_write(Uuid::new_v4()).unwrap(); + } } } @@ -123,4 +153,50 @@ mod tests { // newOpts.spotcolor = opts.spotcolor || createColor(); // return newOpts; -// } \ No newline at end of file +// } +// ], +// - _ => vec![ +// - (ItemAction::RerollStamina, 1), +// - (ItemAction::RerollPhysDamage, 1), +// - (ItemAction::RerollSpellDamage, 1), +// - (ItemAction::RerollSpeed, 1), +// - (ItemAction::RerollArmour, 1), +// - (ItemAction::RerollSpellShield, 1), +// - (ItemAction::RerollEvasion, 1), +// - ], +// + // _ => vec![ +// + // (ItemAction::RerollStamina, 1), +// + // (ItemAction::RerollPhysDamage, 1), +// + // (ItemAction::RerollSpellDamage, 1), +// + // (ItemAction::RerollSpeed, 1), +// + // (ItemAction::RerollArmour, 1), +// + // (ItemAction::RerollSpellShield, 1), +// + // (ItemAction::RerollEvasion, 1), +// + // ], +// } +// } + +// -pub fn item_drop(tx: &mut Transaction, account_id: Uuid, mode: GameMode) -> Result { +// +pub fn item_drop(tx: &mut Transaction, account_id: Uuid, mode: GameMode) -> Result<(), Error> { +// let mut rng = thread_rng(); + +// - let actions = mode_drops(mode); +// + let log_normal = LogNormal::new(1.0, 1.0); +// + let num_drops = log_normal.sample(&mut rng).floor() as u16; +// + +// + println!("{:?} drops", num_drops); + +// - let dist = WeightedIndex::new(actions.iter().map(|item| item.1)).unwrap(); +// - let kind = actions[dist.sample(&mut rng)].0; +// - let item = Item::new(kind, account_id); +// + for _i in 0..num_drops { +// + let actions = mode_drops(mode); + +// - println!("{:?} dropped {:?}", account_id, item); +// + let dist = WeightedIndex::new(actions.iter().map(|item| item.1)).unwrap(); +// + let kind = actions[dist.sample(&mut rng)].0; +// + let item = Item::new(kind, account_id); + +// - return item_create(item, tx, account_id); +// + println!("{:?} dropped {:?}", account_id, item); +// + item_create(item, tx, account_id)?;V \ No newline at end of file