class Reachy::Game

Attributes

created_at[R]
filename[R]
last_updated[RW]
mode[R]
players[R]
scoreboard[RW]

Public Class Methods

new(filename, ondisk=true, players=[]) click to toggle source

Initialize game from given hash Param: filename - name of json file

ondisk   - whether Game object exists on disk
players  - array of player names associatied with this game
# File lib/reachy/game.rb, line 24
def initialize(filename, ondisk=true, players=[])
  if ondisk
    # Game object exists on disk. Read it from file.
    self.read_data(filename)
  else
    # Create new Game object with starting values.
    @filename = filename
    @created_at = DateTime.now
    @last_updated = DateTime.now
    @mode = players.length
    @players = players
    self.initialize_scoreboard
    self.write_data
  end
  @plist = @players.map {|x| x.downcase}
end

Public Instance Methods

add_riichi(player) click to toggle source

Add riichi stick declared by player Param: player - string of player's name Return: true if successful, else false

# File lib/reachy/game.rb, line 158
def add_riichi(player)
  if @players.map(&:downcase).include? player
    ret = @scoreboard.last.add_riichi(player)
    if ret then self.write_data end
    return ret
  else
    printf "Error: \"%s\" not in current game's players list\n", player
    return false
  end
end
add_round(type, dealer, winner, loser, hand) click to toggle source

Add new round result

# File lib/reachy/game.rb, line 103
def add_round(type, dealer, winner, loser, hand)
  if not @scoreboard.last.update_round(type, dealer, winner, loser, hand)
    printf "  An error occurred while updating round score.\n" \
           "  Please check your input (winner, hand) and try again.\n\n"
    return
  end
  self.clone_last_round
  self.write_data
end
clone_last_round(to_next=false) click to toggle source

Clone last round as next round and add to scoreboard Param: to_next - bool indicating whether to move to next round (optional)

# File lib/reachy/game.rb, line 115
def clone_last_round(to_next=false)
  new_round = @scoreboard.last.clone
  if (to_next || new_round.name == "") then new_round.next_round end
  new_round.update_name
  @scoreboard << new_round
end
delete_from_disk() click to toggle source

Move data file of this game to trash

# File lib/reachy/game.rb, line 136
def delete_from_disk
  FileUtils.mv(DATA_PATH + @filename,
               TRASH_PATH + @filename)
end
initialize_scoreboard() click to toggle source

Populate @scoreboard with starting Round objects

# File lib/reachy/game.rb, line 42
def initialize_scoreboard
  # Make initial scores e.g. { "joshua" => 35000, "kenta" => 35000, "thao" => 35000 }
  start_score = mode == 3 ? Scoring::P_START_3 : Scoring::P_START_4
  init_scores = Hash[ @players.map{ |p| [p.downcase, start_score] } ]
  init_round = {"name" => "",
                "wind" => nil,
                "number" => 0,
                "bonus" => 0,
                "riichi" => 0,
                "scores" => init_scores}
  @scoreboard = [Round.new(init_round)]
  self.clone_last_round(true)
end
populate(db) click to toggle source

Populate Game object with info from hash Param: db - hash of game data

# File lib/reachy/game.rb, line 58
def populate(db)
  @filename = db["filename"]
  @created_at = DateTime.parse(db["created_at"])
  @last_updated = DateTime.parse(db["last_updated"])
  @mode = db["mode"]
  @players = db["players"]
  @scoreboard = []
  db["scoreboard"].each do |round|
    @scoreboard << Round.new(round)
  end
end
print_current_sticks() click to toggle source

Print current round sticks

print_header() click to toggle source

Print scoreboard header Round Joshua Kenta Thao

print_last_round() click to toggle source

Print last round scores Round Joshua Kenta Thao E1B1R2 33400 39800 31800

print_scoreboard() click to toggle source

Print entire scoreboard

print_title() click to toggle source

Print 1-line game title game1: 3P (E1B1R1) ~ Joshua, Kenta, Thao ~ 2016-11-05T14:05:00-07:00

read_data(filename) click to toggle source

Read JSON database file and repopulate object

# File lib/reachy/game.rb, line 85
def read_data(filename)
  filepath = DATA_PATH + filename
  file = File.read(filepath)
  db = JSON.parse(file)
  self.populate(db)
end
remove_last_round() click to toggle source

Remove latest round from scoreboard

# File lib/reachy/game.rb, line 123
def remove_last_round
  if @scoreboard.length > 2
    @scoreboard.pop
    @scoreboard.pop
    self.clone_last_round
    self.write_data
  else
    printf "Error: Current game already in initial state. " \
      "No round deleted.\n"
  end
end
to_h() click to toggle source

Return Hash object representing Game object

# File lib/reachy/game.rb, line 71
def to_h
  hash = {}
  self.instance_variables.each do |var|
    hash[var.to_s[1..-1]] = self.instance_variable_get(var)
  end
  hash["scoreboard"] = []
  @scoreboard.each do |r|
    hash["scoreboard"] << r.to_h
  end
  hash.delete("plist")
  return hash
end
validate_players(players) click to toggle source

Validate players input Param: players - list of players to check Return: true if all players in list are in this game, else false

# File lib/reachy/game.rb, line 144
def validate_players(players)
  flag = true
  players.each do |p|
    if not @plist.include?(p)
      printf "Error: Player \"%s\" not in current list of players\n", p
      flag = false
    end
  end
  return flag
end
write_data() click to toggle source

Write Game object to JSON database file

# File lib/reachy/game.rb, line 93
def write_data
  @last_updated = DateTime.now
  hash = self.to_h
  filepath = DATA_PATH + @filename
  File.open(filepath, "w") do |f|
    f.write(JSON.pretty_generate(hash))
  end
end