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 team-related functions to IITC.utils.getTeamId #782

Merged
merged 4 commits into from
Nov 19, 2024
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
15 changes: 14 additions & 1 deletion core/code/_deprecated.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global L -- eslint */
/* global IITC, L -- eslint */

/**
* @file This file contains functions that are not use by IITC itself
Expand Down Expand Up @@ -174,3 +174,16 @@ window.findPortalLatLng = function (guid) {
window.androidCopy = function () {
return true; // i.e. execute other actions
};

/**
* Given the entity detail data, returns the team the entity belongs to.
* Uses TEAM_* enum values.
*
* @deprecated
* @function getTeam
* @param {Object} details - The details hash of an entity.
* @returns {number} The team ID the entity belongs to.
*/
window.getTeam = function (details) {
return IITC.utils.getTeamId(details.team);
};
34 changes: 0 additions & 34 deletions core/code/entity_info.js

This file was deleted.

24 changes: 24 additions & 0 deletions core/code/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,28 @@ const isPointInPolygon = (polygon, point) => {
return !!inside;
};

/**
* Converts a team string or object to a team ID.
* Accepts either team string directly (e.g. "RESISTANCE", "R") or an object with team property.
* Returns TEAM_NONE if no match found.
*
* @memberof IITC.utils
* @function getTeamId
* @param {(Object|string)} input - Input to convert to team ID
* @param {string} [input.team] - Team string when input is an object
* @returns {number} The team ID corresponding to the team string.
*/
const getTeamId = (input) => {
const teamStr = typeof input === 'string' ? input : input?.team;
if (window.TEAM_CODENAMES.includes(teamStr)) {
return window.TEAM_CODENAMES.indexOf(teamStr);
}
if (window.TEAM_CODES.includes(teamStr)) {
return window.TEAM_CODES.indexOf(teamStr);
}
return window.TEAM_NONE;
};

IITC.utils = {
getURLParam,
getCookie,
Expand All @@ -497,6 +519,7 @@ IITC.utils = {
clampLatLng,
clampLatLngBounds,
isPointInPolygon,
getTeamId,
};

// Map of legacy function names to their new names (or the same name if not renamed)
Expand Down Expand Up @@ -524,6 +547,7 @@ const legacyFunctionMappings = {
clampLatLng: 'clampLatLng',
clampLatLngBounds: 'clampLatLngBounds',
pnpoly: 'isPointInPolygon',
teamStringToId: 'getTeamId',
};

// Set up synchronization between `window` and `IITC.utils` with new names
Expand Down
83 changes: 82 additions & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, beforeEach } from 'mocha';
import { describe, it, before, beforeEach } from 'mocha';
import { expect } from 'chai';

/* global IITC */
Expand Down Expand Up @@ -667,3 +667,84 @@ describe('IITC.utils.clamp', () => {
expect(result).to.equal(-5);
});
});

describe('IITC.utils.getTeamId', () => {
before(() => {
window.TEAM_CODENAMES = ['NEUTRAL', 'RESISTANCE', 'ENLIGHTENED', 'MACHINA'];
window.TEAM_CODES = ['N', 'R', 'E', 'M'];
window.TEAM_NONE = 0;
});

describe('string input', () => {
it('should return correct ID for valid team names from TEAM_CODENAMES', () => {
expect(IITC.utils.getTeamId('NEUTRAL')).to.equal(0);
expect(IITC.utils.getTeamId('RESISTANCE')).to.equal(1);
expect(IITC.utils.getTeamId('ENLIGHTENED')).to.equal(2);
expect(IITC.utils.getTeamId('MACHINA')).to.equal(3);
});

it('should return correct ID for valid team codes from TEAM_CODES', () => {
expect(IITC.utils.getTeamId('N')).to.equal(0);
expect(IITC.utils.getTeamId('R')).to.equal(1);
expect(IITC.utils.getTeamId('E')).to.equal(2);
expect(IITC.utils.getTeamId('M')).to.equal(3);
});

it('should be case sensitive', () => {
expect(IITC.utils.getTeamId('resistance')).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId('Resistance')).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId('r')).to.equal(window.TEAM_NONE);
});

it('should return TEAM_NONE for invalid team names or codes', () => {
expect(IITC.utils.getTeamId('ALIENS')).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId('X')).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId('')).to.equal(window.TEAM_NONE);
});
});

describe('object input', () => {
it('should return correct ID for objects with valid team names', () => {
expect(IITC.utils.getTeamId({ team: 'NEUTRAL' })).to.equal(0);
expect(IITC.utils.getTeamId({ team: 'RESISTANCE' })).to.equal(1);
expect(IITC.utils.getTeamId({ team: 'ENLIGHTENED' })).to.equal(2);
expect(IITC.utils.getTeamId({ team: 'MACHINA' })).to.equal(3);
});

it('should return correct ID for objects with valid team codes', () => {
expect(IITC.utils.getTeamId({ team: 'N' })).to.equal(0);
expect(IITC.utils.getTeamId({ team: 'R' })).to.equal(1);
expect(IITC.utils.getTeamId({ team: 'E' })).to.equal(2);
expect(IITC.utils.getTeamId({ team: 'M' })).to.equal(3);
});

it('should return TEAM_NONE for objects with invalid team property values', () => {
expect(IITC.utils.getTeamId({ team: 'ALIENS' })).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId({ team: 'X' })).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId({ team: '' })).to.equal(window.TEAM_NONE);
});
});

describe('error handling', () => {
it('should return TEAM_NONE for null/undefined input', () => {
expect(IITC.utils.getTeamId(null)).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId(undefined)).to.equal(window.TEAM_NONE);
});

it('should return TEAM_NONE for objects without team property', () => {
expect(IITC.utils.getTeamId({})).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId({ notTeam: 'RESISTANCE' })).to.equal(window.TEAM_NONE);
});

it('should return TEAM_NONE for objects with null/undefined team property', () => {
expect(IITC.utils.getTeamId({ team: null })).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId({ team: undefined })).to.equal(window.TEAM_NONE);
});

it('should return TEAM_NONE for non-string/non-object inputs', () => {
expect(IITC.utils.getTeamId(123)).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId(true)).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId([])).to.equal(window.TEAM_NONE);
});
});
});
Loading