예제 #1
0
파일: artefact.c 프로젝트: microlefes/Game
/*
 * Retrieve artefact_class for the given id.
 */
void get_artefact_class_by_artefact_id (db_t *database, int artefactID,
             struct Artefact_class *artefact_class)
{
  db_result_t *result;
  dstring_t *ds;

  ds = dstring_new("SELECT ac.* FROM " DB_TABLE_ARTEFACT_CLASS
                    " ac LEFT JOIN " DB_TABLE_ARTEFACT
                    " a ON ac.ArtefactClassID = a.ArtefactClassID "
                    " WHERE a.artefactID = %d "
                    " AND caveID = 0 "
                    " AND initiated = %d", artefactID, ARTEFACT_UNINITIATED);
  result = db_query_dstring(database, ds);
  debug(DEBUG_ARTEFACT, "get_artefact_class_by_artefact_id: %s", dstring_str(ds));

  /* Bedingung: ArtefaktID muss vorhanden sein */
  if (db_result_num_rows(result) != 1)
    throw(SQL_EXCEPTION, "get_artefact_class_by_artefact_id: no such artefactID");

  db_result_next_row(result);

  artefact_class->artefactClassID       = db_result_get_int(result, "artefactClassID");
  artefact_class->name                  = db_result_get_string(result, "name");
  artefact_class->name_initiated        = db_result_get_string(result, "name_initiated");
  artefact_class->resref                = db_result_get_string(result, "resref");
  artefact_class->description           = db_result_get_string(result, "description");
  artefact_class->description_initiated = db_result_get_string(result, "description_initiated");
  artefact_class->initiationID          = db_result_get_int(result, "initiationID");
  artefact_class->noZeroFood            = db_result_get_int(result, "noZeroFood");
  artefact_class->destroyOnMove         = db_result_get_int(result, "destroyOnMove");
  get_effect_list(result, artefact_class->effect);
}
예제 #2
0
파일: cave.c 프로젝트: zu-gabe/Game
/*
 * Retrieve cave table information for the given cave id.
 */
void get_cave_info (db_t *database, int cave_id, struct Cave *cave)
{
  db_result_t *result = db_query(database,
	"SELECT * FROM " DB_TABLE_CAVE " WHERE caveID = %d", cave_id);

  if (!db_result_next_row(result))
    throwf(SQL_EXCEPTION, "get_cave_info: cave %d not found", cave_id);

  cave->result = result;
  cave->cave_id = cave_id;
  cave->xpos = db_result_get_int(result, "xCoord");
  cave->ypos = db_result_get_int(result, "yCoord");
  cave->name = db_result_get_string(result, "name");
  cave->player_id = db_result_get_int(result, "playerID");
  cave->terrain = db_result_get_int(result, "terrain");
  cave->takeoverable = db_result_get_int(result, "takeoverable");
  cave->artefacts = db_result_get_int(result, "artefacts");
  cave->heroID = db_result_get_int(result, "hero");
  cave->monster_id = db_result_get_int(result, "monsterID");
  cave->secure = db_result_get_int(result, "secureCave");
  cave->protect_end = db_result_get_time(result, "protection_end");
  get_resource_list(result, cave->resource);
  get_building_list(result, cave->building);
  get_science_list(result, cave->science);
  get_defense_system_list(result, cave->defense_system);
  get_unit_list(result, cave->unit);
  get_effect_list(result, cave->effect);
}
예제 #3
0
파일: cave.c 프로젝트: zu-gabe/Game
/*
 * Retrieve player table information for the given player id.
 */
void get_player_info (db_t *database, int player_id, struct Player *player)
{
  db_result_t *result = db_query(database,
	"SELECT * FROM " DB_TABLE_PLAYER " WHERE playerID = %d", player_id);

  if (!db_result_next_row(result))
    throwf(SQL_EXCEPTION, "get_player_info: player %d not found", player_id);

  player->player_id = player_id;
  player->name = db_result_get_string(result, "name");
  player->tribe = db_result_get_string(result, "tribe");
  player->max_caves = db_result_get_int(result, "takeover_max_caves");
  player->locale = db_result_get_string(result, "language");
  player->locale_id = get_locale_id(player->locale);
  get_science_list(result, player->science);
}
예제 #4
0
파일: cave.c 프로젝트: zu-gabe/Game
/*
 * Retrieve monster table information for the given monster id.
 */
void get_monster_info (db_t *database, int monster_id, struct Monster *monster)
{
  db_result_t *result = db_query(database,
	"SELECT * FROM Monster WHERE monsterID = %d", monster_id);

  if (!db_result_next_row(result))
    throwf(SQL_EXCEPTION, "get_monster_info: monster %d not found", monster_id);

  monster->monster_id = monster_id;
  monster->name = db_result_get_string(result, "name");
  monster->attack = db_result_get_int(result, "angriff");
  monster->defense = db_result_get_int(result, "verteidigung");
  monster->mental = db_result_get_int(result, "mental");
  monster->strength = db_result_get_int(result, "koerperkraft");
  monster->exp_value = db_result_get_int(result, "erfahrung");
  monster->attributes = db_result_get_string(result, "eigenschaft");
}
예제 #5
0
/*
 * Retrieve artefact_class for the given id.
 */
void get_artefact_class_by_id (db_t *database, int artefactClassID,
			       struct Artefact_class *artefact_class)
{
  db_result_t *result =
    db_query(database, "SELECT * FROM Artefact_class"
		       " WHERE artefactClassID = %d", artefactClassID);

  /* Bedingung: Artefaktklasse muss vorhanden sein */
  if (db_result_num_rows(result) != 1)
    throw(SQL_EXCEPTION, "get_artefact_class_by_id: no such artefactClassID");

  db_result_next_row(result);

  artefact_class->artefactClassID       = artefactClassID;
  artefact_class->name                  = db_result_get_string(result, "name");
  artefact_class->resref                = db_result_get_string(result, "resref");
  artefact_class->description           = db_result_get_string(result, "description");
  artefact_class->description_initiated = db_result_get_string(result, "description_initiated");
  artefact_class->initiationID          = db_result_get_int(result, "initiationID");
  get_effect_list(result, artefact_class->effect);
}
예제 #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()");
}
예제 #7
0
파일: ticker.c 프로젝트: microlefes/Game
/*
 * The event scheduler and resource ticker.
 */
static void run_ticker (db_t *database)
{
  time_t last = time(NULL);
  int events = 0, sleeps = 0;

  debug(DEBUG_TICKER, "running");

  while (!finish) {
    /* set up memory pool */
    struct memory_pool *pool = memory_pool_new();
    time_t now = time(NULL);
    int secs = now - last;

    if (secs >= TICKER_LOADAVG_TIME)
    {
	debug(DEBUG_TICKER, "ticker load: %.2f (%d events/min)",
	      1 - sleep_time / 1000000.0 * sleeps / secs, 60 * events / secs);

	events = sleeps = 0;
	last = now;
#ifdef DEBUG_MALLOC
	CHECK_LEAKS();
#endif
    }

    if (reload)
    {
	debug(DEBUG_TICKER, "reload config");
	reload = 0;

	/* read config file */
	config_read_file(config_file);
	fetch_config_values();
	log_handler_set_file(debug_log, debug_logfile);
	log_handler_set_file(error_log, error_logfile);
	log_handler_set_file(msg_log, msg_logfile);
    }

    try {
      char resource_timestamp[TIMESTAMP_LEN];
      char timestamp[TIMESTAMP_LEN];
      db_result_t *next_result = NULL;
      const char *next_timestamp =
	make_timestamp_gm(resource_timestamp, tick_next_event());
      const char *next_db_eventID;
      int next_eventType;
      int i;

      /* check each event queue to find the next event to process */
      debug(DEBUG_EVENTS, "start loop, next resource event");

      for (i = 0; i < eventTableSize; ++i)
      {
	db_result_t *result;

	/* get only the next non-blocked event, if its timestamp
	   is smaller than the smallest found timestamp */
	debug(DEBUG_EVENTS, "query event table: %s", eventTableList[i].table);
//	debug(DEBUG_TICKER, "query event table: %s", eventTableList[i].table);

	result = db_query(database,
		    "SELECT * FROM %s WHERE blocked = 0 AND end < '%s' "
		    "ORDER BY end ASC, %s ASC LIMIT 0,1",
		    eventTableList[i].table, next_timestamp,
		    eventTableList[i].id_field);

	if (db_result_num_rows(result))	/* is there an earlier event? */
	{
	  /* extract this earlier event's needed data */
	  db_result_next_row(result);

	  next_result = result;		/* remember the earlier one */
	  next_timestamp = db_result_get_string(result, "end");
	  next_db_eventID =
	    db_result_get_string(result, eventTableList[i].id_field);
	  next_eventType = i;
	}
      }

      if (strcmp(next_timestamp, make_timestamp_gm(timestamp, time(NULL))) > 0)
      {
	debug(DEBUG_EVENTS, "no event pending, sleep");
	++sleeps;
	usleep(sleep_time);
      }
      else
      {
	debug(DEBUG_TICKER, "event: scheduled %s, now %s",
	      next_timestamp, timestamp);

	/* check which handler to call (resource ticker or event handler) */
	if (next_result)
	{
	  /* found an event in the event tables: block the event */
	  debug(DEBUG_EVENTS, "block event: %s", next_db_eventID);
	  ++events;

	  db_query(database, "UPDATE %s SET blocked = 1 WHERE %s = %s",
			  eventTableList[next_eventType].table,
			  eventTableList[next_eventType].id_field,
			  next_db_eventID);

	  /* call handler and delete event */
	  eventTableList[next_eventType].handler(database, next_result);

	  debug(DEBUG_EVENTS_DELETE, "delete event: %s", next_db_eventID);

	  db_query(database, "DELETE FROM %s WHERE %s = %s",
			  eventTableList[next_eventType].table,
			  eventTableList[next_eventType].id_field,
			  next_db_eventID);
	}
	else
	{
	  /* next event is resource tick: call resource ticker */
	  debug(DEBUG_TICKER, "resource tick %s", resource_timestamp);

	  resource_ticker(database, tick_advance());
	  debug(DEBUG_TICKER, "resource tick ended");
	  tick_log();			/* log last successful update */
	}
      }
    } catch (BAD_ARGUMENT_EXCEPTION) {
      warning("%s", except_msg);
    } catch (SQL_EXCEPTION) {
      warning("%s", except_msg);
    } catch (GENERIC_EXCEPTION) {
      warning("%s", except_msg);
    } catch (DB_EXCEPTION) {
      block_ticker(except_msg);
    } catch (NULL) {
      error("%s", except_msg);
    } end_try;

    memory_pool_free(pool);
  }

  debug(DEBUG_TICKER, "end");
}