在测试出牌的时候我才想到,
同一回合如果只有我出牌,对方应该是要看不到我出什麽牌才对
我觉得这项功能从原本的 Card.Game 那层来做会比较好,
但是有点懒得回去条了,我直接在 liveview 这层做,算是还可以拉,
也是在服务器端。
虽然感觉不是最好的做法,我做了一个判断需不需要显示对手新的牌的方法
如果对手的桌面长度比玩家的长,就把对手的最後一张牌换成 :fold
defp maybe_fold_last(game, current_player, opponent) do
if length(desk(game, opponent)) > length(desk(game, current_player)) do
opponent_status =
game
|> Map.get(opponent)
|> Map.replace(:desk, Enum.drop(desk(game, opponent), -1) ++ [:fold])
Map.replace(game, opponent, opponent_status)
else
game
end
end
defp desk(game, player) do
game
|> Map.get(player)
|> Map.get(:desk)
end
用在 mount 的 game 状态上面
mount 方法
def mount(
# 省略...
{:ok,
assign(socket, %{
pid: pid,
id: id,
game: maybe_fold_last(game, current_player, opponent),
current_player: current_player,
opponent: opponent
})}
end
除了 mount 方法要加,接收广播的 handle_info
才是主要改变状态的地方
def handle_info(game, socket) do
%{assigns: %{current_player: current_player, opponent: opponent}} = socket
{:noreply,
assign(socket, %{game: maybe_fold_last(game, current_player, opponent)})}
end
最後调整一下 card component
如果是覆盖状态的话就用第一个
def card(%{name: :fold} = assigns) do
~H"""
<div class="border border-gray-200 rounded-lg p-0.5 w-14 h-22 m-1 shadow">
<div class="border-2 p-0.5 border-blue-500 rounded-md flex justify-center items-center w-full h-full">
<div class="bg-blue-300 rounded w-full h-full">
</div>
</div>
</div>
"""
end
def card(assigns) do
~H"""
<div class="border border-gray-200 rounded-lg p-0.5 w-14 h-22 m-1 shadow">
<div class="border-2 border-blue-500 rounded-md flex justify-center items-center w-full h-full">
<div class="text-xl font-bold text-blue-500">
<%= card_name(@name) %>
</div>
</div>
</div>
"""
end
完成之後对手如果先出牌,我们就会暂时看不到了
<<: [Day27] Flutter with GetX connectivity
>>: Day41 ( 游戏设计 ) 翻翻卡 ( 卡牌记忆 )
Vuex 结构分为 state getters mutations actions 四个部分 Sta...
设定完成 AdventureWork 资料库做高可用性资料库後,来测试资料是否会正确的同步到次要资料...
跟字型相关的CSS设定 font-family 指定使用的字体,可以设定多个,让浏览器依照排序使用 ...
Conclusions and Discussion 在这二十几天来的技术文章分享,我们可以看到每个...
在不同电脑之间因为有分享资源和资讯的需求而产生彼此连结的电脑系统称之为网路 (networks),网...