Usuário:SandroHcBot/Código Fonte/Main

De RuneScape Wiki
Ir para: navegação, pesquisa

Código principal que chama classes de tarefas relevantes.

/*--------------------------------------------------------
 * AmauriceBot - RuneScape Wikia update task robot
 * Copyright (c) 2009-2012  Maurice Abraham.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *   None
 *------------------------------------------------------*/

package amauricebot;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.util.Date;

@Description(summary = "Código principal que chama classes de tarefas relevantes.")
public class Main {
	public static void main(String[] args) {
		if (args.length < 2)
			usageError(); // does not return

		String wikiUser = args[0];
		String wikiPass = args[1];

		try {
			WikiSession wiki = new WikiSession();
			if (!wiki.login(wikiUser, wikiPass)) {
				System.err.println("Ocorreu um erro ao iniciar sessão");
				return;
			}

			Stats stats = new Stats(wiki);

			if (args.length >= 3 && args[2].equals("auto")) {
				Config config = new Config(wiki);
				for (String taskName : config.getRunList()) {
					performTask(wiki, taskName, stats);
				}
			} else if (args.length > 2) {
				for (int i = 2; i < args.length; i++) {
					performTask(wiki, args[i], stats);
				}
			} else {
				System.out.println("Para sair, escreve 'exit' e para uma lista de tarefas escreve 'help'");
				BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
				while (true) {
					System.out.print("Tarefa> ");
					String taskName = reader.readLine();
					if (taskName == null)
						break;
					taskName = taskName.toLowerCase();

					if (taskName.equals("exit") || taskName.equals("quit"))
						break;
					if (taskName.length() < 2)
						taskName = "help";

					performTask(wiki, taskName, stats);
				}
			}

			wiki.logout();
		} catch (Exception ex) {
			Utils.log("EXCEPTION: " + ex.toString());
			ex.printStackTrace();
		}
	}

	private static void usageError() {
		System.err.println("Uso:");
		System.err
				.println("  java -jar AmauriceBot.jar nomeDeUtilizador senha [tarefa]");
		System.err.println("");
		performTask(null, "help", null);
		System.exit(1);
	}

	private static void performTask(WikiSession wiki, String taskName,
			Stats stats) {
		try {
			// get task class
			String className = "amauricebot."
					+ taskName.substring(0,1).toUpperCase()
					+ taskName.substring(1).toLowerCase() + "Task";
			Class<?> taskClass = Class.forName(className);

			// get task constructor
			Class<?> paramTypes[] = new Class[1];
			paramTypes[0] = WikiSession.class;
			Constructor<?> taskConstructor = taskClass.getConstructor(paramTypes);

			// construct task object
			Object paramValues[] = new Object[1];
			paramValues[0] = wiki;
			BotTask task = (BotTask) taskConstructor.newInstance(paramValues);

			// perform task
			Date started = new Date();
			task.perform();
			Date finished = new Date();

			// update statistics
			if (taskName.equals("log"))
				stats.save();
			else if (!taskName.equals("help"))
				stats.setTaskStats(taskName, started, finished);
		} catch (ClassNotFoundException ex) {
			System.err.println("A tarefa '" + taskName + "' não existe");
		} catch (Exception ex) {
			Utils.log("EXCEPTION: " + ex.toString());
			ex.printStackTrace();
		}
	}
}