部屋IDをチェックするforループがあるphpゲームで使用される関数があります。基本的に、for ループは 2 回実行する必要があるときに 1 回だけ実行されます。以下のコード ブロックは、($i を使用した最初の for ループ) を示しています。
// successful sql login & $player is correctly set with '', and all below sql
// statements execute correctly. $count is 2
function setMonstersLocation($player,$rooms_id){
if (mysql_result(mysql_query("SELECT state FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL"),0) == 0){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT monsters_id FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300 ORDER BY monsters_id");
for ($i = 0; $i < $count; $i+= 1) {
$sql = mysql_query("SELECT monsters_id FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300 ORDER BY monsters_id");
$result = mysql_result($sql,$i);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id=$result AND quantity = 1"),0) == 1){
$final = -1;
$array[1] = $rooms_id - 1;
$array[2] = $rooms_id + 1;
$array[3] = $rooms_id - 100;
$array[4] = $rooms_id + 100;
$array[5] = $rooms_id - 200;
$array[6] = $rooms_id - 101;
$array[7] = $rooms_id - 99;
$array[8] = $rooms_id - 2;
$array[9] = $rooms_id + 200;
$array[10] = $rooms_id + 101;
$array[11] = $rooms_id + 99;
$array[12] = $rooms_id + 2;
if ($result < 200){
for ($j = 1; $j < 5; $j+=1){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id={$array[$j]}");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT COUNT(*) FROM rooms WHERE (rooms_id={$array[$j]})");
$roompresent = mysql_result($sql,0);
if ($count == 0 and $final == -1 and $roompresent == 1){
$final = $array[$j];
}
}
} else if ($result > 200){
for ($k = 1; $k < 13; $k+=1){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE (player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id={$array[$k]})");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT COUNT(*) FROM rooms WHERE (rooms_id={$array[$k]})");
$roompresent = mysql_result($sql,0);
if ($count == 0 and $final == -1 and $roompresent == 1){ $final = $array[$k]; }
}
if ($final == -1){
$sql = mysql_query("SELECT COUNT(*) FROM rooms");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT rooms_id FROM rooms");
for ($l = 0; $l < $count; $l+=1){
$rooms = mysql_query($sql,$l);
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id=$rooms");
if (mysql_result($sql,0) == 0 and $final == -1){ $final = $rooms; }
}
}
}
if ($final != -1){ $sql = mysql_query("UPDATE present SET rooms_id=$final WHERE player = $player AND items_id IS NULL AND monsters_id=$result;"); }
else { $sql = mysql_query("UPDATE present SET rooms_id=-1 WHERE player = $player AND items_id IS NULL AND monsters_id=$result");}
}
}
$sql = mysql_query("UPDATE present SET state = 1 WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL");
} else {
$sql = mysql_query("UPDATE present SET state = 0 WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL");
}
}
$count が 2 の場合、for ループは 1 回だけ実行されます。for ループが実行されるたびに mysql テーブルの値に 1 を追加して、これを確認しました。値は 1 回だけ増加します。
上部の if ステートメントは、1 ティックおきに関数を実行することです。