Skip to content

Commit

Permalink
Improve map code
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Sep 15, 2024
1 parent 570e1b0 commit 08c92f7
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 17 deletions.
4 changes: 2 additions & 2 deletions components/maps/mapTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function MapTile({ onPencilClick, showEditControls, map, onHeart,
res.json().then(data => {
if(res.ok) {
toast.success(data.message);
refreshHome();
refreshHome({removeMap: mapId});
} else {
toast.error(data.message);
refreshHome();
Expand Down Expand Up @@ -154,7 +154,7 @@ export default function MapTile({ onPencilClick, showEditControls, map, onHeart,
</div>
)}
</div>
{!country && map.created_by_name && (
{!country && map.created_by_name && !map.in_review && !map.reject_reason && (
<div style={{ display: 'flex', alignItems: 'center', gap: '1rem' }}>
<button className={`map-tile__heart ${!canHeart ? 'disabled' : ''}`} onClick={handleHeartClick} disabled={!canHeart}>
{map.hearts}&nbsp;
Expand Down
18 changes: 16 additions & 2 deletions components/maps/mapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ export default function MapView({ gameOptions, setGameOptions, singleplayer, clo
const [searchResults, setSearchResults] = useState([]);
const [heartingMap, setHeartingMap] = useState("");

function refreshHome() {
function refreshHome(removeMapId) {

console.log("refreshing home", removeMapId);
// if removeMapId is set, remove it from the mapHome temporarily
if(removeMapId) {
setMapHome((prev) => {
const newMapHome = { ...prev };
Object.keys(newMapHome).forEach((section) => {
newMapHome[section] = newMapHome[section].filter((m) => m.id !== removeMapId.removeMap);
});
return newMapHome;
});
return;
}

fetch("/api/map/mapHome", {
method: "POST",
headers: {
Expand Down Expand Up @@ -340,7 +354,7 @@ export default function MapView({ gameOptions, setGameOptions, singleplayer, clo

return mapsArray.length > 0 ? (
<div className={`mapSection ${section==="recent"?"recent":""}`} key={si}>
<h2 className="mapSectionTitle">{text(section)}</h2>
<h2 className="mapSectionTitle">{text(section)}{["myMaps", "likedMaps", "reviewQueue"].includes(section) && ` (${mapsArray.length})`}</h2>

<div className={`mapSectionMaps`}>
<ScrollMenu drag>
Expand Down
10 changes: 5 additions & 5 deletions components/utils/sendableMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function sendableMap(map, user, hearted=false) {
export default function sendableMap(map, creator, hearted=false, staff=false, isCreator=false) {
return {
created_at: Date.now() - map.created_at.getTime(),
slug: map.slug,
Expand All @@ -7,15 +7,15 @@ export default function sendableMap(map, user, hearted=false) {
hearted,
plays: map.plays,
description_short: map.description_short,
description_long: (map.created_by== user._id || user.staff)?map.description_long:undefined,
data: map.data,
created_by_name: user.username ?? map.created_by,
description_long: (isCreator || staff)?map.description_long:undefined,
data: (isCreator || staff)?map.data:undefined,
created_by_name: map.map_creator_name ?? creator?.username,
id: map._id,
in_review: map.in_review,
official: map.official,
accepted: map.accepted,
reject_reason: map.reject_reason,
resubmittable: map.resubmittable,
yours: map.created_by == user._id,
yours: isCreator||staff,
}
}
4 changes: 4 additions & 0 deletions models/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const mapSchema = new mongoose.Schema({
type: String,
required: true
},
map_creator_name: {
type: String,
required: true
},
plays: {
type: Number,
default: 0
Expand Down
1 change: 1 addition & 0 deletions pages/api/map/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export default async function handler(req, res) {
maxDist: validation.maxDist,
in_review: user.instant_accept_maps ? false : true,
accepted: user.instant_accept_maps ? true : false,
map_creator_name: user.username
});

return res.status(200).json({ message: 'Map created', map });
Expand Down
48 changes: 42 additions & 6 deletions pages/api/map/mapHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export default async function handler(req, res) {
let user;

if(secret) {
console.time('findUser');
user = await User.findOne({ secret: secret });
console.timeEnd('findUser');
if(typeof secret !== 'string') {
return res.status(400).json({ message: 'Invalid input' });
}
Expand All @@ -47,12 +49,27 @@ export default async function handler(req, res) {

if(user?.staff) {
// reviewQueue
console.time('findReviewQueue');
let queueMaps = await Map.find({ in_review: true });
console.timeEnd('findReviewQueue');

console.time('findReviewQueueOwner');
let queueMapsSendable = await Promise.all(queueMaps.map(async (map) => {
const owner = await User.findById(map.created_by);
return sendableMap(map, owner, hearted_maps?hearted_maps.has(map._id.toString()):false);
let owner;
if(!map.map_creator_name) {
owner = await User.findById(map.created_by);
// save map creator name
console.log('updating map creator name', map._id, owner.username, map.name);
map.map_creator_name = owner.username;
await map.save();
} else {
owner = { username: map.map_creator_name };
}

const isCreator = map.created_by === user._id.toString();
return sendableMap(map, owner, hearted_maps?hearted_maps.has(map._id.toString()):false, true, isCreator);
}));
console.timeEnd('findReviewQueueOwner');

// oldest to newest
queueMapsSendable.sort((a,b) => b.created_at - a.created_at);
Expand All @@ -63,15 +80,25 @@ export default async function handler(req, res) {
// find maps made by user
if(user) {
let myMaps = await Map.find({ created_by: user._id.toString() });
myMaps = myMaps.map((map) => sendableMap(map, user, hearted_maps?hearted_maps.has(map._id.toString()):false));
myMaps = myMaps.map((map) => sendableMap(map, user, hearted_maps?hearted_maps.has(map._id.toString()):false, user.staff, true));
myMaps.sort((a,b) => a.created_at - b.created_at);
if(myMaps.length > 0) response.myMaps = myMaps;
// likedMaps
// find maps liked by user
const likedMaps = user.hearted_maps ? await Map.find({ _id: { $in: Array.from(user.hearted_maps.keys()) } }) : [];
let likedMapsSendable = await Promise.all(likedMaps.map(async (map) => {
const owner = await User.findById(map.created_by);
return sendableMap(map, owner, true);
let owner;
if(!map.map_creator_name) {
owner = await User.findById(map.created_by);
// save map creator name
console.log('updating map creator name', map._id, owner.username, map.name);
map.map_creator_name = owner.username;
await map.save();

} else {
owner = { username: map.map_creator_name };
}
return sendableMap(map, owner, true, user.staff, map.created_by === user._id.toString());
}));
likedMapsSendable.sort((a,b) => b.created_at - a.created_at);
if(likedMapsSendable.length > 0) response.likedMaps = likedMapsSendable;
Expand Down Expand Up @@ -106,7 +133,16 @@ export default async function handler(req, res) {
}

let sendableMaps = await Promise.all(maps.map(async (map) => {
const owner = await User.findById(map.created_by);
let owner;
if(!map.map_creator_name) {
owner = await User.findById(map.created_by);
// save map creator name
console.log('updating map creator name', map._id, owner.username, map.name);
map.map_creator_name = owner.username;
await map.save();
} else {
owner = { username: map.map_creator_name };
}
return sendableMap(map, owner,hearted_maps?hearted_maps.has(map._id.toString()):false);
}));

Expand Down
19 changes: 17 additions & 2 deletions pages/api/map/searchMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,23 @@ export default async function searchMaps(req, res) {

// Convert maps to sendable format
let sendableMaps = await Promise.all(maps.map(async (map) => {
const owner = await User.findById(map.created_by);
return sendableMap(map, owner, hearted_maps?hearted_maps.has(map._id.toString()):false);

let owner;
if(!map.map_creator_name) {
owner = await User.findById(map.created_by);
// if owner is not found, set to null
if(!owner) {
owner = null;
}
// save map creator name
console.log('updating map creator name', map._id, owner.username, map.name);
map.map_creator_name = owner.username;
await map.save();
} else{
owner = { username: map.map_creator_name };
}

return sendableMap(map, owner, hearted_maps?hearted_maps.has(map._id.toString()):false, user?.staff, map.created_by === user?._id.toString());
}));

res.status(200).json(sendableMaps);
Expand Down

0 comments on commit 08c92f7

Please sign in to comment.