Skip to content

Commit

Permalink
await promises, confirm commands, more shit
Browse files Browse the repository at this point in the history
  • Loading branch information
mariansam committed Jun 15, 2023
1 parent fae470c commit 657fb1f
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 24 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"filldb": "tsx --tsconfig ./tsconfig.node.json scripts/filldb.ts",
"resetdb": "tsx --tsconfig ./tsconfig.node.json scripts/resetdb.ts"
"resetdb": "tsx --tsconfig ./tsconfig.node.json scripts/resetdb.ts",
"stats": "tsx --tsconfig ./tsconfig.node.json scripts/stats.ts"
},
"dependencies": {
"bootstrap": "^5.2.3",
Expand Down
8 changes: 8 additions & 0 deletions scripts/filldb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ import { parseTeamsPlayers } from './filldb/parse';
import { addMembersToTeams, createEmptyTeams } from './filldb/teams';
import { createPlayers } from './filldb/players';
import { createGames, createTeachersGame } from './filldb/games';
import { confirm } from './util';

const { POCKETBASE_ENDPOINT, POCKETBASE_ADMIN_EMAIL, POCKETBASE_ADMIN_PASSWORD } = process.env;
assert(POCKETBASE_ENDPOINT, 'missing env var POCKETBASE_ENDPOINT');
assert(POCKETBASE_ADMIN_EMAIL, 'missing env var POCKETBASE_ADMIN_EMAIL');
assert(POCKETBASE_ADMIN_PASSWORD, 'missing env var POCKETBASE_ADMIN_PASSWORD');

console.log(`Do you really want to fill the db at ${POCKETBASE_ENDPOINT} with teams and players?`)
console.warn('This might lead to duplicit data!');
console.log('Confirm with y<Enter>')
if (!await confirm())
process.exit(1);

const csv = fs.readFileSync('./tymy.csv', 'utf8');
const teams = parseTeamsPlayers(csv);

Expand All @@ -35,3 +42,4 @@ const nextNo = await createGames(studentTeams, pb);
const teachersTeam = emptyTeams.find(t => t.teachers);
assert(teachersTeam);
await createTeachersGame(teachersTeam, nextNo, pb);
process.exit(0);
8 changes: 8 additions & 0 deletions scripts/resetdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import assert from 'assert';
import 'dotenv/config';
import PocketBase from 'pocketbase';
import type { Game, GameState, Goal, News, Player, Team } from '../src/types';
import { confirm } from './util';

const { POCKETBASE_ENDPOINT, POCKETBASE_ADMIN_EMAIL, POCKETBASE_ADMIN_PASSWORD } = process.env;
assert(POCKETBASE_ENDPOINT, 'missing env var POCKETBASE_ENDPOINT');
assert(POCKETBASE_ADMIN_EMAIL, 'missing env var POCKETBASE_ADMIN_EMAIL');
assert(POCKETBASE_ADMIN_PASSWORD, 'missing env var POCKETBASE_ADMIN_PASSWORD');

console.log(`Do you really want to reset the db at ${POCKETBASE_ENDPOINT}?`)
console.warn('This will delete data!');
console.log('Confirm with y<Enter>')
if (!await confirm())
process.exit(1);

const TEACHERS_TEAM_ID = 'pg7a6r910fvfhlq';

const pb = new PocketBase(POCKETBASE_ENDPOINT);
Expand Down Expand Up @@ -108,3 +115,4 @@ console.log();


console.log('DONE');
process.exit(0);
42 changes: 42 additions & 0 deletions scripts/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assert from 'assert';
import 'dotenv/config';
import PocketBase from 'pocketbase';
import type { Game, GameState, Goal, News, Player, Team } from '../src/types';

const { POCKETBASE_ENDPOINT, POCKETBASE_ADMIN_EMAIL, POCKETBASE_ADMIN_PASSWORD } = process.env;
assert(POCKETBASE_ENDPOINT, 'missing env var POCKETBASE_ENDPOINT');
assert(POCKETBASE_ADMIN_EMAIL, 'missing env var POCKETBASE_ADMIN_EMAIL');
assert(POCKETBASE_ADMIN_PASSWORD, 'missing env var POCKETBASE_ADMIN_PASSWORD');

const transformTeam = (team: Team, allGames: Game[]) => {
const { id, no, name, points } = team;
return { id, no, name, points, games: completedGames(team, allGames) };
};

const completedGames = (team: Team, allGames: Game[]) => {
let res = 0;
for (const game of allGames) {
if (!game.finished)
continue;

if (game.team1 === team.id || game.team2 === team.id)
res += 1;
}

return res >= 9 ? 'DONE' : res;
};

const TEACHERS_TEAM_ID = 'pg7a6r910fvfhlq';

const pb = new PocketBase(POCKETBASE_ENDPOINT);
await pb.admins.authWithPassword(POCKETBASE_ADMIN_EMAIL, POCKETBASE_ADMIN_PASSWORD);

const teamsColl = pb.collection('teams');
const teams = await teamsColl.getFullList<Team>();

const gamesColl = pb.collection('games');
const games = await gamesColl.getFullList<Game>();

const sortedTeams = [...teams].sort((a, b) => b.points - a.points);

console.table(sortedTeams.map(t => transformTeam(t, games)));
11 changes: 11 additions & 0 deletions scripts/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const confirm = () => {
return new Promise((resolve, reject) => {
process.stdin.once('data', data => {
const letterCode = data[0];
if (letterCode === 121)
resolve(true);
else
resolve(false);
});
});
};
31 changes: 20 additions & 11 deletions src/admin-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ export const useAdminLogic = () => {
gameMiscActions,
} = useGameLogic();

const startMatchFromStart = () => {
gameMiscActions.update(gameState.id as ReferenceTo<GameState>, {
const startMatchFromStart = async () => {
await gameMiscActions.update(gameState.id as ReferenceTo<GameState>, {
matchStarted: true,
currentGameNo: 0,
currentGameStart: new Date(),
});
};

const startMatchFromNo = (no: number) => {
gameMiscActions.update(gameState.id as ReferenceTo<GameState>, {
const startMatchFromNo = async (no: number) => {
await gameMiscActions.update(gameState.id as ReferenceTo<GameState>, {
matchStarted: true,
currentGameNo: no,
currentGameStart: new Date(),
});
};

const startGameNo = (no: number) => {
gameMiscActions.update(gameState.id as ReferenceTo<GameState>, {
const startGameNo = async (no: number) => {
await gameMiscActions.update(gameState.id as ReferenceTo<GameState>, {
currentGameNo: no,
currentGameStart: new Date(),
});
Expand Down Expand Up @@ -68,13 +68,21 @@ export const useAdminLogic = () => {
};
}

gamesActions.update(game.id as ReferenceTo<Game>, gameObjChange);
playersActions.update(player.id as ReferenceTo<Player>, { goals: [...player.goals, theGoal.id] } as Partial<Player>);
await Promise.all([
gamesActions.update(game.id as ReferenceTo<Game>, gameObjChange),
playersActions.update(player.id as ReferenceTo<Player>, { goals: [...player.goals, theGoal.id] } as Partial<Player>),
]);
};

const savePoints = (team1: Team, team2: Team, newPoints1: number, newPoints2: number) => {
teamsActions.update(team1.id as ReferenceTo<Player>, { points: team1.points + newPoints1 } as Partial<Team>);
teamsActions.update(team2.id as ReferenceTo<Player>, { points: team2.points + newPoints2 } as Partial<Team>);
const savePoints = async (team1: Team, team2: Team, newPoints1: number, newPoints2: number) => {
await Promise.all([
teamsActions.update(team1.id as ReferenceTo<Player>, { points: team1.points + newPoints1 } as Partial<Team>),
teamsActions.update(team2.id as ReferenceTo<Player>, { points: team2.points + newPoints2 } as Partial<Team>),
]);
};

const finishGame = async (game: Game) => {
await gamesActions.update(game.id as ReferenceTo<Game>, { finished: true } as Partial<Game>);
};

return {
Expand All @@ -83,5 +91,6 @@ export const useAdminLogic = () => {
startGameNo,
newGoal,
savePoints,
finishGame,
};
};
1 change: 0 additions & 1 deletion src/components/app-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const AppHeader: React.FC = () => {
<Link href='/table'><Nav.Link>Velká tabulka</Nav.Link></Link>
<Link href='/bufet'><Nav.Link>Bufet</Nav.Link></Link>
<Link href='/voting'><Nav.Link>Hlasování</Nav.Link></Link>
<Link href='/admin'><Nav.Link>Admin</Nav.Link></Link>
</Nav>
</Navbar.Collapse>
</Container>
Expand Down
21 changes: 10 additions & 11 deletions src/pages/admin/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const AdminHomePage: React.FC = () => {
teams,
games,
gameState,
currentGame,
// startNextGame,
// prevGame,
// startMatch,
Expand All @@ -29,6 +30,7 @@ export const AdminHomePage: React.FC = () => {
startMatchFromNo,
startGameNo,
savePoints,
finishGame,
} = useAdminLogic();

const [showGameNo, setShowGameNo] = useState(gameState.currentGameNo);
Expand Down Expand Up @@ -80,20 +82,17 @@ export const AdminHomePage: React.FC = () => {
setShowGameNo(prev => prev - 1);
};

const startNextGame = () => {
showNextGame();
startGameNo(showGameNo + 1);
};

const startCurrentGame = () => {
startGameNo(showGameNo);
const startCurrentGame = async () => {
await startGameNo(showGameNo);
};

const saveStartNext = () => {
if (!team1 || !team2)
const saveStartNext = async () => {
if (!team1 || !team2 || !currentGame.game)
return;
savePoints(team1, team2, newPoints1, newPoints2);
startNextGame();
await savePoints(team1, team2, newPoints1, newPoints2);
await finishGame(currentGame.game);
await startGameNo(showGameNo + 1);
showNextGame();
}

return (
Expand Down

0 comments on commit 657fb1f

Please sign in to comment.