Skip to content

Instantly share code, notes, and snippets.

@hellok
Created March 15, 2014 14:14

Revisions

  1. hellok created this gist Mar 15, 2014.
    120 changes: 120 additions & 0 deletions -
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    #!/usr/bin/env ruby
    require 'open-uri'
    require 'json'
    require 'colorize'

    RANDOM = false

    MARGIN_TOP = 2
    MARGIN_LEFT = 3
    SERVICE_CELL = " " * 3

    #API = "http://127.0.0.1:8000/blue.txt"
    API = "http://pwn.ztx.io/api/scoreboard/1"

    COLORS = [ :on_green, :on_red, :on_yellow, :on_blue, :to_s ]
    ANSI_CLEAR = "\e[H\e[2J"


    hash="{ap:1,dp:2,cp:3}"

    BANNER = <<-JS
    8 8 o o
    8 8 8
    8oPYo. .oPYo. .oPYo. 8 .o o8 odYo. .oPYo. .oPYo. o8P .oPYo. oPYo.
    8 8 .oooo8 8 ' 8oP' 8 8' `8 8 8 Yb.. 8 .oooo8 8 `'
    8 8 8 8 8 . 8 `b. 8 8 8 8 8 'Yb. 8 8 8 8
    8 8 `YooP8 `YooP' 8 `o. 8 8 8 `YooP8 `YooP' 8 `YooP8 8
    ..:::..:.....::.....:..::...:....::..:....8 :::.....:::..::.....:..::::
    :::::::::::::::::::::::::::::::::::::::ooP'.:::::::::::::::::::::::::::
    :::::::::::::::::::::::::::::::::::::::...:::::::::::::::::::::::::::::
    JS


    class Team
    def initialize(hash)
    @hash = JSON.parse("{\"ap\":1,\"dp\":2,\"name\":{\"a\":\"Hello\",\"b\": \"World\"}}")
    hash=@hash
    p hash['ap']
    @@total['ap'] += hash['ap']
    @@total['dp'] += hash['dp']
    @@max['ap'] = [ @@max['ap'], hash['ap'] ].max
    @@max['dp'] = [ @@max['dp'], hash['dp'] ].max
    @@name_max_len = [ hash['name'].length, @@name_max_len ].max
    end

    def score(type=nil)
    return 10
    end

    def scorep(width, type=nil)
    s = "%#{width}.2f%%" % (10.0 * score(type))
    if type and @hash[type] == @@max[type] then
    s = s.green
    end
    return s
    end

    def method_missing(m, *args)
    p "method_missing"#@hash.fetch(m.to_s)
    end

    def state(service)
    return rand 4
    end

    def colored_state(service, str)
    COLORS.map{ |m| str.send(m) }[state(service)]
    end

    def self.width()
    @@name_max_len
    end

    def self.reset()
    @@total = { "ap" => 0, "dp" => 0 }
    @@max = { "ap" => 0, "dp" => 0 }
    @@name_max_len = 0
    end
    end

    class Scoreboard
    def initialize(url=API)
    @url = url
    update()
    end

    def update()
    Team.reset()
    begin
    json = open(@url).read
    data = JSON.parse(json)
    out =(ANSI_CLEAR+BANNER.colorize(:green)+th().colorize(:white)+"\n"+hr().colorize(:white))+"\n"
    data.each do |x|
    len = x['uid'].length
    out+= ((" "*MARGIN_LEFT) +" "*(13-len)+ x['uid']+"|"+ " "*MARGIN_LEFT + x['score'].to_s()).colorize(:white)+"\n"
    end
    out+= (hr()).colorize(:white)


    #services = data['services']
    end
    end


    def th()
    r = ""
    r += " "*MARGIN_LEFT+" "*(13-4)+ [ "name", " Score " ].join(" ")
    r
    end

    def hr()
    (" " * MARGIN_LEFT) + "+-" + ("-" * 10) + "-+" + ("-" * (2 + 10 * SERVICE_CELL.length)) + ("-"+("-"*7))*3 + "+"
    end

    end

    if __FILE__ == $0
    a=Scoreboard.new()
    puts a.update()
    end