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

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

Classe que lida com configurações.

/*--------------------------------------------------------
 * 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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Description(summary = "Classe que lida com configurações.")
public class Config extends LinkedHashMap<String,String>
{
	private static final long serialVersionUID = 1L;

	public Config(WikiSession wiki)
    {
        super();
        String configPage = "Utilizador:" + wiki.getLoginUser() + "/Config";

        try
        {
            String text = wiki.getText(configPage);
            if (text == null)
                return;
            
            for (String line : text.split("\n"))
            {
                if (!line.matches("^\\w.*"))
                    continue;
                
                String[] toks = line.split("=", 2);
                if (toks.length >= 2)
                    put(toks[0].trim(), toks[1].trim());
            }   
        }
        catch (Exception ex)
        {
            Utils.log("AVISO: Não foi possível ler a configuração [[" + configPage
                        + "]] :" + ex);            
        }
    }
    
    public List<String> getRunList()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE");
        String dayOfWeek = sdf.format(new Date());
        Pattern keyPattern = Pattern.compile("^(\\w+)\\.run$");
        List<String> list = new ArrayList<String>();
        for (String key : keySet())
        {
            String value = get(key);
            Matcher matcher = keyPattern.matcher(key);
            if (!matcher.matches())
                continue;
            String taskName = matcher.group(1);
            if (value.contains(dayOfWeek) || value.contains("All"))
                list.add(taskName);
            if (list.size() >= 20)
                break; // too many
        }
        return list;
    }
}