Exemple #1
0
/*
 * Put artefact into cave after finished movement.
 */
void put_artefact_into_cave (db_t *database, int artefactID, int caveID)
{
  dstring_t *ds;

  ds = dstring_new("UPDATE " DB_TABLE_ARTEFACT " SET caveID = %d "
         "WHERE artefactID = %d "
         "AND caveID = 0 "
         "AND initiated = %d",
     caveID, artefactID, ARTEFACT_UNINITIATED);
  db_query_dstring(database, ds);

  debug(DEBUG_ARTEFACT, "put_artefact_into_cave: %s", dstring_str(ds));

  /* Bedingung: Artefakt muss vorhanden sein; darf in keiner anderen Höhle liegen; */
  /*   muss uninitialisiert sein */
  if (db_affected_rows(database) != 1)
    throw(BAD_ARGUMENT_EXCEPTION, "put_artefact_into_cave: no such artefactID or "
                             "artefact already in another cave or not uninitiated");

  ds = dstring_new("UPDATE Cave SET artefacts = artefacts + 1 "
         "WHERE caveID = %d", caveID);
  db_query_dstring(database, ds);

  debug(DEBUG_ARTEFACT, "put_artefact_into_cave: %s", dstring_str(ds));

  /* Bedingung: Höhle muss vorhanden sein */
  if (db_affected_rows(database) != 1)
    throw(SQL_EXCEPTION, "put_artefact_into_cave: no such caveID");
}
Exemple #2
0
int new_artefact (db_t *database, int artefactClassID)
{
  db_result_t *result;
  dstring_t *ds;

  /* get artefact class */
  ds = dstring_new("SELECT * FROM " DB_TABLE_ARTEFACT_CLASS
            " WHERE artefactClassID = %d", artefactClassID);
  result = db_query_dstring(database, ds);

  debug(DEBUG_ARTEFACT, "new_artefact: %s", dstring_str(ds));

  /* no such class */
  if (db_affected_rows(database) != 1)
    throw(SQL_EXCEPTION, "new_artefact: no such artefact class");

  ds = dstring_new("INSERT INTO " DB_TABLE_ARTEFACT
         " (artefactClassID, caveID, initiated)"
         " VALUES (%d, 0, 0)", artefactClassID);
  db_query_dstring(database, ds);

  debug(DEBUG_ARTEFACT, "new_artefact: %s", dstring_str(ds));

  /* successfully inserted? */
  if (db_affected_rows(database) != 1)
    throw(SQL_EXCEPTION, "new_artefact: could not insert artefact");

  return db_sequence_value(database, "Artefact_artefactID_seq");
}
Exemple #3
0
/*
 * Initiating finished. Now set the status of the artefact to
 * ARTEFACT_INITIATED.
 */
void initiate_artefact (db_t *database, int artefactID)
{
  struct Artefact artefact;
  dstring_t *ds;

  /* get artefact values; throws exception, if that artefact is missing */
  get_artefact_by_id(database, artefactID, &artefact);

  /* Bedingung: muss gerade eingeweiht werden */
  if (artefact.initiated != ARTEFACT_INITIATING)
    throw(BAD_ARGUMENT_EXCEPTION, "initiate_artefact: artefact was not initiating");

  /* Bedingung: muss in einer Höhle liegen */
  if (artefact.caveID == 0)
    throw(BAD_ARGUMENT_EXCEPTION, "initiate_artefact: artefact was not in a cave");

  ds = dstring_new("UPDATE " DB_TABLE_ARTEFACT " SET initiated = %d WHERE artefactID = %d AND caveID = %d",
     ARTEFACT_INITIATED, artefact.artefactID, artefact.caveID);
  db_query_dstring(database, ds);

  debug(DEBUG_ARTEFACT, "initiate_artefact: %s", dstring_str(ds));

  /* Bedingung: Artefakt und Höhle müssen existieren */
  if (db_affected_rows(database) != 1)
    throw(SQL_EXCEPTION, "initiate_artefact: no such artefactID or caveID");
}
Exemple #4
0
int new_artefact (db_t *database, int artefactClassID)
{
  db_result_t *result;

  /* get artefact class */
  result = db_query(database, "SELECT * FROM Artefact_class "
			      "WHERE artefactClassID = %d", artefactClassID);

  /* no such class */
  if (db_affected_rows(database) != 1)
    throw(SQL_EXCEPTION, "new_artefact: no such artefact class");

  db_query(database, "INSERT INTO " DB_TABLE_ARTEFACT
		     " (artefactClassID, caveID, initiated)"
		     " VALUES (%d, 0, 0)", artefactClassID);

  /* successfully inserted? */
  if (db_affected_rows(database) != 1)
    throw(SQL_EXCEPTION, "new_artefact: could not insert artefact");

  return db_sequence_value(database, "Artefact_artefactID_seq");
}
Exemple #5
0
/*
 * User wants to remove the artefact from a cave or another user just robbed
 * that user. Remove the artefact from its cave.
 */
void remove_artefact_from_cave (db_t *database, int artefactID)
{
  struct Artefact artefact;

  /* save artefact values; throws exception, if that artefact is missing */
  get_artefact_by_id(database, artefactID, &artefact);

  db_query(database, "UPDATE " DB_TABLE_ARTEFACT " SET caveID = 0 "
		     "WHERE artefactID = %d", artefactID);

  db_query(database, "UPDATE Cave SET artefacts = artefacts - 1 "
		     "WHERE caveID = %d", artefact.caveID);

  /* Bedingung: Höhle muss vorhanden sein */
  if (db_affected_rows(database) != 1)
    throw(SQL_EXCEPTION, "remove_artefact_from_cave: no such caveID");
}
Exemple #6
0
/*
 * This function is responsible for all the movement.
 *
 * @params database  the function needs this link to the DB
 * @params result    current movement event (from DB)
 */
void movement_handler (db_t *database, db_result_t *result)
{
  int movementID;
  int target_caveID;
  int source_caveID;
  const char *speed_factor;

  time_t event_start;
  time_t event_end;
  const char *return_start;
  char return_end[TIMESTAMP_LEN];

  struct Cave cave1;
  struct Cave cave2;
  struct Player player1;
  struct Player player2;
  struct Relation relation1;
  struct Relation relation2;

  int i;
  int units[MAX_UNIT];
  int resources[MAX_RESOURCE];
  int takeover_multiplier;
  int change_owner;
  int isFarming = 0;
  
  Battle *battle;
  dstring_t *ds;
  double spy_result;

  /* time related issues */
  const float *battle_bonus;

  /* artefacts */
  int artefact = 0;
  int artefact_def = 0;
  int artefact_id = 0;
  int lostTo = 0;

  int body_count = 0;
  int attacker_lose = 0;
  int defender_lose = 0;
  int defender_va_lose = 0;

  int war_points_attacker = 0;
  int war_points_defender = 0;
  int war_points_sender = 0;
  int war_points_show = 0;

	int  takeover = 0;
  debug(DEBUG_TICKER, "entering function movement_handler()");

  /* get movement id and target/source cave id */
  movementID    = db_result_get_int(result, "movementID");
  target_caveID = db_result_get_int(result, "target_caveID");
  source_caveID = db_result_get_int(result, "source_caveID");
  speed_factor  = db_result_get_string(result, "speedFactor");

  /* get event_start and event_end */
  event_start  = db_result_get_gmtime(result, "start");
  return_start = db_result_get_string(result, "end");
  event_end    = make_time_gm(return_start);
  make_timestamp_gm(return_end, event_end + (event_end - event_start));

  /* get resources, units and artefact id */
  get_resource_list(result, resources);
  get_unit_list(result, units);
  artefact = db_result_get_int(result, "artefactID");

  /* TODO reduce number of queries */
  get_cave_info(database, source_caveID, &cave1);
  get_cave_info(database, target_caveID, &cave2);

  if (cave1.player_id)
    get_player_info(database, cave1.player_id, &player1);
  else{	/* System */
    memset(&player1, 0, sizeof player1);
    player1.tribe = "";
  }
  if (cave2.player_id == cave1.player_id)
    player2 = player1;
  else if (cave2.player_id)
    get_player_info(database, cave2.player_id, &player2);
  else{	/* System */
    memset(&player2, 0, sizeof player2);
    player2.tribe = "";
  }
  debug(DEBUG_TICKER, "caveID = %d, movementID = %d",
	target_caveID, movementID);

  /**********************************************************************/
  /*** THE INFAMOUS GIANT SWITCH ****************************************/
  /**********************************************************************/

  switch (movementID) {

    /**********************************************************************/
    /*** ROHSTOFFE BRINGEN ************************************************/
    /**********************************************************************/
    case ROHSTOFFE_BRINGEN:

      /* record in takeover table */
      ds = dstring_new("UPDATE " DB_TABLE_CAVE_TAKEOVER " SET ");

      for (i = 0; i < MAX_RESOURCE; ++i)
	dstring_append(ds, "%s%s = %s + %d", i > 0 ? "," : "",
		resource_type[i]->dbFieldName,
		resource_type[i]->dbFieldName, resources[i]);

      dstring_append(ds, " WHERE caveID = %d AND playerID = %d",
		     target_caveID, cave1.player_id);

      db_query_dstring(database, ds);
			if(db_affected_rows(database)!=0){
							takeover=1;
			}
	/* put resources into cave */
	dstring_set(ds, "UPDATE " DB_TABLE_CAVE " SET ");

	for (i = 0; i < MAX_RESOURCE; ++i)
	  dstring_append(ds, "%s%s = LEAST(%s + %d, %s)", i > 0 ? "," : "",
		  resource_type[i]->dbFieldName,
		  resource_type[i]->dbFieldName, 
			(takeover==1)?resources[i] * TAKEOVER_RESOURCE_SAVE_PERCENTAGE / 100:resources[i],
		  function_to_sql(resource_type[i]->maxLevel));

	dstring_append(ds, " WHERE caveID = %d", target_caveID);

	db_query_dstring(database, ds);

      if (artefact > 0)
	put_artefact_into_cave(database, artefact, target_caveID);

      /* send all units back */
      dstring_set(ds, "INSERT INTO Event_movement"
		      " (caveID, target_caveID, source_caveID, movementID,"
		      " speedFactor, start, end");

      for (i = 0; i < MAX_UNIT; ++i)
	dstring_append(ds, ",%s", unit_type[i]->dbFieldName);

      dstring_append(ds, ") VALUES (%d, %d, %d, %d, %s, '%s', '%s'",
	      source_caveID, source_caveID, target_caveID, RUECKKEHR,
	      speed_factor, return_start, return_end);

      for (i = 0; i < MAX_UNIT; ++i)
	dstring_append(ds, ",%d", units[i]);

      dstring_append(ds, ")");

      db_query_dstring(database, ds);

      /* generate trade report and receipt for sender */
      trade_report(database, &cave1, &player1, &cave2, &player2,
		   resources, NULL, artefact);
      break;

    /**********************************************************************/
    /*** EINHEITEN/ROHSTOFFE VERSCHIEBEN **********************************/
    /**********************************************************************/
    case VERSCHIEBEN:
      get_relation_info(database, player1.tribe, player2.tribe, &relation1);
      /*überprüfen ob sender und versender eine kriegsbeziehung haben */
      if(!(isVerschiebenAllowed(database, &player1, &player2, &relation1) ||
	      isTakeoverableCave(database, target_caveID))){
        //bewegung umdrehen//
	    /* send remaining units back */
	    ds = dstring_new("INSERT INTO Event_movement"
		  " (caveID, target_caveID, source_caveID, movementID,"
		  " speedFactor, start, end, artefactID");

	    for (i = 0; i < MAX_RESOURCE; ++i)
	      dstring_append(ds, ",%s", resource_type[i]->dbFieldName);
	    for (i = 0; i < MAX_UNIT; ++i)
	      dstring_append(ds, ",%s", unit_type[i]->dbFieldName);

	    dstring_append(ds, ") VALUES (%d, %d, %d, %d, %s, '%s', '%s', %d",
		  source_caveID, source_caveID, target_caveID, RUECKKEHR,
		  speed_factor, return_start, return_end, artefact);

	    for (i = 0; i < MAX_RESOURCE; ++i)
	      dstring_append(ds, ",%d", resources[i]);
	    for (i = 0; i < MAX_UNIT; ++i)
	      dstring_append(ds, ",%d", units[i]);

	    dstring_append(ds, ")");

	    db_query_dstring(database, ds);
	    break;
      }
      /* record in takeover table */
      ds = dstring_new("UPDATE " DB_TABLE_CAVE_TAKEOVER " SET ");

      for (i = 0; i < MAX_RESOURCE; ++i)
	    dstring_append(ds, "%s%s = %s + %d", i > 0 ? "," : "",
		  resource_type[i]->dbFieldName,
		  resource_type[i]->dbFieldName, resources[i]);

      dstring_append(ds, " WHERE caveID = %d AND playerID = %d",
	      target_caveID, cave1.player_id);

      db_query_dstring(database, ds);
      if(db_affected_rows(database)!=0){
              takeover=1;
      }

      /* put resources and units into cave */
      dstring_set(ds, "UPDATE " DB_TABLE_CAVE " SET ");

      for (i = 0; i < MAX_RESOURCE; ++i)
        dstring_append(ds, "%s%s = LEAST(%s + %d, %s)", i > 0 ? "," : "",
                       resource_type[i]->dbFieldName,
                       resource_type[i]->dbFieldName, 
                      (takeover==1)?resources[i] * TAKEOVER_RESOURCE_SAVE_PERCENTAGE / 100:resources[i],
                      function_to_sql(resource_type[i]->maxLevel));
      for (i = 0; i < MAX_UNIT; ++i){
        war_points_sender += ((struct Unit *)unit_type[i])->warpoints * units[i];
        dstring_append(ds, ",%s = %s + %d",
	               unit_type[i]->dbFieldName,
	               unit_type[i]->dbFieldName, units[i]);
        }
        if(relation1.relationType == RELATION_TYPE_PRE_WAR || relation1.relationType == RELATION_TYPE_WAR){
          war_points_update_verschieben(database, player1.tribe, player2.tribe, -1* war_points_sender);
        }
        dstring_append(ds, " WHERE caveID = %d", target_caveID);

	    db_query_dstring(database, ds);

      if (artefact > 0)
	put_artefact_into_cave(database, artefact, target_caveID);

      /* generate trade report and receipt for sender */
      trade_report(database, &cave1, &player1, &cave2, &player2,
		   resources, units, artefact);
      break;

    /**********************************************************************/
    /*** RUECKKEHR ********************************************************/
    /**********************************************************************/
    case RUECKKEHR:

      /* put resources into cave */
      ds = dstring_new("UPDATE " DB_TABLE_CAVE " SET ");

      for (i = 0; i < MAX_RESOURCE; ++i)
	dstring_append(ds, "%s%s = LEAST(%s + %d, %s)", i > 0 ? "," : "",
		resource_type[i]->dbFieldName,
		resource_type[i]->dbFieldName, resources[i],
		function_to_sql(resource_type[i]->maxLevel));

      for (i = 0; i < MAX_UNIT; ++i)
	dstring_append(ds, ",%s = %s + %d",
		 unit_type[i]->dbFieldName,
		 unit_type[i]->dbFieldName, units[i]);

      dstring_append(ds, " WHERE caveID = %d", target_caveID);

      db_query_dstring(database, ds);

      if (artefact > 0)
	put_artefact_into_cave(database, artefact, target_caveID);

      /* generate return report */
      return_report(database, &cave1, &player1, &cave2, &player2,
		    resources, units, artefact);
      break;

    /**********************************************************************/
    /*** ANGREIFEN ********************************************************/
    /**********************************************************************/
    case ANGREIFEN:

      /* beginner protection active in target cave? */
      if (cave_is_protected(&cave2))
      {
	   debug(DEBUG_BATTLE, "Is protected Cave");
	/* send remaining units back */
	ds = dstring_new("INSERT INTO Event_movement"
			 " (caveID, target_caveID, source_caveID, movementID,"
			 " speedFactor, start, end, artefactID");

	for (i = 0; i < MAX_RESOURCE; ++i)
	  dstring_append(ds, ",%s", resource_type[i]->dbFieldName);
	for (i = 0; i < MAX_UNIT; ++i)
	  dstring_append(ds, ",%s", unit_type[i]->dbFieldName);

	dstring_append(ds, ") VALUES (%d, %d, %d, %d, %s, '%s', '%s', %d",
		       source_caveID, source_caveID, target_caveID, RUECKKEHR,
		       speed_factor, return_start, return_end, artefact);

	for (i = 0; i < MAX_RESOURCE; ++i)
	  dstring_append(ds, ",%d", resources[i]);
	for (i = 0; i < MAX_UNIT; ++i)
	  dstring_append(ds, ",%d", units[i]);

	dstring_append(ds, ")");

	db_query_dstring(database, ds);
     debug(DEBUG_BATTLE,"End Handle Protected Cave attack");
	/* create and send reports */
	protected_report(database, &cave1, &player1, &cave2, &player2);
	break;
      }

      /* get relations between the two players' tribes */
      get_relation_info(database, player1.tribe, player2.tribe, &relation1);
      get_relation_info(database, player2.tribe, player1.tribe, &relation2);
      debug(DEBUG_BATTLE, "Relationtypes: %d and %d", relation1.relationType,
            relation2.relationType);

      battle = battle_create(1, 1);
      battle_bonus = get_battle_bonus();

      debug(DEBUG_BATTLE, "entering prepare_battle");
      /* prepare structs for battle, exceptions are uncaught! */
      prepare_battle(database,
		     battle,
		     &player1,
		     &player2,
		     &cave1, &cave2, battle_bonus, 0,
		     units, resources,
		     &artefact, &artefact_def,
		     &relation1, &relation2);

      /* calculate the fame */
      /* Calculatin is diferent if the battle was just pure farming*/
      isFarming = check_farming(database, cave2.artefacts, &player1,
			      &player2, &relation1);
      if( relation1.relationType == RELATION_TYPE_WAR){
	battle->isWar = 1;
      }

      /* calculate battle result */
      calcBattleResult(battle, &cave2, 0);

      /* change artefact ownership */
      debug(DEBUG_BATTLE, "entering change artefact");
      after_battle_change_artefact_ownership(
	database, battle->winner, &artefact, &artefact_id, &artefact_def,
	target_caveID, &cave2, &lostTo);

      /* attackers artefact (if any) is stored in variable artefact,
	 artefact_id is id of the artefact that changed owner (or 0) */

      /* no relation -> attacker get negative fame*/
      debug(DEBUG_BATTLE, "Relation Type %d",relation1.relationType);

      /* construct attacker update */
      debug(DEBUG_BATTLE, "entering attacker update");
      after_battle_attacker_update(database, player1.player_id, battle,
				   source_caveID, target_caveID, speed_factor,
				   return_start, return_end, artefact,
				   &relation1);

      /* defender update: exception still uncaught (better leave) */
      debug(DEBUG_BATTLE, "entering defender update");
      after_battle_defender_update(database, player2.player_id,
				   battle, target_caveID, &relation2);

 
      /* Farming update */
      if(isFarming){
        increaseFarming(database, player1.player_id);
      }

      /* reset DB_TABLE_CAVE_TAKEOVER */
      ds = dstring_new("UPDATE " DB_TABLE_CAVE_TAKEOVER " SET status = 0");

      for (i = 0; i < MAX_RESOURCE; ++i)
	dstring_append(ds, ",%s = 0", resource_type[i]->dbFieldName);

      dstring_append(ds, " WHERE caveID = %d AND playerID = %d",
		  target_caveID, cave1.player_id);

      db_query_dstring(database, ds);

      /* cave takeover by battle */
      if (battle->winner == FLAG_ATTACKER &&
	  ((struct Terrain *)terrain_type[cave2.terrain])->takeoverByCombat) {
	db_query(database, "UPDATE " DB_TABLE_CAVE " SET playerID = %d"
			   " WHERE caveID = %d",
		 cave1.player_id, target_caveID);

	db_query(database, "DELETE FROM Event_science WHERE caveID = %d",
		 target_caveID);

	science_update_caves(database, cave1.player_id);
      }
     //bodycount calculate
      attacker_lose = bodycount_calculate(battle, FLAG_DEFENDER);
      defender_lose = bodycount_calculate(battle, FLAG_ATTACKER);
      defender_va_lose = bodycount_va_calculate(battle);

      bodycount_update( database, player1.player_id, defender_lose);
      bodycount_update( database, player2.player_id, attacker_lose);
      if(relation1.relationType == RELATION_TYPE_PRE_WAR || relation1.relationType == RELATION_TYPE_WAR){
	war_points_show = 1;
        war_points_attacker = (defender_lose>10||defender_va_lose>5?war_points_calculate(battle,FLAG_ATTACKER):0);
        war_points_defender = (attacker_lose>10?war_points_calculate(battle,FLAG_DEFENDER):0);

        war_points_update(database, player1.tribe, player2.tribe, war_points_attacker, war_points_defender);
      }

      /* create and send reports */
      battle_report(database, &cave1, &player1, &cave2, &player2, battle,
		    artefact_id, lostTo, 0, 0, &relation1, &relation2,
		    war_points_show, war_points_attacker, war_points_defender);
      break;

    /**********************************************************************/
    /*** Spionieren *******************************************************/
    /**********************************************************************/
    case SPIONAGE:

      /* generate spy report */
      spy_result = spy_report(database, &cave1, &player1, &cave2, &player2,
			      resources, units, artefact);

      if (spy_result == 1)
      {
	/* send all units back */
	ds = dstring_new("INSERT INTO Event_movement"
			 " (caveID, target_caveID, source_caveID, movementID,"
			 " speedFactor, start, end, artefactID");

	for (i = 0; i < MAX_RESOURCE; ++i)
	  dstring_append(ds, ",%s", resource_type[i]->dbFieldName);
	for (i = 0; i < MAX_UNIT; ++i)
	  dstring_append(ds, ",%s", unit_type[i]->dbFieldName);

	dstring_append(ds, ") VALUES (%d, %d, %d, %d, %s, '%s', '%s', %d",
		source_caveID, source_caveID, target_caveID, RUECKKEHR,
		speed_factor, return_start, return_end, artefact);

	for (i = 0; i < MAX_RESOURCE; ++i)
	  dstring_append(ds, ",%d", resources[i]);
	for (i = 0; i < MAX_UNIT; ++i)
	  dstring_append(ds, ",%d", units[i]);

	dstring_append(ds, ")");

	db_query_dstring(database, ds);
      }
      else
      {
	/* send remaining units back */
	int count = 0;

	ds = dstring_new("INSERT INTO Event_movement"
			 " (caveID, target_caveID, source_caveID, movementID,"
			 " speedFactor, start, end, artefactID");

	for (i = 0; i < MAX_UNIT; ++i)
	  dstring_append(ds, ",%s", unit_type[i]->dbFieldName);

	dstring_append(ds, ") VALUES (%d, %d, %d, %d, %s, '%s', '%s', %d",
		source_caveID, source_caveID, target_caveID, RUECKKEHR,
		speed_factor, return_start, return_end, artefact);

	for (i = 0; i < MAX_UNIT; ++i)
	{
	  int num = units[i] * spy_result;

	  dstring_append(ds, ",%d", num);
	  count += num;
      body_count += units[i] - num;
	}

	dstring_append(ds, ")");

	if (count)
	  db_query_dstring(database, ds);

	/* put resources into cave */
	ds = dstring_new("UPDATE " DB_TABLE_CAVE " SET ");

	for (i = 0; i < MAX_RESOURCE; ++i)
	  dstring_append(ds, "%s%s = LEAST(%s + %d, %s)", i > 0 ? "," : "",
		  resource_type[i]->dbFieldName,
		  resource_type[i]->dbFieldName, resources[i],
		  function_to_sql(resource_type[i]->maxLevel));

	dstring_append(ds, " WHERE caveID = %d", target_caveID);

	db_query_dstring(database, ds);

	if (artefact > 0)
	  put_artefact_into_cave(database, artefact, target_caveID);
      }
      bodycount_update(database, player2.player_id, body_count);
      break;

    /**********************************************************************/
    /*** UEBERNEHMEN ******************************************************/
    /**********************************************************************/
    case TAKEOVER:

      /* secure or protected target gave? */
      if (cave2.secure || cave_is_protected(&cave2))
      {
	/* send remaining units back */
	ds = dstring_new("INSERT INTO Event_movement"
			 " (caveID, target_caveID, source_caveID, movementID,"
			 " speedFactor, start, end, artefactID");

	for (i = 0; i < MAX_RESOURCE; ++i)
	  dstring_append(ds, ",%s", resource_type[i]->dbFieldName);
	for (i = 0; i < MAX_UNIT; ++i)
	  dstring_append(ds, ",%s", unit_type[i]->dbFieldName);

	dstring_append(ds, ") VALUES (%d, %d, %d, %d, %s, '%s', '%s', %d",
		       source_caveID, source_caveID, target_caveID, RUECKKEHR,
		       speed_factor, return_start, return_end, artefact);

	for (i = 0; i < MAX_RESOURCE; ++i)
	  dstring_append(ds, ",%d", resources[i]);
	for (i = 0; i < MAX_UNIT; ++i)
	  dstring_append(ds, ",%d", units[i]);

	dstring_append(ds, ")");

	db_query_dstring(database, ds);

	/* create and send reports */
	/* FIXME use different message in report (protected -> secure) */
	protected_report(database, &cave1, &player1, &cave2, &player2);
	break;
      }

      get_relation_info(database, player1.tribe, player2.tribe, &relation1);
      get_relation_info(database, player2.tribe, player1.tribe, &relation2);

      battle = battle_create(1, 1);

      battle_bonus = get_battle_bonus();
      takeover_multiplier = get_takeover_multiplier(&cave2);

      /* prepare structs for battle, exceptions are uncaught! */
      prepare_battle(database,
		     battle,
		     &player1,
		     &player2,
		     &cave1, &cave2, battle_bonus, takeover_multiplier,
		     units, resources,
		     &artefact, &artefact_def,
		     &relation1, &relation2);

      /* calculate battle result */
      /*bei ner übernahme kein resi klau möglich*/
      calcBattleResult(battle, &cave2, 1);

      /* change artefact ownership */
      after_battle_change_artefact_ownership(
        database, battle->winner, &artefact, &artefact_id, &artefact_def,
	    target_caveID, &cave2, &lostTo);

      /* attackers artefact (if any) is stored in variable artefact,
	     artefact_id is id of the artefact that changed owner (or 0) */

      /* defender update: exception still uncaught (better leave) */
      after_battle_defender_update(database, player2.player_id,
				   battle, target_caveID, &relation2);


     int war1 = get_tribe_at_war(database,player1.tribe);
     int war2 = get_tribe_at_war(database,player2.tribe);

      /* attacker won:  put survivors into cave, change owner
       * attacker lost: send back survivors */
      change_owner =
          battle->winner == FLAG_ATTACKER && cave2.player_id != PLAYER_SYSTEM &&
          player1.max_caves > get_number_of_caves(database, player1.player_id) &&
          ((relation1.relationType == RELATION_TYPE_WAR &&
 	    relation2.relationType == RELATION_TYPE_WAR) ||
	    (!war1 && !war2) ||
           (strcasecmp(player1.tribe, player2.tribe) == 0)); // Spieler im selben stamm
     //bodycount calculate
      attacker_lose = bodycount_calculate(battle, FLAG_DEFENDER);
      defender_lose = bodycount_calculate(battle, FLAG_ATTACKER);
      defender_va_lose = bodycount_va_calculate(battle);

      bodycount_update( database, player1.player_id, defender_lose);
      bodycount_update( database, player2.player_id, attacker_lose);
      if(relation1.relationType == RELATION_TYPE_PRE_WAR || relation1.relationType == RELATION_TYPE_WAR){
        war_points_show = 1;
        war_points_attacker = (defender_lose>10||defender_va_lose>5?war_points_calculate(battle,FLAG_ATTACKER):0);
        war_points_defender = (attacker_lose>10?war_points_calculate(battle,FLAG_DEFENDER):0);

      }

      if (change_owner){
        debug(DEBUG_TAKEOVER, "change owner of cave %d to new owner %d",
	          target_caveID, cave1.player_id);
        takeover_cave(database, target_caveID, cave1.player_id,return_start);
        after_takeover_attacker_update(database, player1.player_id,
		                               battle, target_caveID,
                                       artefact, &relation1);
        if(relation1.relationType == RELATION_TYPE_PRE_WAR || relation1.relationType == RELATION_TYPE_WAR){
          war_points_attacker += WAR_POINTS_FOR_TAKEOVER;
        }
      } else { /* send survivors back */
        debug(DEBUG_TAKEOVER, "send back attacker's suvivors");
        after_battle_attacker_update(database, player1.player_id, battle,
				     source_caveID, target_caveID, speed_factor,
				     return_start, return_end, artefact,
				     &relation1);
      }
      if(relation1.relationType == RELATION_TYPE_PRE_WAR || relation1.relationType == RELATION_TYPE_WAR){
        war_points_update(database, player1.tribe, player2.tribe, war_points_attacker, war_points_defender);
      }

      /* create and send reports */
      battle_report(database, &cave1, &player1, &cave2, &player2, battle,
		    artefact_id, lostTo, change_owner, 1 + takeover_multiplier,
		    &relation1, &relation2,war_points_show, war_points_attacker, war_points_defender);
     //bodycount calculate


      bodycount_update( database, player1.player_id, defender_lose);
      bodycount_update( database, player2.player_id, attacker_lose);
      break;

    default:
      throw(BAD_ARGUMENT_EXCEPTION, "movement_handler: unknown movementID");
  }

  /**********************************************************************/
  /*** END OF THE INFAMOUS GIANT SWITCH *********************************/
  /**********************************************************************/

  debug(DEBUG_TICKER, "leaving function movement_handler()");
}
Exemple #7
0
int do_worker_job(void)
{
    if (settings.shift_table_type == TABLE_NO_SHIFT)
    {
        create_new_tables();
    }

    struct worker *worker = &settings.workers[settings.worker_id];

    while (true)
    {
        worker_looper();

        char     *sql;
        uint32_t length;
        int      ret;
        bool     empty = false;

        ret = queue_pop(&worker->queue, (void **)&sql, &length);
        if (ret == -1)
        {
            empty = true;
        }
        else if (ret < 0)
        {
            log_error("queue_pop error: %d", ret);

            empty = true;
        }

        if (empty)
        {
            ret = queue_pop(&settings.cache_queue, (void **)&sql, &length);

            if (ret >= 0)
            {
                empty = false;
            }
            else if (ret < -1)
            {
                log_error("queue_pop error: %d", ret);
            }
        }

        if (empty)
        {
            wait_for_notify(worker);

            continue;
        }

        log_debug("worker: %d, sql: %s", settings.worker_id, sql);

        bool is_insert = true;
        if (strncmp(sql, "INSERT", 6) != 0)
            is_insert = false;

        ret = db_safe_query(sql, length - 1);

        if (ret < 0)
        {
            if (is_insert)
            {
                log_error("worker: %d, insert fail: %s", \
                        settings.worker_id, db_error());

                ++insert_db_fail_count;
            }
            else
            {
                log_error("worker: %d, exec sql: %s fail: %s", \
                        settings.worker_id, sql, db_error());

                ++exec_sql_fail_count;
            }

            if (ret < -1 || (ret == -1 && \
                        queue_push(&settings.cache_queue, sql, length) < 0))
            {
                if (is_insert)
                {
                    dlog(settings.fail_insert_log, "%s;", sql);
                }
            }

            if (ret == -1)
            {
                usleep(WORKER_BAD_CONN_USLEEP_TIME);
            }
        }
        else
        {
            if (is_insert)
            {
                int rows = db_affected_rows();
                insert_db_succ_count += rows;
            }
            else
            {
                exec_sql_succ_count += 1;
            }
        }
    }

    return 0;
}