Skip to content

Commit

Permalink
fix broken database
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Feb 2, 2024
1 parent 1525ab7 commit 262fc9b
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 142 deletions.
5 changes: 3 additions & 2 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
"author": "",
"license": "ISC",
"dependencies": {
"@supabase/supabase-js": "^2.39.3",
"@types/node": "^20.11.5",
"cors": "^2.8.5",
"dotenv": "^16.3.2",
"express": "^4.18.2"
"express": "^4.18.2",
"pg": "^8.11.3"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.11",
"@types/pg": "^8.11.0",
"@types/supertest": "^6.0.2",
"jest": "^29.7.0",
"nodemon": "^3.0.3",
Expand Down
40 changes: 19 additions & 21 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// app.ts
import express from "express";
import cors from "cors";
import leaderboard from "./routes/leaderboardRoute";
import fleetleaderboard from "./routes/fleetLeaderboardRoute";
import techleaderboard from "./routes/techLeaderboardRoute";
import battleReportsRoute from "./routes/battleReportsRoute";
import upgradesLevelsRoute from "./routes/upgradesLevelsRoute";
import universeRoute from "./routes/universeRoute";
import express from 'express';
import cors from 'cors';
import leaderboard from './routes/leaderboardRoute';
import fleetleaderboard from './routes/fleetLeaderboardRoute';
import techleaderboard from './routes/techLeaderboardRoute';
import battleReportsRoute from './routes/battleReportsRoute';
import universeRoute from './routes/universeRoute';
const app = express();

const allowedOrigins = [
"https://www.app.testnet.no-game.xyz",
"https://www.api.testnet.no-game.xyz",
"http://localhost:3000",
"http://localhost:5173",
'https://www.app.testnet.no-game.xyz',
'https://www.api.testnet.no-game.xyz',
'http://localhost:3000',
'http://localhost:5173',
];

const corsOptions = {
Expand All @@ -24,7 +23,7 @@ const corsOptions = {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error("CORS policy violation"));
callback(new Error('CORS policy violation'));
}
},
};
Expand All @@ -34,16 +33,15 @@ app.use(cors(corsOptions));
app.use(express.json());

// Define a route handler for the root path to respond with a simple message
app.get("/", (req, res) => {
res.send("Hello, World!");
app.get('/', (req, res) => {
res.send('Welcome to NoGame!');
});

// Use your leaderboard routes
app.use("/leaderboard", leaderboard);
app.use("/fleet", fleetleaderboard);
app.use("/tech", techleaderboard);
app.use("/battle-reports", battleReportsRoute);
app.use("/upgrades-levels", upgradesLevelsRoute);
app.use("/universe", universeRoute);
app.use('/leaderboard', leaderboard);
app.use('/fleet', fleetleaderboard);
app.use('/tech', techleaderboard);
app.use('/battle-reports', battleReportsRoute);
app.use('/universe', universeRoute);

export default app;
16 changes: 16 additions & 0 deletions packages/backend/src/config/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Pool } from 'pg';
import dotenv from 'dotenv';

// Check the NODE_ENV and load the corresponding .env file
const envFile = process.env.NODE_ENV === 'production' ? '.env' : '.env.local';
dotenv.config({ path: envFile });

const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
});

export default pool;
18 changes: 0 additions & 18 deletions packages/backend/src/config/supabaseClient.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/backend/src/controllers/getUpgradesLevels.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/backend/src/routes/upgradesLevelsRoute.ts

This file was deleted.

17 changes: 9 additions & 8 deletions packages/backend/src/services/fetchBattleReportsForPlanet.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import supabase from "../config/supabaseClient";
import pool from '../config/db'; // Ensure you have the correct path to your db config

export const fetchBattleReportsForPlanet = async (planetId: number) => {
const { data, error } = await supabase.rpc("get_battle_reports_for_planet", {
planet_id_param: planetId,
});
try {
// Assuming you have a stored procedure or a direct SQL query equivalent to 'get_battle_reports_for_planet'
const queryText = 'SELECT * FROM get_battle_reports_for_planet($1);'; // or a SELECT query if it's not a procedure
const values = [planetId];

if (error) {
console.error("Error fetching battle reports:", error);
const { rows } = await pool.query(queryText, values);
return rows;
} catch (error) {
console.error('Error fetching battle reports:', error);
return null;
}

return data;
};

export default fetchBattleReportsForPlanet;
14 changes: 8 additions & 6 deletions packages/backend/src/services/fetchFleetLeaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import supabase from "../config/supabaseClient";
import pool from '../config/db'; // Ensure you have the correct path to your db config

export const fetchFleetLeaderboard = async () => {
const { data, error } = await supabase.rpc("fetch_fleet_leaderboard");
try {
// Assuming you have a stored procedure or a direct SQL query equivalent to 'fetch_fleet_leaderboard'
const queryText = 'SELECT * FROM fetch_fleet_leaderboard();'; // or a SELECT query if it's not a procedure

if (error) {
console.error("Error fetching fleet leaderboard:", error);
const { rows } = await pool.query(queryText);
return rows;
} catch (error) {
console.error('Error fetching fleet leaderboard:', error);
throw error;
}

return data;
};

export default fetchFleetLeaderboard;
14 changes: 8 additions & 6 deletions packages/backend/src/services/fetchLeaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import supabase from "../config/supabaseClient";
import pool from '../config/db'; // Ensure you have the correct path to your db config

export const fetchLeaderBoard = async () => {
const { data, error } = await supabase.rpc("fetch_leaderboard"); // This is the name of your stored procedure
try {
// Assuming you have a stored procedure or a direct SQL query equivalent to 'fetch_fleet_leaderboard'
const queryText = 'SELECT * FROM fetch_leaderboard();'; // or a SELECT query if it's not a procedure

if (error) {
console.error("Error fetching leaderboard:", error);
const { rows } = await pool.query(queryText);
return rows;
} catch (error) {
console.error('Error fetching leaderboard:', error);
throw error;
}

return data;
};

export default fetchLeaderBoard;
14 changes: 8 additions & 6 deletions packages/backend/src/services/fetchTechLeaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import supabase from "../config/supabaseClient";
import pool from '../config/db'; // Ensure you have the correct path to your db config

export const fetchTechLeaderboard = async () => {
const { data, error } = await supabase.rpc("fetch_tech_leaderboard");
try {
// Assuming you have a stored procedure or a direct SQL query equivalent to 'fetch_fleet_leaderboard'
const queryText = 'SELECT * FROM fetch_tech_leaderboard();'; // or a SELECT query if it's not a procedure

if (error) {
console.error("Error fetching fleet leaderboard:", error);
const { rows } = await pool.query(queryText);
return rows;
} catch (error) {
console.error('Error fetching tech leaderboard:', error);
throw error;
}

return data;
};

export default fetchTechLeaderboard;
14 changes: 8 additions & 6 deletions packages/backend/src/services/fetchUniverse.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import supabase from "../config/supabaseClient";
import pool from '../config/db';

export const fetchUniverse = async () => {
const { data, error } = await supabase.rpc("get_universe"); // This is the name of your stored procedure
try {
// Assuming you have a stored procedure or a direct SQL query equivalent to 'fetch_fleet_leaderboard'
const queryText = 'SELECT * FROM get_universe();'; // or a SELECT query if it's not a procedure

if (error) {
console.error("Error fetching universe:", error);
const { rows } = await pool.query(queryText);
return rows;
} catch (error) {
console.error('Error fetching universe:', error);
throw error;
}

return data;
};

export default fetchUniverse;
16 changes: 0 additions & 16 deletions packages/backend/src/services/fetchUpgradesLeveles.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const BattleReports = ({ planetId }: Props) => {
const apiUrl =
nodeEnv === 'production'
? `https://www.api.testnet.no-game.xyz/battle-reports?planet_id=${planetId}`
: 'http://localhost:3001/battle_reports?planet_id=${planetId}';
: `http://localhost:3001/battle-reports?planet_id=${planetId}`;

useEffect(() => {
const fetchData = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const LeadearBoardFleet = ({ planetId }: Props) => {
{leaderboard.map((entry: FetchData, index: number) => (
<Row
key={entry.planet_id}
isHighlighted={entry.planet_id === Number(planetId)}
isHighlighted={Number(entry.planet_id) === Number(planetId)}
>
<Data>{index + 1}</Data>
<Data>
Expand All @@ -120,7 +120,7 @@ const LeadearBoardFleet = ({ planetId }: Props) => {
).substring(entry.account.length - 4)}`
: 'Unknown Account'}
</Data>
<Data>{entry.planet_id}</Data>
<Data>{Number(entry.planet_id)}</Data>
<Data>{Math.round(Number(entry.net_amount) / 1000)}</Data>
</Row>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const LeadearBoardMain = ({ planetId }: Props) => {
{leaderboard.map((entry: FetchData, index: number) => (
<Row
key={entry.planet_id}
isHighlighted={entry.planet_id === Number(planetId)}
isHighlighted={Number(entry.planet_id) === Number(planetId)}
>
<Data>{index + 1}</Data>
<Data>
Expand All @@ -127,7 +127,7 @@ const LeadearBoardMain = ({ planetId }: Props) => {
)}...${entry.account.substring(entry.account.length - 4)}`
: 'Unknown Account'}
</Data>
<Data>{entry.planet_id}</Data>
<Data>{Number(entry.planet_id)}</Data>
<Data>{Math.round(Number(entry.net_amount) / 1000)}</Data>
</Row>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const LeadearBoardTech = ({ planetId }: Props) => {
{leaderboard.map((entry: FetchData, index: number) => (
<Row
key={entry.planet_id}
isHighlighted={entry.planet_id === Number(planetId)}
isHighlighted={Number(entry.planet_id) === Number(planetId)}
>
<Data>{index + 1}</Data>
<Data>
Expand All @@ -120,7 +120,7 @@ const LeadearBoardTech = ({ planetId }: Props) => {
)}...${entry.account.substring(entry.account.length - 4)}`
: 'Unknown Account'}
</Data>
<Data>{entry.planet_id}</Data>
<Data>{Number(entry.planet_id)}</Data>
<Data>{Math.round(Number(entry.total_spent) / 1000)}</Data>
</Row>
))}
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/components/leaderboards/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export function useFetchLeaderboardData() {
export function useGetPlanetRanking(planetId: number) {
const { leaderboard, isLoading, error } = useFetchLeaderboardData();
const [position, setPosition] = useState<number | string>('Loading...');

console.log('planetId', planetId);
useEffect(() => {
if (!isLoading && !error) {
const foundPosition = leaderboard.findIndex(
(entry) => entry.planet_id === planetId
(entry) => Number(entry.planet_id) === planetId
);
if (foundPosition === -1) {
setPosition('Planet not found in the leaderboard');
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/panels/UniverseViewTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const UniverseBoxItem = ({
const planetArg = planet.planetId > 500 ? motherPlanet : planet.planetId;

const planetRanking = useGetPlanetRanking(planetArg);
console.log('planetRanking', planetRanking);
const winLoss = useCalculateWinsAndLosses(planetArg);
const lastActive = useLastActive(planetArg);
const isNoobProtected = useGetIsNoobProtected(Number(ownPlanetId), planetArg);
Expand Down

0 comments on commit 262fc9b

Please sign in to comment.