Skip to content

Commit

Permalink
add punishment count to adminuserpopup
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Jan 19, 2025
1 parent 9d58bfa commit 99667e7
Show file tree
Hide file tree
Showing 32 changed files with 98 additions and 107 deletions.
37 changes: 10 additions & 27 deletions server/central/src/db/dbimpl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use globed_shared::{debug, AdminPunishUserAction, PunishmentType, ServerUserEntry, UserPunishment};
use rocket_db_pools::sqlx::{query_as, Result};
use globed_shared::{AdminPunishUserAction, PunishmentType, ServerUserEntry, UserPunishment};
use rocket_db_pools::sqlx::{Result, query_as};
use serde::Serialize;
use sqlx::{prelude::*, query, query_scalar, sqlite::SqliteRow};

Expand Down Expand Up @@ -36,6 +36,7 @@ impl<'r> FromRow<'r, SqliteRow> for UserEntryWrapper {
admin_password_hash,
active_mute,
active_ban,
punishment_count: 0, // this will be initialized later
}))
}
}
Expand Down Expand Up @@ -217,26 +218,7 @@ impl GlobedDb {
Ok(())
}

async fn migrate_admin_password(&self, user: &mut ServerUserEntry) -> Result<()> {
if let Some(password) = user.admin_password.as_ref() {
let hash = globed_shared::generate_argon2_hash(password);
debug!("generating hash for password {password}: {hash}");

query("UPDATE users SET admin_password = NULL, admin_password_hash = ? WHERE account_id = ?")
.bind(&hash)
.bind(user.account_id)
.execute(&self.0)
.await?;

user.admin_password_hash = Some(hash);
};

user.admin_password = None;

Ok(())
}

/// Convert a `Option<UserEntryWrapper>` into `Option<ServerUserEntry>` and expire their ban/mute if needed.
/// Convert a `Option<UserEntryWrapper>` into `Option<ServerUserEntry>`, expire their ban/mute if needed, count amount of punishments, etc.
#[inline]
async fn unwrap_user(&self, user: Option<UserEntryWrapper>) -> Result<Option<ServerUserEntry>> {
let mut user = user.map(|x| x.0);
Expand All @@ -247,11 +229,12 @@ impl GlobedDb {
self.maybe_expire_punishments(user).await?;
}

// TODO: remove this in the near future
// migrate admin password to hashed form
if user.as_ref().is_some_and(|user| user.admin_password.is_some()) {
let user = user.as_mut().unwrap();
self.migrate_admin_password(user).await?;
// count punishments
if let Some(user) = user.as_mut() {
user.punishment_count = query_scalar("SELECT COUNT(*) FROM punishments WHERE account_id = ?")
.bind(user.account_id)
.fetch_one(&self.0)
.await?;
}

Ok(user)
Expand Down
4 changes: 2 additions & 2 deletions server/game/src/client/translator/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod general;
mod room;

#[allow(unused)]
pub use super::{Packet, PacketTranslationError, Translatable, CURRENT_PROTOCOL};
pub use super::{CURRENT_PROTOCOL, Packet, PacketTranslationError, Translatable};
#[allow(unused)]
pub use crate::data::{v13, v_current, Decodable, DynamicSize, Encodable, StaticSize};
pub use crate::data::{Decodable, DynamicSize, Encodable, StaticSize, v_current};
#[allow(unused)]
pub use v_current::{packets::*, types::*};
2 changes: 1 addition & 1 deletion server/game/src/client/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use esp::{ByteReader, Decodable, DecodeResult};

pub use crate::data::{Packet, CURRENT_PROTOCOL};
pub use crate::data::{CURRENT_PROTOCOL, Packet};

mod error;
mod impls;
Expand Down
3 changes: 1 addition & 2 deletions server/game/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ pub use v_current::types;
pub use packets::*;
pub use types::*;

pub mod v13;
pub mod v14;

// change this to the latest version as needed
pub use v13 as v_current;
pub use v14 as v_current;

// our own extension

Expand Down
7 changes: 0 additions & 7 deletions server/game/src/data/v13/mod.rs

This file was deleted.

35 changes: 0 additions & 35 deletions server/game/src/data/v13/packets/mod.rs

This file was deleted.

27 changes: 0 additions & 27 deletions server/game/src/data/v13/types/mod.rs

This file was deleted.

36 changes: 35 additions & 1 deletion server/game/src/data/v14/packets/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
pub use crate::data::v13::packets::*;
pub mod client;
pub mod server;

pub use client::*;
pub use server::*;

use crate::data::*;

pub trait Packet: PacketMetadata {}

// god i hate this
pub trait PacketMetadata {
const PACKET_ID: u16;
const ENCRYPTED: bool;
const SHOULD_USE_TCP: bool;
const NAME: &'static str;
}

#[derive(Encodable, Decodable, StaticSize)]
pub struct PacketHeader {
pub packet_id: u16,
pub encrypted: bool,
}

impl PacketHeader {
#[inline]
pub const fn from_packet<P: PacketMetadata>() -> Self {
Self {
packet_id: P::PACKET_ID,
encrypted: P::ENCRYPTED,
}
}

pub const SIZE: usize = Self::ENCODED_SIZE;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 27 additions & 1 deletion server/game/src/data/v14/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
pub use crate::data::v13::types::*;
pub mod audio_frame;
pub mod cocos;
pub mod crypto;
pub mod game;
pub mod gd;
pub mod misc;
pub mod room;
pub mod user;

use std::sync::atomic::AtomicI64;

pub use audio_frame::*;
pub use cocos::*;
pub use crypto::*;
pub use esp::types::*;
pub use game::*;
pub use gd::*;
pub use misc::*;
pub use room::*;
pub use user::*;
pub type LevelId = i64;
pub type AtomicLevelId = AtomicI64;

pub const fn is_editorcollab_level(id: LevelId) -> bool {
// if changing back to i32 for whatever reason, remove this condition
id > (2 as LevelId).pow(32)
}
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion server/shared/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::time::UNIX_EPOCH;

use super::*;
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString, rand_core::OsRng},
};
use esp::{FastString, InlineString};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -74,6 +74,7 @@ pub struct ServerUserEntry {
pub admin_password_hash: Option<String>,
pub active_mute: Option<i64>,
pub active_ban: Option<i64>,
pub punishment_count: u16,
}

// this is pure laziness i was just too lazy to get derive macros n shit into the central server
Expand Down Expand Up @@ -108,6 +109,7 @@ impl ServerUserEntry {
is_whitelisted: self.is_whitelisted,
active_ban,
active_mute,
punishment_count: self.punishment_count,
}
}

Expand Down Expand Up @@ -157,6 +159,7 @@ pub struct UserEntry {
pub is_whitelisted: bool,
pub active_ban: Option<UserPunishment>,
pub active_mute: Option<UserPunishment>,
pub punishment_count: u16,
}

impl UserEntry {
Expand Down
9 changes: 6 additions & 3 deletions src/data/types/admin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class UserEntry {
const std::vector<std::string>& userRoles,
bool isWhitelisted,
std::optional<UserPunishment> activeBan,
std::optional<UserPunishment> activeMute
) : accountId(accountId), userName(userName), nameColor(nameColor), userRoles(userRoles), isWhitelisted(isWhitelisted), activeBan(activeBan), activeMute(activeMute) {}
std::optional<UserPunishment> activeMute,
uint16_t punishmentCount = 0
) : accountId(accountId), userName(userName), nameColor(nameColor), userRoles(userRoles), isWhitelisted(isWhitelisted), activeBan(activeBan), activeMute(activeMute), punishmentCount(punishmentCount) {}

int accountId;
std::optional<std::string> userName;
Expand All @@ -23,6 +24,7 @@ class UserEntry {
bool isWhitelisted;
std::optional<UserPunishment> activeBan;
std::optional<UserPunishment> activeMute;
uint16_t punishmentCount;
};

GLOBED_SERIALIZABLE_STRUCT(
Expand All @@ -34,6 +36,7 @@ GLOBED_SERIALIZABLE_STRUCT(
userRoles,
isWhitelisted,
activeBan,
activeMute
activeMute,
punishmentCount
)
);
12 changes: 12 additions & 0 deletions src/ui/menu/admin/user_popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <managers/role.hpp>
#include <net/manager.hpp>
#include <ui/menu/admin/edit_role_popup.hpp>
#include <ui/game/chat/unread_badge.hpp>
#include <ui/general/ask_input_popup.hpp>
#include <ui/general/color_input_popup.hpp>
#include <util/math.hpp>
Expand Down Expand Up @@ -236,6 +237,17 @@ void AdminUserPopup::onProfileLoaded() {
// History button
Build<CCSprite>::createSpriteName("button-admin-history.png"_spr)
.scale(btnScale)
.with([&](auto* btn) {
// if the user has past punishments, show a little badge with the count
if (userEntry.punishmentCount > 0) {
Build<UnreadMessagesBadge>::create(userEntry.punishmentCount)
.pos(btn->getScaledContentSize() + CCPoint{1.f, 1.f})
.scale(0.7f)
.id("count-icon"_spr)
.parent(btn)
;
}
})
.intoMenuItem([this] {
AdminPunishmentHistoryPopup::create(userEntry.accountId)->show();
})
Expand Down

0 comments on commit 99667e7

Please sign in to comment.