-% Module tournant en background s'occupant periodiquement de certaines tâches.\r
-% Auteur : G.Burri\r
-% Date : 05.11.2007\r
+% coding: utf-8\r
+% Copyright 2008 Grégory Burri\r
+%\r
+% This file is part of Euphorik.\r
+%\r
+% Euphorik is free software: you can redistribute it and/or modify\r
+% it under the terms of the GNU General Public License as published by\r
+% the Free Software Foundation, either version 3 of the License, or\r
+% (at your option) any later version.\r
+%\r
+% Euphorik is distributed in the hope that it will be useful,\r
+% but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+% GNU General Public License for more details.\r
+%\r
+% You should have received a copy of the GNU General Public License\r
+% along with Euphorik. If not, see <http://www.gnu.org/licenses/>.\r
+% \r
+% Module tournant en background s'occupant periodiquement de certaines tâches :
+% - sélection du prochain troll chaque semaine\r
+% Date : 05.11.2007
+% @author G.Burri\r
+\r
\r
-module(euphorik_daemon).
-export([start/1]).
-
--include("../include/euphorik_defines.hrl").\r
+-include("../include/euphorik_defines.hrl").
-% Démarre le démon
-start(A) ->\r
- loop(A).
-
-\r
-loop(A) ->
+% Démarre le démon
+start(_A) ->\r
+ loop().
+ \r
+
+loop() ->
+ % on attend une minute de plus pour prevenir une dérive nétive
+ timer:sleep(1000 * trunc(temps_prochaine_election() + 60)),
euphorik_bd:elire_troll(),\r
- timer:sleep(1000),\r
- loop(A).
-\r
-
-
+ loop().
+
+
+% Renvoie le nombre de seconde qu'il reste jusque au prochain lundi à l'heure donnée (l'heure est sur 24heures)
+% 86400 est le nombre de seconde dans un jour
+temps_prochaine_election() ->
+ {Date, {H,M,S}} = calendar:local_time(),
+ Delta = (?JOUR_ELECTION_TROLL - 1) * 86400 + ?HEURE_ELECTION_TROLL * 60 * 60
+ -((calendar:day_of_the_week(Date) - 1) * 86400 + H * 60 * 60 + M * 60 + S),
+ % attention au cas où deux dates (maintenant et la date d'éction) ne se trouvent pas dans la même semaine.
+ if Delta =< 0 -> Delta + 7 * 86400; true -> Delta end.