class ElGato::GameServer

Constants

URI

The URI for the server to connect to

Attributes

waiting_players[R]

Public Class Methods

new() click to toggle source
# File lib/el_gato/game_server.rb, line 22
def initialize
  @games = []
  @waiting_players = []
end
start() click to toggle source
# File lib/el_gato/game_server.rb, line 13
def self.start
  @proccess_id = fork do
    DRb.start_service(URI, new)
    DRb.thread.join
  end
end
stop() click to toggle source
# File lib/el_gato/game_server.rb, line 9
def self.stop
  Process.kill 'KILL', @proccess_id
end

Public Instance Methods

games(player_id) click to toggle source
# File lib/el_gato/game_server.rb, line 46
def games player_id
  @games.find_all do |game|
    game.includes_player? BareGato::Player.new player_id
  end.map do |game|
    {
      id: game.id,
      status: :on_progress
    }
  end
end
move(game_id, player_id, args) click to toggle source
# File lib/el_gato/game_server.rb, line 57
def move game_id, player_id, args
  game = find_game game_id
  player = BareGato::Player.new player_id

  raise 'Not your turn bro!' unless game && player && game.next?(player)

  game.play BareGato::Move.new player, args
end
play(player_id) click to toggle source
# File lib/el_gato/game_server.rb, line 27
def play player_id
  add_player player_id

  if waiting_players.size == 2
    @games << BareGato::Game.new(players: waiting_players.dup)
    waiting_players.clear
    status = { status: :ready, id: @games.last.id }

  elsif (games = games(player_id)).size > 0
    status = :ready
    status = { status: :ready, id: games.last[:id] }

  else
    status = { status: :queued }
  end

  status
end

Private Instance Methods

add_player(player_id) click to toggle source
# File lib/el_gato/game_server.rb, line 68
def add_player player_id
  player = BareGato::Player.new player_id
  if !waiting_players.include?(player) && games(player_id).size == 0
    waiting_players << BareGato::Player.new(player_id)
  end
end
find_game(game_id) click to toggle source
# File lib/el_gato/game_server.rb, line 75
def find_game game_id
  @games.find do |game|
    game.id == game_id
  end
end