{% extends "tools/_layout.html" %}
{% import "cartes-thematiques/_layout_card_column.html" as layout with context %}

{% block title %}
Ik-O-Maths
{% endblock %}

{% block content %}
<h1>{{ _("Les Ik-O-Maths") }}</h1>
<p>
	{% trans trimmed %}
	Les Ik-O-Maths simulent les résultats attendus des capacités successives de
	{{ winota_joiner_of_forces }}, {{ kinnan_bonder_prodigy }} et {{ gyruda_doom_of_depths }}.
	En effet, ces capacités suivent les mêmes règles mathématiques pour calculer leurs probabilités
	de succès (<em>ou d'échec</em>).
	{% endtrans %}
</p>

<form class="inline-form" id="win-o-math" autocomplete="off"
	onsubmit="return calculate();">
	<fieldset id="win-o-val">
		<legend>{{ _("Probabilité de réussites successives") }}</legend>
		<span class="double-column">
			<label for="copies">{{ _("nombre de réussites maximum :") }}</label>
			<input type="text" id="copies" placeholder="23" />
		</span>
		<span class="double-column">
			<label for="triggers">{{ _("nombre de capacités successives :") }}</label>
			<input type="text" id="triggers" placeholder="5" />
		</span>
		<span class="double-column">
			<label for="size">{{ _("nombre de cartes vues par capacité :") }}</label>
			<input type="text" id="size" placeholder="6" />
		</span>
		<button type="submit" id="btn-calcul">{{ _("Calcul")}}</button>
		<div id="result"></div>
	</fieldset>
</form>

<div>
	{% call layout.card_column("Winota, Joiner of Forces",_("6 cartes vues à la fois")) %}
	{% endcall %}
	{% call layout.card_column("Kinnan, Bonder Prodigy",  _("5 cartes vues à la fois")) %}
	{% endcall %}
	{% call layout.card_column("Gyruda, Doom of Depths",  _("4 cartes vues à la fois")) %}
	{% endcall %}
</div>
{% endblock %}

{% block script %}
{{ super() }}
<!-- Win-O-Math -->
<script>
	var calculate = function() {
		var hnps = function (n,p,s) { return 1 - hnpss(n,p,s,0) };
		var hnpss = function (n,p,s,e) {
			if (n > p) { n = p }
			let echooses = binom(s,e);
			let nechooseps = binom(p-s,n-e);
			let nchoosep = binom(p,n);
			return echooses * nechooseps / nchoosep;
		};
		var binom = function (n, k) {
			if ((k < 0) || (n < 0) || (k > n)) { return 0 }
			let resultat = 1;
			for (let i = 1; i < (Math.min(k, n-k) +1); i++) {
				resultat = resultat * (n + 1 - i) / i;
			}
			return resultat;
		};
		var pyx = function (y,x) {
			if (y == 0 && x == 0) { return 1 }
			if ((y > x) || (y < 0)) { return 0 }
			return pyx(y-1,x-1) * pryx(y-1,x-1) + pyx(y,x-1) * (1 - pryx(y,x-1));
		};
		var pryx = function (y,x) {
			if (pyx(y,x) == 0) { return 0 }
			let somme = 0;
			for (let s = 0; s <= x * N; s++) { somme += hnps(N,P - x * N,S - s) * pyxs(y,x,s) }
			return 1/pyx(y,x) * somme;
		};
		var pyxs = function (y,x,s) {
			if ((y > s) || (y > x)) { return 0 }
			if ((y == 0) && (s > 0)) { return 0 }
			if ((x == 0) && (y == 0) && (s == 0)) { return 1 }
			if ((x < 0) || (y < 0) || (s < 0)) { return 0 }
			let somme = 0;
			for (let i = 1; i <= N; i++) {
				somme += pyxs(y-1,x-1,s-i) * hnpss(N,P-(x-1)*N,S-s+i,i);
			}
			return somme + pyxs(y,x-1,s) * hnpss(N,P-(x-1)*N,S-s,0);
		};

		// Size of set
		let size = document.getElementById("size");
		var N = parseInt(size.value || size.placeholder);
		// Success in population
		let copies = document.getElementById("copies");
		var S = parseInt(copies.value || copies.placeholder);
		// Size of deck
		var P = 99;

		// Triggers
		let triggers = document.getElementById("triggers");
		let max_triggers = triggers.value || triggers.placeholder;

		// parentNode
		var result = document.getElementById("result");
		while (result.firstChild) { result.removeChild(result.firstChild) }

		// Display
		for (let i = 0; i <= max_triggers; i++) {
			let str = "";
			str = str + "<strong>" + i + "</strong>" + " réussite";
			if (i > 1) { str = str + "s" }
			str = str + " : " + Math.round(pyx(i,max_triggers)*10000)/100+" %";
			let r = document.createElement("span");
			r.setAttribute('class', "double-column");
			r.innerHTML = str;
			result.appendChild(r);
		}

		// Prevent default submitting
		return false;
	}
</script>
{% endblock %}
