auto view

This commit is contained in:
Anna 2022-09-05 05:05:14 -04:00
parent 3a2a3c4d74
commit 4615cb2b0f
6 changed files with 27 additions and 16 deletions

View File

@ -11,4 +11,5 @@ public class Configuration : IPluginConfiguration {
public bool DisableTrials = true; public bool DisableTrials = true;
public bool DisableDeepDungeon = true; public bool DisableDeepDungeon = true;
public bool RemoveGlow = true; public bool RemoveGlow = true;
public bool AutoViewer;
} }

View File

@ -14,12 +14,15 @@ internal class Settings : ITab {
} }
public void Draw() { public void Draw() {
ImGui.PushTextWrapPos();
var anyChanged = false; var anyChanged = false;
var vfx = false; var vfx = false;
anyChanged |= vfx |= ImGui.Checkbox("Disable in trials", ref this.Plugin.Config.DisableTrials); anyChanged |= vfx |= ImGui.Checkbox("Disable in trials", ref this.Plugin.Config.DisableTrials);
anyChanged |= vfx |= ImGui.Checkbox("Disable in Deep Dungeons", ref this.Plugin.Config.DisableDeepDungeon); anyChanged |= vfx |= ImGui.Checkbox("Disable in Deep Dungeons", ref this.Plugin.Config.DisableDeepDungeon);
anyChanged |= vfx |= ImGui.Checkbox("Remove glow effect from signs", ref this.Plugin.Config.RemoveGlow); anyChanged |= vfx |= ImGui.Checkbox("Remove glow effect from signs", ref this.Plugin.Config.RemoveGlow);
anyChanged |= ImGui.Checkbox("Open the viewer automatically when near a sign", ref this.Plugin.Config.AutoViewer);
if (anyChanged) { if (anyChanged) {
this.Plugin.SaveConfig(); this.Plugin.SaveConfig();
@ -31,6 +34,8 @@ internal class Settings : ITab {
} }
this.ExtraCodeInput(); this.ExtraCodeInput();
ImGui.PopTextWrapPos();
} }
private void ExtraCodeInput() { private void ExtraCodeInput() {

View File

@ -35,6 +35,10 @@ internal class Viewer {
.OrderBy(msg => msg.Id) .OrderBy(msg => msg.Id)
.ToList(); .ToList();
if (nearby.Count == 0) { if (nearby.Count == 0) {
if (this.Plugin.Config.AutoViewer) {
this.Visible = false;
}
ImGui.TextUnformatted("No nearby messages"); ImGui.TextUnformatted("No nearby messages");
goto End; goto End;
} }

View File

@ -19,6 +19,11 @@ internal class ViewerButton {
return; return;
} }
if (this.Plugin.Config.AutoViewer) {
this.Plugin.Ui.Viewer.Visible = true;
return;
}
ImGui.SetNextWindowBgAlpha(0.5f); ImGui.SetNextWindowBgAlpha(0.5f);
if (!ImGui.Begin("##ogt-viewer-button", ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.AlwaysAutoResize)) { if (!ImGui.Begin("##ogt-viewer-button", ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.AlwaysAutoResize)) {
ImGui.End(); ImGui.End();

View File

@ -34,6 +34,8 @@ pub struct RetrievedMessage {
pub user_vote: i64, pub user_vote: i64,
#[serde(skip)] #[serde(skip)]
pub created: NaiveDateTime, pub created: NaiveDateTime,
#[serde(skip)]
pub user: String,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]

View File

@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use anyhow::Context; use anyhow::Context;
@ -16,15 +15,13 @@ pub fn get_location(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
.and(warp::path("messages")) .and(warp::path("messages"))
.and(warp::path::param()) .and(warp::path::param())
.and(warp::path::end()) .and(warp::path::end())
.and(warp::query::<HashMap<String, String>>())
.and(super::get_id(Arc::clone(&state))) .and(super::get_id(Arc::clone(&state)))
.and_then(move |location: u32, query: HashMap<String, String>, (id, _)| logic(Arc::clone(&state), id, location, query)) .and_then(move |location: u32, (id, _)| logic(Arc::clone(&state), id, location))
.boxed() .boxed()
} }
async fn logic(state: Arc<State>, id: i64, location: u32, query: HashMap<String, String>) -> Result<impl Reply, Rejection> { async fn logic(state: Arc<State>, id: i64, location: u32) -> Result<impl Reply, Rejection> {
// TODO: when we're not just returning all results, make sure own messages are always present // TODO: when we're not just returning all results, make sure own messages are always present
let filter = query.contains_key("filter");
let location = location as i64; let location = location as i64;
let mut messages = sqlx::query_as!( let mut messages = sqlx::query_as!(
RetrievedMessage, RetrievedMessage,
@ -39,7 +36,8 @@ async fn logic(state: Arc<State>, id: i64, location: u32, query: HashMap<String,
coalesce(sum(v.vote between 0 and 1), 0) as positive_votes, coalesce(sum(v.vote between 0 and 1), 0) as positive_votes,
coalesce(sum(v.vote between -1 and 0), 0) as negative_votes, coalesce(sum(v.vote between -1 and 0), 0) as negative_votes,
v2.vote as user_vote, v2.vote as user_vote,
m.created m.created,
m.user
from messages m from messages m
left join votes v on m.id = v.message left join votes v on m.id = v.message
left join votes v2 on m.id = v2.message and v2.user = ? left join votes v2 on m.id = v2.message and v2.user = ?
@ -53,13 +51,14 @@ async fn logic(state: Arc<State>, id: i64, location: u32, query: HashMap<String,
.context("could not get messages from database") .context("could not get messages from database")
.map_err(AnyhowRejection) .map_err(AnyhowRejection)
.map_err(warp::reject::custom)?; .map_err(warp::reject::custom)?;
if filter { filter_messages(&mut messages, id);
filter_messages(&mut messages);
}
Ok(warp::reply::json(&messages)) Ok(warp::reply::json(&messages))
} }
fn filter_messages(messages: &mut Vec<RetrievedMessage>) { fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) {
// FIXME: make a migration to fix this, smh I'm dumb
let id_str = id.to_string();
// 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() {
@ -81,9 +80,7 @@ fn filter_messages(messages: &mut Vec<RetrievedMessage>) {
nearby += 1; nearby += 1;
} }
println!("{} ({} nearby)", a.id, nearby); if a.user == id_str || nearby <= 2 {
if nearby <= 2 {
// always include groups of three or fewer // always include groups of three or fewer
ids.push(a.id.clone()); ids.push(a.id.clone());
continue; continue;
@ -91,8 +88,6 @@ fn filter_messages(messages: &mut Vec<RetrievedMessage>) {
let score = (a.positive_votes - a.negative_votes).max(0); let score = (a.positive_votes - a.negative_votes).max(0);
let time_since_creation = Utc::now().naive_utc().signed_duration_since(a.created) - Duration::weeks(score as i64); let time_since_creation = Utc::now().naive_utc().signed_duration_since(a.created) - Duration::weeks(score as i64);
println!(" time_since_creation: {}", Utc::now().naive_utc().signed_duration_since(a.created));
println!(" modified: {}", time_since_creation);
if time_since_creation > Duration::weeks(1) { if time_since_creation > Duration::weeks(1) {
continue; continue;
} }
@ -114,7 +109,6 @@ fn filter_messages(messages: &mut Vec<RetrievedMessage>) {
numerator += rounded.max(nearby / 2); numerator += rounded.max(nearby / 2);
} }
println!(" chance: {}/{}", numerator, nearby * 2);
if rand::thread_rng().gen_ratio(numerator.min(nearby), nearby * 2) { if rand::thread_rng().gen_ratio(numerator.min(nearby), nearby * 2) {
ids.push(a.id.clone()); ids.push(a.id.clone());
} }