KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > event > listeners > RaceStatistics


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.event.listeners;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import net.sf.drftpd.FatalException;
26 import net.sf.drftpd.NoAvailableSlaveException;
27 import net.sf.drftpd.SFVFile;
28 import net.sf.drftpd.event.Event;
29 import net.sf.drftpd.event.FtpListener;
30 import net.sf.drftpd.event.TransferEvent;
31 import net.sf.drftpd.master.ConnectionManager;
32 import net.sf.drftpd.master.UploaderPosition;
33 import net.sf.drftpd.master.usermanager.NoSuchUserException;
34 import net.sf.drftpd.master.usermanager.User;
35 import net.sf.drftpd.master.usermanager.UserFileException;
36 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
37
38 import org.drftpd.plugins.SiteBot;
39
40 /**
41  * @author zubov
42  * @version $Id: RaceStatistics.java,v 1.13 2004/05/12 00:45:05 mog Exp $
43  */

44 public class RaceStatistics implements FtpListener {
45
46     private ConnectionManager _cm;
47
48     public RaceStatistics() {
49     }
50
51     public void actionPerformed(Event event) {
52         if (!event.getCommand().equals("STOR"))
53             return;
54         TransferEvent direvent = (TransferEvent) event;
55         LinkedRemoteFileInterface dir;
56         try {
57             dir = direvent.getDirectory().getParentFile();
58         } catch (FileNotFoundException JavaDoc e) {
59             throw new FatalException(e);
60         }
61         SFVFile sfvfile;
62         try {
63             sfvfile = dir.lookupSFVFile();
64             // throws IOException, ObjectNotFoundException, NoAvailableSlaveException
65
} catch (FileNotFoundException JavaDoc ex) {
66             // can't save stats with no sfv file
67
return;
68         } catch (NoAvailableSlaveException e) {
69             // can't save stats with no sfv file
70
return;
71         } catch (IOException JavaDoc e) {
72             // can't save stats with no sfv file
73
return;
74         }
75
76         if (!sfvfile.hasFile(direvent.getDirectory().getName()))
77             return;
78
79         //COMPLETE
80
if (sfvfile.getStatus().isFinished())
81             return;
82         Collection JavaDoc racers = SiteBot.userSort(sfvfile.getFiles(), "bytes", "high");
83         if (racers.size() <= 1)
84             return; // no race
85
int count = 1;
86         for (Iterator JavaDoc iter = racers.iterator(); iter.hasNext(); count++) {
87             UploaderPosition racer = (UploaderPosition) iter.next();
88             User user;
89             try {
90                 user = _cm.getUserManager().getUserByName(racer.getUsername());
91             } catch (NoSuchUserException ex) {
92                 // this should not happen, but if it does, it means the user was deleted
93
// we want to ignore their stats, but the race still did happen
94
continue;
95             } catch (UserFileException ex) {
96                 //if ( ex instanceof CorruptUserFileException )
97
// don't add stats to badd users
98
continue;
99             }
100             if (count == 1)
101                 user.addRacesWon();
102             else if (count == racers.size())
103                 user.addRacesLost();
104             else
105                 user.addRacesParticipated();
106         }
107     }
108
109     /* (non-Javadoc)
110      * @see net.sf.drftpd.Initializeable#init(net.sf.drftpd.master.ConnectionManager)
111      */

112     public void init(ConnectionManager connectionManager) {
113         _cm = connectionManager;
114     }
115
116     public void unload() {
117
118     }
119
120 }
121
Popular Tags