Check events that occurred in a certain period of time

-1

I need to register the following event in my program:

  • Players who kill 5 times during the 1 minute interval will win a prize.

My class mapping is as follows:

public class Game {
    private String name;
    private HashMap<String,Player> players;
    private Player serialKiller;
    private Integer streak; 
}

public class Player implements Comparable<Player> {
        private String name;
        private List<Murder> murderList = new ArrayList<Murder>();
        private List<Death> deathList = new ArrayList<Death>();
}

public class Murder {
    private Date date;
}

Log file:

  

07/30/2013 15:34:22 - New match 11348965 has started

     

30/07/2013 15:36:04 - Roman killed Nick using M16

     

30/07/2013 15:36:05 - Roman killed mane using M16

     

30/07/2013 15:36:06 - Roman killed jose using M16

     

30/07/2013 15:36:07 - Roman killed trevis using M16

     

30/07/2013 15:36:08 - Roman killed santana using M16

     

07/30/2013 15:36:05 - noob1 killed noob2 using M16

     

07/30/2013 15:36:06 - noob1 killed noob3 using M16

     

07/30/2013 15:36:07 - noob1 killed noob4 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

07/30/2013 15:36:07 - noob10 killed noob1 using M16

     

30/07/2013 15:36:33 - killed Nick by DROWN

     

07/30/2013 15:39:22 - Match 11348965 has ended

% time of opponents' death is defined in the user class by the Murder class list.

How can I find out if the player will win the prize without using Threads?

    
asked by anonymous 30.07.2014 / 20:29

1 answer

0

As you have not specified whether you have an aversion to JavaFX, this is probably what you are looking for, since this technology allows lists with Listeners or it is just a matter of checking whenever the list is modified if the player deserves the prize .

public class Player implements Comparable<Player> {
        private String name;
        private List<Death> deathList = new ArrayList<Death>();
        private ObservableList<Murder> murderList = FXCollections.observableArrayList();
        public Player(){
            this.murderList.addListener(new ListChangeListener<Murder>(){
                @Override
                public void onChanged(ListChangeListener.Change<? extends Murder> c) {
                    if(murderList.size()>5){
                        int lastIndex = murderList.size()-1;
                        Date lastMurder = murderList.get(lastIndex).getDate();
                        boolean flag = true;
                        for(int i=lastIndex-1;i>=lastIndex-5;i--){
                            Date tempDate  = murderList.get(i).getDate();
                            //se em algum caso for maior que 60 descarta o prêmio
                            if(timeDiff(tempDate,lastMurder)>60){
                                flag = false;
                                break;
                            }
                        }
                        if(flag){
                            //receivePrize();
                        }
                    }   
                }
            });
        }
        public static long timeDiff(Date d1, Date d2){
            return (d2.getTime() - d1.getTime())/1000;
        }
}
    
30.07.2014 / 21:56