Skip to content

Commit

Permalink
Finish adding bells and whistles to repl mode
Browse files Browse the repository at this point in the history
  • Loading branch information
djsegal committed May 27, 2018
1 parent a8d5f31 commit 54b992c
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 97 deletions.
54 changes: 45 additions & 9 deletions src/actions/hold.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
function hold(cur_player::AbstractPlayer)

# ------------
# start hold
# ------------

can_move(cur_player) || return

cur_player.state.can_hold || return
cur_player.state.can_hold = false

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

# ------------
# clean hold
# ------------

cur_js = """
\$(".js-hold-piece td").removeClass();
var tmp_cell;
"""

if is_repl
cur_coords = map(
cur_block -> calc_block_coords(cur_block),
Expand All @@ -28,17 +41,18 @@ function hold(cur_player::AbstractPlayer)
has_piece = cur_player.grid.table[cur_row, cur_col] != ""
has_piece && continue

push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(8+2*cur_col)H")
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(29+2*cur_col)H")
push!(cur_string, crayon_dict["invisible"])
push!(cur_string, " ")
push!(cur_string, inv(crayon_dict["invisible"]))
end
end

push!(cur_string, "\x1b[u")
print(cur_string...)
end

# -------------------
# show hold preview
# -------------------

work_piece, cur_player.piece =
cur_player.piece, cur_player.hold

Expand All @@ -50,14 +64,20 @@ function hold(cur_player::AbstractPlayer)
:hold
)

cur_js = """
\$(".js-hold-piece td").removeClass();
var tmp_cell;
"""

cur_game = cur_player.game
cur_piece = cur_player.hold

if is_repl
for cur_row in 1:2
for cur_col in 1:4
push!(cur_string, "\x1b[$(21-cur_row);$(9+2*cur_col)H")
push!(cur_string, crayon_dict["invisible"])
push!(cur_string, " ")
push!(cur_string, inv(crayon_dict["invisible"]))
end
end
end

for cur_block in cur_piece.blocks
(cur_row, cur_col) = calc_block_coords(cur_block)

Expand All @@ -70,6 +90,22 @@ function hold(cur_player::AbstractPlayer)
tmp_cell = \$(".js-hold-piece .cs-row-$(cur_row) td:nth-child($(cur_col))");
tmp_cell.addClass("cs-color cs-$(cur_piece.color)");
"""

if is_repl
push!(cur_string, "\x1b[$(21-cur_row);$(9+2*cur_col)H")
push!(cur_string, crayon_dict["$(cur_piece.color)_shadow"])
push!(cur_string, " ")
push!(cur_string, inv(crayon_dict["$(cur_piece.color)_shadow"]))
end
end

# --------------
# wrap-up hold
# --------------

if is_repl
push!(cur_string, "\x1b[u")
print(cur_string...)
end

tetris_js(
Expand Down
129 changes: 57 additions & 72 deletions src/actions/move.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)

# ==================
# handle repl move
# ==================

if is_repl
_move_repl(cur_player)

cur_player.clock.lock =
Nullable{Base.Random.UUID}()

return
end

# ====================
# start from scratch
# ====================
Expand All @@ -14,53 +27,6 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
"""
end

if is_repl
cur_string = []

cur_piece = cur_player.piece

cur_piece_coords = map(
cur_block -> calc_block_coords(cur_block),
cur_piece.blocks
)

row_info = map(first, cur_piece_coords)
col_info = map(last, cur_piece_coords)

max_row = min(cur_player.grid.rows, maximum(row_info) + cur_player.piece.width )

min_col = max(1, minimum(col_info) - cur_player.piece.width )
max_col = min(cur_player.grid.cols, maximum(col_info) + cur_player.piece.width )

cur_shadow = cur_player.shadow

cur_shadow_coords = map(
cur_block -> calc_block_coords(cur_block),
cur_shadow.blocks
)

for cur_row in 1:max_row
for cur_col in 1:cur_player.grid.cols
has_piece = cur_player.grid.table[cur_row, cur_col] != ""
has_piece && continue

if in((cur_row, cur_col), cur_piece_coords)
cur_color = cur_piece.color
elseif in((cur_row, cur_col), cur_shadow_coords)
cur_color = "$(cur_piece.color)_shadow"
else
cur_color = "invisible"
end

push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(8+2*cur_col)H")
push!(cur_string, crayon_dict[cur_color])
push!(cur_string, " ")
push!(cur_string, inv(crayon_dict[cur_color]))
end
end

end

# ==============
# update score
# ==============
Expand All @@ -73,9 +39,7 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
# re-add piece
# ==============

if is_ide
cur_piece = cur_player.piece
end
cur_piece = cur_player.piece

cur_coords = map(
cur_block -> calc_block_coords(cur_block),
Expand All @@ -97,8 +61,6 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
"""
end

is_repl && append!(cur_string, _move_repl(cur_player, cur_coords, false))

# ==============
# return early
# if no shadow
Expand All @@ -117,9 +79,7 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
# add shadow
# ============

if is_ide
cur_shadow = cur_player.shadow
end
cur_shadow = cur_player.shadow

cur_coords = map(
cur_block -> calc_block_coords(cur_block),
Expand All @@ -135,8 +95,6 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
"""
end

is_repl && append!(cur_string, _move_repl(cur_player, cur_coords, true))

# ====================
# remove bad borders
# ====================
Expand Down Expand Up @@ -172,11 +130,6 @@ function move!(cur_player::AbstractPlayer, with_shadow::Bool=true)
# wrap up move
# ==============

if is_repl
push!(cur_string, "\x1b[u")
print(cur_string...)
end

tetris_js(cur_player.game.scope, JSString(cur_js))

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

end

function _move_repl(cur_player::AbstractPlayer, cur_coords::Vector{Tuple{Int64,Int64}}, is_shadow)
function _move_repl(cur_player::AbstractPlayer)
cur_string = []

cur_color = cur_player.piece.color
is_shadow && ( cur_color *= "_shadow" )
cur_piece = cur_player.piece

for (cur_row, cur_col) in cur_coords
( cur_row > cur_player.grid.rows ) && continue
push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(8+2*cur_col)H")
push!(cur_string, crayon_dict[cur_color])
push!(cur_string, " ")
push!(cur_string, inv(crayon_dict[cur_color]))
cur_piece_coords = map(
cur_block -> calc_block_coords(cur_block),
cur_piece.blocks
)

row_info = map(first, cur_piece_coords)
col_info = map(last, cur_piece_coords)

max_row = min(cur_player.grid.rows, maximum(row_info) + cur_player.piece.width )

min_col = max(1, minimum(col_info) - cur_player.piece.width )
max_col = min(cur_player.grid.cols, maximum(col_info) + cur_player.piece.width )

cur_shadow = cur_player.shadow

cur_shadow_coords = map(
cur_block -> calc_block_coords(cur_block),
cur_shadow.blocks
)

for cur_row in 1:max_row
for cur_col in 1:cur_player.grid.cols
has_piece = cur_player.grid.table[cur_row, cur_col] != ""
has_piece && continue

if in((cur_row, cur_col), cur_piece_coords)
cur_color = cur_piece.color
elseif in((cur_row, cur_col), cur_shadow_coords)
cur_color = "$(cur_piece.color)_shadow"
else
cur_color = "invisible"
end

push!(cur_string, "\x1b[$(cur_player.grid.rows-cur_row+4);$(29+2*cur_col)H")
push!(cur_string, crayon_dict[cur_color])
push!(cur_string, " ")
push!(cur_string, inv(crayon_dict[cur_color]))
end
end

return cur_string
push!(cur_string, "\x1b[u")
print(cur_string...)
end
10 changes: 10 additions & 0 deletions src/functions/clear_rows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function clear_rows!(cur_player::AbstractPlayer, cleared_rows::Vector{Int})
cur_js *= """
\$(".js-level-text").text("Level $(cur_player.level)");
"""

if is_repl
cur_string = []
push!(cur_string, crayon_dict["grey"])
push!(cur_string, "\x1b[$(8);$(11)H")
push!(cur_string, " $(lpad(cur_player.level, 2, "0")) ")
push!(cur_string, inv(crayon_dict["grey"]))
push!(cur_string, "\x1b[u")
print(cur_string...)
end
end
end

Expand Down
3 changes: 1 addition & 2 deletions src/functions/glue_piece.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,14 @@ function glue_piece!(cur_player::AbstractPlayer)
cur_color = cur_player.grid.table[cur_row, cur_col]
( cur_color == "" ) && ( cur_color = "invisible" )

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

push!(cur_string, "\x1b[u")

print(cur_string...)
end

Expand Down
10 changes: 10 additions & 0 deletions src/functions/score.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ function score!(cur_player::AbstractPlayer, cur_label::AbstractString, cur_value
cur_player.score += cur_score
end

if is_repl
cur_string = []
push!(cur_string, crayon_dict["grey"])
push!(cur_string, "\x1b[$(14);$(11)H")
push!(cur_string, lpad(cur_player.score, 8, "0"))
push!(cur_string, inv(crayon_dict["grey"]))
push!(cur_string, "\x1b[u")
print(cur_string...)
end

return cur_score

end
Expand Down
27 changes: 27 additions & 0 deletions src/functions/summon_piece.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ function summon_piece!(cur_player::AbstractPlayer)
var tmp_cell;
"""

if is_repl
cur_string = []

for cur_index in 1:cur_previews
for cur_row in 1:2
for cur_col in 1:4
push!(cur_string, "\x1b[$(3-cur_row+6*cur_index);$(61+2*cur_col)H")
push!(cur_string, crayon_dict["invisible"])
push!(cur_string, " ")
push!(cur_string, inv(crayon_dict["invisible"]))
end
end
end
end

for (cur_index, cur_piece) in enumerate(cur_player.bag.pieces[1:cur_previews])
for cur_block in cur_piece.blocks
(cur_row, cur_col) = calc_block_coords(cur_block)
Expand All @@ -55,9 +70,21 @@ function summon_piece!(cur_player::AbstractPlayer)
tmp_cell = \$(".js-preview-piece__$(cur_index) .cs-row-$(cur_row) td:nth-child($(cur_col))");
tmp_cell.addClass("cs-color cs-$(cur_piece.color)");
"""

if is_repl
push!(cur_string, "\x1b[$(3-cur_row+6*cur_index);$(61+2*cur_col)H")
push!(cur_string, crayon_dict[cur_piece.color])
push!(cur_string, " ")
push!(cur_string, inv(crayon_dict[cur_piece.color]))
end
end
end

if is_repl
push!(cur_string, "\x1b[u")
print(cur_string...)
end

tetris_js(
cur_game.scope,
JSString(cur_js)
Expand Down
Loading

0 comments on commit 54b992c

Please sign in to comment.