-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.php
91 lines (85 loc) · 2.61 KB
/
result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
session_start();
session_regenerate_id(true);
require_once __DIR__ . '/lib/common.php';
require_once __DIR__ . '/lib/db.php';
require_once __DIR__ . '/lib/session.php';
require_once __DIR__ . '/lib/janken.php';
$userId = getSessionUserId();
$userName = getSessionUserName();
$winCount = getSessionWinCount();
$lossCount = getSessionLossCount();
$playerHand = JANKEN_HAND_INVALID;
// POSTデータを取得する
$post = h($_POST);
// csrfトークンのチェック
if (!isset($post['csrf_token']) ||
!compareSessionCsrfToken($post['csrf_token'])) {
// csrfトークンエラー
csrfTokenErrorHTML('.');
exit();
} else {
// csrfトークンを削除する
deleteSessionCsrfToken();
}
// 設定されていれば上書きする
if (isset($post['hand']) && is_numeric($post['hand'])) {
$playerHand = intval($post['hand']);
}
// コンピュータの手を取得
$comHand = getRandomJankenHand();
if (!isRightJankenHand($playerHand) ||
!isRightJankenHand($comHand)) {
// じゃんけんの手が異常
$result = '不正な手です。<br>';
} else {
// じゃんけんの手が正常
// じゃんけんの勝敗を判定
$judge = judgeJankenHand($playerHand, $comHand);
if ($judge == JANKEN_JUDGE_WIN) {
$winCount++;
if ($winCount < 0) $winCount = 0;
setSessionWinCount($winCount);
$result = <<< EOM
{$userName}さんの勝ち。<br>
戦績:{$winCount}勝{$lossCount}敗<br>
EOM;
} elseif ($judge == JANKEN_JUDGE_LOSS) {
$lossCount++;
if ($lossCount < 0) $lossCount = 0;
setSessionLossCount($lossCount);
$result = <<< EOM
{$userName}さんの負け。<br>
戦績:{$winCount}勝{$lossCount}敗<br>
EOM;
} else {
$result = <<< EOM
あいこです。<br>
もう一度、じゃんけんの手を選択してください。<br>
EOM;
}
if (isUserLogin() &&
($judge == JANKEN_JUDGE_WIN || $judge == JANKEN_JUDGE_LOSS)) {
// 勝敗についてデータベースを更新
dbEditResultData($userId, $winCount, $lossCount);
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>じゃんけん結果</title>
</head>
<body>
<h1>じゃんけんの結果</h1>
<?= $result ?>
<br>
<?php showJankenHand($playerHand, $comHand) ?>
<br>
<a href="select.php">じゃんけんの手の選択へ</a><br>
<br>
<a href="end.php">じゃんけんをやめる</a><br>
</body>
</html>