mirror of
https://codeberg.org/demostf/frontend.git
synced 2026-06-03 18:24:12 +02:00
chat
This commit is contained in:
parent
1648eeffab
commit
82a82d2aff
7 changed files with 152 additions and 43 deletions
35
src/data/chat.rs
Normal file
35
src/data/chat.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use crate::data::demo::Duration;
|
||||
use crate::Result;
|
||||
use sqlx::{query_as, Executor, Postgres};
|
||||
use tracing::instrument;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Chat {
|
||||
pub from: String,
|
||||
pub text: String,
|
||||
pub time: i32,
|
||||
}
|
||||
|
||||
impl Chat {
|
||||
#[instrument(skip(connection))]
|
||||
pub async fn for_demo(
|
||||
connection: impl Executor<'_, Database = Postgres>,
|
||||
id: u32,
|
||||
) -> Result<Vec<Chat>> {
|
||||
Ok(query_as!(
|
||||
Chat,
|
||||
r#"SELECT
|
||||
"from", text, time
|
||||
FROM chat
|
||||
WHERE demo_id = $1
|
||||
ORDER BY time ASC"#,
|
||||
id as i32
|
||||
)
|
||||
.fetch_all(connection)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub fn time(&self) -> Duration {
|
||||
Duration(self.time)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
use crate::data::chat::Chat;
|
||||
use crate::data::player::Player;
|
||||
use crate::data::steam_id::SteamId;
|
||||
use crate::Result;
|
||||
|
|
@ -27,6 +28,7 @@ pub struct Demo {
|
|||
pub nick: String,
|
||||
pub player_count: i32,
|
||||
pub players: Vec<Player>,
|
||||
pub chat: Vec<Chat>,
|
||||
}
|
||||
|
||||
impl Demo {
|
||||
|
|
@ -76,6 +78,7 @@ impl Demo {
|
|||
};
|
||||
|
||||
let players = Player::for_demo(connection, id).await?;
|
||||
let chat = Chat::for_demo(connection, id).await?;
|
||||
|
||||
Ok(Some(Demo {
|
||||
id: raw.id,
|
||||
|
|
@ -96,6 +99,7 @@ impl Demo {
|
|||
nick: raw.nick,
|
||||
player_count: raw.player_count,
|
||||
players,
|
||||
chat,
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
@ -117,6 +121,22 @@ impl Demo {
|
|||
.or(self.uploader_name.as_deref())
|
||||
.unwrap_or("unknown")
|
||||
}
|
||||
|
||||
pub fn duration(&self) -> Duration {
|
||||
Duration(self.duration)
|
||||
}
|
||||
|
||||
pub fn viewer_url(&self) -> ViewerUrl {
|
||||
ViewerUrl(self.id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ViewerUrl(i32);
|
||||
|
||||
impl Render for ViewerUrl {
|
||||
fn render_to(&self, buffer: &mut String) {
|
||||
write!(buffer, "/viewer/{}", self.0).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, FromRow)]
|
||||
|
|
@ -232,7 +252,7 @@ impl Render for DemoFormat {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Duration(i32);
|
||||
pub struct Duration(pub i32);
|
||||
|
||||
impl Render for Duration {
|
||||
fn render_to(&self, buffer: &mut String) {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod chat;
|
||||
pub mod demo;
|
||||
pub mod player;
|
||||
pub mod steam_id;
|
||||
|
|
|
|||
|
|
@ -89,6 +89,28 @@ impl Page for DemoPage {
|
|||
}
|
||||
}
|
||||
}
|
||||
p.demo-info {
|
||||
span { (self.demo.map) }
|
||||
span.time { (self.demo.duration()) }
|
||||
}
|
||||
p.demo-download {
|
||||
a.button.button-primary href = (self.demo.url) download = (self.demo.name) { "Download" }
|
||||
a.button href = (self.demo.viewer_url()) { "View" }
|
||||
details.chat {
|
||||
summary.button.button-tertiary { "Toggle Chat" }
|
||||
div {
|
||||
table.chat {
|
||||
@for chat in &self.demo.chat {
|
||||
tr {
|
||||
td.user { (chat.from) }
|
||||
td.message { (chat.text) }
|
||||
td.duration { (chat.time()) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue