class Reachy::Game
Attributes
Public Class Methods
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 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 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 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
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
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 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 round sticks
# File lib/reachy/game.rb, line 217 def print_current_sticks @scoreboard.last.print_sticks end
Print scoreboard header Round
Joshua Kenta Thao
# File lib/reachy/game.rb, line 171 def print_header #maxlen = [@players.max_by(&:length).length, 5].max printf "%-#{COL_SPACING}s", "Round" @players.each do |p| printf "%-#{COL_SPACING}s", p end puts nil end
Print last round scores Round
Joshua Kenta Thao E1B1R2 33400 39800 31800
# File lib/reachy/game.rb, line 203 def print_last_round self.print_header @scoreboard[-2].print_scores(nil) end
Print entire scoreboard
# File lib/reachy/game.rb, line 181 def print_scoreboard self.print_title (COL_SPACING * (@mode+1)).times { printf "-" } puts nil self.print_header @scoreboard[0].print_scores(nil) # print init scores @scoreboard[1..-2].each.with_index(1) do |curr,i| delta = {} prev = @scoreboard[i-1] prev.scores.each do |k,v| delta[k] = (curr.scores[k]-prev.scores[k]).to_f/1000 end curr.print_scores(delta) end # print ongoing round @scoreboard.last.print_scores(nil,true) puts nil end
Print 1-line game title game1: 3P (E1B1R1) ~ Joshua, Kenta, Thao ~ 2016-11-05T14:05:00-07:00
# File lib/reachy/game.rb, line 210 def print_title printf "%s: %dP (%s) ~ %s ~ %s", @filename, @mode, @scoreboard[-2].name, @players.join(", "), @last_updated puts nil end
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 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
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 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 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