Skip to content

Commit 54b992c

Browse files
committed
Finish adding bells and whistles to repl mode
1 parent a8d5f31 commit 54b992c

File tree

7 files changed

+195
-97
lines changed

7 files changed

+195
-97
lines changed

src/actions/hold.jl

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
function hold(cur_player::AbstractPlayer)
22

3+
# ------------
4+
# start hold
5+
# ------------
6+
37
can_move(cur_player) || return
48

59
cur_player.state.can_hold || return
610
cur_player.state.can_hold = false
711

812
score!(cur_player, "hold", -1)
913

14+
# ------------
15+
# clean hold
16+
# ------------
17+
18+
cur_js = """
19+
\$(".js-hold-piece td").removeClass();
20+
var tmp_cell;
21+
"""
22+
1023
if is_repl
1124
cur_coords = map(
1225
cur_block -> calc_block_coords(cur_block),
@@ -28,17 +41,18 @@ function hold(cur_player::AbstractPlayer)
2841
has_piece = cur_player.grid.table[cur_row, cur_col] != ""
2942
has_piece && continue
3043

31-
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(8+2*cur_col)H")
44+
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(29+2*cur_col)H")
3245
push!(cur_string, crayon_dict["invisible"])
3346
push!(cur_string, " ")
3447
push!(cur_string, inv(crayon_dict["invisible"]))
3548
end
3649
end
37-
38-
push!(cur_string, "\x1b[u")
39-
print(cur_string...)
4050
end
4151

52+
# -------------------
53+
# show hold preview
54+
# -------------------
55+
4256
work_piece, cur_player.piece =
4357
cur_player.piece, cur_player.hold
4458

@@ -50,14 +64,20 @@ function hold(cur_player::AbstractPlayer)
5064
:hold
5165
)
5266

53-
cur_js = """
54-
\$(".js-hold-piece td").removeClass();
55-
var tmp_cell;
56-
"""
57-
5867
cur_game = cur_player.game
5968
cur_piece = cur_player.hold
6069

70+
if is_repl
71+
for cur_row in 1:2
72+
for cur_col in 1:4
73+
push!(cur_string, "\x1b[$(21-cur_row);$(9+2*cur_col)H")
74+
push!(cur_string, crayon_dict["invisible"])
75+
push!(cur_string, " ")
76+
push!(cur_string, inv(crayon_dict["invisible"]))
77+
end
78+
end
79+
end
80+
6181
for cur_block in cur_piece.blocks
6282
(cur_row, cur_col) = calc_block_coords(cur_block)
6383

@@ -70,6 +90,22 @@ function hold(cur_player::AbstractPlayer)
7090
tmp_cell = \$(".js-hold-piece .cs-row-$(cur_row) td:nth-child($(cur_col))");
7191
tmp_cell.addClass("cs-color cs-$(cur_piece.color)");
7292
"""
93+
94+
if is_repl
95+
push!(cur_string, "\x1b[$(21-cur_row);$(9+2*cur_col)H")
96+
push!(cur_string, crayon_dict["$(cur_piece.color)_shadow"])
97+
push!(cur_string, " ")
98+
push!(cur_string, inv(crayon_dict["$(cur_piece.color)_shadow"]))
99+
end
100+
end
101+
102+
# --------------
103+
# wrap-up hold
104+
# --------------
105+
106+
if is_repl
107+
push!(cur_string, "\x1b[u")
108+
print(cur_string...)
73109
end
74110

75111
tetris_js(

src/actions/move.jl

Lines changed: 57 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
22

3+
# ==================
4+
# handle repl move
5+
# ==================
6+
7+
if is_repl
8+
_move_repl(cur_player)
9+
10+
cur_player.clock.lock =
11+
Nullable{Base.Random.UUID}()
12+
13+
return
14+
end
15+
316
# ====================
417
# start from scratch
518
# ====================
@@ -14,53 +27,6 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
1427
"""
1528
end
1629

17-
if is_repl
18-
cur_string = []
19-
20-
cur_piece = cur_player.piece
21-
22-
cur_piece_coords = map(
23-
cur_block -> calc_block_coords(cur_block),
24-
cur_piece.blocks
25-
)
26-
27-
row_info = map(first, cur_piece_coords)
28-
col_info = map(last, cur_piece_coords)
29-
30-
max_row = min(cur_player.grid.rows, maximum(row_info) + cur_player.piece.width )
31-
32-
min_col = max(1, minimum(col_info) - cur_player.piece.width )
33-
max_col = min(cur_player.grid.cols, maximum(col_info) + cur_player.piece.width )
34-
35-
cur_shadow = cur_player.shadow
36-
37-
cur_shadow_coords = map(
38-
cur_block -> calc_block_coords(cur_block),
39-
cur_shadow.blocks
40-
)
41-
42-
for cur_row in 1:max_row
43-
for cur_col in 1:cur_player.grid.cols
44-
has_piece = cur_player.grid.table[cur_row, cur_col] != ""
45-
has_piece && continue
46-
47-
if in((cur_row, cur_col), cur_piece_coords)
48-
cur_color = cur_piece.color
49-
elseif in((cur_row, cur_col), cur_shadow_coords)
50-
cur_color = "$(cur_piece.color)_shadow"
51-
else
52-
cur_color = "invisible"
53-
end
54-
55-
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(8+2*cur_col)H")
56-
push!(cur_string, crayon_dict[cur_color])
57-
push!(cur_string, " ")
58-
push!(cur_string, inv(crayon_dict[cur_color]))
59-
end
60-
end
61-
62-
end
63-
6430
# ==============
6531
# update score
6632
# ==============
@@ -73,9 +39,7 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
7339
# re-add piece
7440
# ==============
7541

76-
if is_ide
77-
cur_piece = cur_player.piece
78-
end
42+
cur_piece = cur_player.piece
7943

8044
cur_coords = map(
8145
cur_block -> calc_block_coords(cur_block),
@@ -97,8 +61,6 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
9761
"""
9862
end
9963

100-
is_repl && append!(cur_string, _move_repl(cur_player, cur_coords, false))
101-
10264
# ==============
10365
# return early
10466
# if no shadow
@@ -117,9 +79,7 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
11779
# add shadow
11880
# ============
11981

120-
if is_ide
121-
cur_shadow = cur_player.shadow
122-
end
82+
cur_shadow = cur_player.shadow
12383

12484
cur_coords = map(
12585
cur_block -> calc_block_coords(cur_block),
@@ -135,8 +95,6 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
13595
"""
13696
end
13797

138-
is_repl && append!(cur_string, _move_repl(cur_player, cur_coords, true))
139-
14098
# ====================
14199
# remove bad borders
142100
# ====================
@@ -172,11 +130,6 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
172130
# wrap up move
173131
# ==============
174132

175-
if is_repl
176-
push!(cur_string, "\x1b[u")
177-
print(cur_string...)
178-
end
179-
180133
tetris_js(cur_player.game.scope, JSString(cur_js))
181134

182135
cur_player.clock.lock =
@@ -186,19 +139,51 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
186139

187140
end
188141

189-
function _move_repl(cur_player::AbstractPlayer, cur_coords::Vector{Tuple{Int64,Int64}}, is_shadow)
142+
function _move_repl(cur_player::AbstractPlayer)
190143
cur_string = []
191144

192-
cur_color = cur_player.piece.color
193-
is_shadow && ( cur_color *= "_shadow" )
145+
cur_piece = cur_player.piece
194146

195-
for (cur_row, cur_col) in cur_coords
196-
( cur_row > cur_player.grid.rows ) && continue
197-
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(8+2*cur_col)H")
198-
push!(cur_string, crayon_dict[cur_color])
199-
push!(cur_string, " ")
200-
push!(cur_string, inv(crayon_dict[cur_color]))
147+
cur_piece_coords = map(
148+
cur_block -> calc_block_coords(cur_block),
149+
cur_piece.blocks
150+
)
151+
152+
row_info = map(first, cur_piece_coords)
153+
col_info = map(last, cur_piece_coords)
154+
155+
max_row = min(cur_player.grid.rows, maximum(row_info) + cur_player.piece.width )
156+
157+
min_col = max(1, minimum(col_info) - cur_player.piece.width )
158+
max_col = min(cur_player.grid.cols, maximum(col_info) + cur_player.piece.width )
159+
160+
cur_shadow = cur_player.shadow
161+
162+
cur_shadow_coords = map(
163+
cur_block -> calc_block_coords(cur_block),
164+
cur_shadow.blocks
165+
)
166+
167+
for cur_row in 1:max_row
168+
for cur_col in 1:cur_player.grid.cols
169+
has_piece = cur_player.grid.table[cur_row, cur_col] != ""
170+
has_piece && continue
171+
172+
if in((cur_row, cur_col), cur_piece_coords)
173+
cur_color = cur_piece.color
174+
elseif in((cur_row, cur_col), cur_shadow_coords)
175+
cur_color = "$(cur_piece.color)_shadow"
176+
else
177+
cur_color = "invisible"
178+
end
179+
180+
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(29+2*cur_col)H")
181+
push!(cur_string, crayon_dict[cur_color])
182+
push!(cur_string, " ")
183+
push!(cur_string, inv(crayon_dict[cur_color]))
184+
end
201185
end
202186

203-
return cur_string
187+
push!(cur_string, "\x1b[u")
188+
print(cur_string...)
204189
end

src/functions/clear_rows.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ function clear_rows!(cur_player::AbstractPlayer, cleared_rows::Vector{Int})
5151
cur_js *= """
5252
\$(".js-level-text").text("Level $(cur_player.level)");
5353
"""
54+
55+
if is_repl
56+
cur_string = []
57+
push!(cur_string, crayon_dict["grey"])
58+
push!(cur_string, "\x1b[$(8);$(11)H")
59+
push!(cur_string, " $(lpad(cur_player.level, 2, "0")) ")
60+
push!(cur_string, inv(crayon_dict["grey"]))
61+
push!(cur_string, "\x1b[u")
62+
print(cur_string...)
63+
end
5464
end
5565
end
5666

src/functions/glue_piece.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,14 @@ function glue_piece!(cur_player::AbstractPlayer)
9898
cur_color = cur_player.grid.table[cur_row, cur_col]
9999
( cur_color == "" ) && ( cur_color = "invisible" )
100100

101-
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(8+2*cur_col)H")
101+
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(29+2*cur_col)H")
102102
push!(cur_string, crayon_dict[cur_color],)
103103
push!(cur_string, " ")
104104
push!(cur_string, inv(crayon_dict[cur_color]))
105105
end
106106
end
107107

108108
push!(cur_string, "\x1b[u")
109-
110109
print(cur_string...)
111110
end
112111

src/functions/score.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ function score!(cur_player::AbstractPlayer, cur_label::AbstractString, cur_value
7272
cur_player.score += cur_score
7373
end
7474

75+
if is_repl
76+
cur_string = []
77+
push!(cur_string, crayon_dict["grey"])
78+
push!(cur_string, "\x1b[$(14);$(11)H")
79+
push!(cur_string, lpad(cur_player.score, 8, "0"))
80+
push!(cur_string, inv(crayon_dict["grey"]))
81+
push!(cur_string, "\x1b[u")
82+
print(cur_string...)
83+
end
84+
7585
return cur_score
7686

7787
end

src/functions/summon_piece.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ function summon_piece!(cur_player::AbstractPlayer)
4242
var tmp_cell;
4343
"""
4444

45+
if is_repl
46+
cur_string = []
47+
48+
for cur_index in 1:cur_previews
49+
for cur_row in 1:2
50+
for cur_col in 1:4
51+
push!(cur_string, "\x1b[$(3-cur_row+6*cur_index);$(61+2*cur_col)H")
52+
push!(cur_string, crayon_dict["invisible"])
53+
push!(cur_string, " ")
54+
push!(cur_string, inv(crayon_dict["invisible"]))
55+
end
56+
end
57+
end
58+
end
59+
4560
for (cur_index, cur_piece) in enumerate(cur_player.bag.pieces[1:cur_previews])
4661
for cur_block in cur_piece.blocks
4762
(cur_row, cur_col) = calc_block_coords(cur_block)
@@ -55,9 +70,21 @@ function summon_piece!(cur_player::AbstractPlayer)
5570
tmp_cell = \$(".js-preview-piece__$(cur_index) .cs-row-$(cur_row) td:nth-child($(cur_col))");
5671
tmp_cell.addClass("cs-color cs-$(cur_piece.color)");
5772
"""
73+
74+
if is_repl
75+
push!(cur_string, "\x1b[$(3-cur_row+6*cur_index);$(61+2*cur_col)H")
76+
push!(cur_string, crayon_dict[cur_piece.color])
77+
push!(cur_string, " ")
78+
push!(cur_string, inv(crayon_dict[cur_piece.color]))
79+
end
5880
end
5981
end
6082

83+
if is_repl
84+
push!(cur_string, "\x1b[u")
85+
print(cur_string...)
86+
end
87+
6188
tetris_js(
6289
cur_game.scope,
6390
JSString(cur_js)

0 commit comments

Comments
 (0)