[第十五只羊] 迷雾森林舞会IX 玩家加入房间

天亮了 昨晚1号玩家被杀死了

关於迷雾森林故事

杂讯

洛神:昨晚1号玩家被杀死了,1号玩家发动角色技能,请问要带走的对象是?
1号:6号
洛神:6号淘汰,请至淘汰区
2号:1号是猎人他带了6号,但是6号的身份未知,然後我讲一下为什麽我做不成狼,刚两个预言家很明显9号讲的比较有力度,如果我是狼的话我就直接站边9号就好了,干嘛还要让大家有多一个质疑我的..

另一边正在观看直播的森林里突然间,水池上萤幕出现了许多黑白相间的杂讯打断了2号玩家的对话
声音也变得断断续续的
鸭嘴兽泰迪察觉不对劲
见状立刻跳进水池内

图片来源
待续..

动物园派对

我们这15天已经已经可以成功建立房间并指派创建房间的使用者为室长
我们今天要再调整一下这个架构
昨天那个架构的问题会在
使用者离开之後还要脱掉室长的头衔
室长在这个架构情境下
应该要跟着房间里的座位:seat`` 而不是使用者:user但是这桌游情境除了室长之外 我们还有玩家游戏中的身份牌 所以我们既然一开始规划就有把资料库中的:seat:room```中拆分出来
角色身份就理应要跟着座位
这样还有另一个好处是当如果因为一些断线情形
使用者身份没有彻底清除的话
绑在座位的角色身份也不会造成加入别的房间而造成判别错误
也就是这个房间的这个座位职责有多重身份与其他房间无关
所以很抱歉我昨天没想清楚
因为传统上角色会跟使用者绑定是为了类似全站的角色认定或是会员关系管理
这次这个情境的角色与职责只会存在於房间内部的座位上
既然如此
我们把 rolify跟user关联拆掉重新做一次
先把db还原到上一个阶段

$ bundle exec rake db:rollback

rollback之後就可以把昨天加的 migration 删掉
2021092907xxxx_rolify_create_roles.rb
然後删除後再建一次
这次针对 :seat这个资料库做角色设定

$ rails d rolify Role User
$ rails g rolify Role Seat

上面指令会帮忙生成这些档案

      invoke  active_record
      create    app/models/role.rb
      invoke    test_unit
      create      test/models/role_test.rb
      create      test/fixtures/roles.yml
      insert    app/models/role.rb
      create    db/migrate/20210930072913_rolify_create_roles.rb

在 seat.rb 中加上 rolify

class Seat < ApplicationRecord
  rolify
  belongs_to :room
  ...

在 room.rb 中加上 resourcify

class Room < ApplicationRecord
  resourcify
  has_many :seats, dependent: :destroy
  ...

这样我们便可以待会就可以将有玩家的座位指定身份了
在使用者有登入或是注册的情况下
使用者可以从房间列表中进入房间
首先
我们将原本的
assign_seat_to_mayor(@room)
改成
assign_seat_to_user(@room)


  def create
    @room = Room.new(room_params)
    if @room.validate(room_params)
      @room.board = Room.boards[room_params[:board]]
      @room.save
      if @room.seats.count == 0 && @room.roles.pluck(:name).exclude?('mayor')
        create_seats(@room.id, @room.number_of_gamer)
        assign_seat_to_user(@room)
        redirect_to room_path(@room), notice: '房间建立成功'
      else
        redirect_to new_room_path, notice: '发生错误 请重新建立房间'
      end
    else
      render :new
    end
  end
  
  def show
    assign_seat_to_user(@room)
  end
  
  def assign_seat_to_user(room)
    empty_seats = Seat.empty_seat_collection(room)
    occupied_seats = Seat.occupied_seat_collection(room)
    current_user.update(room_id: current_user.id)
    if empty_seats.count > 0 && occupied_seats.pluck(:user_id).exclude?(current_user.id)
      seat = empty_seats.sample
      seat.update(user_id: current_user.id)
      seat.add_role :mayor, room unless @room.roles.pluck(:name).include?('mayor')
    else
      # have no seat, stay this room as visitor
    end
  end

  def purify_user_before_left_room(room)
    occupied_seats = Seat.occupied_seat_collection(room)

    # clear user rolify in the room and re-assign mayor role
    clear_the_seat = occupied_seats.select{|s| s.user_id = current_user.id}
    clear_the_seat.update(user_id: nil) unless clear_the_seat.blank?
    clear_the_seat.remove_role :mayor if clear_the_seat.has_role? :mayor
    current_user.update(room_id: nil)
    # clear seat from occupied to empty

    if empty_seats <= 0
      # hide the empty room
    else
      # assign new mayor
      seat = occupied_seats.sample
      seat.add_role :mayor, room
    end
  end

如此一来 当使用者进入房间时
有座位就可以加入房间变成玩家
没有的话就会是观战模式

阿虎每日选币

这个事$HEGIC,许多小币都在做三角收敛,对应四种三角收敛末端表态来判断走势,但依然要记得小心诱多与大盘持续看空

天黑请闭眼


<<:  JavaScript学习日记 : Day18 - Class

>>:  模组化、重用使用者介面

WordPress 安装 Google Analytics 教学,完整分析网站流量

在 WordPress 上架设的 Blog 已经完成了,也写了数篇的文章,在 Google 上已经可...

Day3 逻辑斯回归(Logistic Regression)

逻辑斯回归是什麽? 讲人话就是一种对数机率模型,主要用来处理二元[非此即彼(Yes or No)]分...

Day 20 - 将 NEWS 後台储存资料提取後,送至前台渲染画面 (下) - News View Page InnerText 应用 - ASP.NET Web Forms C#

=x= 🌵 NEWS 前台 View 页面後端功能制作。 NEWS View 页面资料介绍 : 📌 ...

Angular 深入浅出三十天:表单与测试 Day02 - Template Driven Forms 实作 - 以登入为例

今天要来用 Template Driven Forms 的方式实作一个简单的登入系统,撇开 UI ...

Android学习笔记18

今天把dialogfragment也搭配上bindnig然後试着把dialog的动作移到dialog...