<commit_before>// ==UserScript==
// @name            Better TW Friends
// @author          xShteff, Diggo11
// @match           https://*.the-west.net/game.php*
// @match           https://*.the-west.de/game.php*
// @match           https://*.the-west.pl/game.php*
// @match           https://*.the-west.nl/game.php*
// @match           https://*.the-west.se/game.php*
// @match           https://*.the-west.ro/game.php*
// @match           https://*.the-west.com.pt/game.php*
// @match           https://*.the-west.cz/game.php*
// @match           https://*.the-west.es/game.php*
// @match           https://*.the-west.ru/game.php*
// @match           https://*.the-west.com.br/game.php*
// @match           https://*.the-west.org/game.php*
// @match           https://*.the-west.hu/game.php*
// @match           https://*.the-west.gr/game.php*
// @match           https://*.the-west.dk/game.php*
// @match           https://*.the-west.sk/game.php*
// @match           https://*.the-west.fr/game.php*
// @match           https://*.the-west.it/game.php*
// @grant           none
// @run-at          document-end
// ==/UserScript==

var friends = [];
var lastSent = {};

function getActiveSesKeys() {
	return Object.keys(Game.sesData);
}

function getSesReadyCount() {
	var yesterday = Date.now()/1000 - 3600*24;
	var count = 0;
	$.each(friends, function (i, client) {
		var neverSent = !lastSent.hasOwnProperty(client.player_id);
		if (neverSent || yesterday >= lastSent[client.player_id]) {
			count++;
		}
	});
	return count;
}

function getFriendCount() {
	return friends.length;
}

function getFriendsList(resolve, reject) {
	Ajax.remoteCallMode('friendsbar', 'search', {search_type: 'friends'}, function (data) {
		var clients = $.grep(data.players, client => client.player_id !== Character.playerId);
		friends = $.map(clients, west.storage.FriendsBar.prototype.normalizeAvatars_);

		var sesKey = getActiveSesKeys()[0];
		$.each(data.eventActivations, function (i, eventActivation) {
			if (eventActivation.event_name === sesKey) {
				lastSent[eventActivation.friend_id] = eventActivation.activation_time;
			}
		});

		resolve();
	});
}

// new Promise(getFriendsList)
// .then(getSesReadyCount)
// .then(x => console.log(x));
<commit_msg>ES6 promises are dank + Function to send ses currency to a friend<commit_after>// ==UserScript==
// @name            Better TW Friends
// @author          xShteff, Diggo11
// @match           https://*.the-west.net/game.php*
// @match           https://*.the-west.de/game.php*
// @match           https://*.the-west.pl/game.php*
// @match           https://*.the-west.nl/game.php*
// @match           https://*.the-west.se/game.php*
// @match           https://*.the-west.ro/game.php*
// @match           https://*.the-west.com.pt/game.php*
// @match           https://*.the-west.cz/game.php*
// @match           https://*.the-west.es/game.php*
// @match           https://*.the-west.ru/game.php*
// @match           https://*.the-west.com.br/game.php*
// @match           https://*.the-west.org/game.php*
// @match           https://*.the-west.hu/game.php*
// @match           https://*.the-west.gr/game.php*
// @match           https://*.the-west.dk/game.php*
// @match           https://*.the-west.sk/game.php*
// @match           https://*.the-west.fr/game.php*
// @match           https://*.the-west.it/game.php*
// @grant           none
// @run-at          document-end
// ==/UserScript==

var friends = [];
var lastSent = {};

function getActiveSesKeys() {
	return Object.keys(Game.sesData);
}

function getSesReadyCount() {
	var yesterday = Date.now()/1000 - 3600*24;
	var count = 0;
	$.each(friends, function (i, client) {
		var neverSent = !lastSent.hasOwnProperty(client.player_id);
		if (neverSent || yesterday >= lastSent[client.player_id]) {
			count++;
		}
	});
	return count;
}

function getFriendCount() {
	return friends.length;
}

function getFriendsList() {
	return new Promise(function (resolve, reject) {
		Ajax.remoteCallMode('friendsbar', 'search', {search_type: 'friends'}, function (data) {
			if (data.error) {
				return reject(data.msg);
			}

			var clients = $.grep(data.players, client => client.player_id !== Character.playerId);
			friends = $.map(clients, west.storage.FriendsBar.prototype.normalizeAvatars_);

			var sesKey = getActiveSesKeys()[0];
			$.each(data.eventActivations, function (i, eventActivation) {
				if (eventActivation.event_name === sesKey) {
					lastSent[eventActivation.friend_id] = eventActivation.activation_time;
				}
			});

			return resolve(data.msg);
		});
	});
}

function sendSesCurrency(friendId) {
	return new Promise(function (resolve, reject) {
		Ajax.remoteCall('friendsbar', 'event', {player_id: friendId, event: getActiveSesKeys()[0]}, function (data) {
			if (data.error) {
				return reject(data.msg);
			}
			lastSent[friendId] = data.activationTime;
			return resolve(data.msg);
		});
	});
}

// getFriendsList()
// .then(getSesReadyCount)
// .then(x => console.log(x));
//
// sendSesCurrency(1337)
// .then(msg => MessageSuccess(msg).show())
// .catch(msg => MessageError(msg).show());


{'fraction_non_alphanumeric': 0.19331508991399532, 'fraction_numerical': 0.006645817044566067, 'mean_word_length': 2.5, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 36, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '72321', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1724, 'n_words': 289}