/
log2html
executable file
·192 lines (180 loc) · 6.11 KB
/
log2html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env ruby -wKU
unless ARGV.first and File.exist? ARGV.first
abort "USAGE: #{$PROGRAM_NAME} PATH_TO_LOG"
end
require "erb"
def move(from, plus, horizontal)
x = from[/[A-L]/][0]
y = from[/10|[1-9]/].to_i
horizontal ? "#{(x + plus).chr}#{y}" : "#{x.chr}#{y + plus}"
end
players = [ ]
ships = { }
shots = { }
turn = 0
winner = nil
File.foreach(ARGV.first) do |line|
case line
when /\ASETUP players:(\w+),(\w+)\b/
players << $1 << $2
when /\ASHIPS (\w+)((?: [2-5]:[A-L](?:10|[1-9]):[HV]){5})\Z/
player = $1
ships[player] = Hash.new("water")
$2.strip.split.each do |ship|
length, start, horizontal = ship.split(":")
length = length.to_i
horizontal = horizontal == "H"
length.times do |n|
edge = case n
when 0
"#{horizontal ? 'left' : 'top'}_edge"
when length - 1
"#{horizontal ? 'right' : 'bottom'}_edge"
else
"#{horizontal ? 'horizontal' : 'vertical'}_middle"
end
ships[player][move(start, n, horizontal)] = "ship #{edge}"
end
end
when /\ASHOTS (\w+)((?: [A-L](?:10|[1-9]):(?:hit|miss))+)\Z/
player = $1
other = player == players.first ? players.last : players.first
shots[other] ||= Hash.new(%w[empty ])
turn += 1
$2.strip.split.each do |shot|
where, result = shot.split(":")
shots[other][where] = [ "#{result} turn_#{turn}",
result == "hit" ? "X" : "O" ]
end
when /\AWINNER (\w+)\b/
winner = $1
end
end
ERB.new(DATA.read).run
__END__
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title><%= game = File.basename(ARGV.first, ".log") %></title>
<style type="text/css" media="screen">
body {
margin: 0px;
text-align: center;
}
#page {
width: 800px;
margin: 10px auto;
}
table {
border-spacing: 0px;
border: 1px solid blue;
}
td {
width: 30px;
height: 30px;
margin: 0px;
padding: 0px;
}
div.left {
float: left;
}
div.right {
float: right;
}
td.water {
border: 1px solid blue;
background-color: #66CCFF;
}
td.ship {
background-color: #999999;
}
td.left_edge {
border: 1px solid #E6E6E6;
border-right: 1px solid #999999;
}
td.right_edge {
border: 1px solid #E6E6E6;
border-left: 1px solid #999999;
}
td.top_edge {
border: 1px solid #E6E6E6;
border-bottom: 1px solid #999999;
}
td.bottom_edge {
border: 1px solid #E6E6E6;
border-top: 1px solid #999999;
}
td.horizontal_middle {
border: 1px solid #E6E6E6;
border-left: 1px solid #999999;
border-right: 1px solid #999999;
}
td.vertical_middle {
border: 1px solid #E6E6E6;
border-top: 1px solid #999999;
border-bottom: 1px solid #999999;
}
span {
display: none;
}
.hit {
color: #DD0000;
}
.miss {
color: white;
}
.player {
font-weight: bold;
}
.controls span {
font-weight: bold;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
</head>
<body>
<div id="page">
<h1><%= game %></h1>
<% players.each_with_index do |player, i| %>
<div class="<%= i.zero? ? "left" : "right" %>">
<table>
<% (1..10).each do |y| %>
<tr>
<% ("A".."L").each do |x| %>
<% xy = "#{x}#{y}" %>
<td class="<%= ships[player][xy] %>">
<span class="<%= shots[player][xy][0] %>">
<%= shots[player][xy][1] %>
</span>
</td>
<% end %>
</tr>
<% end %>
</table>
<p class="player"><%= player %></p>
</div>
<% end %>
<div class="controls">
<% 1.upto(turn) do |i| %>
<a href="#" class="link_<%= i %>"
style="<%= i == 1 ? "" : "display: none;" %>"
data-turn="<%= i %>">Next</a>
<% end %>
<span class="link_<%= turn + 1 %>">Winner: <%= winner %></span>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('a').click(function(event) {
var link = $(this);
var turn = link.attr('data-turn');
$('.turn_' + turn).show();
link.hide();
$('.link_' + (parseInt(turn) + 1)).show();
});
});
</script>
</div>
</div>
</body>
</html>