too many fucking messages

This commit is contained in:
Anna 2022-09-08 16:24:05 -04:00
parent a0e9e892bc
commit 9e13c12507
2 changed files with 28 additions and 10 deletions

View File

@ -3,6 +3,7 @@ use std::sync::Arc;
use anyhow::Context; use anyhow::Context;
use chrono::{Duration, Utc}; use chrono::{Duration, Utc};
use rand::Rng; use rand::Rng;
use rand::seq::SliceRandom;
use warp::{Filter, Rejection, Reply}; use warp::{Filter, Rejection, Reply};
use warp::filters::BoxedFilter; use warp::filters::BoxedFilter;
@ -62,6 +63,10 @@ fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) {
// FIXME: make a migration to fix this, smh I'm dumb // FIXME: make a migration to fix this, smh I'm dumb
let id_str = id.to_string(); let id_str = id.to_string();
// shuffle messages since we'll be excluding later based on messages
// that have already been included, so this will be more fair
messages.shuffle(&mut rand::thread_rng());
// just count nearby messages. this is O(n^2) but alternatives are hard // just count nearby messages. this is O(n^2) but alternatives are hard
let mut ids = Vec::with_capacity(messages.len()); let mut ids = Vec::with_capacity(messages.len());
for a in messages.iter() { for a in messages.iter() {
@ -75,7 +80,7 @@ fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) {
continue; continue;
} }
let mut nearby = 0; let mut nearby_ids = Vec::new();
for b in messages.iter() { for b in messages.iter() {
if a.id == b.id { if a.id == b.id {
continue; continue;
@ -84,18 +89,31 @@ fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) {
let distance = (a.x - b.x).powi(2) let distance = (a.x - b.x).powi(2)
+ (a.y - b.y).powi(2) + (a.y - b.y).powi(2)
+ (a.z - b.z).powi(2); + (a.z - b.z).powi(2);
// 7.5 squared // 10 squared
if distance >= 56.25 { if distance >= 100.0 {
continue; continue;
} }
nearby += 1; nearby_ids.push(&b.id);
} }
let mut nearby = nearby_ids.len() as u32;
let (numerator, denominator) = if nearby <= 2 { let (numerator, denominator) = if nearby <= 2 {
// no need to do calculations for groups of three or fewer // no need to do calculations for groups of three or fewer
(17, 20) (17, 20)
} else { } else {
let raw_score = a.positive_votes - a.negative_votes;
if raw_score < -1 {
continue;
}
let already_visible = nearby_ids.iter()
.filter(|id| ids.contains(id))
.count();
if already_visible >= 3 {
continue;
}
let time_since_creation = a.created.signed_duration_since(Utc::now().naive_utc()); let time_since_creation = a.created.signed_duration_since(Utc::now().naive_utc());
let brand_new = time_since_creation < Duration::minutes(30); let brand_new = time_since_creation < Duration::minutes(30);
let new = time_since_creation < Duration::hours(2); let new = time_since_creation < Duration::hours(2);
@ -107,18 +125,18 @@ fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) {
numerator += (nearby / 3).min(1); numerator += (nearby / 3).min(1);
} }
let score = (a.positive_votes - a.negative_votes).max(0); let score = raw_score.max(0);
if score > 0 { if score > 0 {
let pad = score as f32 / nearby as f32; let pad = score as f32 / nearby as f32;
let rounded = pad.round() as u32; let rounded = pad.floor() as u32;
numerator += rounded.max(nearby / 2); numerator += rounded.max(nearby / 2);
} }
nearby *= 2; nearby *= 2;
if numerator * 20 > nearby * 17 { if numerator * 5 > nearby * 4 {
numerator = 17; numerator = 4;
nearby = 20; nearby = 5;
} }
(numerator, nearby) (numerator, nearby)

View File

@ -18,7 +18,7 @@ async fn logic(state: Arc<State>) -> Result<impl Reply, Rejection> {
.await .await
.values() .values()
.filter(|pack| pack.visible) .filter(|pack| pack.visible)
.map(|pack| pack.clone()) .cloned()
.collect(); .collect();
visible.sort_unstable_by_key(|pack| pack.order); visible.sort_unstable_by_key(|pack| pack.order);