Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Last active August 29, 2015 14:01
Show Gist options
  • Save DarkSeraphim/690bf25b9cef99a50dd4 to your computer and use it in GitHub Desktop.
Save DarkSeraphim/690bf25b9cef99a50dd4 to your computer and use it in GitHub Desktop.
Recovering factions from log entries
package net.darkseraphim.fr;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author DarkSeraphim
*/
public class Faction
{
public static final Pattern create = Pattern.compile("^.*\\[Factions v1\\.6\\.9\\.5\\] (.*) created a new faction: (.*)");
public static final Pattern tag = Pattern.compile("^.*\\[PLAYER_COMMAND\\] (.*): /f tag (.*)");
public static final Pattern disband = Pattern.compile("^.*\\[Factions v1\\.6\\.9\\.5\\] The faction (.*) \\((.*)\\) [was|has been] disbanded");
public static final Pattern join = Pattern.compile("^.*\\[Factions v1\\.6\\.9\\.5\\] (.*) joined the faction (.*)\\.");
public static final Pattern leave = Pattern.compile("^.*\\[Factions v1\\.6\\.9\\.5\\] (.*) left the faction: (.*)");
public static final Pattern mod = Pattern.compile("^.*\\[PLAYER_COMMAND\\] (.*): /f mod (.*)"); // Verify rank admin
public static final Pattern admin = Pattern.compile("^.*\\[PLAYER_COMMAND\\] (.*): /f admin (.*)");
public static final Pattern kick = Pattern.compile("^.*\\[Factions v1\\.6\\.9\\.5\\] (.*) kicked (.*) from the faction: (.*)");
public static final Pattern validtag = Pattern.compile("^[0-9a-zA-Z]{3,11}");
private static final Map<String, Faction> facs = new HashMap<String, Faction>();
private static final Map<String, Faction> players = new HashMap<String, Faction>();
protected Faction(String name)
{
this.name = name.toLowerCase();
}
String name;
String a;
Set<String> m = new HashSet<String>();
Set<String> members = new HashSet<String>();
public boolean isMod(String name)
{
return isAdmin(name) || m.contains(name);
}
public boolean isAdmin(String name)
{
return this.a != null && this.a.equalsIgnoreCase(name);
}
// STATICS
public static void createFaction(String name, String admin)
{
if(!Faction.validtag.matcher(name).matches())
return;
Faction fac = new Faction(name);
fac.a = admin;
fac.members.add(admin);
facs.put(name.toLowerCase(), fac);
players.put(admin.toLowerCase(), fac);
if(name.equalsIgnoreCase("heroes"))
System.out.println(admin+" created "+name);
if(admin.equalsIgnoreCase("joshuatodd21"))
System.out.println("joshuatodd21 created faction "+name);
}
public static void disband(Faction fac)
{
if(fac.name.equalsIgnoreCase("heroes"))
System.out.println("HEROES WAS DISBANDED ASDANDKLAMKLSLAMS");
for(String member : fac.members)
players.remove(member.toLowerCase());
for(Map.Entry<String, Faction> entry : players.entrySet())
if(entry.getValue() != null && entry.getValue().name.toLowerCase().equals(fac.name.toLowerCase()))
entry.setValue(null);
facs.remove(fac.name.toLowerCase());
fac.members.clear();
fac.m.clear();
System.out.println("Disband: "+fac.name);
}
public static void tag(Faction fac, String user, String tag)
{
if(!Faction.validtag.matcher(tag).matches())
return;
if(fac.isMod(user))
{
facs.remove(fac.name.toLowerCase());
fac.name = tag;
facs.put(fac.name.toLowerCase(), fac);
}
}
public static void join(Faction fac, String user)
{
fac.members.add(user);
Faction.players.put(user.toLowerCase(), fac);
if(fac.name.equalsIgnoreCase("heroes"))
System.out.println(user+" joined "+fac.name+". We have "+fac.members.size()+" members");
if(user.equalsIgnoreCase("joshuatodd21"))
System.out.println("joshuatodd21 joined faction "+fac.name);
}
public static void leave(Faction fac, String user)
{
fac.members.remove(user);
fac.m.remove(user);
Faction.players.remove(user.toLowerCase());
if(fac.name.equalsIgnoreCase("heroes"))
System.out.println(user+" left "+fac.name+". We have "+fac.members.size()+" members");
}
public static void mod(Faction fac, String user, String other)
{
if(fac.isAdmin(user))
if(fac.m.contains(other))
fac.m.remove(other);
else
fac.m.add(other);
}
public static void admin(Faction fac, String user, String other)
{
if(fac.name.equalsIgnoreCase("heroes"))
{
System.out.println(fac.name + ": " + user + " -> " + other);
}
if(fac.isAdmin(user))
{
fac.m.remove(other);
fac.a = other;
fac.m.add(user);
}
}
public static void kick(Faction fac, String user)
{
fac.members.remove(user);
fac.m.remove(user);
Faction.players.remove(user.toLowerCase());
if(fac.name.equalsIgnoreCase("heroes"))
System.out.println(user+" got kicked from "+fac.name+". We have "+fac.members.size()+" members");
}
static boolean heroes = false;
static boolean dis = false;
public static void process(String line)
{
if(!heroes)
{
heroes = facs.containsKey("heroes");
if(heroes)
System.out.println("Heroes was found");
}
else
{
if(!facs.containsKey("heroes") && !dis)
{
dis = true;
System.out.println("HEROES DISAPPEARED O.o");
}
}
Matcher m = Faction.create.matcher(line);
if(m.matches())
{
String user = m.group(1);
String fname = m.group(2);
if(Faction.players.get(user.toLowerCase()) != null || Faction.facs.get(fname.toLowerCase()) != null)
return;
Faction.createFaction(fname, user);
return;
}
m = Faction.disband.matcher(line);
if(m.matches())
{
Faction f = Faction.facs.get(m.group(1).toLowerCase());
if(f != null)
Faction.disband(f);
return;
}
m = Faction.tag.matcher(line);
if(m.matches())
{
String user = m.group(1);
String tag = m.group(2);
Faction f = Faction.players.get(user.toLowerCase());
if(f != null)
Faction.tag(f, user, tag);
return;
}
m = Faction.join.matcher(line);
if(m.matches())
{
String user = m.group(1);
Faction f = Faction.facs.get(m.group(2).toLowerCase());
//System.out.println("Joining "+f);
if(f != null)
Faction.join(f, user);
return;
}
m = Faction.leave.matcher(line);
if(m.matches())
{
String user = m.group(1);
Faction f = Faction.players.get(user.toLowerCase());
if(f != null)
{
if(!f.name.equalsIgnoreCase(m.group(2)))
{
System.out.println("Player " + user + " left faction " + m.group(2) + " even though he was in faction " + f.name);
Faction x = facs.get(f.name.toLowerCase());
if(x == null)
System.out.println("Old fac was not found");
else if (x.members.contains(user))
System.out.println("He was in fac " + f.name);
x = facs.get(m.group(2).toLowerCase());
if(x == null)
System.out.println("Old fac was not found");
else if (x.members.contains(user))
System.out.println("He was in fac " + f.name);
Faction.leave(f, user);
}
}
return;
}
m = Faction.mod.matcher(line);
if(m.matches())
{
String user = m.group(1);
Faction f = Faction.players.get(user.toLowerCase());
if(f != null)
Faction.mod(f, user, m.group(2));
return;
}
m = Faction.admin.matcher(line);
if(m.matches())
{
String user = m.group(1);
Faction f = Faction.players.get(user.toLowerCase());
if(f != null)
Faction.admin(f, user, m.group(2));
return;
}
m = Faction.kick.matcher(line);
if(m.matches())
{
Faction f = Faction.facs.get(m.group(3).toLowerCase());
if(f != null)
Faction.kick(f, m.group(2));
return;
}
}
public static void print()
{
for(Faction f : facs.values())
{
System.out.println(f.name);
for(String m : f.members)
{
if(f.isAdmin(m))
System.out.println(" **"+m);
else if(f.isMod(m))
System.out.println(" *"+m);
else
System.out.println(" "+m);
}
System.out.println();
}
}
}
package net.darkseraphim.fr;
import java.io.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;
/**
* @Author DarkSeraphim
*/
public class Main
{
TreeMap<Long, TreeMap<Integer, File>> files = new TreeMap<Long, TreeMap<Integer, File>>();
public static void main(String[] args) throws IOException
{
Faction fac = new Faction("I will never exist, but I exist to keep us alive");
new Main().run();
//System.out.println(Faction.join.matcher("[00:46:57] [Server thread/INFO]: [Factions v1.6.9.5] prazenak joined the faction FireLight.").matches());
}
public void run() throws IOException
{
File dir = new File("FactionsRecovery\\logs");
if(!dir.exists())
{
System.out.println(dir.getAbsolutePath());
return;
}
for(File log : dir.listFiles())
{
String name = log.getName();
if(name.endsWith(".log"))
name = name.substring(0, name.length() - 4);
if(name.equalsIgnoreCase("latest"))
{
TreeMap<Integer, File> map = new TreeMap<Integer, File>();
map.put(0, log);
this.files.put(Long.MAX_VALUE, map);
}
else
{
String[] date = name.split("-");
long time = new Date(toInt(date[0]), toInt(date[1]), toInt(date[2])).getTime();
forDate(time).put(toInt(date[3]), log);
}
}
for(Map.Entry<Long, TreeMap<Integer, File>> entry : this.files.entrySet())
{
for(Map.Entry<Integer, File> fe : entry.getValue().entrySet())
processFile(fe.getValue());
}
Faction.print();
}
private static int toInt(String s)
{
return Integer.parseInt(s);
}
private TreeMap<Integer, File> forDate(long timestamp)
{
TreeMap<Integer, File> map = this.files.get(timestamp);
if(map == null)
{
map = new TreeMap<Integer, File>();
this.files.put(timestamp, map);
}
return map;
}
public void processFile(File file) throws IOException
{
System.out.println(file.getName());
BufferedReader br = null;
FileReader fr = null;
try
{
fr = new FileReader(file);
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null)
{
Faction.process(line);
}
}
finally
{
try
{
if(fr != null)
fr.close();
if(br != null)
br.close();
}
catch(IOException ex)
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment