# Life expectancy.

Discussion in 'Plugin Development' started by Deckerz, Feb 24, 2013.

Not open for further replies.
1. Offline

### Deckerz

How would i go about monitoring the length of players lives and store each one and calculate the average life expectancy. If you can provide code example of similar things or for what i want i shall <3 forever

#1
2. Offline

### Bruno Lanevik

Listen to onjoin onleave ondeath events. Create a timestamp when each of those occurs. If onleave you take the onleave-onjoin timestamp and you get the time the player has been alive while on the server. Store this variable somewhere. Perhaps a map or list with the playername as key and the timestamp as value. Add the timestamp each time the player joins and leaves. Incase of a ondeath event you take the (ondeath - onjoin) + timealive and store that in another list. Then you can calculate the average by calculating the average from the last list

#2
3. Offline

### Jozeth

declanmc96 You could get how long they lived for per death then work out an average on all the times.

Jozeth:
Deaths (minutes):
- 5
- 6
- 4

(5+6+4)/3 = 5 minutes is their average life expectancy.

#3
4. Offline

### Deckerz

Thx for the quick reply, i want something like minez life thing but i cant thinks of how to do it without it make a file huge

would MySQL make it big if you just stored the time lived

#4
5. Offline

### Bruno Lanevik

Code:
```long join,death,leave;
Map<String, Integer> aliveTime
Map<String, Integer> deathTime
onJoin(Event e){
join = System.currentTimeMillis() / 1000L;
}
onLeave(Event e){
leave = (System.currentTimeMillis() / 1000L)-join;
aliveTime.put(e.getPlayer().getListName(),leave)
}
onDeath(Event e){
death = (System.currentTimeMillis() / 1000L)-join;
death + aliveTime.get(e.getPlayer().getListName());
deathTime.put(e.getPlayer().getListName,death);
}
calcDeath(){
//Iterate the deathTime Map and calculate the average from the values
}```
Try something like this, note this is just pseudo code

#5
6. Offline

### Jozeth

Depends some admins like databases whilst some like files. It's up to you tbh. But if you do get tons of players and tons of deaths then it will get large.

#6
7. Offline

### Deckerz

wouldn't that join get overwritten when every player joins?

Edit didn't see you say pseudo

#7
8. Offline

### Bruno Lanevik

If you are only gonna need it locally use a sqlitedb

It's just pseudo code to get him started

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 31, 2016
#8
9. Offline

### Deckerz

for a global life expectancy could you just start a number at zero and add 1 each second then divide it by how many deaths there has been

#9
10. Offline

### Bruno Lanevik

Yes but using unixtimestamp would be less redundant.

#10