001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.plugins;
019:
020: import java.io.FileNotFoundException;
021: import java.io.IOException;
022: import java.util.Collection;
023: import java.util.Iterator;
024:
025: import net.sf.drftpd.FatalException;
026: import net.sf.drftpd.NoAvailableSlaveException;
027: import net.sf.drftpd.event.Event;
028: import net.sf.drftpd.event.FtpListener;
029: import net.sf.drftpd.event.TransferEvent;
030: import net.sf.drftpd.master.UploaderPosition;
031:
032: import org.drftpd.SFVFile;
033: import org.drftpd.dynamicdata.Key;
034: import org.drftpd.remotefile.FileStillTransferringException;
035: import org.drftpd.remotefile.LinkedRemoteFileInterface;
036: import org.drftpd.usermanager.NoSuchUserException;
037: import org.drftpd.usermanager.User;
038: import org.drftpd.usermanager.UserFileException;
039:
040: /**
041: * @author zubov
042: * @version $Id: RaceStatistics.java 1452 2006-04-03 17:32:08Z zubov $
043: */
044: public class RaceStatistics extends FtpListener {
045: public static final Key RACESWON = new Key(RaceStatistics.class,
046: "racesWon", Integer.class);
047: public static final Key RACES = new Key(RaceStatistics.class,
048: "races", Integer.class);
049: public static final Key RACESLOST = new Key(RaceStatistics.class,
050: "racesLost", Integer.class);
051:
052: public RaceStatistics() {
053: }
054:
055: public void actionPerformed(Event event) {
056: if (!event.getCommand().equals("STOR")) {
057: return;
058: }
059:
060: TransferEvent direvent = (TransferEvent) event;
061: LinkedRemoteFileInterface dir;
062:
063: try {
064: dir = direvent.getDirectory().getParentFile();
065: } catch (FileNotFoundException e) {
066: throw new FatalException(e);
067: }
068:
069: SFVFile sfvfile;
070:
071: try {
072: sfvfile = dir.lookupSFVFile();
073:
074: // throws IOException, ObjectNotFoundException,
075: // NoAvailableSlaveException
076: } catch (FileNotFoundException ex) {
077: // can't save stats with no sfv file
078: return;
079: } catch (NoAvailableSlaveException e) {
080: // can't save stats with no sfv file
081: return;
082: } catch (IOException e) {
083: // can't save stats with no sfv file
084: return;
085: } catch (FileStillTransferringException e) {
086: // can't save stats with no sfv file
087: return;
088: }
089:
090: if (!sfvfile.hasFile(direvent.getDirectory().getName())) {
091: return;
092: }
093:
094: //COMPLETE
095: if (sfvfile.getStatus().isFinished()) {
096: return;
097: }
098:
099: Collection racers = SiteBot.userSort(sfvfile.getFiles(),
100: "bytes", "high");
101:
102: if (racers.size() <= 1) {
103: return; // no race
104: }
105:
106: int count = 1;
107:
108: for (Iterator iter = racers.iterator(); iter.hasNext(); count++) {
109: UploaderPosition racer = (UploaderPosition) iter.next();
110: User user;
111:
112: try {
113: user = getGlobalContext().getUserManager()
114: .getUserByName(racer.getUsername());
115: } catch (NoSuchUserException ex) {
116: // this should not happen, but if it does, it means the user was
117: // deleted
118: // we want to ignore their stats, but the race still did happen
119: continue;
120: } catch (UserFileException ex) {
121: //if ( ex instanceof CorruptUserFileException )
122: // don't add stats to badd users
123: continue;
124: }
125:
126: if (count == 1) {
127: user.getKeyedMap().incrementObjectLong(RACESWON);
128: } else if (count == racers.size()) {
129: user.getKeyedMap().incrementObjectLong(RACESLOST);
130: } else {
131: user.getKeyedMap().incrementObjectLong(RACES);
132: }
133: }
134: }
135:
136: public void unload() {
137: }
138: }
|