mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Implement most of clans functionality; close #89
This commit is contained in:
@@ -23,7 +23,7 @@ impl MigrationTrait for Migration {
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::factory::vehicle::Column::CreationTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::friend::Column::CreationTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::friend::Column::FriendSource).integer().not_null())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20260221_000001_create_clan_table"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
// Define how to apply this migration: Create the Clans table.
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(crate::schema::clan::Entity)
|
||||
.col(
|
||||
ColumnDef::new(crate::schema::clan::Column::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::clan::Column::CreationTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::clan::Column::Name).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::clan::Column::Description).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::clan::Column::Variant).string().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
// Define how to rollback this migration: Drop the Clans table.
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(crate::schema::clan::Entity).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20260221_000002_create_clan_member_table"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
// Define how to apply this migration: Create the Clan Members table.
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(crate::schema::clan_member::Entity)
|
||||
.col(
|
||||
ColumnDef::new(crate::schema::clan_member::Column::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::clan_member::Column::CreationTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::clan_member::Column::UserId).integer().not_null())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-clan_members-user_id")
|
||||
.from(crate::schema::clan_member::Entity, crate::schema::clan_member::Column::UserId)
|
||||
.to(crate::schema::user::Entity, crate::schema::user::Column::Id),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::clan_member::Column::ClanId).integer().not_null())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-clan_members-clan_id")
|
||||
.from(crate::schema::clan_member::Entity, crate::schema::clan_member::Column::ClanId)
|
||||
.to(crate::schema::clan::Entity, crate::schema::clan::Column::Id),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::clan_member::Column::Rank).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::clan_member::Column::Status).string().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
// Define how to rollback this migration: Drop the Clan Members table.
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(crate::schema::clan_member::Entity).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ mod m20251228_000001_create_score_table;
|
||||
#[cfg(feature = "factory")]
|
||||
mod m20260126_000001_create_factory_vehicle_table;
|
||||
mod m20260215_000001_create_friend_table;
|
||||
mod m20260221_000001_create_clan_table;
|
||||
mod m20260221_000002_create_clan_member_table;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -39,6 +41,8 @@ impl MigratorTrait for Migrator {
|
||||
#[cfg(feature = "factory")]
|
||||
Box::new(m20260126_000001_create_factory_vehicle_table::Migration),
|
||||
Box::new(m20260215_000001_create_friend_table::Migration),
|
||||
Box::new(m20260221_000001_create_clan_table::Migration),
|
||||
Box::new(m20260221_000002_create_clan_member_table::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
35
rc_database/src/schema/clan.rs
Normal file
35
rc_database/src/schema/clan.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "clans")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub creation_time: i64, // seconds since unix epoch
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub variant: ClanType,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::clan_member::Entity")]
|
||||
ClanMember,
|
||||
}
|
||||
|
||||
impl Related<super::clan_member::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::ClanMember.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)", rename_all = "PascalCase")]
|
||||
pub enum ClanType {
|
||||
Public,
|
||||
Private,
|
||||
Banned,
|
||||
Abandoned,
|
||||
}
|
||||
59
rc_database/src/schema/clan_member.rs
Normal file
59
rc_database/src/schema/clan_member.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "clan_members")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub creation_time: i64, // seconds since unix epoch
|
||||
pub user_id: i32,
|
||||
pub clan_id: i32,
|
||||
pub rank: ClanMemberRank,
|
||||
pub status: ClanMemberStatus,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::user::Entity",
|
||||
from = "Column::UserId",
|
||||
to = "super::user::Column::Id"
|
||||
)]
|
||||
User,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::clan::Entity",
|
||||
from = "Column::ClanId",
|
||||
to = "super::clan::Column::Id"
|
||||
)]
|
||||
Clan,
|
||||
}
|
||||
|
||||
impl Related<super::user::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::User.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::clan::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Clan.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)", rename_all = "PascalCase")]
|
||||
pub enum ClanMemberRank {
|
||||
Member,
|
||||
Officer,
|
||||
Leader,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)", rename_all = "PascalCase")]
|
||||
pub enum ClanMemberStatus {
|
||||
Invited,
|
||||
Confirmed,
|
||||
Deactivated,
|
||||
}
|
||||
@@ -13,6 +13,8 @@ pub mod multiplayer_game_score;
|
||||
#[cfg(feature = "factory")]
|
||||
pub mod factory;
|
||||
pub mod friend;
|
||||
pub mod clan;
|
||||
pub mod clan_member;
|
||||
|
||||
pub fn parse_int_csv(s: &str) -> Vec<u32> {
|
||||
s.split(',').filter_map(|i_as_s| {
|
||||
|
||||
@@ -628,6 +628,154 @@ impl Database {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn clan_by_name(&self, name: String) -> Result<Option<crate::schema::clan::Model>, sea_orm::DbErr> {
|
||||
crate::schema::clan::Entity::find()
|
||||
.filter(
|
||||
sea_orm::sea_query::Func::lower(crate::schema::clan::Column::Name.into_expr())
|
||||
.eq(name.to_lowercase())
|
||||
)
|
||||
.one(self.orm.as_ref()).await
|
||||
}
|
||||
|
||||
pub async fn clan_by_user_id(&self, user_id: i32) -> Result<Option<(crate::schema::clan::Model, crate::schema::clan_member::Model)>, sea_orm::DbErr> {
|
||||
Ok(crate::schema::clan::Entity::find()
|
||||
.find_also_related(crate::schema::clan_member::Entity)
|
||||
.filter(crate::schema::clan_member::Column::UserId.eq(user_id))
|
||||
.filter(crate::schema::clan_member::Column::Status.eq(crate::schema::clan_member::ClanMemberStatus::Confirmed))
|
||||
.one(self.orm.as_ref())
|
||||
.await?
|
||||
.and_then(|(clan, member)| member.map(|member| (clan, member))))
|
||||
}
|
||||
|
||||
pub async fn clans_invited_to_for_user_id(&self, user_id: i32) -> Result<Vec<(crate::schema::clan::Model, crate::schema::clan_member::Model)>, sea_orm::DbErr> {
|
||||
Ok(crate::schema::clan::Entity::find()
|
||||
.find_also_related(crate::schema::clan_member::Entity)
|
||||
.filter(crate::schema::clan_member::Column::UserId.eq(user_id))
|
||||
.filter(crate::schema::clan_member::Column::Status.eq(crate::schema::clan_member::ClanMemberStatus::Invited))
|
||||
.all(self.orm.as_ref())
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter_map(|(clan, member)| member.map(|member| (clan, member)))
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn clan_invited_to_for_user_id_and_clan_id(&self, user_id: i32, clan_id: i32) -> Result<Option<(crate::schema::clan::Model, crate::schema::clan_member::Model)>, sea_orm::DbErr> {
|
||||
Ok(crate::schema::clan::Entity::find()
|
||||
.find_also_related(crate::schema::clan_member::Entity)
|
||||
.filter(crate::schema::clan_member::Column::UserId.eq(user_id))
|
||||
.filter(crate::schema::clan_member::Column::ClanId.eq(clan_id))
|
||||
.filter(crate::schema::clan_member::Column::Status.eq(crate::schema::clan_member::ClanMemberStatus::Invited))
|
||||
.one(self.orm.as_ref())
|
||||
.await?
|
||||
.and_then(|(clan, member)| member.map(|member| (clan, member))))
|
||||
}
|
||||
|
||||
pub async fn clan_invited_to_for_user_id_and_clan_name(&self, user_id: i32, clan_name: String) -> Result<Option<(crate::schema::clan::Model, crate::schema::clan_member::Model)>, sea_orm::DbErr> {
|
||||
Ok(crate::schema::clan::Entity::find()
|
||||
.find_also_related(crate::schema::clan_member::Entity)
|
||||
.filter(crate::schema::clan_member::Column::UserId.eq(user_id))
|
||||
.filter(
|
||||
sea_orm::sea_query::Func::lower(crate::schema::clan::Column::Name.into_expr())
|
||||
.eq(clan_name.to_lowercase())
|
||||
)
|
||||
.filter(crate::schema::clan_member::Column::Status.eq(crate::schema::clan_member::ClanMemberStatus::Invited))
|
||||
.one(self.orm.as_ref())
|
||||
.await?
|
||||
.and_then(|(clan, member)| member.map(|member| (clan, member))))
|
||||
}
|
||||
|
||||
pub async fn clans_by_search(&self, s: String, start: u64, _end: u64, types: impl std::iter::Iterator<Item=crate::schema::clan::ClanType>) -> Result<Vec<crate::schema::clan::Model>, sea_orm::DbErr> {
|
||||
let lower_s_like = format!("%{}%", s.trim_matches('%').to_lowercase());
|
||||
let types: Vec<_> = types.collect();
|
||||
crate::schema::clan::Entity::find()
|
||||
.filter(
|
||||
sea_orm::sea_query::Expr::expr(
|
||||
sea_orm::sea_query::Func::lower(crate::schema::clan::Column::Name.into_expr())
|
||||
).like(&lower_s_like)
|
||||
)
|
||||
.filter(if types.is_empty() {
|
||||
crate::schema::clan::Column::Variant.is_in([
|
||||
crate::schema::clan::ClanType::Public,
|
||||
crate::schema::clan::ClanType::Private,
|
||||
])
|
||||
} else {
|
||||
crate::schema::clan::Column::Variant.is_in(types)
|
||||
})
|
||||
// FIXME don't return clans that are not strictly in the start..end range
|
||||
.paginate(self.orm.as_ref(), 50)
|
||||
.fetch_page(start / 50)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn insert_clan(&self, entity: crate::schema::clan::ActiveModel) -> Result<crate::schema::clan::Model, sea_orm::DbErr> {
|
||||
#[cfg(debug_assertions)]
|
||||
assert!(matches!(entity.id, sea_orm::ActiveValue::NotSet));
|
||||
entity.insert(self.orm.as_ref()).await
|
||||
}
|
||||
|
||||
pub async fn update_clan(&self, entity: crate::schema::clan::ActiveModel) -> Result<crate::schema::clan::Model, sea_orm::DbErr> {
|
||||
entity.update(self.orm.as_ref()).await
|
||||
}
|
||||
|
||||
pub async fn clan_members_by_clan_id(&self, clan_id: i32) -> Result<Vec<(crate::schema::clan_member::Model, crate::schema::user::Model)>, sea_orm::DbErr> {
|
||||
Ok(crate::schema::clan_member::Entity::find()
|
||||
.find_also_related(crate::schema::user::Entity)
|
||||
.filter(crate::schema::clan_member::Column::ClanId.eq(clan_id))
|
||||
.filter(crate::schema::clan_member::Column::Status.is_in([
|
||||
crate::schema::clan_member::ClanMemberStatus::Invited,
|
||||
crate::schema::clan_member::ClanMemberStatus::Confirmed,
|
||||
]))
|
||||
.all(self.orm.as_ref())
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter_map(|(member, user)| user.map(|user| (member, user)))
|
||||
.collect()
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn clan_leaders_by_clan_ids(&self, clan_ids: impl std::iter::Iterator<Item=i32>) -> Result<Vec<(crate::schema::clan_member::Model, crate::schema::user::Model)>, sea_orm::DbErr> {
|
||||
Ok(crate::schema::clan_member::Entity::find()
|
||||
.find_also_related(crate::schema::user::Entity)
|
||||
.filter(crate::schema::clan_member::Column::ClanId.is_in(clan_ids))
|
||||
.filter(crate::schema::clan_member::Column::Status.eq(crate::schema::clan_member::ClanMemberStatus::Confirmed))
|
||||
.filter(crate::schema::clan_member::Column::Rank.eq(crate::schema::clan_member::ClanMemberRank::Leader))
|
||||
.all(self.orm.as_ref())
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter_map(|(member, user)| user.map(|user| (member, user)))
|
||||
.collect()
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn insert_clan_member(&self, entity: crate::schema::clan_member::ActiveModel) -> Result<crate::schema::clan_member::Model, sea_orm::DbErr> {
|
||||
#[cfg(debug_assertions)]
|
||||
assert!(matches!(entity.id, sea_orm::ActiveValue::NotSet));
|
||||
entity.insert(self.orm.as_ref()).await
|
||||
}
|
||||
|
||||
pub async fn update_clan_member(&self, entity: crate::schema::clan_member::ActiveModel) -> Result<crate::schema::clan_member::Model, sea_orm::DbErr> {
|
||||
entity.update(self.orm.as_ref()).await
|
||||
}
|
||||
|
||||
pub async fn update_clan_member_decline_all_invites(&self, user_id: i32) -> Result<(), sea_orm::DbErr> {
|
||||
crate::schema::clan_member::Entity::update_many()
|
||||
.filter(crate::schema::clan_member::Column::UserId.eq(user_id))
|
||||
.filter(crate::schema::clan_member::Column::Status.eq(crate::schema::clan_member::ClanMemberStatus::Invited))
|
||||
.col_expr(crate::schema::clan_member::Column::Status, sea_orm::sea_query::Expr::value(crate::schema::clan_member::ClanMemberStatus::Deactivated))
|
||||
.exec(self.orm.as_ref())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn count_clan_members_by_clan_id(&self, clan_id: i32) -> Result<u64, sea_orm::DbErr> {
|
||||
crate::schema::clan_member::Entity::find()
|
||||
.find_also_related(crate::schema::user::Entity)
|
||||
.filter(crate::schema::clan_member::Column::ClanId.eq(clan_id))
|
||||
.filter(crate::schema::clan_member::Column::Status.eq(crate::schema::clan_member::ClanMemberStatus::Confirmed))
|
||||
.count(self.orm.as_ref())
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn metrics(&self) -> super::DatabaseMetrics {
|
||||
self.metrics.lock().unwrap().snapshot()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user