Skip to content

Commit

Permalink
chore: cargo fmt and fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ashhhleyyy committed Dec 1, 2023
1 parent 992691c commit 279af37
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
6 changes: 5 additions & 1 deletion src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ impl Handler<OutgoingChat> for Controller {

#[async_trait]
impl Handler<OutgoingCommand> for Controller {
async fn handle(&mut self, message: OutgoingCommand, _ctx: &mut Context<Self>) -> <OutgoingCommand as Message>::Result {
async fn handle(
&mut self,
message: OutgoingCommand,
_ctx: &mut Context<Self>,
) -> <OutgoingCommand as Message>::Result {
println!(
"[{}] <@{}> /{}",
message.channel, message.sender, message.command
Expand Down
6 changes: 5 additions & 1 deletion src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ impl DiscordHandler {
self.relay.connect(channel, ctx, message).await
}
["relay", "disconnect"] if admin => self.relay.disconnect(ctx, message).await,
["relay", "command", channel, command @ ..] if admin => self.relay.send_relay_command(ctx, message, channel, command).await,
["relay", "command", channel, command @ ..] if admin => {
self.relay
.send_relay_command(ctx, message, channel, command)
.await
}
["ping", "add", ping, role] if admin => self.pings.add(ctx, message, ping, role).await,
["ping", "remove", ping] if admin => self.pings.remove(ctx, message, ping).await,
["ping", "allow", ping, role] if admin => {
Expand Down
11 changes: 9 additions & 2 deletions src/discord/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,22 @@ impl Handler {
}
}

pub async fn send_relay_command(&self, ctx: &SerenityContext, message: &SerenityMessage, channel: &str, command: &[&str]) -> CommandResult {
pub async fn send_relay_command(
&self,
ctx: &SerenityContext,
message: &SerenityMessage,
channel: &str,
command: &[&str],
) -> CommandResult {
let sender = self.sender_name(ctx, message).await;
let roles = if let Ok(member) = message.member(&ctx).await {
member.roles.iter().map(ToString::to_string).collect()
} else {
Vec::new()
};

let success = self.controller
let success = self
.controller
.send(OutgoingCommand {
channel: channel.to_string(),
command: command.join(" "),
Expand Down
75 changes: 40 additions & 35 deletions src/statistics/wrapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ pub struct NucleoidWrapped {
}

impl NucleoidWrapped {
pub fn new(
clickhouse_pool: clickhouse_rs::Pool,
) -> Self {
Self {
clickhouse_pool,
}
pub fn new(clickhouse_pool: clickhouse_rs::Pool) -> Self {
Self { clickhouse_pool }
}

async fn played_count(&self, player: &Uuid) -> Result<u64, clickhouse_rs::errors::Error> {
Expand All @@ -28,14 +24,17 @@ impl NucleoidWrapped {
// safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql
player_id = player
)).fetch_all().await?;
if let Some(row) = results.rows().nth(0) {
if let Some(row) = results.rows().next() {
Ok(row.get("total")?)
} else {
Ok(0)
}
}

async fn top_games(&self, player: &Uuid) -> Result<Vec<PerGameStat>, clickhouse_rs::errors::Error> {
async fn top_games(
&self,
player: &Uuid,
) -> Result<Vec<PerGameStat>, clickhouse_rs::errors::Error> {
let mut ch_handle = self.clickhouse_pool.get_handle().await?;
let results = ch_handle.query(format!(
r#"
Expand All @@ -57,10 +56,7 @@ impl NucleoidWrapped {
for row in results.rows() {
let namespace: String = row.get("namespace")?;
let total = row.get("total")?;
top_games.push(PerGameStat {
namespace,
total,
});
top_games.push(PerGameStat { namespace, total });
}

Ok(top_games)
Expand All @@ -80,14 +76,17 @@ impl NucleoidWrapped {
// safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql
player_id = player
)).fetch_all().await?;
if let Some(row) = results.rows().nth(0) {
if let Some(row) = results.rows().next() {
Ok(row.get("total")?)
} else {
Ok(0)
}
}

async fn days_played_games(&self, player: &Uuid) -> Result<Vec<PerGameStat>, clickhouse_rs::errors::Error> {
async fn days_played_games(
&self,
player: &Uuid,
) -> Result<Vec<PerGameStat>, clickhouse_rs::errors::Error> {
let mut ch_handle = self.clickhouse_pool.get_handle().await?;
let results = ch_handle.query(format!(
r#"
Expand All @@ -109,19 +108,17 @@ impl NucleoidWrapped {
for row in results.rows() {
let namespace: String = row.get("namespace")?;
let total = row.get("total")?;
top_games.push(PerGameStat {
namespace,
total,
});
top_games.push(PerGameStat { namespace, total });
}

Ok(top_games)
}

async fn most_players(&self, player: &Uuid) -> Result<u64, clickhouse_rs::errors::Error> {
let mut ch_handle = self.clickhouse_pool.get_handle().await?;
let results = ch_handle.query(format!(
r#"
let results = ch_handle
.query(format!(
r#"
SELECT
MAX(total) as total
FROM
Expand All @@ -139,20 +136,26 @@ impl NucleoidWrapped {
INNER JOIN player_statistics ON player_statistics.game_id = games.game_id
GROUP BY game_id)
"#,
// safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql
player_id = player
)).fetch_all().await?;
if let Some(row) = results.rows().nth(0) {
// safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql
player_id = player
))
.fetch_all()
.await?;
if let Some(row) = results.rows().next() {
Ok(row.get("total")?)
} else {
Ok(0)
}
}

async fn most_players_games(&self, player: &Uuid) -> Result<Vec<PerGameStat>, clickhouse_rs::errors::Error> {
async fn most_players_games(
&self,
player: &Uuid,
) -> Result<Vec<PerGameStat>, clickhouse_rs::errors::Error> {
let mut ch_handle = self.clickhouse_pool.get_handle().await?;
let results = ch_handle.query(format!(
r#"
let results = ch_handle
.query(format!(
r#"
SELECT
MAX(total) as total,
namespace
Expand All @@ -175,25 +178,27 @@ impl NucleoidWrapped {
GROUP BY namespace
ORDER BY total DESC
"#,
// safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql
player_id = player
)).fetch_all().await?;
// safety: player is a uuid, which has a fixed format which is safe to insert directly into the sql
player_id = player
))
.fetch_all()
.await?;

let mut top_games = Vec::with_capacity(results.row_count());

for row in results.rows() {
let namespace: String = row.get("namespace")?;
let total = row.get("total")?;
top_games.push(PerGameStat {
namespace,
total,
});
top_games.push(PerGameStat { namespace, total });
}

Ok(top_games)
}

pub async fn build_wrapped(&self, player: &Uuid) -> Result<PlayerWrappedData, clickhouse_rs::errors::Error> {
pub async fn build_wrapped(
&self,
player: &Uuid,
) -> Result<PlayerWrappedData, clickhouse_rs::errors::Error> {
let played_count = self.played_count(player).await?;
let top_games = self.top_games(player).await?;
let days_played = self.days_played(player).await?;
Expand Down
3 changes: 2 additions & 1 deletion src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ async fn get_player_username(mojang_client: Address<MojangApiClient>, id: Uuid)

async fn nucleoid_wrapped(controller: Address<Controller>, player_id: Uuid) -> ApiResult {
let statistics = get_statistics_controller(controller).await?;
let res = statistics.send(WrappedData(player_id))
let res = statistics
.send(WrappedData(player_id))
.await
.expect("controller disconnected");
handle_result(res)
Expand Down

0 comments on commit 279af37

Please sign in to comment.