Skip to content

Instantly share code, notes, and snippets.

@akoskovacs
Last active April 17, 2022 15:22
Show Gist options
  • Save akoskovacs/697352eb826ceff7613800104e64e4ea to your computer and use it in GitHub Desktop.
Save akoskovacs/697352eb826ceff7613800104e64e4ea to your computer and use it in GitHub Desktop.
Ugly/slow/unoptimized/naive PoC r/place exporter from DB to PNG
#!/usr/bin/ruby
require 'io/console'
require 'pg'
require 'chunky_png'
WIDTH = 2000
HEIGHT = 2000
ROWS = 10000
OUTFILE = 'rplace.png'
UNTIL = '2022-04-04 18:00:00'
begin
puts "RPlaceRender v0.1 (C) Akos Kovacs"
pass = IO::console.getpass "DB Password: "
con = PG.connect :dbname => 'rplace', :user => 'akos', :password => pass
png = ChunkyPNG::Image.new(WIDTH, HEIGHT, ChunkyPNG::Color::TRANSPARENT)
puts "User: #{con.user} Database name: #{con.db}"
con.transaction do
con.exec "DECLARE curhist SCROLL CURSOR FOR SELECT h.id, pos_x, pos_y, pixel_color, timestamp FROM history h JOIN palette p ON h.palette_id = p.id WHERE timestamp <= '#{UNTIL}' ORDER BY timestamp ASC"
x = y = hexcolor = timestamp = nil
while true
rows = con.exec "FETCH FORWARD #{ROWS} FROM curhist"
#puts "FETCHed #{rows.ntuples}..."
#puts "(#{x}, #{y}) = '#{hexcolor}' @ [#{timestamp}] first of #{rows.ntuples} ROWS" if x != nil
#rows.fields.collect.each_with_index { |r, i| puts "#{i}. - #{r}" }
#puts rows.fields.collect {|fname| "%-15s" % [fname] }.join( '' )
if rows.ntuples == 0
puts "No other rows remained, quit."
con.exec "CLOSE curhist"
break
end
rows.each do |row|
x = row["pos_x"].to_i
y = row["pos_y"].to_i
hexcolor = row["pixel_color"]
timestamp = row["timestamp"]
#puts "(#{x}, #{y}) = '#{hexcolor}' @ [#{timestamp}]"
png[x, y] = ChunkyPNG::Color.from_hex(hexcolor)
end
end
end
png.metadata['Author'] = 'RPlaceRender by Akos Kovacs'
png.save("#{UNTIL}_#{OUTFILE}", :fast_rgba)
rescue PG::Error => e
puts e.message
ensure
con.close if con
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment