Skip to content

Commit

Permalink
Restrict !randomplonk to map boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
enigma committed Jun 25, 2023
1 parent acfd7f3 commit 2573152
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/GameHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ class GameHandler {
}

if (message === settings.randomPlonkCmd) {
const { lat, lng } = await GameHelper.getRandomCoordsInLand();
const { lat, lng } = await GameHelper.getRandomCoordsInLand(this.#game.seed.bounds);
const randomGuess = `!g ${lat}, ${lng}`;
this.#handleGuess(userstate, randomGuess).catch((error) => {
console.error(error);
Expand Down
14 changes: 11 additions & 3 deletions src/utils/GameHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,19 @@ async function makeLink(accessToken, bot, streamer, map, mode, locations, gameRe

/**
* Returns random coordinates within land, no Antarctica
* @param {import("../types").Bounds} bounds Optional bounds to further restrict random coordinates
* @return {Promise<LatLng>}
*/
async function getRandomCoordsInLand() {
const lat = Math.random() * (85 + 60) - 60;
const lng = Math.random() * 360 - 180;
async function getRandomCoordsInLand(bounds = null) {
var lat_north = 85, lat_south = -60, lng_west = -180, lng_east = 180;
if(bounds != null) {
lat_north = bounds.max.lat;
lat_south = Math.max(bounds.min.lat, lat_south)
lng_east = bounds.max.lng;
lng_west = bounds.min.lng;
}
const lat = Math.random() * (lat_north - lat_south) + lat_south;
const lng = Math.random() * (lng_east - lng_west) + lng_west;
const localResults = countryIso(lat, lng, true);
if (!localResults.length) return await getRandomCoordsInLand();
return { lat, lng };
Expand Down
25 changes: 25 additions & 0 deletions src/utils/GameHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,28 @@ describe('parseCoordinates', () => {
expect(GameHelper.parseCoordinates("30.12345, 190.54321")).toBeFalsy();
});
});

describe('randomPlonk', () => {
// Requires ~30ms/test
const repeats = 100;
it("Checks if randomplonk without bounds avoids antarctica", async () => {
// This takes about 30ms/test, so ...
for(var i = 0; i < repeats; i++) {
const {lat, lng} = await GameHelper.getRandomCoordsInLand();
expect(lat > -60).toBeTruthy();
expect(lat < 90).toBeTruthy();
expect(lng > -180).toBeTruthy();
expect(lng < 180).toBeTruthy();
}
});
it("Checks if randomplonk with bounds remains in bounds", async () => {
const inBounds = { min: { lat: 32, lng: -117 }, max: { lat: 33, lng: -116}};
for(var i = 0; i < repeats; i++) {
const {lat, lng} = await GameHelper.getRandomCoordsInLand(inBounds);
expect(lat > inBounds.min.lat).toBeTruthy();
expect(lat < inBounds.max.lat).toBeTruthy();
expect(lng > inBounds.min.lng).toBeTruthy();
expect(lng < inBounds.max.lng).toBeTruthy();
}
})
})

0 comments on commit 2573152

Please sign in to comment.