2009-02-01 Ajax を使ってじゃんけんをする - データベースを使って勝敗の履歴を取る ================================================================================ じゃんけんの手の文字のところにマウスを置くと、コンピューターと勝負するようにして ください。なお、コンピューター側は、乱数を使います。 じゃんけんの履歴を取り、そのセッション中の過去の勝敗が表示できるようにしてください。 また、セッションを終了したときでも、続きができるようにしてください。 さらに、結果をクリアできるようにしてください。 ■Janken.html --- XMLHttpRequest じゃんけんの手の文字のところにマウスを置いてください。
グー チョキ パー     クリア

--- ■Janken.php --- user = $user->__toString(); $play->computer = $computer->__toString(); $play->result = $user->play($computer); try { // データベースと接続する。 $connection = JankenDao::connect("root", "", "symfony"); // 現在の勝敗数を取得する。 $play->count = JankenDao::getWinAndLoseCount($_SERVER['REMOTE_ADDR'], $connection); // 勝敗結果をカウントする。 $count = new WinAndLose(); $count->win = 0; $count->draw = 0; $count->lose = 0; if ($play->result == "勝ち") { $play->count->win++; $count->win = 1; } else if ($play->result == "引分") { $play->count->draw++; $count->draw = 1; } else { $play->count->lose++; $count->lose = 1; } // 勝敗結果をデータベースに記録する。 JankenDao::recordWinAndLose($_SERVER['REMOTE_ADDR'], $count, $connection); // データベースを切断する。 $connection->disconnect(); } catch (Exception $e) { // 例外が起きた場合、メッセージを組み立てる。 $play->message->level = 1; $play->message->content = $e->getMessage(); } // レスポンスをエンコードする。 echo json_encode($play); ?> --- ■JankenClear.php --- disconnect(); // 成功したことのメッセージの組み立て $message->level = 0; $message->content = "結果をクリアしました。"; } catch (Exception $e) { // 例外が起きた場合、メッセージを組み立てる。 $message->level = 1; $message->content = $e->getMessage(); } // レスポンスをエンコードする。 echo json_encode($message); ?> --- ■Message.class.php --- --- ■WinAndLose.class.php --- --- ■JankenDao.class.php --- getMessage()); } return $connection; } /** * 現在の勝敗数を取得する。 * @param $host * @return 勝敗数 */ public static function getWinAndLoseCount($host, $connection) { $sql = "SELECT coalesce(sum(win), 0) AS win, coalesce(sum(draw), 0) AS draw, coalesce(sum(lose), 0) AS lose FROM janken WHERE host = '{$host}'"; $res = $connection->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); } $row = $res->fetchRow(DB_FETCHMODE_ASSOC); $count = new WinAndLose(); $count->win = $row['win']; $count->draw = $row['draw']; $count->lose = $row['lose']; return $count; } /** * 勝敗結果を記録する。 * @param $host * @param $win * @param $draw * @param $lose * @param $connection * @return unknown_type */ public static function recordWinAndLose($host, $count, $connection) { $sql = "INSERT INTO janken VALUES('{$host}', $count->win, $count->draw, $count->lose)"; $res = $connection->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); } } /** * ホストで指定された結果表のデータをクリアする。 * @param $host * @param $connection * @return unknown_type */ public static function clearBy($host, $connection) { $sql = "DELETE FROM janken WHERE host = '{$host}'"; $res = $connection->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); } } /** * 結果表のデータをクリアする。 * @param $connection * @return unknown_type */ public static function clear($connection) { $sql = "TRUNCATE janken "; $res = $connection->query($sql); if (PEAR::isError($res)) { throw new Exception($res->getMessage()); } } } ?> --- ■createJankenTable.sql --- -- createJankenTable.sql create table janken ( host varchar(255) , win int, draw int, lose int ); --- 以上