<commit_before>Config.origindomain = 'play.pokemonshowdown.com';
// `defaultserver` specifies the server to use when the domain name in the
// address bar is `Config.origindomain`.
Config.defaultserver = {
	id: 'showdown',
	host: 'sim.smogon.com',
	port: 443,
	httpport: 8000,
	altport: 80,
	registered: true
};

function Storage() {}

Storage.initialize = function () {
	if (window.nodewebkit) {
		window.fs = require('fs');

		this.initDirectory();
		this.startLoggingChat = this.nwStartLoggingChat;
		this.stopLoggingChat = this.nwStopLoggingChat;
		this.logChat = this.nwLogChat;
	}
	Storage.initPrefs();
};

/*********************************************************
 * Background
 *********************************************************/

// Backgrounds are handled separately from other prefs because
// they're server-specific and should be loaded faster

Storage.bg = {
	id: '',
	changeCount: 0,
	set: function (bgUrl, bgid, noSave) {
		if (!this.load(bgUrl, bgid)) {
			this.extractMenuColors(bgUrl, bgid, noSave);
		} else if (bgid) {
			localStorage.setItem('showdown_bg', bgUrl + '\n' + bgid);
		} else {
			localStorage.removeItem('showdown_bg');
		}
	},
	/**
	 * Load a background. Returns true if hues were loaded, or false if
	 * they still need to be extracted using Color Thief.
	 */
	load: function (bgUrl, bgid, hues) {
		this.id = bgid;
		if (!bgid) {
			bgid = ['horizon', 'ocean', 'waterfall', 'shaymin', 'charizards'][Math.floor(Math.random() * 5)];
			bgUrl = Tools.resourcePrefix + 'fx/client-bg-' + bgid + '.jpg';
		}
		$(document.body).css({
			background: (bgUrl.charAt(0) === '#' ? bgUrl : '#546bac url(' + bgUrl + ') no-repeat left center fixed'),
			'background-size': 'cover'
		});
		this.changeCount++;

		if (!hues) switch (bgid) {
		case 'horizon':
			hues = ["318.87640449438203,35.177865612648226%", "216,46.2962962962963%", "221.25,32.25806451612904%", "197.8021978021978,52.60115606936417%", "232.00000000000003,19.480519480519483%", "228.38709677419354,60.7843137254902%"];
			break;
		case 'ocean':
			hues = ["82.8169014084507,34.63414634146342%", "216.16438356164383,29.55465587044534%", "212.92682926829266,59.42028985507245%", "209.18918918918916,57.51295336787566%", "199.2857142857143,48.275862068965495%", "213.11999999999998,55.06607929515419%"];
			break;
		case 'waterfall':
			hues = ["70.34482758620689,20.567375886524818%", "184.36363636363635,23.012552301255226%", "108.92307692307692,37.14285714285714%", "119.31034482758622,37.66233766233767%", "98.39999999999998,36.76470588235296%", "140,38.18181818181818%"];
			break;
		case 'shaymin':
			hues = ["20,5.660377358490567%", "170.00000000000003,2.380952380952378%", "157.5,11.88118811881188%", "174.78260869565216,12.041884816753928%", "185.00000000000003,12.76595744680851%", "39.000000000000064,21.7391304347826%"];
			break;
		case 'charizards':
			hues = ["192.3076923076923,80.41237113402063%", "10.874999999999998,70.79646017699115%", "179.51612903225808,52.10084033613446%", "20.833333333333336,36.73469387755102%", "37.159090909090914,74.57627118644066%", "210,29.629629629629633%"];
			break;
		}
		if (!hues && bgUrl.charAt(0) === '#') {
			var r = parseInt(bgUrl.slice(1, 3), 16) / 255;
			var g = parseInt(bgUrl.slice(3, 5), 16) / 255;
			var b = parseInt(bgUrl.slice(5, 7), 16) / 255;
			var hs = this.getHueSat(r, g, b);
			hues = [hs, hs, hs, hs, hs, hs];
		}
		if (hues) {
			this.loadHues(hues);
		}
		return !!hues;
	},
	loadHues: function (hues) {
		$('#mainmenubuttoncolors').remove();
		var cssBuf = '';
		for (var i = 0; i < 6; i++) {
			var n = i + 1;
			var hs = hues[i];
			cssBuf += '.mainmenuwrapper .button.mainmenu' + n + ' { background: linear-gradient(to bottom,  hsl(' + hs + ',72%),  hsl(' + hs + ',52%)); border-color: hsl(' + hs + ',40%); }\n';
			cssBuf += '.mainmenuwrapper .button.mainmenu' + n + ':hover { background: linear-gradient(to bottom,  hsl(' + hs + ',62%),  hsl(' + hs + ',42%)); border-color: hsl(' + hs + ',21%); }\n';
			cssBuf += '.mainmenuwrapper .button.mainmenu' + n + ':active { background: linear-gradient(to bottom,  hsl(' + hs + ',42%),  hsl(' + hs + ',58%)); border-color: hsl(' + hs + ',21%); }\n';
		}
		$('head').append('<style id="mainmenubuttoncolors">' + cssBuf + '</style>');
	},
	extractMenuColors: function (bgUrl, bgid, noSave) {
		var changeCount = this.changeCount;
		// We need the image object to load it on a canvas to detect the main color.
		var img = new Image();
		img.onload = function () {
			// in case ColorThief throws from canvas,
			// or localStorage throws
			try {
				var colorThief = new ColorThief();
				var colors = colorThief.getPalette(img, 6);

				var hues = [];
				for (var i = 0; i < 6; i++) {
					var color = colors[i];
					var hs = Storage.bg.getHueSat(color[0] / 255, color[1] / 255, color[2] / 255);
					hues.unshift(hs);
				}
				Storage.bg.loadHues(hues);
				if (!noSave && Storage.bg.changeCount === changeCount) {
					localStorage.setItem('showdown_bg', bgUrl + '\n' + Storage.bg.id + '\n' + hues.join('\n'));
				}
			} catch (e) {}
		};
		img.src = bgUrl;
	},
	getHueSat: function (r, g, b) {
		var max = Math.max(r, g, b);
		var min = Math.min(r, g, b);
		var h;
		var s;
		var l = (max + min) / 2;
		if (max === min) {
			h = s = 0;
			return '0, 0%';
		} else {
			var d = max - min;
			s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
			switch (max) {
			case r: h = (g - b) / d + (g < b ? 6 : 0); break;
			case g: h = (b - r) / d + 2; break;
			case b: h = (r - g) / d + 4; break;
			}
			h /= 6;
		}
		return '' + (h * 360) + ',' + (s * 100) + '%';
	}
};

try {
	var bg = localStorage.getItem('showdown_bg').split('\n');
	if (bg.length >= 2) {
		Storage.bg.load(bg[0], bg[1]);
		if (bg.length === 8) Storage.bg.loadHues(bg.slice(2));
	}
} catch (e) {}

if (!Storage.bg.id) {
	if (location.host === 'smogtours.psim.us') {
		Storage.bg.load('//play.pokemonshowdown.com/fx/client-bg-shaymin.jpg', 'shaymin');
	} else if (location.host === 'play.pokemonshowdown.com') {
		Storage.bg.load();
	}
}

/*********************************************************
 * Prefs
 *********************************************************/

// Prefs are canonically stored in showdown_prefs in localStorage
// in the origin https://play.pokemonshowdown.com

// We try loading things from the origin, anyway, in case third-party
// localStorage is banned, and since prefs are cached in other
// places in certain cases.

Storage.origin = 'https://play.pokemonshowdown.com';
<commit_msg>Fix 'Default' background button for alt servers<commit_after>Config.origindomain = 'play.pokemonshowdown.com';
// `defaultserver` specifies the server to use when the domain name in the
// address bar is `Config.origindomain`.
Config.defaultserver = {
	id: 'showdown',
	host: 'sim.smogon.com',
	port: 443,
	httpport: 8000,
	altport: 80,
	registered: true
};

function Storage() {}

Storage.initialize = function () {
	if (window.nodewebkit) {
		window.fs = require('fs');

		this.initDirectory();
		this.startLoggingChat = this.nwStartLoggingChat;
		this.stopLoggingChat = this.nwStopLoggingChat;
		this.logChat = this.nwLogChat;
	}
	Storage.initPrefs();
};

/*********************************************************
 * Background
 *********************************************************/

// Backgrounds are handled separately from other prefs because
// they're server-specific and should be loaded faster

Storage.bg = {
	id: '',
	changeCount: 0,
	set: function (bgUrl, bgid, noSave) {
		if (!this.load(bgUrl, bgid)) {
			this.extractMenuColors(bgUrl, bgid, noSave);
		} else if (bgid) {
			localStorage.setItem('showdown_bg', bgUrl + '\n' + bgid);
		} else {
			localStorage.removeItem('showdown_bg');
		}
	},
	/**
	 * Load a background. Returns true if hues were loaded, or false if
	 * they still need to be extracted using Color Thief.
	 */
	load: function (bgUrl, bgid, hues) {
		this.id = bgid;
		if (!bgid) {
			if (location.host === 'smogtours.psim.us') {
				bgid = 'shaymin';
			} else if (location.host === 'play.pokemonshowdown.com') {
				bgid = ['horizon', 'ocean', 'waterfall', 'shaymin', 'charizards'][Math.floor(Math.random() * 5)];
			} else {
				$(document.body).css({
					background: '',
					'background-size': ''
				});
				$('#mainmenubuttoncolors').remove();
				return true;
			}
			bgUrl = Tools.resourcePrefix + 'fx/client-bg-' + bgid + '.jpg';
		}
		$(document.body).css({
			background: (bgUrl.charAt(0) === '#' ? bgUrl : '#546bac url(' + bgUrl + ') no-repeat left center fixed'),
			'background-size': 'cover'
		});
		this.changeCount++;

		if (!hues) switch (bgid) {
		case 'horizon':
			hues = ["318.87640449438203,35.177865612648226%", "216,46.2962962962963%", "221.25,32.25806451612904%", "197.8021978021978,52.60115606936417%", "232.00000000000003,19.480519480519483%", "228.38709677419354,60.7843137254902%"];
			break;
		case 'ocean':
			hues = ["82.8169014084507,34.63414634146342%", "216.16438356164383,29.55465587044534%", "212.92682926829266,59.42028985507245%", "209.18918918918916,57.51295336787566%", "199.2857142857143,48.275862068965495%", "213.11999999999998,55.06607929515419%"];
			break;
		case 'waterfall':
			hues = ["70.34482758620689,20.567375886524818%", "184.36363636363635,23.012552301255226%", "108.92307692307692,37.14285714285714%", "119.31034482758622,37.66233766233767%", "98.39999999999998,36.76470588235296%", "140,38.18181818181818%"];
			break;
		case 'shaymin':
			hues = ["20,5.660377358490567%", "170.00000000000003,2.380952380952378%", "157.5,11.88118811881188%", "174.78260869565216,12.041884816753928%", "185.00000000000003,12.76595744680851%", "39.000000000000064,21.7391304347826%"];
			break;
		case 'charizards':
			hues = ["192.3076923076923,80.41237113402063%", "10.874999999999998,70.79646017699115%", "179.51612903225808,52.10084033613446%", "20.833333333333336,36.73469387755102%", "37.159090909090914,74.57627118644066%", "210,29.629629629629633%"];
			break;
		}
		if (!hues && bgUrl.charAt(0) === '#') {
			var r = parseInt(bgUrl.slice(1, 3), 16) / 255;
			var g = parseInt(bgUrl.slice(3, 5), 16) / 255;
			var b = parseInt(bgUrl.slice(5, 7), 16) / 255;
			var hs = this.getHueSat(r, g, b);
			hues = [hs, hs, hs, hs, hs, hs];
		}
		if (hues) {
			this.loadHues(hues);
		}
		return !!hues;
	},
	loadHues: function (hues) {
		$('#mainmenubuttoncolors').remove();
		var cssBuf = '';
		for (var i = 0; i < 6; i++) {
			var n = i + 1;
			var hs = hues[i];
			cssBuf += '.mainmenuwrapper .button.mainmenu' + n + ' { background: linear-gradient(to bottom,  hsl(' + hs + ',72%),  hsl(' + hs + ',52%)); border-color: hsl(' + hs + ',40%); }\n';
			cssBuf += '.mainmenuwrapper .button.mainmenu' + n + ':hover { background: linear-gradient(to bottom,  hsl(' + hs + ',62%),  hsl(' + hs + ',42%)); border-color: hsl(' + hs + ',21%); }\n';
			cssBuf += '.mainmenuwrapper .button.mainmenu' + n + ':active { background: linear-gradient(to bottom,  hsl(' + hs + ',42%),  hsl(' + hs + ',58%)); border-color: hsl(' + hs + ',21%); }\n';
		}
		$('head').append('<style id="mainmenubuttoncolors">' + cssBuf + '</style>');
	},
	extractMenuColors: function (bgUrl, bgid, noSave) {
		var changeCount = this.changeCount;
		// We need the image object to load it on a canvas to detect the main color.
		var img = new Image();
		img.onload = function () {
			// in case ColorThief throws from canvas,
			// or localStorage throws
			try {
				var colorThief = new ColorThief();
				var colors = colorThief.getPalette(img, 6);

				var hues = [];
				if (!colors) {
					hues = ['0, 0%', '0, 0%', '0, 0%', '0, 0%', '0, 0%', '0, 0%'];
				} else {
					for (var i = 0; i < 6; i++) {
						var color = colors[i];
						var hs = Storage.bg.getHueSat(color[0] / 255, color[1] / 255, color[2] / 255);
						hues.unshift(hs);
					}
				}
				Storage.bg.loadHues(hues);
				if (!noSave && Storage.bg.changeCount === changeCount) {
					localStorage.setItem('showdown_bg', bgUrl + '\n' + Storage.bg.id + '\n' + hues.join('\n'));
				}
			} catch (e) {}
		};
		img.src = bgUrl;
	},
	getHueSat: function (r, g, b) {
		var max = Math.max(r, g, b);
		var min = Math.min(r, g, b);
		var h;
		var s;
		var l = (max + min) / 2;
		if (max === min) {
			h = s = 0;
			return '0, 0%';
		} else {
			var d = max - min;
			s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
			switch (max) {
			case r: h = (g - b) / d + (g < b ? 6 : 0); break;
			case g: h = (b - r) / d + 2; break;
			case b: h = (r - g) / d + 4; break;
			}
			h /= 6;
		}
		return '' + (h * 360) + ',' + (s * 100) + '%';
	}
};

try {
	var bg = localStorage.getItem('showdown_bg').split('\n');
	if (bg.length >= 2) {
		Storage.bg.load(bg[0], bg[1]);
		if (bg.length === 8) Storage.bg.loadHues(bg.slice(2));
	}
} catch (e) {}

if (!Storage.bg.id) {
	Storage.bg.load();
}

/*********************************************************
 * Prefs
 *********************************************************/

// Prefs are canonically stored in showdown_prefs in localStorage
// in the origin https://play.pokemonshowdown.com

// We try loading things from the origin, anyway, in case third-party
// localStorage is banned, and since prefs are cached in other
// places in certain cases.

Storage.origin = 'https://play.pokemonshow<EMAIL>';

{'fraction_non_alphanumeric': 0.21763729631864817, 'fraction_numerical': 0.15193120096560048, 'mean_word_length': 3.701063829787234, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 16, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3163457', 'n_tokens_mistral': 6825, 'n_tokens_neox': 5112, 'n_words': 1129}