From 443d39725f4986fb9862b574d51ba54768f2291f Mon Sep 17 00:00:00 2001 From: ntr Date: Sat, 14 Sep 2019 19:46:14 +1000 Subject: [PATCH] shapes migration --- ops/knexfile.SAMPLE.js | 12 ++--------- ops/migrations/20190914191207_shapes-mtx.js | 11 ++++++++++ ops/package.json | 2 +- server/src/construct.rs | 2 +- server/src/img.rs | 23 ++++++++++++++------- server/src/instance.rs | 6 +++--- server/src/mtx.rs | 1 + 7 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 ops/migrations/20190914191207_shapes-mtx.js diff --git a/ops/knexfile.SAMPLE.js b/ops/knexfile.SAMPLE.js index 9004afd4..32cf7b5b 100644 --- a/ops/knexfile.SAMPLE.js +++ b/ops/knexfile.SAMPLE.js @@ -1,11 +1,9 @@ -// Update with your config settings. - -const local = { +module.exports = { client: 'postgresql', connection: { database: 'mnml', user: 'mnml', - password: process.env.MNML_PG_PASSWORD, + password: 'gggggggggg', }, pool: { min: 2, @@ -15,9 +13,3 @@ const local = { tableName: 'knex_migrations' } }; - - -module.exports = { - development: local, - production: local, -}; diff --git a/ops/migrations/20190914191207_shapes-mtx.js b/ops/migrations/20190914191207_shapes-mtx.js new file mode 100644 index 00000000..4d7f5e3c --- /dev/null +++ b/ops/migrations/20190914191207_shapes-mtx.js @@ -0,0 +1,11 @@ +const uuidv4 = require('uuid/v4'); + +// give everybody the shapes mtx +exports.up = async knex => { + await knex.raw(` + INSERT INTO mtx (id, account, variant) + SELECT uuid_generate_v4() as id, id as account, 'Shapes' + FROM accounts; + `); +}; +exports.down = async () => {}; \ No newline at end of file diff --git a/ops/package.json b/ops/package.json index 805c3f1a..098d9a1a 100755 --- a/ops/package.json +++ b/ops/package.json @@ -17,6 +17,6 @@ "pg": "^7.4.3", "request": "^2.88.0", "sdftosvg": "0.0.4", - "uuid": "^3.3.2" + "uuid": "^3.3.3" } } diff --git a/server/src/construct.rs b/server/src/construct.rs index c7a4f35c..2d0e4fe8 100644 --- a/server/src/construct.rs +++ b/server/src/construct.rs @@ -891,7 +891,7 @@ pub fn construct_spawn(tx: &mut Transaction, account: Uuid, name: String, team: let _returned = result.iter().next().ok_or(err_msg("no row returned"))?; - img::molecular_write(construct.img)?; + img::shapes_write(construct.img)?; info!("spawned construct account={:} name={:?}", account, construct.name); return Ok(construct); diff --git a/server/src/img.rs b/server/src/img.rs index acd5ca69..87f2e732 100644 --- a/server/src/img.rs +++ b/server/src/img.rs @@ -102,8 +102,8 @@ pub fn shapes_write(id: Uuid) -> Result { // distribution for lightness // bellcurve around 75% - let l_dist = Normal::new(70.0, 10.0); - let s_dist = Normal::new(25.0, 10.0); + let l_dist = Normal::new(50.0, 10.0); + let s_dist = Normal::new(75.0, 10.0); // 8 6 or 4 points in shape // 1 head point @@ -123,17 +123,27 @@ pub fn shapes_write(id: Uuid) -> Result { ]; let shape_dist = WeightedIndex::new(shapes.iter().map(|v| v.1))?; - let n_shapes = rng.gen_range(4, 5); - let n_shapes = 4; + let n_shapes_items = [ + (2, 1), + (3, 5), + (4, 10), + (5, 10), + ]; + let num_dist = WeightedIndex::new(n_shapes_items.iter().map(|v| v.1))?; + let n_shapes = num_dist.sample(&mut rng) as usize; write!(&mut svg, "")?; - for i in 0..n_shapes { + 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 scalar = rng.gen_range(50.0, 200.0); + let scalar = match i == n_shapes { + true => 0.0, + false => rng.gen_range(50.0, 200.0), + }; + let rotation = rng.gen_range(0, 180); let angle: f64 = i as f64 * (1.0 / n_shapes as f64) * f64::consts::PI; let x_translate = angle.cos() * scalar; @@ -193,7 +203,6 @@ pub fn shapes_write(id: Uuid) -> Result { write!(&mut 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)?; diff --git a/server/src/instance.rs b/server/src/instance.rs index 4f8d8804..0d230dbf 100644 --- a/server/src/instance.rs +++ b/server/src/instance.rs @@ -712,7 +712,7 @@ pub fn instance_practice(tx: &mut Transaction, account: &Account) -> Result Result, Error> { // generate bot imgs for the client to see for c in bot.constructs.iter() { - img::molecular_write(c.img)?; + img::shapes_write(c.img)?; }; let bot2 = bot_player(); // generate bot imgs for the client to see for c in bot2.constructs.iter() { - img::molecular_write(c.img)?; + img::shapes_write(c.img)?; }; diff --git a/server/src/mtx.rs b/server/src/mtx.rs index 4a28dc6b..3352d100 100644 --- a/server/src/mtx.rs +++ b/server/src/mtx.rs @@ -63,6 +63,7 @@ impl TryFrom for MtxVariant { "Rename" => Ok(MtxVariant::Rename), "Molecular" => Ok(MtxVariant::Molecular), "Invader" => Ok(MtxVariant::Invader), + "Shapes" => Ok(MtxVariant::Shapes), _ => Err(format_err!("mtx variant not found variant={:?}", v)), } }