//---------------------------------------------------------------------------
void ribi::gtst::ServerStateChooseAction::OnTimer()
{
  if (CanGoToNextState())
  {
    GoToNextState();
  }
}
//---------------------------------------------------------------------------
void ribi::gtst::ServerStateGroupAssign::OnTimer()
{
  if (CanGoToNextState())
  {
    GetServer()->GetGroups()->MoveLoggedInToParticipating();
    GoToNextState();
  }
}
//---------------------------------------------------------------------------
void ServerStateVoting::Start()
{
  ///Keeps track of Participants having voted
  m_has_voted.clear();
  assert(m_has_voted.empty());

  BOOST_FOREACH(const boost::shared_ptr<const Participant>& p,
    GetServer()->GetGroups()->CollectParticipants(false,false,true,false))
  {
    assert(p);
    m_has_voted[p] = false;
    StateVoting * const state = dynamic_cast<const StateVoting*>(this);
    assert(state);
    FindParticipant(p)->SetState(state);
  }
  assert(!CanGoToNextState());
}
//---------------------------------------------------------------------------
void ribi::gtst::ServerStateChooseAction::Start()
{
  ///Keeps track of Participants having voted
  m_has_chosen_action.clear();
  assert(m_has_chosen_action.empty());

  BOOST_FOREACH(
    const boost::shared_ptr<const Participant>& p,
    GetServer()->GetGroups()->CollectParticipants(false,false,true,false))
  {
    assert(!GetServer()->GetGroups()->CollectParticipants(false,false,true,false).empty());
    assert(p);
    m_has_chosen_action.insert(std::make_pair(p,false));
    StateChooseAction * const state = dynamic_cast<const StateChooseAction*>(this);
    assert(state);
    FindParticipant(p)->SetState(state);
  }
  assert(!CanGoToNextState());
}
//---------------------------------------------------------------------------
void ServerStateGroupReAssign::OnTimer()
{
  if (CanGoToNextState())
  {
    //Actually move the Participants
    const Group * const most_successful_group = m_worst_and_best_group.second;
    //Get the group size of the group with the highest payoffs
    const int group_size = most_successful_group->GetSize();

    if (group_size == 3)
    {
      std::clog << "A group of size 3 grows to 5!\n";
      NotifyGroupGrowth(most_successful_group);
    }
    else if (group_size == 5)
    {
      std::clog << "A group of size 5 splits!\n";
      const Group * const least_successful_group = m_worst_and_best_group.first;
      assert(least_successful_group);
      assert(least_successful_group != most_successful_group);

      ///\note
      ///The operation below invalidates const Group * const least_successful_group
      NotifyKillGroup(least_successful_group);
      //Again find the most successful group and let it split
      //Find the group with the highest payoff
      assert(most_successful_group->GetSize() == 5);

      NotifyGroupSplit(most_successful_group);
    }

    GoToNextState();

  }


}
void ribi::gtst::ServerStateViewResultsVoting::OnTimer()
{
  if (CanGoToNextState()) GoToNextState();
}
//---------------------------------------------------------------------------
void ribi::gtst::ServerStateChat::OnTimer()
{
  if (CanGoToNextState()) GoToNextState();
}
//---------------------------------------------------------------------------
void ServerStateVoting::OnTimer()
{
  if (CanGoToNextState()) GoToNextState();
}