refactor: move database models to entity crate
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1685,6 +1685,7 @@ dependencies = [
|
|||||||
name = "mixer-discord-bot"
|
name = "mixer-discord-bot"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"entity",
|
||||||
"image",
|
"image",
|
||||||
"imageproc",
|
"imageproc",
|
||||||
"itertools 0.11.0",
|
"itertools 0.11.0",
|
||||||
|
|||||||
@@ -2,5 +2,6 @@
|
|||||||
members = [
|
members = [
|
||||||
"mixer-discord-bot",
|
"mixer-discord-bot",
|
||||||
"migration",
|
"migration",
|
||||||
|
"entity"
|
||||||
]
|
]
|
||||||
default-members = ["mixer-discord-bot"]
|
default-members = ["mixer-discord-bot"]
|
||||||
11
entity/Cargo.toml
Normal file
11
entity/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "entity"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "entity"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
sea-orm = "0.11.3"
|
||||||
@@ -1,17 +1,16 @@
|
|||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, DeriveEntityModel)]
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||||
#[sea_orm(table_name = "guilds")]
|
#[sea_orm(table_name = "guilds")]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key, auto_increment)]
|
#[sea_orm(primary_key)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
#[sea_orm(unique)]
|
#[sea_orm(unique)]
|
||||||
pub guild_id: i64,
|
pub guild_id: i64,
|
||||||
#[sea_orm(default_value = false)]
|
|
||||||
pub verified: bool,
|
pub verified: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {}
|
pub enum Relation {}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
6
entity/src/lib.rs
Normal file
6
entity/src/lib.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
pub mod prelude;
|
||||||
|
|
||||||
|
pub mod guilds;
|
||||||
|
pub mod lobbies;
|
||||||
|
pub mod players;
|
||||||
|
pub mod sea_orm_active_enums;
|
||||||
@@ -1,18 +1,17 @@
|
|||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, DeriveEntityModel)]
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||||
#[sea_orm(table_name = "lobbies")]
|
#[sea_orm(table_name = "lobbies")]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key, auto_increment)]
|
#[sea_orm(primary_key)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
|
|
||||||
pub guild_id: i64,
|
pub guild_id: i64,
|
||||||
pub main_voice_id: i64,
|
pub main_voice_id: i64,
|
||||||
pub red_team_voice_id: i64,
|
pub red_team_voice_id: i64,
|
||||||
pub blue_team_voice_id: i64,
|
pub blue_team_voice_id: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {}
|
pub enum Relation {}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
@@ -1,42 +1,34 @@
|
|||||||
|
use super::sea_orm_active_enums::Role;
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
use super::role::Role;
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, DeriveEntityModel)]
|
|
||||||
#[sea_orm(table_name = "players")]
|
#[sea_orm(table_name = "players")]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key, auto_increment)]
|
#[sea_orm(primary_key)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
|
|
||||||
#[sea_orm(unique)]
|
#[sea_orm(unique)]
|
||||||
pub discord_id: i64,
|
pub discord_id: i64,
|
||||||
pub bn_name: Option<String>,
|
pub bn_name: Option<String>,
|
||||||
pub bn_tag: Option<String>,
|
pub bn_tag: Option<String>,
|
||||||
|
|
||||||
pub last_played: Option<DateTime>,
|
pub last_played: Option<DateTime>,
|
||||||
|
#[sea_orm(column_type = "Float")]
|
||||||
#[sea_orm(default_value = 2500.0)]
|
|
||||||
pub tank_rating: f32,
|
pub tank_rating: f32,
|
||||||
#[sea_orm(default_value = 300.0)]
|
#[sea_orm(column_type = "Float")]
|
||||||
pub tank_rd: f32,
|
pub tank_rd: f32,
|
||||||
#[sea_orm(default_value = 0.06)]
|
#[sea_orm(column_type = "Float")]
|
||||||
pub tank_volatility: f32,
|
pub tank_volatility: f32,
|
||||||
|
#[sea_orm(column_type = "Float")]
|
||||||
#[sea_orm(default_value = 2500.0)]
|
|
||||||
pub dps_rating: f32,
|
pub dps_rating: f32,
|
||||||
#[sea_orm(default_value = 300.0)]
|
#[sea_orm(column_type = "Float")]
|
||||||
pub dps_rd: f32,
|
pub dps_rd: f32,
|
||||||
#[sea_orm(default_value = 0.06)]
|
#[sea_orm(column_type = "Float")]
|
||||||
pub dps_volatility: f32,
|
pub dps_volatility: f32,
|
||||||
|
#[sea_orm(column_type = "Float")]
|
||||||
#[sea_orm(default_value = 2500.0)]
|
|
||||||
pub support_rating: f32,
|
pub support_rating: f32,
|
||||||
#[sea_orm(default_value = 300.0)]
|
#[sea_orm(column_type = "Float")]
|
||||||
pub support_rd: f32,
|
pub support_rd: f32,
|
||||||
#[sea_orm(default_value = 0.06)]
|
#[sea_orm(column_type = "Float")]
|
||||||
pub support_volatility: f32,
|
pub support_volatility: f32,
|
||||||
|
|
||||||
#[sea_orm(default_value = true)]
|
|
||||||
pub flex: bool,
|
pub flex: bool,
|
||||||
pub primary_role: Option<Role>,
|
pub primary_role: Option<Role>,
|
||||||
pub secondary_role: Option<Role>,
|
pub secondary_role: Option<Role>,
|
||||||
4
entity/src/prelude.rs
Normal file
4
entity/src/prelude.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub use super::guilds::Entity as Guilds;
|
||||||
|
pub use super::lobbies::Entity as Lobbies;
|
||||||
|
pub use super::players::Entity as Players;
|
||||||
|
pub use super::sea_orm_active_enums::Role;
|
||||||
35
entity/src/sea_orm_active_enums.rs
Normal file
35
entity/src/sea_orm_active_enums.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, EnumIter, DeriveActiveEnum)]
|
||||||
|
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "role")]
|
||||||
|
pub enum Role {
|
||||||
|
#[sea_orm(string_value = "tank")]
|
||||||
|
Tank,
|
||||||
|
#[sea_orm(string_value = "dps")]
|
||||||
|
Dps,
|
||||||
|
#[sea_orm(string_value = "support")]
|
||||||
|
Support,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&str> for Role {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_from(val: &str) -> Result<Self, Self::Error> {
|
||||||
|
match val {
|
||||||
|
"tank" => Ok(Role::Tank),
|
||||||
|
"dps" => Ok(Role::Dps),
|
||||||
|
"support" => Ok(Role::Support),
|
||||||
|
_ => Err(format!("Unknown enum variant '{}'", val)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&Role> for i32 {
|
||||||
|
fn from(val: &Role) -> Self {
|
||||||
|
match val {
|
||||||
|
Role::Tank => 0,
|
||||||
|
Role::Dps => 1,
|
||||||
|
Role::Support => 2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,12 +5,14 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
migration = { path = "../migration" }
|
migration = { path = "../migration" }
|
||||||
|
entity = { path = "../entity" }
|
||||||
|
|
||||||
|
tokio = { version = "1.29.1", features = ["full"] }
|
||||||
tracing = "0.1.37"
|
tracing = "0.1.37"
|
||||||
serenity = {version = "0.11.5", default-features = false, features = ["rustls_backend", "client", "gateway", "model", "cache", "collector", "utils"] }
|
serenity = {version = "0.11.6", default-features = false, features = ["rustls_backend", "client", "gateway", "model", "cache", "collector", "utils"] }
|
||||||
sqlx = { version = "0.6.3", features = ["runtime-tokio-rustls", "postgres"] }
|
sqlx = { version = "0.6.3", features = ["runtime-tokio-rustls", "postgres"] }
|
||||||
sea-orm = { version = "0.11.3", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros"] }
|
sea-orm = { version = "0.11.3", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros"] }
|
||||||
itertools = "0.10.5"
|
itertools = "0.11.0"
|
||||||
shuttle-secrets = "0.20.0"
|
shuttle-secrets = "0.20.0"
|
||||||
shuttle-serenity = "0.20.0"
|
shuttle-serenity = "0.20.0"
|
||||||
shuttle-runtime = "0.20.0"
|
shuttle-runtime = "0.20.0"
|
||||||
|
|||||||
@@ -18,14 +18,14 @@ use std::borrow::Cow;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::bot::commands::MixerCommand;
|
use crate::bot::commands::MixerCommand;
|
||||||
use crate::database::models::lobby::Model;
|
|
||||||
use crate::database::models::role::Role;
|
|
||||||
use crate::database::queries::prelude::*;
|
use crate::database::queries::prelude::*;
|
||||||
use crate::database::DatabaseContainer;
|
use crate::database::DatabaseContainer;
|
||||||
use crate::image_manipulation::ImageGeneratorContainer;
|
use crate::image_manipulation::ImageGeneratorContainer;
|
||||||
use crate::mixer::mixer;
|
use crate::mixer::mixer;
|
||||||
use crate::mixer::player::Player;
|
use crate::mixer::player::Player;
|
||||||
use crate::mixer::team::Team;
|
use crate::mixer::team::Team;
|
||||||
|
use entity::lobbies;
|
||||||
|
use entity::prelude::Role;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct LobbyCommand;
|
pub struct LobbyCommand;
|
||||||
@@ -296,7 +296,7 @@ impl LobbyCommand {
|
|||||||
&self,
|
&self,
|
||||||
ctx: &Context,
|
ctx: &Context,
|
||||||
interaction: ApplicationCommandInteraction,
|
interaction: ApplicationCommandInteraction,
|
||||||
lobby: Model,
|
lobby: lobbies::Model,
|
||||||
teams: (Team, Team),
|
teams: (Team, Team),
|
||||||
players: Vec<Player>,
|
players: Vec<Player>,
|
||||||
) -> serenity::Result<()> {
|
) -> serenity::Result<()> {
|
||||||
@@ -305,7 +305,7 @@ impl LobbyCommand {
|
|||||||
let team1_names = team1
|
let team1_names = team1
|
||||||
.players
|
.players
|
||||||
.iter()
|
.iter()
|
||||||
.sorted_by(|((a, _), _), ((b, _), _)| i32::from(*a).cmp(&i32::from(*b)))
|
.sorted_by(|((a, _), _), ((b, _), _)| i32::from(a).cmp(&i32::from(b)))
|
||||||
.map(|(_, i)| async {
|
.map(|(_, i)| async {
|
||||||
if let Ok(user) = players[i.unwrap()].discord_id.to_user(ctx).await {
|
if let Ok(user) = players[i.unwrap()].discord_id.to_user(ctx).await {
|
||||||
user.name
|
user.name
|
||||||
@@ -320,7 +320,7 @@ impl LobbyCommand {
|
|||||||
let team2_names = team2
|
let team2_names = team2
|
||||||
.players
|
.players
|
||||||
.iter()
|
.iter()
|
||||||
.sorted_by(|((a, _), _), ((b, _), _)| i32::from(*a).cmp(&i32::from(*b)))
|
.sorted_by(|((a, _), _), ((b, _), _)| i32::from(a).cmp(&i32::from(b)))
|
||||||
.map(|(_, i)| async {
|
.map(|(_, i)| async {
|
||||||
if let Ok(user) = players[i.unwrap()].discord_id.to_user(ctx).await {
|
if let Ok(user) = players[i.unwrap()].discord_id.to_user(ctx).await {
|
||||||
user.name
|
user.name
|
||||||
@@ -472,7 +472,7 @@ impl LobbyCommand {
|
|||||||
async fn process_valid_teams_start(
|
async fn process_valid_teams_start(
|
||||||
&self,
|
&self,
|
||||||
ctx: &Context,
|
ctx: &Context,
|
||||||
lobby: Model,
|
lobby: lobbies::Model,
|
||||||
team1: &Team,
|
team1: &Team,
|
||||||
team2: &Team,
|
team2: &Team,
|
||||||
players: Vec<Player>,
|
players: Vec<Player>,
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ use serenity::model::prelude::interaction::application_command::CommandDataOptio
|
|||||||
use serenity::model::Permissions;
|
use serenity::model::Permissions;
|
||||||
|
|
||||||
use crate::bot::commands::MixerCommand;
|
use crate::bot::commands::MixerCommand;
|
||||||
use crate::database::models::role::Role;
|
|
||||||
use crate::database::queries::prelude::*;
|
use crate::database::queries::prelude::*;
|
||||||
use crate::database::DatabaseContainer;
|
use crate::database::DatabaseContainer;
|
||||||
|
use entity::prelude::Role;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PreferenceCommand;
|
pub struct PreferenceCommand;
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ use serenity::model::prelude::command::CommandOptionType;
|
|||||||
use serenity::model::Permissions;
|
use serenity::model::Permissions;
|
||||||
|
|
||||||
use crate::bot::commands::MixerCommand;
|
use crate::bot::commands::MixerCommand;
|
||||||
use crate::database::models::role::Role;
|
|
||||||
use crate::database::queries::prelude::*;
|
use crate::database::queries::prelude::*;
|
||||||
use crate::database::DatabaseContainer;
|
use crate::database::DatabaseContainer;
|
||||||
use crate::mixer::rating::Rating;
|
use crate::mixer::rating::Rating;
|
||||||
|
use entity::prelude::Role;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RatingCommand;
|
pub struct RatingCommand;
|
||||||
@@ -163,7 +163,7 @@ impl MixerCommand for RatingCommand {
|
|||||||
.content(format!(
|
.content(format!(
|
||||||
"Setting rank for user <@{}> to {} {}",
|
"Setting rank for user <@{}> to {} {}",
|
||||||
user.id,
|
user.id,
|
||||||
String::from(role),
|
role,
|
||||||
rating
|
rating
|
||||||
))
|
))
|
||||||
.allowed_mentions(|mentions| mentions.empty_users())
|
.allowed_mentions(|mentions| mentions.empty_users())
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use std::collections::hash_map::RandomState;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::bot::commands::MixerCommand;
|
use crate::bot::commands::MixerCommand;
|
||||||
use crate::database::models::role::Role;
|
use entity::prelude::Role;
|
||||||
|
|
||||||
pub struct SettingsCommand;
|
pub struct SettingsCommand;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
pub mod models;
|
|
||||||
pub mod queries;
|
pub mod queries;
|
||||||
|
|
||||||
use sea_orm::{DatabaseConnection, SqlxPostgresConnector};
|
use sea_orm::{DatabaseConnection, SqlxPostgresConnector};
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
pub(super) mod prelude;
|
|
||||||
|
|
||||||
pub mod guild;
|
|
||||||
pub mod lobby;
|
|
||||||
pub mod player;
|
|
||||||
pub mod role;
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
pub use super::guild::Entity as Guilds;
|
|
||||||
pub use super::lobby::Entity as Lobbies;
|
|
||||||
pub use super::player::Entity as Players;
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
use sea_orm::{DeriveActiveEnum, EnumIter};
|
|
||||||
|
|
||||||
#[derive(EnumIter, DeriveActiveEnum, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "role")]
|
|
||||||
pub enum Role {
|
|
||||||
#[sea_orm(string_value = "tank")]
|
|
||||||
Tank,
|
|
||||||
#[sea_orm(string_value = "dps")]
|
|
||||||
Dps,
|
|
||||||
#[sea_orm(string_value = "support")]
|
|
||||||
Support,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Role> for String {
|
|
||||||
fn from(role: Role) -> Self {
|
|
||||||
match role {
|
|
||||||
Role::Tank => "tank".to_string(),
|
|
||||||
Role::Dps => "dps".to_string(),
|
|
||||||
Role::Support => "support".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<&str> for Role {
|
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn try_from(role: &str) -> Result<Self, Self::Error> {
|
|
||||||
match role {
|
|
||||||
"tank" => Ok(Role::Tank),
|
|
||||||
"dps" => Ok(Role::Dps),
|
|
||||||
"support" => Ok(Role::Support),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Role> for i32 {
|
|
||||||
fn from(role: Role) -> Self {
|
|
||||||
match role {
|
|
||||||
Role::Tank => 0,
|
|
||||||
Role::Dps => 1,
|
|
||||||
Role::Support => 2,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,8 @@ use sea_orm::prelude::*;
|
|||||||
use sea_orm::{DatabaseConnection, IntoActiveModel, Set};
|
use sea_orm::{DatabaseConnection, IntoActiveModel, Set};
|
||||||
use serenity::model::prelude::GuildId;
|
use serenity::model::prelude::GuildId;
|
||||||
|
|
||||||
use crate::database::models::*;
|
use entity::guilds;
|
||||||
|
use entity::prelude::*;
|
||||||
|
|
||||||
pub struct Query;
|
pub struct Query;
|
||||||
|
|
||||||
@@ -10,14 +11,14 @@ impl Query {
|
|||||||
pub async fn create(
|
pub async fn create(
|
||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
guild_id: GuildId,
|
guild_id: GuildId,
|
||||||
) -> Option<guild::Model> {
|
) -> Option<guilds::Model> {
|
||||||
let guild = guild::ActiveModel {
|
let guild = guilds::ActiveModel {
|
||||||
guild_id: Set(guild_id.0 as i64),
|
guild_id: Set(guild_id.0 as i64),
|
||||||
verified: Set(false),
|
verified: Set(false),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
guild::Entity::insert(guild).exec(connection).await.ok()?;
|
Guilds::insert(guild).exec(connection).await.ok()?;
|
||||||
|
|
||||||
Self::guild_by_guild_id(connection, guild_id).await
|
Self::guild_by_guild_id(connection, guild_id).await
|
||||||
}
|
}
|
||||||
@@ -25,7 +26,7 @@ impl Query {
|
|||||||
pub async fn create_if_not_exists(
|
pub async fn create_if_not_exists(
|
||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
guild_id: GuildId,
|
guild_id: GuildId,
|
||||||
) -> Option<guild::Model> {
|
) -> Option<guilds::Model> {
|
||||||
if let Some(guild) = Self::guild_by_guild_id(connection, guild_id).await {
|
if let Some(guild) = Self::guild_by_guild_id(connection, guild_id).await {
|
||||||
Some(guild)
|
Some(guild)
|
||||||
} else {
|
} else {
|
||||||
@@ -36,9 +37,9 @@ impl Query {
|
|||||||
pub async fn guild_by_guild_id(
|
pub async fn guild_by_guild_id(
|
||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
guild_id: GuildId,
|
guild_id: GuildId,
|
||||||
) -> Option<guild::Model> {
|
) -> Option<guilds::Model> {
|
||||||
guild::Entity::find()
|
Guilds::find()
|
||||||
.filter(guild::Column::GuildId.eq(guild_id.0 as i64))
|
.filter(guilds::Column::GuildId.eq(guild_id.0 as i64))
|
||||||
.one(connection)
|
.one(connection)
|
||||||
.await
|
.await
|
||||||
.ok()?
|
.ok()?
|
||||||
@@ -48,13 +49,13 @@ impl Query {
|
|||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
guild_id: GuildId,
|
guild_id: GuildId,
|
||||||
verified: bool,
|
verified: bool,
|
||||||
) -> Option<guild::Model> {
|
) -> Option<guilds::Model> {
|
||||||
let mut guild = Query::guild_by_guild_id(connection, guild_id)
|
let mut guild = Query::guild_by_guild_id(connection, guild_id)
|
||||||
.await?
|
.await?
|
||||||
.into_active_model();
|
.into_active_model();
|
||||||
|
|
||||||
guild.verified = Set(verified);
|
guild.verified = Set(verified);
|
||||||
|
|
||||||
guild::Entity::update(guild).exec(connection).await.ok()
|
Guilds::update(guild).exec(connection).await.ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ use sea_orm::ActiveValue::Set;
|
|||||||
use sea_orm::DatabaseConnection;
|
use sea_orm::DatabaseConnection;
|
||||||
use serenity::model::prelude::*;
|
use serenity::model::prelude::*;
|
||||||
|
|
||||||
use crate::database::models::prelude::*;
|
use entity::lobbies;
|
||||||
use crate::database::models::{self, lobby};
|
use entity::prelude::*;
|
||||||
|
|
||||||
pub struct Query;
|
pub struct Query;
|
||||||
|
|
||||||
@@ -15,8 +15,8 @@ impl Query {
|
|||||||
main_voice_id: ChannelId,
|
main_voice_id: ChannelId,
|
||||||
red_team_voice_id: ChannelId,
|
red_team_voice_id: ChannelId,
|
||||||
blue_team_voice_id: ChannelId,
|
blue_team_voice_id: ChannelId,
|
||||||
) -> Option<lobby::Model> {
|
) -> Option<lobbies::Model> {
|
||||||
let lobby = models::lobby::ActiveModel {
|
let lobby = lobbies::ActiveModel {
|
||||||
guild_id: Set(guild_id.0 as i64),
|
guild_id: Set(guild_id.0 as i64),
|
||||||
main_voice_id: Set(main_voice_id.0 as i64),
|
main_voice_id: Set(main_voice_id.0 as i64),
|
||||||
red_team_voice_id: Set(red_team_voice_id.0 as i64),
|
red_team_voice_id: Set(red_team_voice_id.0 as i64),
|
||||||
@@ -33,14 +33,14 @@ impl Query {
|
|||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
guild_id: GuildId,
|
guild_id: GuildId,
|
||||||
channel_id: ChannelId,
|
channel_id: ChannelId,
|
||||||
) -> Option<lobby::Model> {
|
) -> Option<lobbies::Model> {
|
||||||
Lobbies::find()
|
Lobbies::find()
|
||||||
.filter(
|
.filter(
|
||||||
lobby::Column::GuildId.eq(guild_id.0 as i64).and(
|
lobbies::Column::GuildId.eq(guild_id.0 as i64).and(
|
||||||
lobby::Column::MainVoiceId
|
lobbies::Column::MainVoiceId
|
||||||
.eq(channel_id.0 as i64)
|
.eq(channel_id.0 as i64)
|
||||||
.or(lobby::Column::RedTeamVoiceId.eq(channel_id.0 as i64))
|
.or(lobbies::Column::RedTeamVoiceId.eq(channel_id.0 as i64))
|
||||||
.or(lobby::Column::BlueTeamVoiceId.eq(channel_id.0 as i64)),
|
.or(lobbies::Column::BlueTeamVoiceId.eq(channel_id.0 as i64)),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.one(connection)
|
.one(connection)
|
||||||
|
|||||||
@@ -3,20 +3,23 @@ use sea_orm::prelude::*;
|
|||||||
use sea_orm::{DatabaseConnection, IntoActiveModel, Set};
|
use sea_orm::{DatabaseConnection, IntoActiveModel, Set};
|
||||||
use serenity::model::prelude::UserId;
|
use serenity::model::prelude::UserId;
|
||||||
|
|
||||||
use crate::database::models::player;
|
|
||||||
use crate::database::models::role::Role;
|
|
||||||
use crate::mixer::rating::Rating;
|
use crate::mixer::rating::Rating;
|
||||||
|
use entity::players;
|
||||||
|
use entity::prelude::*;
|
||||||
|
|
||||||
pub struct Query;
|
pub struct Query;
|
||||||
|
|
||||||
impl Query {
|
impl Query {
|
||||||
pub async fn create(connection: &DatabaseConnection, user_id: UserId) -> Option<player::Model> {
|
pub async fn create(
|
||||||
let player = player::ActiveModel {
|
connection: &DatabaseConnection,
|
||||||
|
user_id: UserId,
|
||||||
|
) -> Option<players::Model> {
|
||||||
|
let player = players::ActiveModel {
|
||||||
discord_id: Set(user_id.0 as i64),
|
discord_id: Set(user_id.0 as i64),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
player::Entity::insert(player).exec(connection).await.ok()?;
|
Players::insert(player).exec(connection).await.ok()?;
|
||||||
|
|
||||||
Self::player_by_user_id(connection, user_id).await
|
Self::player_by_user_id(connection, user_id).await
|
||||||
}
|
}
|
||||||
@@ -24,7 +27,7 @@ impl Query {
|
|||||||
pub async fn create_if_not_exists(
|
pub async fn create_if_not_exists(
|
||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
) -> Option<player::Model> {
|
) -> Option<players::Model> {
|
||||||
if let Some(player) = Self::player_by_user_id(connection, user_id).await {
|
if let Some(player) = Self::player_by_user_id(connection, user_id).await {
|
||||||
Some(player)
|
Some(player)
|
||||||
} else {
|
} else {
|
||||||
@@ -35,9 +38,9 @@ impl Query {
|
|||||||
pub async fn player_by_user_id(
|
pub async fn player_by_user_id(
|
||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
) -> Option<player::Model> {
|
) -> Option<players::Model> {
|
||||||
player::Entity::find()
|
Players::find()
|
||||||
.filter(player::Column::DiscordId.eq(user_id.0 as i64))
|
.filter(players::Column::DiscordId.eq(user_id.0 as i64))
|
||||||
.one(connection)
|
.one(connection)
|
||||||
.await
|
.await
|
||||||
.ok()?
|
.ok()?
|
||||||
@@ -46,10 +49,10 @@ impl Query {
|
|||||||
pub async fn players_by_user_ids(
|
pub async fn players_by_user_ids(
|
||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
user_ids: Vec<UserId>,
|
user_ids: Vec<UserId>,
|
||||||
) -> Option<Vec<player::Model>> {
|
) -> Option<Vec<players::Model>> {
|
||||||
player::Entity::find()
|
Players::find()
|
||||||
.filter(
|
.filter(
|
||||||
player::Column::DiscordId
|
players::Column::DiscordId
|
||||||
.is_in(user_ids.iter().map(|id| id.0 as i64).collect_vec()),
|
.is_in(user_ids.iter().map(|id| id.0 as i64).collect_vec()),
|
||||||
)
|
)
|
||||||
.all(connection)
|
.all(connection)
|
||||||
@@ -62,7 +65,7 @@ impl Query {
|
|||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
role: Role,
|
role: Role,
|
||||||
rating: Rating,
|
rating: Rating,
|
||||||
) -> Option<player::Model> {
|
) -> Option<players::Model> {
|
||||||
let mut player = Self::player_by_user_id(connection, user_id)
|
let mut player = Self::player_by_user_id(connection, user_id)
|
||||||
.await?
|
.await?
|
||||||
.into_active_model();
|
.into_active_model();
|
||||||
@@ -85,7 +88,7 @@ impl Query {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
player::Entity::update(player).exec(connection).await.ok()
|
Players::update(player).exec(connection).await.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_preference(
|
pub async fn update_preference(
|
||||||
@@ -95,7 +98,7 @@ impl Query {
|
|||||||
primary: Option<Role>,
|
primary: Option<Role>,
|
||||||
secondary: Option<Role>,
|
secondary: Option<Role>,
|
||||||
tertiary: Option<Role>,
|
tertiary: Option<Role>,
|
||||||
) -> Option<player::Model> {
|
) -> Option<players::Model> {
|
||||||
let mut player = Self::player_by_user_id(connection, user_id)
|
let mut player = Self::player_by_user_id(connection, user_id)
|
||||||
.await?
|
.await?
|
||||||
.into_active_model();
|
.into_active_model();
|
||||||
@@ -105,20 +108,20 @@ impl Query {
|
|||||||
player.secondary_role = Set(secondary);
|
player.secondary_role = Set(secondary);
|
||||||
player.tertiary_role = Set(tertiary);
|
player.tertiary_role = Set(tertiary);
|
||||||
|
|
||||||
player::Entity::update(player).exec(connection).await.ok()
|
Players::update(player).exec(connection).await.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_last_played(
|
pub async fn update_last_played(
|
||||||
connection: &DatabaseConnection,
|
connection: &DatabaseConnection,
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
last_played: DateTime,
|
last_played: DateTime,
|
||||||
) -> Option<player::Model> {
|
) -> Option<players::Model> {
|
||||||
let mut player = Self::player_by_user_id(connection, user_id)
|
let mut player = Self::player_by_user_id(connection, user_id)
|
||||||
.await?
|
.await?
|
||||||
.into_active_model();
|
.into_active_model();
|
||||||
|
|
||||||
player.last_played = Set(Some(last_played));
|
player.last_played = Set(Some(last_played));
|
||||||
|
|
||||||
player::Entity::update(player).exec(connection).await.ok()
|
Players::update(player).exec(connection).await.ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::database::models::role::Role;
|
|
||||||
use crate::mixer::player::Player;
|
use crate::mixer::player::Player;
|
||||||
use crate::mixer::team::Team;
|
use crate::mixer::team::Team;
|
||||||
|
use entity::prelude::Role;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct PlayerRoleEntry {
|
struct PlayerRoleEntry {
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ use serenity::model::id::UserId;
|
|||||||
use sqlx::types::chrono::Utc;
|
use sqlx::types::chrono::Utc;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::database::models::player::Model;
|
|
||||||
use crate::database::models::role::Role;
|
|
||||||
use crate::mixer::rating::Rating;
|
use crate::mixer::rating::Rating;
|
||||||
|
use entity::players;
|
||||||
|
use entity::prelude::Role;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
@@ -22,7 +22,7 @@ pub struct Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
pub fn new(model: Model) -> Self {
|
pub fn new(model: players::Model) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: model.id,
|
id: model.id,
|
||||||
discord_id: UserId::from(model.discord_id as u64),
|
discord_id: UserId::from(model.discord_id as u64),
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use sea_orm::Iterable;
|
use sea_orm::Iterable;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::database::models::role::Role;
|
|
||||||
use crate::mixer::player::Player;
|
use crate::mixer::player::Player;
|
||||||
use crate::mixer::rating::Rating;
|
use crate::mixer::rating::Rating;
|
||||||
|
use entity::prelude::Role;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Team {
|
pub struct Team {
|
||||||
|
|||||||
Reference in New Issue
Block a user