Class: CEML::Bundle

Inherits:
Struct
  • Object
show all
Includes:
Redis::Objects
Defined in:
lib/ceml/models/bundle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



2
3
4
# File 'lib/ceml/models/bundle.rb', line 2

def id
  @id
end

Instance Method Details

#absorb?(player, stanza_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/ceml/models/bundle.rb', line 88

def absorb?(player, stanza_name = nil)
  join_running_incident?(player, stanza_name) or cast_player?(player, stanza_name)
end

#all_roomsObject



24
25
26
# File 'lib/ceml/models/bundle.rb', line 24

def all_rooms
  find_castables.map(&:all_rooms).flatten.uniq
end

#cast_player?(player, stanza_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ceml/models/bundle.rb', line 74

def cast_player?(player, stanza_name = nil)
  incident_id  = gen_code
  find_castables(stanza_name).each do |castable|
    if cast = grab_cast?(player, castable, seeded=stanza_name)
      castable.advertise_roles(incident_id, cast)
      i = IncidentModel.new(incident_id)
      i.bytecode.value = castable.bytecode
      i.add_castings(cast.castings)
      return incident_id
    end
  end
  false
end

#clear_from_all_rooms(player_id) ⇒ Object

waiting rooms =



20
21
22
# File 'lib/ceml/models/bundle.rb', line 20

def clear_from_all_rooms(player_id)
  CastingPool.destroy_everywhere(id, player_id)
end

#find_castables(stanza_name = nil) ⇒ Object

castables =



10
11
12
13
# File 'lib/ceml/models/bundle.rb', line 10

def find_castables(stanza_name = nil)
  return castables.value unless stanza_name
  return [castables.value.find{ |c| c.stanza_name == stanza_name }]
end

#grab_cast?(player, castable, seeded_p) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ceml/models/bundle.rb', line 48

def grab_cast?(player, castable, seeded_p)
  room_ids = castable.hot_waiting_rooms_given_player(player, seeded_p)
  # CEML.log 1, "Checking cast for #{castable.stanza_name}: #{room_ids.inspect}"
  casting_pool = CastingPool.new(room_ids)
  loop do
    hot_players = casting_pool.hot_players + [player]
    # CEML.log 1, "Hot players: #{hot_players.inspect}"
    return nil unless cast = castable.cast_from(hot_players)
    return cast if casting_pool.claim(cast.player_ids - [player[:id]])
    sleep 0.02
  end
end

#join_running_incident?(player, stanza_name = nil) ⇒ Boolean

incident joining/launching =

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/ceml/models/bundle.rb', line 65

def join_running_incident?(player, stanza_name = nil)
  rooms_for_player(player, stanza_name).each do |room_id|
    if incident_id = WaitingRoom.new(room_id).audition_for_incidents(player)
      return incident_id
    end
  end
  false
end

#register_in_rooms(player, stanza_name = nil) ⇒ Object



38
39
40
41
# File 'lib/ceml/models/bundle.rb', line 38

def register_in_rooms(player, stanza_name = nil)
  room_ids = rooms_for_player(player, stanza_name)
  CastingPool.new(room_ids).post(id, player[:id])
end

#resetObject



28
29
30
31
32
# File 'lib/ceml/models/bundle.rb', line 28

def reset
  # puts "TOTAL RESET"
  CastingTokens.destroy_all(id)
  all_rooms.each{ |room| room.waiting_incident_roles.clear }
end

#reset_player(player_id) ⇒ Object



43
44
45
46
# File 'lib/ceml/models/bundle.rb', line 43

def reset_player(player_id)
  CastingPool.destroy_everywhere(id, player_id)
  Player.new(player_id).reset
end

#rooms_for_player(player, stanza_name = nil) ⇒ Object



34
35
36
# File 'lib/ceml/models/bundle.rb', line 34

def rooms_for_player(player, stanza_name = nil)
  find_castables(stanza_name).map{ |c| c.waiting_rooms_for_player(player, stanza_name) }.flatten.uniq
end