lint codebase

This commit is contained in:
AlecM33
2022-01-11 20:36:10 -05:00
parent 553f1025a8
commit f5c0984211
38 changed files with 2422 additions and 968 deletions

View File

@@ -1,17 +1,17 @@
import { globals } from "../config/globals.js";
import {toast} from "./Toast.js";
import {ModalManager} from "./ModalManager";
import { globals } from '../config/globals.js';
import { toast } from './Toast.js';
import { ModalManager } from './ModalManager';
export class DeckStateManager {
constructor() {
constructor () {
this.deck = null;
this.customRoleOptions = [];
this.createMode = false;
this.currentlyEditingRoleName = null;
}
addToDeck(role) {
let option = this.customRoleOptions.find((option) => option.role === role);
addToDeck (role) {
const option = this.customRoleOptions.find((option) => option.role === role);
if (option) {
option.quantity = 0;
this.deck.push(option);
@@ -19,102 +19,102 @@ export class DeckStateManager {
}
}
addToCustomRoleOptions(role) {
addToCustomRoleOptions (role) {
this.customRoleOptions.push(role);
localStorage.setItem("play-werewolf-custom-roles", JSON.stringify(this.customRoleOptions.concat(this.deck.filter(card => card.custom === true))));
localStorage.setItem('play-werewolf-custom-roles', JSON.stringify(this.customRoleOptions.concat(this.deck.filter(card => card.custom === true))));
}
updateCustomRoleOption(option, name, description, team) {
updateCustomRoleOption (option, name, description, team) {
option.role = name;
option.description = description;
option.team = team;
localStorage.setItem("play-werewolf-custom-roles", JSON.stringify(this.customRoleOptions.concat(this.deck.filter(card => card.custom === true))));
localStorage.setItem('play-werewolf-custom-roles', JSON.stringify(this.customRoleOptions.concat(this.deck.filter(card => card.custom === true))));
}
removeFromCustomRoleOptions(name) {
let option = this.customRoleOptions.find((option) => option.role === name);
removeFromCustomRoleOptions (name) {
const option = this.customRoleOptions.find((option) => option.role === name);
if (option) {
this.customRoleOptions.splice(this.customRoleOptions.indexOf(option), 1);
localStorage.setItem("play-werewolf-custom-roles", JSON.stringify(this.customRoleOptions.concat(this.deck.filter(card => card.custom === true))));
localStorage.setItem('play-werewolf-custom-roles', JSON.stringify(this.customRoleOptions.concat(this.deck.filter(card => card.custom === true))));
toast('"' + name + '" deleted.', 'error', true, true, 3);
}
}
addCopyOfCard(role) {
let existingCard = this.deck.find((card) => card.role === role)
addCopyOfCard (role) {
const existingCard = this.deck.find((card) => card.role === role);
if (existingCard) {
existingCard.quantity += 1;
}
}
removeCopyOfCard(role) {
let existingCard = this.deck.find((card) => card.role === role)
removeCopyOfCard (role) {
const existingCard = this.deck.find((card) => card.role === role);
if (existingCard && existingCard.quantity > 0) {
existingCard.quantity -= 1;
}
}
getCurrentDeck() { return this.deck; }
getCurrentDeck () { return this.deck; }
getCard(role) {
getCard (role) {
return this.deck.find(
(card) => card.role.toLowerCase().trim() === role.toLowerCase().trim()
);
}
getCurrentCustomRoleOptions() { return this.customRoleOptions; }
getCurrentCustomRoleOptions () { return this.customRoleOptions; }
getCustomRoleOption(role) {
getCustomRoleOption (role) {
return this.customRoleOptions.find(
(option) => option.role.toLowerCase().trim() === role.toLowerCase().trim()
)
);
};
getDeckSize() {
getDeckSize () {
let total = 0;
for (let role of this.deck) {
for (const role of this.deck) {
total += role.quantity;
}
return total;
}
loadCustomRolesFromCookies() {
let customRoles = localStorage.getItem('play-werewolf-custom-roles');
loadCustomRolesFromCookies () {
const customRoles = localStorage.getItem('play-werewolf-custom-roles');
if (customRoles !== null && validateCustomRoleCookie(customRoles)) {
this.customRoleOptions = JSON.parse(customRoles); // we know it is valid JSON from the validate function
}
}
loadCustomRolesFromFile(file, updateRoleListFunction, loadDefaultCardsFn, showIncludedCardsFn) {
let reader = new FileReader();
loadCustomRolesFromFile (file, updateRoleListFunction, loadDefaultCardsFn, showIncludedCardsFn) {
const reader = new FileReader();
reader.onerror = (e) => {
toast(reader.error.message, "error", true, true, 5);
}
toast(reader.error.message, 'error', true, true, 5);
};
reader.onload = (e) => {
let string;
if (typeof e.target.result !== "string") {
string = new TextDecoder("utf-8").decode(e.target.result);
if (typeof e.target.result !== 'string') {
string = new TextDecoder('utf-8').decode(e.target.result);
} else {
string = e.target.result;
}
if (validateCustomRoleCookie(string)) {
this.customRoleOptions = JSON.parse(string); // we know it is valid JSON from the validate function
ModalManager.dispelModal("upload-custom-roles-modal", "modal-background");
toast("Roles imported successfully", "success", true, true, 3);
localStorage.setItem("play-werewolf-custom-roles", JSON.stringify(this.customRoleOptions));
updateRoleListFunction(this, document.getElementById("deck-select"));
ModalManager.dispelModal('upload-custom-roles-modal', 'modal-background');
toast('Roles imported successfully', 'success', true, true, 3);
localStorage.setItem('play-werewolf-custom-roles', JSON.stringify(this.customRoleOptions));
updateRoleListFunction(this, document.getElementById('deck-select'));
// loadDefaultCardsFn(this);
// showIncludedCardsFn(this);
} else {
toast("Invalid formatting. Make sure you import the file as downloaded from this page.", "error", true, true, 5);
toast('Invalid formatting. Make sure you import the file as downloaded from this page.', 'error', true, true, 5);
}
}
};
reader.readAsText(file);
}
// via https://stackoverflow.com/a/18197341
downloadCustomRoles(filename, text) {
let element = document.createElement('a');
downloadCustomRoles (filename, text) {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
@@ -125,21 +125,20 @@ export class DeckStateManager {
document.body.removeChild(element);
}
}
// this is user-supplied, so we should validate it fully
function validateCustomRoleCookie(cookie) {
let valid = false;
if (typeof cookie === "string" && new Blob([cookie]).size <= 1000000) {
function validateCustomRoleCookie (cookie) {
const valid = false;
if (typeof cookie === 'string' && new Blob([cookie]).size <= 1000000) {
try {
let cookieJSON = JSON.parse(cookie);
const cookieJSON = JSON.parse(cookie);
if (Array.isArray(cookieJSON)) {
for (let entry of cookieJSON) {
if (typeof entry === "object") {
if (typeof entry.role !== "string" || entry.role.length > globals.MAX_CUSTOM_ROLE_NAME_LENGTH
|| typeof entry.team !== "string" || (entry.team !== globals.ALIGNMENT.GOOD && entry.team !== globals.ALIGNMENT.EVIL)
|| typeof entry.description !== "string" || entry.description.length > globals.MAX_CUSTOM_ROLE_DESCRIPTION_LENGTH
for (const entry of cookieJSON) {
if (typeof entry === 'object') {
if (typeof entry.role !== 'string' || entry.role.length > globals.MAX_CUSTOM_ROLE_NAME_LENGTH
|| typeof entry.team !== 'string' || (entry.team !== globals.ALIGNMENT.GOOD && entry.team !== globals.ALIGNMENT.EVIL)
|| typeof entry.description !== 'string' || entry.description.length > globals.MAX_CUSTOM_ROLE_DESCRIPTION_LENGTH
) {
return false;
}
@@ -149,7 +148,7 @@ function validateCustomRoleCookie(cookie) {
}
return true;
}
} catch(e) {
} catch (e) {
return false;
}
}