From 2f661e09c5b1bfb7f78e8e35b9834334e9946dc3 Mon Sep 17 00:00:00 2001
From: mehbark <terezi@pyrope.net>
Date: Mon, 24 Mar 2025 18:02:33 -0400
Subject: [PATCH] normalize usage

---
 src/main.rs | 39 +++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index ec5c3e0..5bd0ac1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,11 +18,12 @@ fn main() -> Result<(), Box<dyn Error>> {
     log::info!("opening connection");
     let mut conn = Connection::open(output_file)?;
     log::info!("creating tables");
+    // TODO: checks and counters (actually very cool)
     conn.execute_batch(
         "
         BEGIN;
-        CREATE TABLE usage (
-            mon STRING,
+        CREATE TABLE mon (
+            name STRING,
             usage REAL NOT NULL
         );
         CREATE TABLE ability (
@@ -62,32 +63,54 @@ fn main() -> Result<(), Box<dyn Error>> {
     for (i, (mon, data)) in stats.data.iter().enumerate() {
         log::debug!("Processing mon #{i} ({mon})…");
         let tx = conn.transaction()?;
-        tx.execute("INSERT INTO usage VALUES (?1, ?2)", (mon, data.usage))?;
+
+        // normalizing with mon_count gives us data that is much easier to work
+        // with. for example, if pikachu has 10k raw count and thunderbolt is used
+        // 9k times (weighted), we'd like 0.9 so that we can say pikachu runs
+        // thunderbolt 90% of the time.
+
+        // HACK: we get the weighted mon count by summing the ability usage
+        // there's is 1000% a better way to do this
+        let mon_count: f32 = data.abilities.values().sum();
+        tx.execute("INSERT INTO mon VALUES (?1, ?2)", (mon, data.usage))?;
 
         for (ability, count) in &data.abilities {
             tx.execute(
                 "INSERT INTO ability VALUES (?1, ?2, ?3)",
-                (mon, ability, count),
+                (mon, ability, count / mon_count),
             )?;
         }
 
         for (r#move, count) in &data.moves {
-            tx.execute("INSERT INTO move VALUES (?1, ?2, ?3)", (mon, r#move, count))?;
+            tx.execute(
+                "INSERT INTO move VALUES (?1, ?2, ?3)",
+                (mon, r#move, count / mon_count),
+            )?;
         }
 
         for (item, count) in &data.items {
-            tx.execute("INSERT INTO item VALUES (?1, ?2, ?3)", (mon, item, count))?;
+            tx.execute(
+                "INSERT INTO item VALUES (?1, ?2, ?3)",
+                (mon, item, count / mon_count),
+            )?;
         }
 
         for (tera, count) in &data.tera {
             tx.execute(
                 "INSERT INTO tera VALUES (?1, ?2, ?3)",
-                (mon, format!("{tera:?}").to_ascii_lowercase(), count),
+                (
+                    mon,
+                    format!("{tera:?}").to_ascii_lowercase(),
+                    count / mon_count,
+                ),
             )?;
         }
 
         for (mate, count) in &data.teammates {
-            tx.execute("INSERT INTO team VALUES (?1, ?2, ?3)", (mon, mate, count))?;
+            tx.execute(
+                "INSERT INTO team VALUES (?1, ?2, ?3)",
+                (mon, mate, count / mon_count),
+            )?;
         }
         tx.commit()?;
     }