Check events that occurred in a given period of time

Asked

Viewed 250 times

-1

I need to register the following event in my program:

  • Players who kill 5 times during the 1 minute break 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:


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

30/07/2013 15:36:04 - Roman nick Killed 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

The time Date of the death of opponents is defined in the user class by the Murder class list.

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

  • Create a Thread that checks and counts system time. Doubt: Do you need all 4 next deaths to be in less than 60s of the first? Or can you implement to check if there have been more than 5 deaths in a minute?

  • @Kyllopardiun the stop is like this, I am uploading a log of a file that has the departure information... then from it I submit the information.... and on "Doubt: need all the next 4 deaths to be in less than 60s of the first?" Yes need to occur in a 1-minute range

  • Take a look at this link here: http://www.java2s.com/Code/Java/Threads/Simple-Threads.htm It teaches how to make threads...what you need is to make a thread run for 60 seconds decreasing and doing something like a "deaths++" and if it reaches 5 you fire your event.

  • @Dante I’m not seeing the need to use a thread, since I already have everything, the amount of murders the guy has and the date he killed... the file I upload tells me that understand??? I’m not sure how to use the information...

  • 1

    then put the file in your question and also put the part of the code that reads it.

  • @Blacksheep, would you mind using JavaFX in a solution?

  • If you already have all the information what is missing? Make a code that reads the file (which should be in text format probably) and each line identifies who the guy is by name and adds 1 kill to him according to the time differential

Show 2 more comments

1 answer

0


Since you did not specify if you have any aversion to Javafx, this is probably what you are looking for, as this technology allows lists with Listeners ie it is only 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;
        }
}
  • I will see here... I think I can have a direction with this there, I did not know this Javafx... so I looked here not have in the jar Repospositorios of Maven right... but I will manage here... valeu @Kyllopardiun

Browser other questions tagged

You are not signed in. Login or sign up in order to post.