Added the keys for the mailer and the frequency
[breadcrumbs.git] / src / lib / Bcd / Bots / NewAntNestsBot.pm
blobd41da9d03427d4fa81a8376c6d15e1dc83e6a69a
1 package Bcd::Bots::NewAntNestsBot;
3 # This file is part of the breadcrumbs daemon (bcd).
4 # Copyright (C) 2007 Pasqualino Ferrentino
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
23 #this bot will simply watch the creation of the ant nests...
25 use strict;
26 use warnings;
27 use Data::Dumper;
30 use Bcd::Data::Founders;
31 use Bcd::Data::FoundersVotings;
32 use Bcd::Data::Users;
33 use Bcd::Constants::NewAntNestsConstants;
34 use Bcd::Bots::BcdBot;
35 use base(qw/Bcd::Bots::BcdBot/);
37 use Log::Log4perl qw(:easy);
38 Log::Log4perl->easy_init($DEBUG);
40 #all times in seconds
41 use constant {
42 MAXIMUM_TIME_TO_CHECK_IN => 86400,
43 MY_FREQUENCY => 7200,
47 #of course you could use the %s syntax to perform a substitution...
48 use constant {
50 SELECT_FIRST_NEW_ANT_NEST =>
51 qq{SELECT id from ant_nests_bookings ORDER BY id LIMIT 1},
53 SELECT_NEXT_NEW_ANT_NEST =>
54 qq{SELECT id from ant_nests_bookings WHERE id > ? ORDER BY id LIMIT 1},
56 DELETE_INITIAL_VOTINGS =>
57 qq{DELETE FROM ant_nest_initial_votings WHERE id_ant_nest = ?},
59 DELETE_LOOKERS =>
60 qq{DELETE FROM ant_nests_looking_users WHERE id_ant_nest = ? },
62 DELETE_FOUNDERS_TRUSTS =>
63 qq{DELETE FROM ant_nests_founders_trusts WHERE u1 = ? or u2 = ?},
65 SELECT_OLD_FOUNDERS_STATE =>
66 qq{SELECT * from ant_nests_founders where id_status = ? and id_ant_nest = ? } .
67 qq{and localtimestamp - last_status_change > interval '} . MAXIMUM_TIME_TO_CHECK_IN . " seconds'",
74 sub new {
75 my ($class, $server) = @_;
76 my $self = $class->SUPER::new(@_);
78 $self->_add_rule(\&delete_founders_ants_not_checked);
79 $self->_add_rule(\&all_ants_have_checked_in);
80 $self->_add_rule(\&all_ants_have_immitted_data);
81 $self->_add_rule(\&all_ants_have_built_the_web_of_trust);
82 $self->_add_rule(\&after_the_candidate_choice_rule);
83 $self->_add_rule(\&votes_count_rule);
84 $self->_add_rule(\&new_ant_nest_final_rule);
87 $self->{my_frequency} = defined($server->{new_ant_nest_bot_frequency}) ?
88 $server->{new_ant_nest_bot_frequency} : MY_FREQUENCY;
91 bless($self, $class);
92 return $self;
95 sub get_frequency {
96 my $self = shift;
97 return $self->{my_frequency};
100 #I should start a new cycle.
101 sub awake{
102 my ($self, $stash) = @_;
104 my $st = $stash->prepare_cached(SELECT_FIRST_NEW_ANT_NEST);
105 $st->execute();
107 my $row = $st->fetch();
108 $st->finish();
110 if ( ! defined($row)){
111 #nothing to do
112 return 0;
113 } else {
114 $self->{current_ant_nest} = $row->[0];
115 return 1;
119 sub _advance_ant_nest{
120 my ($self, $stash) = @_;
122 my $st = $stash->prepare_cached(SELECT_NEXT_NEW_ANT_NEST);
123 $st->bind_param(1, $self->{current_ant_nest});
125 $st->execute();
127 my $row = $st->fetch();
128 $st->finish();
130 if (! defined($row)){
131 return 0;
132 } else {
133 $self->{current_ant_nest} = $row->[0];
134 return 1;
138 #this is the final rule, it is triggered when the ant nest has
139 #finished the creation phase
140 sub new_ant_nest_final_rule{
141 my ($self, $stash, $ant_nest_to_check) = @_;
143 my $ant_nest = Bcd::Data::NewAntNests->select_new_ant_nest_booking_arr($stash, $ant_nest_to_check);
144 return undef if (!defined($ant_nest));
145 return undef if ($ant_nest->[1] != Bcd::Constants::NewAntNestsConstants::WAITING_FOR_INITIAL_SET_UP);
147 #ok, the ant nest is in the right state... now I simply see if the boss and the treasurer
148 #have finished their setup
149 my $count = Bcd::Data::NewAntNests->get_founders_with_state_count
150 ($stash, $ant_nest_to_check, Bcd::Constants::FoundersConstants::SET_UP_DONE);
152 #I have two ants in this state, they are the boss and the treasurer
153 if ($count != 2){
154 return undef;
157 my $assigned_code = $ant_nest->[4];
159 my $template_vars = {
160 id_ant_nest => $ant_nest_to_check,
161 assigned_code => $assigned_code,
164 #ok, I can send the mail
166 $self->_mail_to_all_the_founders_and_change_their_state
167 ($stash, $ant_nest_to_check, "ant_nest_created.tt2",
168 Bcd::Constants::FoundersConstants::FINISHED_CREATED_AS_AN_ANT, $template_vars);
170 #I can update the ant nest state...
171 Bcd::Data::NewAntNests->update_ant_nest_state
172 ($stash, $ant_nest_to_check,
173 Bcd::Constants::NewAntNestsConstants::CREATED);
175 #ok, now I can update also the state for all the ants in the assigned code
176 Bcd::Data::Users->update_state_of_all_users_in_ant_nest
177 ($stash, $ant_nest->[4], Bcd::Constants::Users::NORMAL_ACTIVE_ANT);
179 #ok, I can mail all the observers... to do... this is not a priority
180 $self->_mail_all_the_observers($stash, $ant_nest_to_check, "observed_ant_nest_is_ready.tt2", $template_vars);
183 #I must change the state of the ant nest...
184 Bcd::Data::AntNests->_this_ant_nest_is_ready($stash, $assigned_code);
186 #the last act is the removal of all the data of this ant nest
187 $self->_delete_the_new_ant_nest_booking($stash, $ant_nest_to_check);
189 return "Final creation: the ant nest with code=$assigned_code is ready";
192 sub _delete_the_new_ant_nest_booking{
193 my ($self, $stash, $ant_nest_to_check) = @_;
196 #ok, I have to delete all the things which were of this ant nest...
198 #the votings
199 $stash->prep_exec(DELETE_INITIAL_VOTINGS, $ant_nest_to_check);
200 #the lookers
201 $stash->prep_exec(DELETE_LOOKERS, $ant_nest_to_check);
203 #the trusts...
204 my $founders = Bcd::Data::Founders->get_all_founders_ant_nest_arr
205 ($stash, $ant_nest_to_check);
207 foreach(@{$founders}){
208 $stash->prep_exec(DELETE_FOUNDERS_TRUSTS, $_->[0], $_->[0]);
209 #then I can delete also the user...
210 #the user in this case it is not lazy, the function should change name
211 $self->_delete_this_lazy_to_check_in_founder_ant($stash, $_->[0]);
214 #ok, now I can delete also the new ant nest
215 $self->_delete_this_new_ant_nest_aborted($stash, $ant_nest_to_check);
219 sub _mail_all_the_observers{
220 my ($self, $stash, $code, $letter_to_send, $template_vars) = @_;
222 my $lookers = Bcd::Data::NewAntNests->get_all_the_observers($stash, $code);
224 foreach(@{$lookers}){
225 $template_vars->{name} = $_->[1];
226 $stash->get_mailer()->mail_this_message
227 ($stash, $_->[2], $letter_to_send, $template_vars);
233 #this rule is used whenever the ants have finished to vote...
234 sub votes_count_rule{
235 my ($self, $stash, $ant_nest_to_check) = @_;
237 my $ant_nest = Bcd::Data::NewAntNests->select_new_ant_nest_booking_arr($stash, $ant_nest_to_check);
238 return undef if (!defined($ant_nest));
239 return undef if ($ant_nest->[1] != Bcd::Constants::NewAntNestsConstants::MAKING_POOL);
241 my $count = Bcd::Data::NewAntNests->get_founders_with_state_count
242 ($stash, $ant_nest_to_check, Bcd::Constants::FoundersConstants::VOTED);
245 if ($count != Bcd::Constants::NewAntNestsConstants::ANT_NEST_INITIAL_SIZE){
246 return undef;
249 #ok, all have voted... let's get the votes...
250 my $boss_vote = 0;
251 my $treasurer_vote = 0;
253 my $bosses = Bcd::Data::Founders->get_candidates_bosses ($stash, $ant_nest_to_check);
254 my $treasurers = Bcd::Data::Founders->get_candidates_treasurers($stash, $ant_nest_to_check);
256 #ok, now I should be able to convert this array in a hash... just for convenience
258 my $bosses_hash = {};
259 my $treasurers_hash = {};
261 foreach(@{$bosses}){
262 $bosses_hash->{$_->[0]} = $_->[1];
265 foreach(@{$treasurers}){
266 $treasurers_hash->{$_->[0]} = $_->[1];
269 my $winner_boss;
270 my $winner_treasurer;
272 #I should see if this ant nest has voted for boss or treasurer
273 if (scalar(@{$bosses}) > 1){
274 $boss_vote = 1;
275 } else {
276 #the winner is this without any vote
277 $winner_boss = $bosses->[0]->[0];
280 if (scalar(@{$treasurers}) > 1){
281 $treasurer_vote = 1;
282 } else {
283 $winner_treasurer = $treasurers->[0]->[0];
286 #ok, now I should get the votes
287 my $votes = Bcd::Data::FoundersVotings->get_votes_result($stash, $ant_nest_to_check);
289 #I should check the validity of the bosses votes
290 my $fail = 0;
292 if ( $boss_vote == 1 ){
293 ($fail, $winner_boss) = $self->_check_votes_validity_fail($votes->{bosses})
296 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FAIL $fail \n";
298 if ( $fail == 0 and $treasurer_vote == 1){
299 ($fail, $winner_treasurer) = $self->_check_votes_validity_fail($votes->{treasurers});
302 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FAIL after t $fail \n";
304 my $template_vars = {
305 bosses => $bosses_hash,
306 treasurers => $treasurers_hash,
307 votes => $votes,
308 winner_boss => $winner_boss,
309 winner_treasurer => $winner_treasurer,
310 boss_vote => $boss_vote,
311 treasurer_vote => $treasurer_vote,
312 id_ant_nest => $ant_nest_to_check,
315 if ($fail == 1) {
317 $template_vars->{vote_result} = '0';
319 #ko, vote is not valid
320 $self->_mail_to_all_the_founders_and_change_their_state
321 ($stash, $ant_nest_to_check, "initial_votes_result.tt2",
322 Bcd::Constants::FoundersConstants::NO_CHOICE_OF_CANDIDATE, $template_vars);
324 Bcd::Data::NewAntNests->update_ant_nest_state
325 ($stash, $ant_nest_to_check, Bcd::Constants::NewAntNestsConstants::CANDIDATES_RESEARCH);
326 return "vote failed, downgraded to CANDIDATES_RESEARCH";
328 } else {
329 #ok, vote valid
330 $template_vars->{vote_result} = '1';
334 my $assigned_code =
335 $self->_create_ant_nest($stash, $template_vars, $ant_nest->[2], $winner_boss, $winner_treasurer);
337 #advance the state of the ant nest and the ants...
338 $self->_mail_to_all_the_founders($stash, $ant_nest_to_check,
339 "initial_votes_result.tt2",
340 $template_vars);
342 #and then I grant the role to boss and treasurer in the new ant nest
344 return "vote OK, created ant nest code=$assigned_code";
350 sub _create_ant_nest{
352 my ($self, $stash, $template_vars, $name_ant_nest, $winner_boss, $winner_treasurer) = @_;
354 #print "[[[[[[[[[\n\n\n ok you have won boss $winner_boss treasurer $winner_treasurer \n\n\n]]]]]]]]]";
356 #nominate the boss and treasurer, reset their flag
357 Bcd::Data::Founders->nominate_boss_and_treasurer
358 ($stash, $template_vars->{id_ant_nest}, $winner_boss, $winner_treasurer);
360 #create the new ant nest... to comunicate to the ants that has been created...
361 my $assigned_code = Bcd::Data::AntNests->create_ant_nest_from_booking
362 ($stash, $template_vars->{id_ant_nest}, $name_ant_nest);
364 $template_vars->{assigned_code} = $assigned_code;
366 #ok, now I should create the users..., I get the ids of the boss and treasurer
367 #because I grant to them the role
368 my ($boss_id, $treasurer_id) =
369 $self->_create_the_users($stash, $template_vars->{id_ant_nest}, $assigned_code);
371 #print "\nI HAVE new boss $boss_id and treasurer $treasurer_id\n";
372 $self->_grant_role_to_boss_and_treasurer($stash, $boss_id, $treasurer_id);
375 Bcd::Data::NewAntNests->update_ant_nest_state
376 ($stash, $template_vars->{id_ant_nest},
377 Bcd::Constants::NewAntNestsConstants::WAITING_FOR_INITIAL_SET_UP);
379 #You should put the $assigned_code into the table of the ant nest booking
380 Bcd::Data::NewAntNests->update_assigned_id
381 ($stash, $template_vars->{id_ant_nest}, $assigned_code);
383 #send instructions to treasurer and boss to connect to the site and do the initial setup
384 #to do... but this is simple...
386 $self->_send_instructions_to_new_boss_and_treasurer
387 ($stash, $winner_boss, $winner_treasurer, $template_vars);
389 return $assigned_code;
393 sub _grant_role_to_boss_and_treasurer{
394 my ($self, $stash, $boss_id, $treasurer_id) = @_;
396 #grant the role to these two users.
397 Bcd::Data::Users->grant_role_to_user_id($stash, $boss_id, Bcd::Data::Users::BOSS_ROLE);
398 Bcd::Data::Users->grant_role_to_user_id($stash, $treasurer_id, Bcd::Data::Users::TREASURER_ROLE);
401 sub _send_instructions_to_new_boss_and_treasurer{
402 my ($self, $stash, $winner_boss, $winner_treasurer, $template_vars) = @_;
404 #ok, first I should get the boss...
405 my $boss = Bcd::Data::Founders->get_founder_with_email($stash, $winner_boss);
406 $self->_mail_to_a_founder($stash, $boss, "new_boss_mail.tt2", $template_vars);
408 #and then the treasurer.
409 my $treasurer = Bcd::Data::Founders->get_founder_with_email($stash, $winner_treasurer);
410 $self->_mail_to_a_founder($stash, $treasurer, "new_treasurer_mail.tt2", $template_vars);
414 sub _create_the_users{
415 my ($self, $stash, $booking_code, $assigned_code) = @_;
417 #Ok, I should get all the founders...
418 my $founders = Bcd::Data::Founders->get_all_founders_ant_nest_arr($stash, $booking_code);
419 my $boss_id;
420 my $treasurer_id;
422 #this hash stores the connection between old and new ids.
423 my %old_to_new_id;
425 foreach(@{$founders}){
427 my $user_id = Bcd::Data::Users->create_user_from_booking
428 ($stash, $assigned_code, $_->[3], $_->[5], $_->[6]);
430 my $old = $_->[0];
432 #print "THE USER WHO was $old now is $user_id\n";
433 #my $user_new = Bcd::Data::Users->select_user_data($stash, $user_id);
434 #my $user_new_from_db = $user_new->{id};
435 #my $nick = $user_new->{nick};
436 #my $ant_nest = $user_new->{id_ant_nest};
437 #print "From the db I have id=$user_new_from_db nick=$nick, ant_nest=$ant_nest\n";
439 $old_to_new_id{$old} = $user_id;
441 #is he a boss?
442 if ($_->[7] == '1'){
443 $boss_id = $user_id;
444 } elsif ($_->[8] == '1'){
445 $treasurer_id = $user_id;
450 #ok, now I have the correspondence, I can transfer also the trusts...
451 $self->_transfer_trusts($stash, $founders, \%old_to_new_id);
453 return ($boss_id, $treasurer_id);
456 sub _transfer_trusts{
457 my ($self, $stash, $founders, $old_to_new_id) = @_;
459 #I have another cycle
460 foreach(@{$founders}){
461 my $trusts_to_move = Bcd::Data::FoundersTrusts
462 ->get_all_trusts_for_this_user_arr($stash, $_->[0]);
464 foreach(@{$trusts_to_move}){
466 my $new_1 = $old_to_new_id->{$_->[0]};
467 my $new_2 = $old_to_new_id->{$_->[1]};
469 #print "ADDING>>>>>>>>>>>>>>>>>>>> trust between $new_1 and $new_2\n";
471 Bcd::Data::Trust->create_trust_between
473 $stash,
474 $new_1,
475 $new_2,
476 $_->[2],
477 $_->[3]
486 sub _check_votes_validity_fail{
487 my ($self, $votes) = @_;
489 my $winner_id;
490 my $winner_votes = 0;
491 my $number_of_winners = 2; #initially fail
493 for (@{$votes}){
494 if (defined($_->[0])){
495 #this is a vote to a real person
496 if ($_->[1] > $winner_votes){
497 #I have a new winner
498 $winner_id = $_->[0];
499 $winner_votes = $_->[1];
500 $number_of_winners = 1;
501 } elsif ($_->[1] == $winner_votes){
502 $number_of_winners++;
507 if ($number_of_winners == 1){
508 #all ok
509 return (0, $winner_id);
510 } else {
511 #fail
512 return (1, undef);
518 #this rule is called whenever the founders have chosen their candidates
519 sub after_the_candidate_choice_rule{
520 my ($self, $stash, $ant_nest_to_check) = @_;
522 my $ant_nest = Bcd::Data::NewAntNests->select_new_ant_nest_booking_arr($stash, $ant_nest_to_check);
523 return undef if (!defined($ant_nest));
524 return undef if ($ant_nest->[1] != Bcd::Constants::NewAntNestsConstants::CANDIDATES_RESEARCH);
526 my $count = Bcd::Data::NewAntNests->get_founders_with_state_count
527 ($stash, $ant_nest_to_check, Bcd::Constants::FoundersConstants::CHOICE_MADE);
530 if ($count != Bcd::Constants::NewAntNestsConstants::ANT_NEST_INITIAL_SIZE){
531 return undef;
534 #ok, all the ants have made a choice.
536 my $count_of_bosses;
537 my $count_of_treasurers;
539 my $bosses = Bcd::Data::Founders->get_candidates_bosses ($stash, $ant_nest_to_check);
540 my $treasurers = Bcd::Data::Founders->get_candidates_treasurers($stash, $ant_nest_to_check);
542 $count_of_bosses = scalar(@{$bosses});
543 $count_of_treasurers = scalar(@{$treasurers});
545 my $template_vars = {
546 bosses => $bosses,
547 treasurers => $treasurers,
548 id_ant_nest => $ant_nest_to_check,
551 #print Dumper($bosses);
552 #print Dumper($treasurers);
554 if ($count_of_treasurers == 0 or $count_of_bosses == 0){
555 #this step is not valid... You should repeat it
557 #all the founders downgraded to NO_CHOICE_OF_CANDIDATE
559 $self->_mail_to_all_the_founders_and_change_their_state
560 ($stash, $ant_nest_to_check, "no_candidates_you_have_to_repeat.tt2",
561 Bcd::Constants::FoundersConstants::NO_CHOICE_OF_CANDIDATE, $template_vars);
563 #ant nest not change
565 return "no boss or no treasurer found. Downgraded to CANDIDATES_RESEARCH";
567 } elsif ($count_of_bosses == 1 and $count_of_treasurers == 1){
569 #founders upgraded to waiting_to_be_created
571 #print Dumper($bosses);
572 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> tr\n\n\n";
573 #print Dumper($treasurers);
574 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END\n\n\n";
576 my $assigned_code = $self->_create_ant_nest
577 ($stash, $template_vars, $ant_nest->[2], $bosses->[0]->[0], $treasurers->[0]->[0]);
579 $self->_mail_to_all_the_founders
580 ($stash, $ant_nest_to_check, "only_one_candidate_vote_not_needed.tt2", $template_vars);
582 #ant_nest upgraded to WAITING_FOR_INITIAL_SET_UP
584 Bcd::Data::NewAntNests->update_ant_nest_state
585 ($stash, $ant_nest_to_check, Bcd::Constants::NewAntNestsConstants::WAITING_FOR_INITIAL_SET_UP);
587 return "all ok, no vote needed created ant nest with assigned_code=$assigned_code";
589 } else {
591 #founders upgraded to NO_VOTED_YET
592 #ant_nest upgraded to MAKING_POOL
594 $self->_mail_to_all_the_founders_and_change_their_state
595 ($stash, $ant_nest_to_check, "you_have_to_vote_now.tt2",
596 Bcd::Constants::FoundersConstants::NO_VOTED_YET, $template_vars);
598 Bcd::Data::NewAntNests->update_ant_nest_state
599 ($stash, $ant_nest_to_check, Bcd::Constants::NewAntNestsConstants::MAKING_POOL);
601 #I should delete all the votes
602 Bcd::Data::FoundersVotings->delete_all_votes_for_this_ant_nest($stash, $ant_nest_to_check);
604 return "all ok, vote needed";
609 sub _mail_to_a_founder{
610 my ($self, $stash, $founder, $letter_to_send, $stash_vars) = @_;
612 $stash_vars->{name} = $founder->[1];
613 $stash_vars->{nick} = $founder->[0];
615 $stash->get_mailer()->mail_this_message
616 ($stash, $founder->[2], $letter_to_send, $stash_vars);
619 sub _mail_to_all_the_founders{
620 my ($self, $stash, $ant_nest_to_check, $letter_to_send, $stash_vars) = @_;
622 $stash_vars = {} if (!defined($stash_vars));
624 my $founders = Bcd::Data::Founders->get_founders_to_email($stash, $ant_nest_to_check);
626 foreach(@{$founders}){
628 $self->_mail_to_a_founder($stash, $_, $letter_to_send, $stash_vars);
632 sub _mail_to_all_the_founders_and_change_their_state{
633 my ($self, $stash, $ant_nest_to_check, $letter_to_send, $upgraded_founder_state, $stash_vars) = @_;
635 $stash_vars = {} if (!defined($stash_vars));
637 my $founders = Bcd::Data::Founders->get_founders_to_email($stash, $ant_nest_to_check);
639 foreach(@{$founders}){
641 $self->_mail_to_a_founder($stash, $_, $letter_to_send, $stash_vars);
643 #I should update the state of all the founders...
644 Bcd::Data::Founders->update_founder_state($stash, $_->[3], $upgraded_founder_state);
648 #this is a generic rule for all the intial states where I must have
649 #all the ants which run in parallel.
650 sub _all_ants_have_reached_a_particular_state_rule {
651 #a very short list of parameters...
652 my ($self, $stash, $ant_nest_to_check,
653 $name_of_rule, $triggering_state, $required_founders_state,
654 $upgraded_ant_nest_state, $letter_to_send, $upgraded_founder_state,
655 $message_to_return) = @_;
657 #this rule checks if an ant nest have completed the checkin
658 #get_logger()->debug("NewAntNestsBot: $name_of_rule : $ant_nest_to_check");
660 #first of all I get the state of this ant nest
661 my $ant_nest = Bcd::Data::NewAntNests->select_new_ant_nest_booking_arr($stash, $ant_nest_to_check);
662 #the ant nest can be not defined if for example I have deleted it before
663 return undef if (!defined($ant_nest));
665 #the rule is valid only in this state.
666 return undef if ($ant_nest->[1] != $triggering_state);
668 #I should get the count of the founders in a particular state.
669 my $count = Bcd::Data::NewAntNests->get_founders_with_state_count
670 ($stash, $ant_nest_to_check, $required_founders_state);
673 if ($count == Bcd::Constants::NewAntNestsConstants::ANT_NEST_INITIAL_SIZE){
674 #ok, I can upgrade this ant nest...
675 Bcd::Data::NewAntNests->update_ant_nest_state
676 ($stash, $ant_nest_to_check, $upgraded_ant_nest_state);
679 #I should advise all founders
680 $self->_mail_to_all_the_founders_and_change_their_state
681 ($stash, $ant_nest_to_check, $letter_to_send, $upgraded_founder_state,
682 { id_ant_nest => $ant_nest_to_check});
684 # my $founders = Bcd::Data::Founders->get_founders_to_email($stash, $ant_nest_to_check);
686 # foreach(@{$founders}){
688 # $stash->get_mailer()->mail_this_message
689 # ($stash, $_->[2],
690 # $letter_to_send,
692 # name => $_->[1],
693 # nick => $_->[0],
694 # id_ant_nest => $ant_nest_to_check,
696 # );
698 # #I should update the state of all the founders...
699 # Bcd::Data::Founders->update_founder_state($stash, $_->[3], $upgraded_founder_state);
702 return "ant nest $ant_nest_to_check: upgraded to $message_to_return";
705 return undef;
708 sub all_ants_have_built_the_web_of_trust{
709 my ($self, $stash, $ant_nest_to_check) = @_;
711 return $self->_all_ants_have_reached_a_particular_state_rule
713 $stash,
714 $ant_nest_to_check,
715 "all_ants_have_built_the_web_of_trust",
716 Bcd::Constants::NewAntNestsConstants::TRUSTS_NETWORK_BUILDING,
717 Bcd::Constants::FoundersConstants::TRUST_PRESENT,
718 Bcd::Constants::NewAntNestsConstants::CANDIDATES_RESEARCH,
719 "you_have_to_candidate_yourself.tt2",
720 Bcd::Constants::FoundersConstants::NO_CHOICE_OF_CANDIDATE,
721 "selecting candidates."
725 sub all_ants_have_immitted_data{
726 my ($self, $stash, $ant_nest_to_check) = @_;
728 return $self->_all_ants_have_reached_a_particular_state_rule
730 $stash,
731 $ant_nest_to_check,
732 "all_ants_have_immitted_data",
733 Bcd::Constants::NewAntNestsConstants::FOUNDERS_IMMITTING_DATA,
734 Bcd::Constants::FoundersConstants::PERSONAL_DATA_IMMITTED,
735 Bcd::Constants::NewAntNestsConstants::TRUSTS_NETWORK_BUILDING,
736 "you_have_to_compile_the_trust.tt2",
737 Bcd::Constants::FoundersConstants::TRUST_NOT_PRESENT,
738 "waiting for trust."
742 sub all_ants_have_checked_in{
743 my ($self, $stash, $ant_nest_to_check) = @_;
745 return $self->_all_ants_have_reached_a_particular_state_rule
747 $stash,
748 $ant_nest_to_check,
749 "all_ants_have_checked_in",
750 Bcd::Constants::NewAntNestsConstants::WAITING_FOR_CHECKING_IN,
751 Bcd::Constants::FoundersConstants::USER_BOOKED_CONFIRMED,
752 Bcd::Constants::NewAntNestsConstants::FOUNDERS_IMMITTING_DATA,
753 "you_should_give_me_your_personal_data.tt2",
754 Bcd::Constants::FoundersConstants::WAITING_FOR_PERSONAL_DATA,
755 "immitting data state."
759 sub delete_founders_ants_not_checked{
760 my ($self, $stash, $ant_nest_to_check) = @_;
762 #get_logger()->debug("delete_founders_ants_not_checked rule called ant nest $ant_nest_to_check");
765 my $st = $stash->prepare_cached(SELECT_OLD_FOUNDERS_STATE);
767 $st->bind_param(1, Bcd::Constants::FoundersConstants::USER_BOOKED_CREATED);
768 $st->bind_param(2, $ant_nest_to_check);
771 $st->execute();
773 my $row;
775 while (defined($row = $st->fetch())){
777 #OK... This ant is too lazy, I should delete it...
778 $self->_delete_this_lazy_to_check_in_founder_ant($stash, $row->[0]);
780 #if this was the last ant (no founders and no lookers)
781 #, then the ant nest should be deleted too.
783 my $founders = Bcd::Data::NewAntNests->get_ant_nest_founders_count($stash, $ant_nest_to_check);
784 my $lookers = Bcd::Data::NewAntNests->get_ant_nest_founders_count($stash, $ant_nest_to_check);
786 my $status;
787 if ($founders + $lookers == 0){
788 #I should delete also the ant nest
789 $self->_delete_this_new_ant_nest_aborted($stash, $row->[2]);
790 $status = "deleted ONLY lazy ant $row->[3] in ant nest $row->[2], deleted also the ant nest";
791 } else {
792 # there are some founders or lookers left, let's see if I should downgrade this ant nest
793 my $downgraded = $self->_check_this_ant_nest_state($stash, $ant_nest_to_check, $row->[3]);
794 if ($downgraded == 1){
795 $status = "deleted lazy ant $row->[3]; DOWNGRADED ant nest $row->[2]";
796 } else {
797 $status = "deleted lazy ant $row->[3] in ant nest $row->[2]";
801 $st->finish();
803 return $status;
806 $st->finish();
808 #no firing....
809 return undef;
812 sub _delete_this_new_ant_nest_aborted{
814 my ($self, $stash, $id_aborted_ant_nest) = @_;
816 #first of all I delete the token
817 my $sql = qq{delete from ant_nests_bookings where id = ?};
818 my $st2 = $stash->get_connection()->prepare($sql);
819 $st2->bind_param(1, $id_aborted_ant_nest);
820 $st2->execute();
823 sub _delete_this_lazy_to_check_in_founder_ant{
824 my ($self, $stash, $id_lazy_ant) = @_;
826 #first of all I delete the token
827 my $sql = qq{delete from ant_nests_founders_tokens where id_user = ?};
828 my $st2 = $stash->get_connection()->prepare($sql);
829 $st2->bind_param(1, $id_lazy_ant);
830 $st2->execute();
832 #then I delete the user
833 $sql = qq{delete from ant_nests_founders where id_user = ?};
834 $st2 = $stash->get_connection()->prepare($sql);
835 $st2->bind_param(1, $id_lazy_ant);
836 $st2->execute();
839 sub _check_this_ant_nest_state{
840 my ($self, $stash, $ant_nest_to_check, $lazy_ant) = @_;
842 #ok, now I should test if this ant nest was in the state waiting for check_in
843 my $ant_nest = Bcd::Data::NewAntNests->select_new_ant_nest_booking_arr($stash, $ant_nest_to_check);
845 #if it was in the WAITING_FOR_CHECKING_IN state I downgrade it to WAITING_FOR_INITIAL_FOUNDERS state
846 if ($ant_nest->[1] == Bcd::Constants::NewAntNestsConstants::WAITING_FOR_CHECKING_IN){
848 #downgrade the state
849 Bcd::Data::NewAntNests->update_ant_nest_state
850 ($stash, $ant_nest_to_check, Bcd::Constants::NewAntNestsConstants::WAITING_FOR_INITIAL_FOUNDERS);
852 #Ok, now I should inform the other ants...
854 my $founders = Bcd::Data::Founders->get_confirmed_founders_to_email
855 ($stash, $ant_nest_to_check);
857 foreach(@{$founders}){
859 $stash->get_mailer()->mail_this_message
860 ($stash, $_->[2],
861 'downgraded_ant_nest_because_of_lazy_ant.tt2',
863 name => $_->[1],
864 nick => $_->[0],
865 id_ant_nest => $ant_nest_to_check,
866 lazy_ant => $lazy_ant,
871 return 1;
872 } else {
873 return 0; #no downgrade necessary.