Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move initial node filtering logic to backend #50

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public NodeService(NodeRepo nodeRepo, IdService idService, MongoUserService mong
}

public List<Node> list() {
return this.nodeRepo.findAll();
String username = SecurityContextHolder.getContext().getAuthentication().getName();
Player player = this.playerService.getPlayer(username);
return this.nodeRepo.findAll().stream().filter(node -> node.ownerId().equals(player.id()) || getDistance(player.coordinates().latitude(), player.coordinates().longitude(), node.coordinates().latitude(), node.coordinates().longitude()) <= 25000).toList();
}

public Node add(NodeData nodeData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void returnsAllNodesInList_whenGettingNodes() throws Exception {
Node node2 = new Node("def", "456", "Office", 2, 100, new Coordinates(0, 0, 0), 0, 0);
nodeRepo.save(node1);
nodeRepo.save(node2);
playerRepo.insert(new Player("123", "2", "test", new Coordinates(0, 0, 0), 1, 0, 100, 100, 100, 5, 15, 0, 0));
String expected = """
[
{
Expand Down Expand Up @@ -499,6 +500,7 @@ void returnListWithoutDeletedNode_whenDeletingNode() throws Exception {
Node node2 = new Node("def", "456", "Office", 2, 100, new Coordinates(0, 0, 0), 0, 0);
nodeRepo.save(node1);
nodeRepo.save(node2);
playerRepo.insert(new Player("123", "1", "admin", new Coordinates(0, 0, 0), 1, 0, 100, 100, 100, 5, 15, 0, 0));
String expected = """
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NodeServiceTest {
MongoUser player = new MongoUser("abc", playerName, "[email protected]", "password", Role.PLAYER);
MongoUser admin = new MongoUser("1", adminName, "[email protected]", "admin", Role.ADMIN);
Player playerunknown = new Player("123", "abc", "playerunknown", new Coordinates(0, 0, Instant.now().getEpochSecond()), 1, 0, 100, 100, 100, 5, 10, 0, Instant.now().getEpochSecond());
Player adminPlayer = new Player("456", "1", "admin", new Coordinates(0, 0, Instant.now().getEpochSecond()), 1, 0, 100, 100, 100, 5, 10, 0, Instant.now().getEpochSecond());

@Test
void expectAllNodesInList() {
Expand All @@ -50,6 +51,10 @@ void expectAllNodesInList() {
Node node2 = new Node("def", "456", "Office", 2, 100, new Coordinates(0, 0, 0), 0, 0);
List<Node> expected = List.of(node1, node2);
//when
when(authentication.getName()).thenReturn(playerName);
when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
when(playerService.getPlayer(playerName)).thenReturn(playerunknown);
when(nodeRepo.findAll()).thenReturn(expected);
List<Node> actual = nodeService.list();
//then
Expand Down Expand Up @@ -185,8 +190,9 @@ void expectListWithoutNodeWhenNodeIsDeleted() {
when(authentication.getName()).thenReturn(adminName);
when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
when(nodeRepo.findAll()).thenReturn(List.of(node1));
when(mongoUserService.getUserByUsername(adminName)).thenReturn(admin);
when(playerService.getPlayer(adminName)).thenReturn(adminPlayer);
when(nodeRepo.findAll()).thenReturn(List.of(node1));
nodeService.delete("abc");
List<Node> actual = nodeService.list();
//then
Expand Down
Binary file removed frontend/src/assets/images/intelligence.png
Binary file not shown.
9 changes: 2 additions & 7 deletions frontend/src/components/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default function MapView({player}: Props) {
const gps = useStore(state => state.gps)
const nodes = useStore(state => state.nodes)
const isScanning = useStore(state => state.isScanning)
const initialNodeFilter = useStore(state => state.initialNodeFilter)

const range = 0.05;

Expand All @@ -41,13 +40,9 @@ export default function MapView({player}: Props) {
// reload markers when scanning is done
useEffect(() => {
if (player && nodes) {
const initialFilteredNodes = initialNodeFilter({
latitude: player.coordinates.latitude,
longitude: player.coordinates.longitude
}, nodes);
setMarkers(initialFilteredNodes)
setMarkers(nodes)
}
}, [initialNodeFilter, nodes, player]);
}, [nodes, player]);

function getBounds(player: Player) {
return L.latLngBounds(L.latLng(player.coordinates.latitude - range, player.coordinates.longitude - range),
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {AppBar, Avatar, Box, Container, IconButton, Menu, MenuItem, Toolbar, Tooltip, Typography} from "@mui/material";
import styled from "@emotion/styled";
import Intelligence from "../assets/images/intelligence.png"
import Intelligence from "../../../../NetwalkerMobile/assets/intelligence.png"
import PlayerAvatar from "../assets/images/defaultAvatar.webp"
import {useLocation, useNavigate} from "react-router-dom";
import React, {useState} from "react";
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/components/NodeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ export default function NodeList({ player, nodes }: Props) {
const ownerNodesFilter = useStore(state => state.ownerNodesFilter);
const filterNodesByOwner = useStore(state => state.filterNodesByOwner);
const rangeFilter = useStore(state => state.rangeFilter);
const initialNodeFilter = useStore(state => state.initialNodeFilter);
const filterNodesByRange = useStore(state => state.filterNodesByRange);

useEffect(() => {
if (user !== "" && user !== "anonymousUser") {
if (nodes && player) {
const initialFilteredNodes = initialNodeFilter({latitude: player.coordinates.latitude, longitude: player.coordinates.longitude}, nodes);
const filteredNodesbyOwner = filterNodesByOwner(player.id, initialFilteredNodes);
const filteredNodesbyOwner = filterNodesByOwner(player.id, nodes);
const filteredNodesByRange = filterNodesByRange({latitude: player.coordinates.latitude, longitude: player.coordinates.longitude}, filteredNodesbyOwner, 250);
setSortedNodes(sortNodesByDistance({latitude: player.coordinates.latitude, longitude: player.coordinates.longitude}, filteredNodesByRange));
}
}
}, [getNodes, nodes, player, user, sortDirection, ownerNodesFilter, rangeFilter, initialNodeFilter, filterNodesByOwner, filterNodesByRange, sortNodesByDistance]);
}, [getNodes, nodes, player, user, sortDirection, ownerNodesFilter, rangeFilter, filterNodesByOwner, filterNodesByRange, sortNodesByDistance]);

if (player) {
return (
Expand Down
11 changes: 0 additions & 11 deletions frontend/src/hooks/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type State = {
filterNodesByOwner: (ownerId: string, nodes: Node[]) => Node[]
rangeFilter: boolean
toggleRangeFilter: () => void
initialNodeFilter: (position: { latitude: number, longitude: number }, nodes: Node[]) => Node[]
filterNodesByRange: (position: { latitude: number, longitude: number }, nodes: Node[], range: number) => Node[]
volume: number
setVolume: (volume: number) => void
Expand Down Expand Up @@ -265,16 +264,6 @@ export const useStore = create<State>(set => ({
set((state) => ({rangeFilter: !state.rangeFilter}));
},

initialNodeFilter: (position: { latitude: number, longitude: number }, nodes: Node[]) => {
return nodes.filter((node) => {
const distance = getDistanceBetweenCoordinates(position, {
latitude: node.coordinates.latitude,
longitude: node.coordinates.longitude
});
return distance <= 25000;
});
},

filterNodesByRange: (position: { latitude: number, longitude: number }, nodes: Node[], range: number) => {
const filter = useStore.getState().rangeFilter;
if (filter) {
Expand Down