Skip to content

Commit

Permalink
feat: support elo rank and rating for players
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeg committed Sep 5, 2023
1 parent d022a37 commit 99e0810
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
6 changes: 4 additions & 2 deletions daos/UserDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ class UserDAO {
return user;
}

async getUserByLogin(login) {
async getUserByLogin(login, force_fetch = false) {
let user = this.users_by_login.get(login);

if (!user) {
if (!user || force_fetch) {
const result = await dbPool.query(
'SELECT * FROM twitch_users WHERE login=$1',
[login]
Expand All @@ -131,6 +131,8 @@ class UserDAO {

if (!user) {
user = this.addUserFromData(result.rows[0]);
} else {
user.updateUserFields(result.rows[0]);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions domains/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class User extends EventEmitter {
this.profile_image_url = user_object.profile_image_url;
this.dob = user_object.dob;

this.elo_rank = user_object.elo_rank || 0;
this.elo_rating = user_object.elo_rating || 0;

this.country_code = user_object.country_code || this.country_code || 'US';
this.city = user_object.city || this.city || '';
this.timezone = user_object.timezone || this.timezone || 'UTC';
Expand Down
9 changes: 6 additions & 3 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import layouts from '../modules/layouts.js';
import UserDAO from '../daos/UserDAO.js';
import ScoreDAO from '../daos/ScoreDAO.js';

import { readableScoreFomatter } from '../public/views/utils.js';

const router = express.Router();

router.get('/debug/session', (req, res) => {
Expand Down Expand Up @@ -140,7 +142,7 @@ function getAge(dob) {
}

router.get('/view/profile_card/:login', async (req, res) => {
const user = await UserDAO.getUserByLogin(req.params.login);
const user = await UserDAO.getUserByLogin(req.params.login, true);

if (!user) {
res.status(404).send('Not found');
Expand All @@ -149,8 +151,9 @@ router.get('/view/profile_card/:login', async (req, res) => {

res.render('profile_card', {
user,
age: user.dob ? getAge(user.dob) : 9,
pb: await ScoreDAO.getPB(user),
age: user.dob ? getAge(user.dob) : 9, // 😅
pb: readableScoreFomatter(await ScoreDAO.getPB(user)),
elo_rating: readableScoreFomatter(Math.floor(user.elo_rating)),
});
});

Expand Down
3 changes: 3 additions & 0 deletions setup/20230905.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE twitch_users
ADD COLUMN elo_rank INTEGER NOT NULL DEFAULT 0,
ADD COLUMN elo_rating DOUBLE PRECISION NOT NULL DEFAULT 0;
3 changes: 3 additions & 0 deletions setup/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ CREATE TABLE twitch_users (
style play_style default 'das',
timezone TEXT NOT NULL CHECK (now() AT TIME ZONE timezone IS NOT NULL) DEFAULT 'UTC',

elo_rank INTEGER NOT NULL DEFAULT 0,
elo_rating DOUBLE PRECISION NOT NULL DEFAULT 0,

created_on timestamptz NOT NULL,
last_login timestamptz NOT NULL
);
Expand Down
73 changes: 37 additions & 36 deletions views/profile_card.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,26 @@ html, body {
line-height: 24px;
font-size: var(--fsize);
color: var(--beige);
height: 100%;
}
.frame {
background: black;
height: 100%;
min-height: 100%;
background: black;
overflow: hidden;
text-align: center;
}
.container {
padding: 6px 3px;
padding: 0 3px;
display: flex;
flex-direction: column;
}
#name {
border-bottom: 3px solid var(--beige);
margin-bottom: 0.5em;
padding-top: 6px;
flex-shrink: 0;
flex-grow: 0;
}
h1 {
Expand All @@ -50,7 +55,14 @@ h1 {
line-height: 48px;
}
.container {
display: flex;
flex-direction: column;
flex: 1;
}
section {
flex: 1;
margin-top: 0.65em;
}
Expand All @@ -65,9 +77,11 @@ p {
}
#twitch {
position: absolute;
flex-grow: 0;
flex-shrink: 0;
position: relative;
bottom: 0;
margin-bottom: 20px;
margin: 20px 0;
color: white;
width: 100%;
}
Expand Down Expand Up @@ -100,49 +114,36 @@ p {
vertical-align: middle;
}
#play_style span {
margin-right: 0.2em;
}
#play_style span:last-of-type {
margin-right: 0;
}
</style>
</head>
<body>

<div class="frame">
<div class="container">
<section id="name">
<h1><%= (user.display_name || '').replace(/^\d+\.\s+/, '') %></h1>
</section>
<section id="name">
<h1><%= (user.display_name || '').replace(/^\d+\.\s+/, '') %></h1>
</section>

<div class="container">
<section id="flag">
<h1><%= [age, user.city].filter(v => v).join(' - ') %></h1>
<img src="/vendor/country-flag-icons/3x2/<%= user.country_code %>.svg" width="64">
</section>

<section id="bio">
<h1>Player bio</h1>
<p><%= user.description %></p>
</section>

<section id="play_style">
<h1>Play info</h1>
<p>Style: <%= user.style %></p>
<p>PB: <%
let remainder = pb || 0;
let parts = [];
while (remainder) {
parts.unshift(remainder % 1000);
remainder = Math.floor(remainder / 1000);
}
if (!parts.length) parts.push(0);
parts = parts
.map((part, idx) => idx > 0 ? `${part}`.padStart(3, '0') : part)
.map(part => `<span>${part}</span>`);
<p class="play_style">Style: <%= (user.style || '').toUpperCase() %></p>
<p class="pb">PB: <%= pb %></p>
</section>

%><%- parts.join('') %></p>
<section id="elo">
<h1>ELO</h1>
<p>Rank: <%= user.elo_rank %></p>
<p>Rating: <%= elo_rating %></p>
</section>

<section id="interests">
Expand Down

0 comments on commit 99e0810

Please sign in to comment.