Skip to content

Commit

Permalink
Merge pull request #1 from ededdneddyfan/add_leaderboard_tab
Browse files Browse the repository at this point in the history
Add leaderboard tab, about tab, remove a bunch of unused stuff. point db at readonly mysql
  • Loading branch information
ededdneddyfan authored Dec 13, 2024
2 parents 9610102 + 6f894ce commit 6101d46
Show file tree
Hide file tree
Showing 34 changed files with 161 additions and 693 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ validator = { version = "0.16" }
sea-orm = { version = "1.0.0", features = [
"sqlx-sqlite",
"sqlx-postgres",
"sqlx-mysql",
"runtime-tokio-rustls",
"macros",
] }
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/components/About.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from "react";

const About: React.FC = () => (
<div className="container mx-auto px-4 mt-8">
<div className="bg-gray-800 shadow-md rounded-lg p-8 max-w-3xl mx-auto text-gray-200">
<h2 className="text-2xl font-bold mb-6">TFPugs</h2>
<div className="space-y-4">
<p>
TFpugs is a discord community for playing Team Fortress Classic pickup games, we mostly play 4v4 CTF games.
</p>
<p>
I'm EDEdDNEdDYFaN on discord as well as everywhere else online.
</p>
<div className="flex items-center space-x-2">
<a
href="https://bsky.app/profile/sethn.gg"
target="_blank"
rel="noopener noreferrer"
className="text-blue-400 hover:text-blue-300 flex items-center"
>
<svg className="w-5 h-5 mr-1" viewBox="0 0 24 24" fill="currentColor">
<path d="M12 2L2 19.5h20L12 2zm0 4l6.5 11.5h-13L12 6z" />
</svg>
@sethn.gg
</a>
</div>
</div>
</div>
</div>
);

export default About;
77 changes: 77 additions & 0 deletions frontend/src/components/Leaderboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";

interface Player {
id: number;
player_name: string;
current_elo: number;
discord_id: string;
pug_wins: number;
pug_losses: number;
pug_draws: number;
}

const Leaderboard: React.FC = () => {
const [players, setPlayers] = useState<Player[]>([]);
const [loading, setLoading] = useState(true);

useEffect(() => {
fetch('/api/players/by-elo')
.then(response => response.json())
.then(data => {
setPlayers(data);
setLoading(false);
})
.catch(error => {
console.error('Error fetching leaderboard:', error);
setLoading(false);
});
}, []);

if (loading) return <div className="text-center mt-4">Loading...</div>;

return (
<div className="container mx-auto px-4 mt-8">
<h2 className="text-2xl font-bold mb-4">Leaderboard</h2>
<div className="bg-gray-800 shadow-md rounded-lg overflow-hidden">
<table className="min-w-full">
<thead className="bg-gray-900 text-white">
<tr>
<th className="px-6 py-3 text-left">Rank</th>
<th className="px-6 py-3 text-left">Player</th>
<th className="px-6 py-3 text-right">ELO</th>
<th className="px-6 py-3 text-right">W</th>
<th className="px-6 py-3 text-right">L</th>
<th className="px-6 py-3 text-right">D</th>
<th className="px-6 py-3 text-right">Win %</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-700">
{players.map((player, index) => (
<tr key={player.id} className="bg-gray-800 hover:bg-gray-700 text-gray-200">
<td className="px-6 py-4">{index + 1}</td>
<td className="px-6 py-4">
<Link
to={`/player/${player.player_name}`}
className="text-blue-400 hover:text-blue-300"
>
{player.player_name}
</Link>
</td>
<td className="px-6 py-4 text-right">{player.current_elo}</td>
<td className="px-6 py-4 text-right text-green-400">{player.pug_wins}</td>
<td className="px-6 py-4 text-right text-red-400">{player.pug_losses}</td>
<td className="px-6 py-4 text-right text-gray-400">{player.pug_draws}</td>
<td className="px-6 py-4 text-right">
{((player.pug_wins / (player.pug_wins + player.pug_losses)) * 100 || 0).toFixed(1)}%
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
};

export default Leaderboard;
6 changes: 2 additions & 4 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import ReactDOM from "react-dom/client";
import { BrowserRouter as Router, Route, Routes, Link } from 'react-router-dom';
import MatchesTable from "./components/MatchesTable";
import PlayerMatches from "./components/PlayerMatches";
import Leaderboard from "./components/Leaderboard";
import About from "./components/About";

import "./index.css";

Expand Down Expand Up @@ -39,10 +41,6 @@ const Navigation: React.FC = () => {
);
};

// Stub components for new routes
const Leaderboard: React.FC = () => <div>Leaderboard Coming Soon!</div>;
const About: React.FC = () => <div>About Page Coming Soon!</div>;

const App: React.FC = () => {
return (
<Router>
Expand Down
6 changes: 1 addition & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sea_orm::DatabaseConnection;

use crate::{
controllers, initializers,
models::_entities::{notes, users},
models::_entities::notes,
tasks,
workers::downloader::DownloadWorker,
};
Expand Down Expand Up @@ -54,8 +54,6 @@ impl Hooks for App {
.add_route(controllers::players::routes())
.add_route(controllers::matches::routes())
.add_route(controllers::notes::routes())
.add_route(controllers::auth::routes())
.add_route(controllers::user::routes())
}

fn connect_workers<'a>(p: &'a mut Processor, ctx: &'a AppContext) {
Expand All @@ -67,13 +65,11 @@ impl Hooks for App {
}

async fn truncate(db: &DatabaseConnection) -> Result<()> {
truncate_table(db, users::Entity).await?;
truncate_table(db, notes::Entity).await?;
Ok(())
}

async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
db::seed::<notes::ActiveModel>(db, &base.join("notes.yaml").display().to_string()).await?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/shuttle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use shuttle_runtime::DeploymentMetadata;

#[shuttle_runtime::main]
async fn main(
#[shuttle_shared_db::Postgres] conn_str: String,
#[shuttle_shared_db::Postgres] _conn_str: String,
#[shuttle_runtime::Metadata] meta: DeploymentMetadata,
) -> shuttle_axum::ShuttleAxum {
std::env::set_var("DATABASE_URL", conn_str);
// std::env::set_var("DATABASE_URL", conn_str);
let environment = match meta.env {
shuttle_runtime::Environment::Local => Environment::Development,
shuttle_runtime::Environment::Deployment => Environment::Production,
Expand Down
150 changes: 0 additions & 150 deletions src/controllers/auth.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/controllers/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn list(State(ctx): State<AppContext>) -> Result<Response> {
}

#[debug_handler]
pub async fn get_one(Path(id): Path<i32>, State(ctx): State<AppContext>) -> Result<Response> {
pub async fn get_one(Path(id): Path<u32>, State(ctx): State<AppContext>) -> Result<Response> {
let match_item = Matches::find_by_id(id).one(&ctx.db).await?;
match match_item {
Some(m) => format::json(m),
Expand All @@ -25,8 +25,8 @@ pub async fn get_one(Path(id): Path<i32>, State(ctx): State<AppContext>) -> Resu
pub async fn get_matches_by_player_name(Path(player_name): Path<String>, State(ctx): State<AppContext>) -> Result<Response> {
// First, find the player's discord_id
let statement = Statement::from_sql_and_values(
DbBackend::Postgres,
r#"SELECT * FROM players WHERE LOWER(player_name) = LOWER($1)"#,
DbBackend::MySql,
r#"SELECT * FROM players WHERE LOWER(player_name) = LOWER(?)"#,
[player_name.clone().into()]
);
let player = Players::find()
Expand Down
2 changes: 0 additions & 2 deletions src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub mod auth;
pub mod notes;
pub mod user;
pub mod matches;
pub mod players;
pub mod player_elo;
12 changes: 10 additions & 2 deletions src/controllers/player_elo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ pub async fn get_player_elo_by_player_name(
Path(player_name): Path<String>,
State(ctx): State<AppContext>
) -> Result<Response> {
println!("Received player_name: {}", player_name);

let statement = Statement::from_sql_and_values(
DbBackend::Postgres,
r#"SELECT * FROM player_elo WHERE LOWER(player_name) = LOWER($1) ORDER BY created_at ASC"#,
DbBackend::MySql,
r#"SELECT * FROM player_elo WHERE LOWER(player_name) = LOWER(?) ORDER BY created_at ASC"#,
[player_name.clone().into()]
);

println!("SQL Query: {}, Parameters: {:?}", statement.sql, statement.values);

let player_elo = Entity::find()
.from_raw_sql(statement)
.all(&ctx.db)
.await?;

println!("Query result: {:?}", player_elo);

format::json(player_elo)
}

Expand Down
Loading

0 comments on commit 6101d46

Please sign in to comment.