Skip to content

Commit

Permalink
moved Event and Plot Stats out of Leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
20Alexanderxx committed Apr 14, 2024
1 parent 2d59431 commit 90b4806
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 44 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"start": "node --inspect index.js",
"start-nodemon": "nodemon index.js",
"start:migrate": "npx prisma migrate deploy && node index.js",
"build": "tsc"
Expand Down
2 changes: 1 addition & 1 deletion backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ model Region {

model User {
id String @id @default(uuid())
ssoId String @unique
ssoId String? @unique
discordId String?
regions Region[]
blockedFromReports Boolean @default(false)
Expand Down
46 changes: 38 additions & 8 deletions backend/src/controllers/StatsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
******************************************************************************/

import Core from "../Core.js";
import {Request, Response} from "express";
import {validationResult} from "express-validator";
import { Request, Response } from "express";
import { validationResult } from "express-validator";


export default class StatsController {
Expand All @@ -20,29 +20,56 @@ export default class StatsController {

public async getGeneralStats(request: Request, response: Response) {
const regionCount = await this.core.getPrisma().region.count();
const {_sum: sums} = await this.core.getPrisma().region.aggregate({
const { _sum: sums } = await this.core.getPrisma().region.aggregate({
_sum: {
area: true,
buildings: true
}
})
});
const totalArea = sums.area;
const totalBuildings = sums.buildings;
response.send({regionCount, totalArea, totalBuildings});

const { _sum: plotSums } = await this.core.getPrisma().region.aggregate({
_sum: {
area: true
},
where: {
isPlotRegion: true
}
});

const totalPlotArea = plotSums.area;

const { _sum: eventSums } = await this.core.getPrisma().region.aggregate({
_sum: {
area: true
},
where: {
isEventRegion: true
}
});

const totalEventArea = eventSums.area;

response.send({ regionCount, totalArea, totalBuildings, totalPlotArea, totalEventArea });
}

public async getLeaderboard(request: Request, response: Response) {
const errors = validationResult(request);
if (!errors.isEmpty()) {
return response.status(400).json({errors: errors.array()});
return response.status(400).json({ errors: errors.array() });
}

const groupUsers = await this.core.getPrisma().region.groupBy({
by: ['username'],
_sum: {
area: true,
buildings: true
buildings: true,
},
where: {
isEventRegion: false,
isPlotRegion: false,
}
})
let count = groupUsers.length;

Expand All @@ -59,7 +86,10 @@ export default class StatsController {
},
skip: parseInt(<string>request.query.page) * 10,
take: 10,

where: {
isEventRegion: false,
isPlotRegion: false
}
})


Expand Down
4 changes: 3 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"react-icons": "^4.7.1",
"react-router-dom": "6.6.2",
"socket.io-client": "^4.5.4",
"three": "^0.148.0"
"three": "^0.148.0",
"unplugin": "^1.4.0",
"yarn": "^1.22.19"
},
"devDependencies": {
"@import-meta-env/cli": "^0.6.5",
Expand Down
22 changes: 5 additions & 17 deletions frontend/src/components/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,10 @@ const Map = forwardRef(({openDialog, setRegionViewData, updateMap, setUpdateMap}
const query = useQuery();

const styles = [
{
title: "Basemap",
uri: "https://sgx.geodatenzentrum.de/gdz_basemapde_vektor/styles/bm_web_col.json"
}, {
title: "Basemap Dark",
uri: "https://basemap.de/data/produkte/web_vektor/styles/bm_web_drk.json"
},
{
title: "Dark",
uri: "mapbox://styles/nachwahl/cl2nl1qes00bn14ksw5y85arm"
},

{
title: "Light",
uri: "mapbox://styles/mapbox/light-v9"
},
{title: "Basemap", uri: "https://sgx.geodatenzentrum.de/gdz_basemapde_vektor/styles/bm_web_col.json"},
{title: "Basemap Dark", uri: "https://basemap.de/data/produkte/web_vektor/styles/bm_web_drk.json"},
{title: "Dark", uri: "mapbox://styles/nachwahl/cl2nl1qes00bn14ksw5y85arm"},
{title: "Light", uri: "mapbox://styles/mapbox/light-v9"},
{title: "Outdoors", uri: "mapbox://styles/mapbox/outdoors-v11"},
{title: "Satellite", uri: "mapbox://styles/mapbox/satellite-streets-v11"},
{title: "Streets", uri: "mapbox://styles/mapbox/streets-v11"}
Expand Down Expand Up @@ -255,7 +243,7 @@ const Map = forwardRef(({openDialog, setRegionViewData, updateMap, setUpdateMap}
break;
}
}
let regions = await axios.get("/api/v1/region/all/geojson");
var regions = await axios.get("/api/v1/region/all/geojson");
setShowLoadingOverlay(false);
_map.addSource('regions', {
'type': 'geojson',
Expand Down
38 changes: 22 additions & 16 deletions frontend/src/pages/Stats.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,28 @@ const Stats = props => {

useEffect(() => {
getLeaderboard();
}, [activePage])
}, [activePage]);

const getData = async () => {
const {data: generalData} = await axios.get("/api/v1/stats/general")
const {data: leaderboardData} = await axios.get("/api/v1/stats/leaderboard?page=" + (activePage - 1))
const {data: generalData} = await axios.get("/api/v1/stats/general");
const {data: leaderboardData} = await axios.get("/api/v1/stats/leaderboard?page=" + (activePage - 1));
console.log(generalData);
setGeneralStats(generalData);
setLeaderboard(leaderboardData.leaderboard);
setTotalPages(Math.ceil(leaderboardData.count / 10));

setLoading(false);
}
};

const getLeaderboard = async () => {
const {data: leaderboardData} = await axios.get("/api/v1/stats/leaderboard?page=" + (activePage - 1))
const {data: leaderboardData} = await axios.get("/api/v1/stats/leaderboard?page=" + (activePage - 1));
setLeaderboard(leaderboardData.leaderboard);
setTotalPages(Math.ceil(leaderboardData.count / 10));
}
};

const numberWithCommas = (x) => {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
};


return (
Expand All @@ -82,14 +83,19 @@ const Stats = props => {
<Title mb={"md"}>Stats</Title>
<Grid>
<Grid.Col sm={12} lg={6}><StatsCard icon={<FiList/>} title={"Total number of regions"}
value={parseInt(generalStats.regionCount).toLocaleString()}
/></Grid.Col>
value={parseInt(generalStats.regionCount).toLocaleString()}/></Grid.Col>
<Grid.Col sm={12} lg={6}><StatsCard icon={<BiBuilding/>} title={"Finished Buildings"}
value={parseInt(generalStats.totalBuildings).toLocaleString()}
/></Grid.Col>
value={parseInt(generalStats.totalBuildings).toLocaleString()}/></Grid.Col>
<Grid.Col sm={12} lg={6}><StatsCard icon={<BiArea/>} title={"Total area of all regions"}
value={numberWithCommas(generalStats.totalArea) + " m²"}
valueSmall={"this is about " + ((generalStats.totalArea / 357386000000) * 100).toFixed(10).toLocaleString() + "% of Germany's area"}/></Grid.Col>
<Grid.Col sm={12} lg={6}><StatsCard icon={<BiArea/>} title={"finished Area of Germany"}
value={"69420 m²"}
valueSmall={"this is about " + ((69420 / 357386000000) * 100).toFixed(10).toLocaleString() + "% of Germany's area"}/></Grid.Col>
<Grid.Col sm={12} lg={6}><StatsCard icon={<BiBuilding/>} title={"Event Area of Germany"}
value={generalStats.totalEventArea ? numberWithCommas(generalStats.totalEventArea) + " m²" : "Data not available"}/></Grid.Col>
<Grid.Col sm={12} lg={6}><StatsCard icon={<BiBuilding/>} title={"Plot Area of Germany"}
value={generalStats.totalPlotArea ? numberWithCommas(generalStats.totalPlotArea) + " m²" : "Data not available"}/></Grid.Col>
</Grid>

<Title my={"md"}>Leaderboard</Title>
Expand Down Expand Up @@ -121,7 +127,7 @@ const Stats = props => {
<td>{player.area}</td>
<td>{player.buildings}</td>
</tr>
)
);
})
}
</tbody>
Expand All @@ -133,7 +139,7 @@ const Stats = props => {

</div>
);
}
};

const StatsCard = ({title, value, icon, valueSmall}) => {
const {classes} = useStyles();
Expand Down Expand Up @@ -169,7 +175,7 @@ const StatsCard = ({title, value, icon, valueSmall}) => {
</Text>}

</Paper>
)
}
);
};

export default Stats
export default Stats;

0 comments on commit 90b4806

Please sign in to comment.