Skip to content

Commit

Permalink
nit
Browse files Browse the repository at this point in the history
  • Loading branch information
jagnani73 committed Sep 2, 2024
1 parent e231fd3 commit 71d9b42
Show file tree
Hide file tree
Showing 44 changed files with 835 additions and 425 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Build
on:
push:
branches:
- main
pull_request:
branches:
- main
types: [opened, synchronize]
jobs:
client:
runs-on: ubuntu-latest
defaults:
run:
working-directory: packages/frontend
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Lint & Build Client
run: pnpm build

server:
runs-on: ubuntu-latest
defaults:
run:
working-directory: packages/backend
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Build Server
run: pnpm build
35 changes: 35 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Lint
on:
push:
branches:
- main
pull_request:
branches:
- main
types: [opened, synchronize]
jobs:
client:
runs-on: ubuntu-latest
defaults:
run:
working-directory: client
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install dependencies
run: yarn install
- name: Lint Client
run: yarn lint

server:
runs-on: ubuntu-latest
defaults:
run:
working-directory: server
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install dependencies
run: yarn install
- name: Lint Server
run: yarn lint
8 changes: 6 additions & 2 deletions packages/backend/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { gameTiersRouter } from "./microservices/game-tiers/game-tiers.routes";
import { gamesRouter } from "./microservices/games/games.routes";
import { leaderboardRouter } from "./microservices/leaderboard/leaderboard.routes";
import { playedGamesRouter } from "./microservices/played-games/played-games.routes";
import { playersRouter } from "./microservices/players/players.routes";
import { seasonsRouter } from "./microservices/seasons/seasons.routes";
import { usersRouter } from "./microservices/users/users.routes";
import { RedisService, SupabaseService, WSService } from "./services";
import cors from "cors";
import "dotenv/config";
Expand All @@ -28,8 +30,10 @@ app.use("/api/v1", v1Router);

v1Router.use("/game-tiers", gameTiersRouter);
v1Router.use("/games", gamesRouter);
v1Router.use("/users", usersRouter);
v1Router.use("/players", playersRouter);
v1Router.use("/seasons", seasonsRouter);
v1Router.use("/played-games", playedGamesRouter);
v1Router.use("/leaderboard", leaderboardRouter);

app.use("*", (_req: Request, res: Response) => {
res.status(404).json({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
ARCADE_CONTRACT_ADDRESS,
} from "../../utils/constants/contracts.constants";
import { NETWORK_RPC_URL } from "../../utils/constants/network-config.constants";
import { MappedPlayedGame, MappedUser } from "../../utils/types/mappers.types";
import {
MappedPlayedGame,
MappedPlayer,
} from "../../utils/types/mappers.types";
import { Contract, ethers } from "ethers";

export class ArcadeService {
Expand All @@ -22,8 +25,8 @@ export class ArcadeService {

public static async createGame(
game_id: MappedPlayedGame["game_id"],
player_1_address: MappedUser["wallet_address"],
player_2_address: MappedUser["wallet_address"],
player_1_address: MappedPlayer["wallet_address"],
player_2_address: MappedPlayer["wallet_address"],
game_tier_id: MappedPlayedGame["game_tier_id"],
is_betting_active: boolean,
) {
Expand Down Expand Up @@ -58,8 +61,8 @@ export class ArcadeService {

public static async endGame(
game_id: MappedPlayedGame["game_id"],
winner: MappedUser["wallet_address"],
loser: MappedUser["wallet_address"],
winner: MappedPlayer["wallet_address"],
loser: MappedPlayer["wallet_address"],
) {
try {
const tx = await this.arcade_contract.endGame(
Expand All @@ -78,7 +81,7 @@ export class ArcadeService {
}

public static async withdrawUserReward(
user_address: MappedUser["wallet_address"],
user_address: MappedPlayer["wallet_address"],
amount: number,
) {
try {
Expand Down
42 changes: 21 additions & 21 deletions packages/backend/microservices/connect-four/connect-four.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {
game_id,
season_id,
tier_id,
user_id,
player_id,
}: ConnectFour.JoinEvent["payload"]) => {
try {
const roomKey: string = `${season_id}::${game_id}::${tier_id}`;
const logId: string = `[${season_id}][${game_id}][${tier_id}][${user_id}]`;
const logId: string = `[${season_id}][${game_id}][${tier_id}][${player_id}]`;

let roomId: string | null =
(await RedisClient.lpop(roomKey)) || null;
Expand All @@ -38,7 +38,7 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {
);

const { played_game_id } = await createGame(
user_id,
player_id,
season_id,
game_id,
tier_id,
Expand All @@ -58,9 +58,9 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {
Omit<ConnectFour.ServerGameState, "player2">
>({
winner_id: null,
active_player: user_id,
active_player: player_id,
player1: {
user_id,
player_id,
currentMove: null,
},
board: ConnectFour.emptyBoard,
Expand All @@ -76,15 +76,15 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {

// TODO: fix self joining room

await addPlayer2ToGame(roomId, user_id);
await addPlayer2ToGame(roomId, player_id);

await RedisClient.hset(
roomId,
stringifyObjectValues<
Pick<ConnectFour.ServerGameState, "player2">
>({
player2: {
user_id,
player_id,
currentMove: null,
},
}),
Expand All @@ -103,7 +103,7 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {
type: "player-joined",
payload: {
room_id: roomId,
user_id,
player_id,
},
};
io.to(roomId).emit(
Expand All @@ -124,11 +124,11 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {
payload: {
player1: {
currentMove: null,
user_id: player1.user_id,
player_id: player1.player_id,
},
player2: {
currentMove: null,
user_id: player2.user_id,
player_id: player2.player_id,
},
active_player,
board,
Expand All @@ -150,51 +150,51 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {
async ({
move,
room_id,
user_id,
player_id,
}: ConnectFour.MoveEvent["payload"]) => {
try {
const gameState =
parseStringifiedValues<ConnectFour.ServerGameState>(
await RedisClient.hgetall(room_id),
);

if (user_id !== gameState.active_player) {
if (player_id !== gameState.active_player) {
return;
}

if (gameState.winner_id) {
return;
}

if (user_id === gameState.player1.user_id) {
if (player_id === gameState.player1.player_id) {
if (gameState.player1.currentMove === null) {
gameState.player1.currentMove = move;
} else {
return;
}
} else if (user_id === gameState.player2.user_id) {
} else if (player_id === gameState.player2.player_id) {
if (gameState.player2.currentMove === null) {
gameState.player2.currentMove = move;
} else {
return;
}
} else {
throw Error(
`Player ${user_id} does not exist in room ${room_id}`,
`Player ${player_id} does not exist in room ${room_id}`,
);
}

const activePlayer =
user_id === gameState.player1.user_id &&
player_id === gameState.player1.player_id &&
gameState.player1.currentMove
? gameState.player1
: gameState.player2.user_id === user_id &&
: gameState.player2.player_id === player_id &&
gameState.player2.currentMove
? gameState.player2
: null;

if (!activePlayer?.currentMove) {
throw Error(`no active player for player ${user_id}`);
throw Error(`no active player for player ${player_id}`);
}

for (let row = ConnectFour.rowCount - 1; row >= 0; row--) {
Expand All @@ -204,12 +204,12 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {
] === null
) {
gameState.board[row][activePlayer.currentMove.column] =
activePlayer.user_id;
activePlayer.player_id;
break;
}
}

const win = checkWinner(gameState.board, user_id);
const win = checkWinner(gameState.board, player_id);

if (!win) {
const moveEndEvent: ConnectFour.MoveEndEvent = {
Expand Down Expand Up @@ -242,7 +242,7 @@ export const ConnectFourRoutes = (socket: Socket, io: Namespace) => {
),
);
} else {
gameState.winner_id = activePlayer.user_id;
gameState.winner_id = activePlayer.player_id;
await setWinnerToGame(room_id, gameState.winner_id);

await RedisClient.del(room_id);
Expand Down
31 changes: 31 additions & 0 deletions packages/backend/microservices/game-history/game-history.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { validateQuery } from "../../middlewares/rest";
import { MappedSeason } from "../../utils/types/mappers.types";
import { getSeasonLeaderboardParams } from "./game-history.schema";
import { fetchPlayerGameHistory } from "./game-history.service";
import type { NextFunction, Request, Response } from "express";
import { Router } from "express";

export const gameHistoryRouter = Router();

const handleGetPlayerGameHistory = async (
req: Request,
res: Response,
next: NextFunction,
) => {
try {
const { season_id } = req.params as unknown as MappedSeason;
const data = await fetchPlayerGameHistory(season_id);
return res.json({
success: true,
data,
});
} catch (error) {
next(error);
}
};

gameHistoryRouter.get(
"/:season_id",
validateQuery("params", getSeasonLeaderboardParams),
handleGetPlayerGameHistory,
);
12 changes: 12 additions & 0 deletions packages/backend/microservices/game-history/game-history.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type MappedSeason } from "../../utils/types/mappers.types";
import { type PartialYupSchema } from "../../utils/types/shared.types";
import * as yup from "yup";

export const getSeasonLeaderboardParams = yup
.object()
.shape<PartialYupSchema<MappedSeason>>({
season_id: yup.string().trim().required(),
})
.strict()
.noUnknown()
.required();
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { SupabaseService } from "../../services";
import { MappedLeaderboard } from "../../utils/types/mappers.types";

export const fetchPlayerGameHistory = async (
season_id: MappedLeaderboard["season_id"],
) => {
const { data, error } = await SupabaseService.getSupabase()
.from("leaderboard")
.select()
.eq("season_id", season_id!);

if (error) {
console.error(error);
throw error;
}

return data;
};
Loading

0 comments on commit 71d9b42

Please sign in to comment.