シミュレーション英雄連盟ゲームボックスの英雄ランキングの実現

3822 ワード

シミュレーション英雄連盟ゲームボックスの英雄ランキングの実現


コード実装:

 //      ,    (    )    ,    redis    
    static class HeroScoreGenerate implements Runnable{

        public void run() {
            String[] heros = new String[]{"  ","   ","  ","   ","  ","  ","  ","   "};

            Random random = new Random();
            Jedis jedis = new Jedis("192.168.90.131");
            //              
            while(true){
                try {
                    Thread.sleep(100+random.nextInt(10)*100);
                    //         ,                   
                    int index = random.nextInt(8);
                    String hero = heros[index];
                    // redis             
                    jedis.zincrby("TopRankChuChang",1,hero);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //         
    public static void main(String[] args) {
        new Thread(new HeroScoreGenerate()).start();

        Jedis jedis = new Jedis("192.168.90.131");
        int i = 1;
        //           
        while(true){

            try {
                Thread.sleep(3000);
                Set heros = jedis.zrevrangeWithScores("TopRankChuChang", 0, -1);
                System.out.printf(" %s      :",i);
                System.out.println();
                for(Tuple t:heros){
                    System.out.println(t.getElement()+":"+t.getScore());
                }
                i++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }