-
Notifications
You must be signed in to change notification settings - Fork 0
/
janken.php
114 lines (107 loc) · 3.55 KB
/
janken.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
$db = new PDO('sqlite:' . __DIR__ . '/ranking.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!empty($_POST['username'])) {
$username = mb_substr($db->quote($_POST['username']), 0, 50);
mb_regex_encoding("UTF-8");
if (!preg_match("/[^ぁ-んァ-ンーa-zA-Z0-9一-龠0-9\-\r]+/u", $username)) {
return;
}
$count = (int) $_POST['count'];
$query = sprintf("insert into ranking values(%s, %d)", $username, $count);
try {
$db->exec($query);
} catch(Exception $e) {
var_dump($e);
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ジャンケンファイト on WEB!</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<style>
#battle, #register {
display: none;
}
</style>
</head>
<body>
<div class="container">
<h1>ジャンケンファイト! on WEB</h1>
<p><img src="hikakin.jpg" alt="ブンブンジャンケン"></p>
<p>君は何連勝できるかな!?ランキングで競い合おう!</p>
<form id="battle">
<h2 id="message"></h2>
<p><span id="username"></span>さんの連勝記録: <span id="count"></span> 回</p>
<input class="btn btn-primary" type="submit" value="グー" />
<input class="btn btn-success" type="submit" value="チョキ" />
<input class="btn btn-danger" type="submit" value="パー" />
</form>
<form id="start">
<h2>あなたの名前:<input type="text" id="input-username"></h2>
<input class="btn btn-primary" type="submit" name="start" value="スタート!">
</form>
<h2>連勝者ランキング</h2>
<form id="register" action="janken.php" method="POST">
<input class="btn btn-primary" name="register" type="submit" value="ランキング登録!">
</form>
<table class="table table-striped">
<tr>
<th>順位</th>
<th>ユーザー名</th>
<th>連勝数</th>
</tr>
<?php foreach($db->query('select * from ranking order by `count` desc') as $i => $row) : ?>
<tr>
<td><?php echo ($i + 1) ?></td>
<td><?php echo htmlspecialchars($row['username'], ENT_QUOTES, 'UTF-8') ?></td>
<td><?php echo number_format($row['count']) ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script>
$(function() {
$('#start').submit(function() {
var username = $('#input-username').val();
$('#username').text(username);
$('#count').text('0');
$('#start').hide();
$('#battle').show();
$('#register').show();
return false;
});
$('#battle').submit(function() {
var rand = Math.floor(Math.random() * 3);
var message = '';
var count = parseInt($('#count').text());
if(rand == 0) {
message = 'あなたの勝ち!';
count += 1;
} else if (rand == 1) {
message = 'あなたの負け...';
count = 0;
} else {
message = 'あいこです';
count = 0;
}
$('#message').text(message);
$('#count').text(count.toString());
return false;
});
$('#register').submit(function() {
$.post('janken.php', {username: $('#username').text(), count: $('#count').text()})
.done(function() {
location.reload();
});
return false;
});
});
</script>
</body>
</html>