Sha256: 572e45292859be3174b3c2305f8a3b63d3264815a74bdc72be7a6431aead9a32

Contents?: true

Size: 1.31 KB

Versions: 20

Compression:

Stored size: 1.31 KB

Contents

module Example
  class Game < Metacosm::Model
    has_many :players

    def iterate!
      # check for dropped players
      players_to_drop = self.players.all.select do |player|
        player.pinged_at < 3.seconds.ago
      end

      players_to_drop.each do |player|
        drop_player(player)
      end

      self
    end

    def ping(player_id:)
      player = self.players.where(id: player_id).first
      player.update(pinged_at: Time.now)
      self
    end

    def admit_player(player_name:, player_id:)
      self.players.create(
        name: player_name,
        id: player_id,
        joined_at: Time.now,
        pinged_at: Time.now
      )

      emit(
        PlayerAdmittedEvent.create(
          player_id: player_id,
          player_name: player_name,
          connected_player_list: connected_player_list,
        )
      )

      self
    end

    private
    def drop_player(player)
      lost_player_id = player.id
      player.destroy

      emit(
        PlayerDroppedEvent.create(
          player_id: lost_player_id,
          connected_player_list: connected_player_list
        )
      )
    end

    def connected_player_list
      self.players.map do |player|
        {
          id: player.id,
          name: player.name,
          joined_at: player.joined_at
        }
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
joyce-0.2.6 example/lib/models/game.rb
joyce-0.2.5 example/lib/models/game.rb
joyce-0.2.4 example/lib/models/game.rb
joyce-0.2.3 example/lib/models/game.rb
joyce-0.2.2 example/lib/models/game.rb
joyce-0.2.1 example/lib/models/game.rb
joyce-0.2.0 example/lib/models/game.rb
joyce-0.1.14 example/lib/models/game.rb
joyce-0.1.13 example/lib/models/game.rb
joyce-0.1.12 example/lib/models/game.rb
joyce-0.1.11 example/lib/models/game.rb
joyce-0.1.10 example/lib/models/game.rb
joyce-0.1.9 example/lib/models/game.rb
joyce-0.1.8 example/lib/models/game.rb
joyce-0.1.7 example/lib/models/game.rb
joyce-0.1.6 example/lib/models/game.rb
joyce-0.1.5 example/models/game.rb
joyce-0.1.4 example/models/game.rb
joyce-0.1.3 example/models/game.rb
joyce-0.1.2 example/models/game.rb