<?php
//
// BBWA
// score.php
// スコアを出力・更新する
// Teppei KAKU <teppei@sat.co.jp>
//

include("variables.php");
include("functions.php");

if ($_GET['vote'] == "g" || $_GET['vote'] == "b") {
  // インデックスを修正
  $oldList = file("${dirName}INDEX");
  $filePtr = fopen("${dirName}INDEX", "w");
  while (!flock($filePtr, LOCK_EX)) {};
  foreach ($oldList as $listText) {
    $listText = trim($listText);
    if (!strstr($listText, $_GET['img'])) {
      fwrite($filePtr, "${listText}\n");
    } else {
      $piece = explode("|||", $listText);
    }
  }
  if ($_GET['vote'] == "g") {
    $piece[2]++;
  }
  $piece[3]++;
  $listText = "${piece[0]}|||${piece[1]}|||${piece[2]}|||${piece[3]}\n";
  fwrite($filePtr, $listText);
  flock($filePtr, LOCK_UN);
  fclose($filePtr);

  printHtml("スコアを投票しました",
    "right.php?img=${_GET['img']}", "SCO");
} else {
  // スコアをグラフ化する
  $sim = imagecreate(100, 8);
  $blue = imagecolorallocate($sim, 100, 100, 255);
  $gray1 = imagecolorallocate($sim, 200, 200, 200);
  $gray2 = imagecolorallocate($sim, 100, 100, 100);

  imagefilledrectangle($sim, 0, 0, $_GET['score'], 7, $blue);
  imagefilledrectangle($sim, ($_GET['score'] + 1), 0, 99, 7, $gray1);
  imagerectangle($sim, 0, 0, 99, 7, $gray2);
  imageline($sim, $_GET['score'], 0, $_GET['score'], 7, $gray2);

  imagepng($sim);
}
?>
