ใใฎ่จไบใฏ ้ใฎ้ญ่กใซๅฏพใใ้ฒ่ก่ก Advent Calendar 2020 ใฎ24ๆฅ็ฎใฎ่จไบใงใใ
ๆๆฅใๆ็ตๆฅใฏใซใฌใณใใผใฎไฝๆ่ ใงใใใ @nfujita55a ใใใฎ ใใ็ฅใใชใใขใใชใฑใผใทใงใณใฎๆง่ฝใจๆฆใใชใใจใใใชใใจใใฎ้ฒ่ก่ก๏ผๅพ็ทจ๏ผ ใงใ๏ผ๏ผ
THE HOUSE OF THE DEAD CODE
ๆจ็คบใใใrubyใฎใณใผใใใใ ใใใใณใผใใจใชใฃใฆใใ Am I dead?
้จๅใใๅถ้ๆ้ๅ
ใซๅ
จใฆใฏใชใใฏ ใใฆใใ ใใใ
ใณใผใใฏใruby 2.7.2 ใง ruby
ใณใใณใใง่ตทๅ ใใใใใฎใจใใพใใ
See the Pen HODCใRubyใ by j5c8k6m8 (@j5c8k6m8) on CodePen.
็งใฎ็ฐๅขใงใฏCodePenใฎๅใ่พผใฟใๅใใพใใใงใใใCodePenใฎๅใ่พผใฟใๅใใชใๅ ดๅใฏใไธ่จใฎใชใณใฏใใๅ็
งใใฆใใ ใใ
https://codepen.io/j5c8k6m8/full/PoGJxmE
ไฝๆ็ต็ทฏใชใฉ
้ใฎ้ญ่กใซๅฏพใใ้ฒ่ก่ก Advent Calendar 2020 ใฎ๏ผๆฅ็ฎใฎ่จไบ DEAD CODE COOKBOOK ~ใใใใณใผใใฎไฝใๆนใจๅพฉๆดปใฎๅชๆ~ ใฎ็ดนไป ๅท็ญๆใซใTHE HOUSE OF THE DEAD CODE ใจใใใฟใคใใซใๆใไปใใใฎใงใๅขใใง24ๆฅ็ฎใซใจใณใใชใผใใพใใใ
ๅฝๅใฏใๆงใ ใชใใใใณใผใใๅซใใ ใณใผใใๅ็ดใซๆฒ่ผใใใใจ่ใใฆใใพใใใใ The Typing of the Dead ใฎใใใชใฒใผใ ่ฆ็ด ใๅ ฅใใฆใๆฅฝใใใใชใผ๏ผใจๆใใชใใใไฝใใใใซใใใฃใจใใ้ใซ๏ผๆฅๅใจใชใฃใฆใใพใใพใใ
ใจใซใใๅฝขใซใใใ๏ผใใ ใใณใผใใฎใใคใฉใคใใ ใใฏๆญปๅฎใใใใจ่ใใCDNใงใฎ prism.js ๅฉ็จใๅๆใจใใฆ่ใใพใใใ
ๆ้ใใชใใใCDNใ ใใใใคใฉใคใๅคๆๅพใฎDOMๆไฝๅฟ
่ฆใ ใใ ใชใฏใณใณใฎ jQuery ไฝฟใฃใกใใ๏ผใจใใฉใคใใฉใชใฏใทใณใใซใซ prism.js + jQuery ใ ใใงไฝใใพใใใ
็ตๆใใ4ๆ้็จๅบฆใงไฝใใใใjsใฏ86่กใ ใใใฏใคใบใจใใฆใๆฅฝใใใใใพใใใใใชใๆๅญๅใซ "
ไฝฟใ่จ่ชใชใ HTML ๆธใใ ใใง็ฐกๅใซๅ้กไฝใใใใใ GitHub ใซๅ
ฅใใฆใใใใใใจใใ้ฉๅฝใณใผใใงใใ
ใฒใผใ ้จๅใฎใณใผใใ่ฆใ
setTimeout(() => {
const pages = {};
let current_page, current_zombies;
let current_id = 0;
const start = i => {
current_page = i;
current_id += 1;
if (pages[i]) {
$('pre.active').removeClass('active');
$("#stage-num").html(i);
$("#stage-time").html(pages[i]['time']);
current_zombies = pages[i]['zombies'].concat();
current_num = current_zombies.length;
$('pre.stage').addClass('active');
setTimeout(() => {
$('pre.active').removeClass('active');
$(`[data-page="${i}"]`).addClass('active');
const action_id = current_id;
setTimeout(() => {
if (action_id === current_id && current_zombies.length > 0) {
current_id += 1;
$('pre.active').removeClass('active');
$('pre.dead').addClass('active');
}
}, pages[i]['time'] * 1000);
}, 1500);
} else {
$('pre.active').removeClass('active');
$('pre.complete').addClass('active');
}
}
$('[data-page]').each(function() {
pages[Number($(this).attr('data-page'))] = {
zombies: $(this).attr('data-zombie').split(',').map( s => `"${s} Am I dead?"`),
time: Number($(this).attr('data-time')),
};
$(this).find('span.token.string').each(function() {
if (/โญ"$/.test($(this).html())) {
$(this).html($(this).html().replace("โญ", "Am I dead?"))
$(this).click(function () {
const action_id = current_id;
const s = $(this).html();
if (/Am I dead\?"$/.test(s)) {
const i = current_zombies.indexOf(s)
if (i > -1) {
current_zombies.splice(i, 1);
const $this = $(this);
$this.attr('data-org-code', $(this).html());
$this.html('"๐ง"');
setTimeout(() => {
if (action_id === current_id) { // 2้ฑ็ฎใฎ่ๆ
ฎใฏใใชใ
if ($this.html() === '"๐ง"') {
$this.html('"โ "');
setTimeout(() => {
if (action_id === current_id) { // 2้ฑ็ฎใฎ่ๆ
ฎใฏใใชใ
current_num += -1;
if (current_num === 0) {
start(current_page + 1);
}
}
}, 400);
}
}
}, 400);
} else {
current_id += 1;
$('pre.active').removeClass('active');
$('pre.miss').addClass('active');
}
}
});
}
});
});
$(".retry").click(function() {
$('pre.active').removeClass('active');
$('pre.top').addClass('active');
});
$("#start").click(function() {
$('[data-org-code]').each(function() {
$(this).html($(this).attr('data-org-code'))
});
start(1)
});
$('pre.top').addClass('active');
}, 1000); // prism.jsใฎใใผใฏใขใใใๅพ
ๅ
body {
width :320px;
}
pre {
display: none;
}
pre.active {
display: block;
}
.top,.stage,.dead,.miss,.complete {
text-align: center;
}
button {
margin-top: 18px;
}
span {
touch-action: manipulation;
}
ๅไฝไฟ่จผใฏใใพใใใใไปฅไธใฎใใใช HTML ใฎใฟใง็ฐกๅใซๅ้กไฝใใพใใ
ๆฌๅ้กใฎ HTML ใ่ฆใ
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HODCใRubyใ</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/themes/prism.min.css" integrity="sha512-tN7Ec6zAFaVSG3TpNAKtk4DOHNpSwKHxxrsiw4GHKESGPs5njn/0sMCUMl2svV4wo4BK/rCP7juYz+zx+l6oeQ==" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/themes/prism-twilight.min.css" integrity="sha512-akb4nfKzpmhujLUyollw5waBPeohuVf0Z5+cL+4Ngc4Db+V8szzx6ZTujguFjpmD076W8LImVIbOblmQ+vZMKA==" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/j5c8k6m8/HODC@v0.0.2/HODC.css"/>
</head>
<body>
<pre class="top" style="padding-top: 20px;">
<span style="font-size: 24px;">THE</span><span style="font-size: 40px;margin-left: 4px">HOUSE</span><span style="font-size: 32px;margin:10px;">OF</span><span style="font-size: 24px;">THE</span><span style="font-size: 40px;margin-left: 4px;margin-right:18px;">DEAD</span>
<span style="font-size: 36px;line-height: 40px;">CODE</span>
<span style="font-size: 24px;line-height: 40px;">ใRubyใ</span>
<button id="start" type="button">START ๐ก</button>
<span style="font-size: 18px;line-height: 60px;"> ้ใณๆน: DEAD CODEใใฏใชใใฏใใใ </span>
</pre>
<pre class="stage" style="font-size: 24px; padding-top: 80px;">
ROOM <span id="stage-num"></span>
(ๅถ้ๆ้ <span id="stage-time"></span> ็ง)
</pre>
<pre class="complete" style="padding-top: 80px;">
<span style="font-size:30px;">๐ Congratulations! ๐</span>
<button class="retry" type="button">ใชใใฉใค</button>
</pre>
<pre class="dead" style="padding-top: 80px;">
<span style="font-size:30px;">๐ง Time Out! ๐ง</span>
<button class="retry" type="button">ใชใใฉใค</button>
</pre>
<pre class="miss" style="padding-top: 80px;">
<span style="font-size:30px;">๐ฎ Miss Shot! ๐ฎ</span>
<button class="retry" type="button">ใชใใฉใค</button>
</pre>
<pre data-page="1", data-zombie="๐๐พโโ๏ธ" data-time="5">
<code class="language-rb">
puts "๐โโ๏ธ โญ"
return
puts "๐๐พโโ๏ธ โญ"
</code>
</pre>
<pre data-page="2", data-zombie="๐
๐ฝโโ๏ธ" data-time="10">
<code class="language-rb">
3.times do |i|
case i
when 1,2 then
puts "๐๐ผโโ๏ธ โญ"
when 3 then
puts "๐
๐ฝโโ๏ธ โญ"
else
puts "๐คฆ๐ฝ โญ"
end
end
</code>
</pre>
<pre data-page="3", data-zombie="๐คท๐ฝโโ๏ธ,๐๐ปโโ๏ธ" data-time="10">
<code class="language-rb">
def add a, b
puts "๐คท๐ฝโโ๏ธ โญ"
return a + b
puts "๐๐ปโโ๏ธ โญ"
end
puts 3 + 5
puts "๐
๐ฝโโ๏ธ โญ"
</code>
</pre>
<pre data-page="4", data-zombie="๐จ๐ผโ๐" data-time="10">
<code class="language-rb">
3.times do |i|
if i % 2 == 0
unless i % 3 == 0
puts "๐ฉ๐พโ๐พ โญ"
end
if i % 3 == 1
puts "๐จ๐ผโ๐ โญ"
end
end
end
</code>
</pre>
<pre data-page="5", data-zombie="๐จ๐ฝโ๐ญ,๐จ๐ปโ๐ซ" data-time="10">
<code class="language-rb">
if false
puts "๐จ๐ฝโ๐ญ โญ"
end
if 0
puts "๐จ๐ฟโ๐ โญ"
end
if ""
puts "๐จโ๐ณ โญ"
end
if nil
puts "๐จ๐ปโ๐ซ โญ"
end
</code>
</pre>
<pre data-page="6", data-zombie="๐ฉโ๐ฉโ๐ง,๐ฉ๐พโ๐คโ๐จ๐ป,๐ฉโ๐ฆ,๐ฉ๐ผโ๐คโ๐จ๐ฟ" data-time="20">
<code class="language-rb">
while true do
puts "๐จโ๐จโ๐ฆ โญ"
for num in 1..3 do
puts "๐ฉ๐ผโ๐คโ๐จ๐ป โญ"
until false do
puts "๐จ๐ฟโ๐คโ๐จ๐ผ โญ"
end
puts "๐ฉโ๐ฉโ๐ง โญ"
continue
puts "๐ฉ๐พโ๐คโ๐จ๐ป โญ"
end
puts "๐ฉโ๐ฆ โญ"
end
puts "๐ฉ๐ผโ๐คโ๐จ๐ฟ โญ"
</code>
</pre>
<pre data-page="7", data-zombie="๐ง๐ผโโ๏ธ,๐๐ฝโโ๏ธ,๐" data-time="15">
<code class="language-rb">
def a
puts "๐คธ๐พ โญ"
b
c
puts "๐ง๐ผโโ๏ธ โญ"
end
def b
puts "๐คพ๐ปโโ๏ธ โญ"
c
puts "๐คน๐ผโโ๏ธ โญ"
a
puts "๐๐ฝโโ๏ธ โญ"
end
def c
puts "๐๐พ โญ"
end
a
puts "๐ โญ"
</code>
</pre>
<pre data-page="8", data-zombie="๐ฆน๐ฟโโ๏ธ,๐ง๐ฝ" data-time="10">
<code class="language-rb">
def a
puts "๐ฆธ๐ปโโ๏ธ โญ"
yield
puts "๐ง๐ผโโ๏ธ โญ"
end
def b
puts "๐ฆน๐ฝโโ๏ธ โญ"
yield
puts "๐ฆน๐ฟโโ๏ธ โญ"
end
a do
puts "๐ง๐ผโโ๏ธ โญ"
end
b do
puts "๐ฆธ๐ฟ โญ"
break
puts "๐ง๐ฝ โญ"
end
</code>
</pre>
<pre data-page="9", data-zombie="๐ด,๐,๐ซ" data-time="30">
<code class="language-rb">
puts [*1..3].filter{ |x|
puts "๐ โญ"
x % 2 == 1
}.filter{ |x|
puts "๐ โญ"
x % 3 == 2
}.filter{ |x|
puts "๐ด โญ"
x % 4 == 3
}.any? { |x|
puts "๐ โญ"
x % 4 == 3
}&.tap {
puts "๐ โญ"
} ? "๐ซ โญ"
: "๐ โญ"
</code>
</pre>
<pre data-page="10", data-zombie="๐ฆธ๐ฝ" data-time="6">
<code class="language-rb">
def raise
puts "๐จโ๐ป โญ"
end
begin
puts "๐
โญ"
raise
puts "๐ โญ"
rescue RuntimeError
puts "๐ฆธ๐ฝ โญ"
end
</code>
</pre>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/components/prism-core.min.js" integrity="sha512-hqRrGU7ys5tkcqxx5FIZTBb7PkO2o3mU6U5+qB9b55kgMlBUT4J2wPwQfMCxeJW1fC8pBxuatxoH//z0FInhrA==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/plugins/autoloader/prism-autoloader.min.js" integrity="sha512-Q3qGP1uJL/B0sEmu57PKXjCirgPKMbg73OLRbTJ6lfHCVU5zkHqmcTI5EV2fSoPV1MHdKsCBE7m/aS6q0pPjRQ==" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/gh/j5c8k6m8/HODC@v0.0.2/HODC.js"></script>
</body>
</html>
ใใใใณใผใใๅถ้ๆ้ๅ
ใซ่ฆใคใใใฒใผใ ใฃใฆใใใญใฐใฉใใณใฐๆ่ฒใๆฑใใใใฆใใไปใใใใณใทใฃใซใใใใใใ ใใใSEGAใใ ่ชฐใใใกใใใจใใใฎใไฝใฃใฆใใใชใใใชใผใไปไบบไปปใ
ใใใใซ
ๅฎใฏใใขใใใณใใซใฌใณใใผๆ้ใฏๆฏๆฅ ใใใใณใผใใฏใใฏใใใฏ ใๆดๆฐใใไบๅฎใงใใใใ้ไธญใงๆซๆใใพใใใ(ไปๅพใ็ถ็ถ็ใซๆดๆฐใใฆใใไบๅฎใงใใใ)
**้ใฎ้ญ่กใซๅฏพใใ้ฒ่ก่ก Advent Calendar 2020**ใซใฏ็ตๅฑ6ๅใฎใใจใ ใๆธใใ
ใใใใใใฆใไปๆนใใชใใฎใงใ ruby3.0ใจใใใฏใชในใในใใฌใผใณใ ใใใใใๆๆฅใๆฅฝใใฟใซๅพ ใค๏ผ