#!/usr/bin/ruby # Stockage/update de vue # Licence GPL - 2007 - par tyrollus 72681 (Yoann Guillot) def parsehttp(s, sep='&') s.to_s.split(sep).inject({}) { |h, a| k, v = a.split('=', 2).map { |s| s.gsub('+', ' ').gsub(/%(..)/) { $1.hex.chr } } h.update k => v } end def htmlesc(s) s.gsub('<', '<').gsub('>', '>').gsub('"', '"') end if ARGV.empty? str = $stdin.read postdata = parsehttp str else postdata = {'vue' => File.read(ARGV.first)} end if not vue = postdata['vue'] puts "location: ../jjvue.html\r\n\r\n" exit end XMIN = -600 XMAX = 600 YMIN = -600 YMAX = 600 NOMS = {} IO.foreach(File.join(File.dirname(__FILE__), '../demange/.mh_cache/file_Trolls2')) { |l| mat, nom, foo = l.split(';') NOMS[mat.to_i] = nom } # parse la vue vue_parsed = {} # id => [x, y, z] vue_view = {} # y => x => n => mat vue_history = {} # id => [date, x, y, z], ...] cat = :trolls vue.each_line { |l| case l when /^\s*Monstres\s/ cat = :monstres when /^\s*TROLLS\s/ cat = :trolls when /^\s*TR.*SORS\s/ break when /^\s*#DEBUT TROLLS/ cat = :sp_trolls when /^\s*#DEBUT MONSTRES/ cat = :sp_monstres when /^\s*#FIN MONSTRES/ break end case cat when :monstres # dist mat level(mz) nom x y z if l =~ /^\s*\d+\s+(\d+)\s+(?:-|\d+\s+)?(.*\])\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)/ mat, nom, x, y, z = $1.to_i, $2, $3.to_i, $4.to_i, $5.to_i NOMS[mat] = nom vue_parsed[mat] = [x, y, z] if not NOMS[mat] File.open(__FILE__, 'a') { |ffdd| ffdd.puts "#{mat} #{nom}" } end end when :trolls # dist mat nom lvl? race guilde x y z # if l =~ /^\s*\d+\s+(\d+)\s+(.*)\s+(?:-|\d+)?\s+(?:Durakuir|Kastar|Skrim|Tomawak)\s+.*\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)/ if l =~ /^\s*\d+\s+(\d+)\s+(.*)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)/ mat, nom, x, y, z = $1.to_i, $2, $3.to_i, $4.to_i, $5.to_i NOMS[mat] = nom vue_parsed[mat] = [x, y, z] end when :sp_monstres if l =~ /^\s*(\d+);(.*);(-?\d+);(-?\d+);(-?\d+)/ mat, nom, x, y, z = $1.to_i, $2, $3.to_i, $4.to_i, $5.to_i NOMS[mat] = nom vue_parsed[mat] = [x, y, z] end when :sp_trolls if l =~ /^\s*(\d+);(-?\d+);(-?\d+);(-?\d+)/ mat, x, y, z = $1.to_i, $2.to_i, $3.to_i, $4.to_i vue_parsed[mat] = [x, y, z] end end } # prend la main sur la DB if false fdlock = nil starttime = Time.now loop do break if (fdlock = File.open('../vue.lock', 'w')).flock(File::LOCK_EX | File::LOCK_NB) == 0 fdlock.close if Time.now - starttime > 5 # timeout.. puts "content-type: text/html\r\n" puts "Refresh: 10; url=../jjvue.html\r\n" puts "\r\n" puts "vue: erreur

La db est occupee... reessayez plus tard !
la vue

" exit end sleep rand end end dbfile = '../vue.db.txt' File.open(dbfile, 'w') {} #if not File.exist? dbfile File.open(dbfile, 'r+') { |dbfd| dbfd.each_line { |l| mat, time, x, y, z = l.chomp.split(';').map { |i| i.to_i } (vue_history[mat] ||= []) << [time, x, y, z] } dbfd.seek(0, File::SEEK_END) tnow = Time.now.to_i vue_parsed.each { |mat, (x, y, z)| dbfd.puts "#{mat};#{tnow};#{x};#{y};#{z}" (vue_history[mat.to_i] ||= []) << [tnow, x, y, z] } } # libere la db #fdlock.flock(File::LOCK_UN) #fdlock.close vue_history.each { |mat, list| lastpos = list.sort.last (vue_view[[lastpos[1], lastpos[2]]] ||= []) << [lastpos[3], mat] } $vue_history = vue_history def disp_mat(mat) if nom = NOMS[mat] s = "#{mat} #{htmlesc(nom).gsub(' ', ' ')}" else s = "#{mat}" end cls = if [79037, 84776, 85102].include? mat: 'team1' elsif mat < 150000: 'team2' else 'unk' end page = mat < 150000 ? 'PJView_Events' : 'MonsterView' history = " #{mat} #{NOMS[mat]}   x\n" + $vue_history[mat].to_a.sort.map { |time, x, y, z| Time.at(time).strftime(" vu le %d à %H:%M en #{x}, #{y}, #{z}") }.join("\n") "#{s}" end # cree le fichier statique de vue begin File.open("../jjvue.html.tmp#{Process.pid}", 'w') { |fd| fd.puts < Vision lointaine et en 2d EOH xs = vue_view.keys.map { |e| e.first } ys = vue_view.keys.map { |e| e.last } fd.puts "sous la surface:" fd.puts '' fd.puts " " + (xs.min..xs.max).map { |x| "" }.join + "" (ys.min..ys.max).to_a.reverse_each { |y| fd.puts " " if vue_view.keys.find { |k| k[1] == y } (xs.min..xs.max).each { |x| fd.print " " } end fd.puts " " } fd.puts "
Y \\ X#{x}
#{y}" curz = nil fd.print vue_view[[x, y]].to_a.sort.reverse.map { |z, mat| " #{z}: " + disp_mat(mat) if z != 0 }.compact.join('
') fd.puts " 
" fd.puts 'surface:' fd.puts '' fd.puts " " + (xs.min..xs.max).map { |x| "" }.join + "" (ys.min..ys.max).to_a.reverse_each { |y| fd.puts " " if vue_view.keys.find { |k| k[1] == y } (xs.min..xs.max).each { |x| fd.print " " } end fd.puts " " } fd.puts "
Y \\ X#{x}
#{y}" curz = nil fd.print vue_view[[x, y]].to_a.sort.reverse.map { |z, mat| disp_mat(mat) if z == 0 }.compact.join('
') fd.puts " 
" fd.puts <
Vue (normale ou script publique):

EOH } rescue File.open('vue-dumperr' + Process.pid.to_s, 'w') { |ffd| ffd.puts "#{$!.class} #{$!.message} #{$!.backtrace[0..2]}" } ensure File.rename("../jjvue.html.tmp#{Process.pid}", '../html/jjvue.html') end # redirect vers la vue #puts "content-type: text/html\r\n" puts "location: ../jjvue.html\r\n" puts "\r\n"