Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
8.8 kB
2
Indexable
Never
package ballistickemu.Types;

import ballistickemu.Lobby.LobbyServer;
import ballistickemu.Main;
import ballistickemu.Tools.DatabaseTools;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;

public class StickRoom
{
  private StickClientRegistry CR;
  private String Name;
  private String MapID;
  private int CycleMode;
  private Boolean isPrivate;
  private int RoundTime;
  private int StorageKey;
  private String MapCycleList;
  private Boolean requiresPass;
  private LinkedHashMap<String, StickClient> VIPs;
  public ReentrantReadWriteLock VIPLock;
  private String CreatorName;
  
  public StickRoom()
  {
    this.CR = new StickClientRegistry(Boolean.valueOf(false));
  }
  
  public StickRoom(String paramString1, String paramString2, int paramInt, Boolean paramBoolean1, LinkedHashMap<String, StickClient> paramLinkedHashMap, Boolean paramBoolean2, String paramString3)
  {
    this.Name = paramString1;
    this.MapID = paramString2;
    this.CycleMode = paramInt;
    this.isPrivate = paramBoolean1;
    this.RoundTime = 330;
    
    this.CR = new StickClientRegistry(Boolean.valueOf(false));
    
    this.VIPs = paramLinkedHashMap;
    this.requiresPass = paramBoolean2;
    this.VIPLock = new ReentrantReadWriteLock();
    this.CreatorName = paramString3;
    Main.getLobbyServer().getRoomRegistry().scheduleRoomTimer(new OnTimedEvent());
  }
  
  public void BroadcastToRoom(StickPacket paramStickPacket)
  {
    ArrayList localArrayList = new ArrayList();
    this.CR.ClientsLock.readLock().lock();
    StickClient localStickClient;
    try {
      for (localIterator = this.CR.getAllClients().iterator(); localIterator.hasNext();) { localStickClient = (StickClient)localIterator.next();
        
        try
        {
          if (!localStickClient.getLobbyStatus().booleanValue()) {
            localStickClient.write(paramStickPacket);
          }
        }
        catch (Exception localException) {
          localException.printStackTrace();
          localArrayList.add(localStickClient);
        }
      }
    } finally {
      this.CR.ClientsLock.readLock().unlock();
    }
    for (Iterator localIterator = localArrayList.iterator(); localIterator.hasNext();) { localStickClient = (StickClient)localIterator.next();
      
      this.CR.deregisterClient(localStickClient);
    }
    localArrayList.removeAll(localArrayList);
  }
  
  public StickClientRegistry GetCR()
  {
    return this.CR;
  }
  
  public void setName(String paramString)
  {
    this.Name = paramString;
  }
  
  public void setCreatorName(String paramString)
  {
    this.Name = paramString;
  }
  
  public void setMapID(String paramString)
  {
    this.MapID = paramString;
  }
  
  public void setCycleMode(int paramInt)
  {
    this.CycleMode = paramInt;
  }
  
  public void setPrivacy(Boolean paramBoolean)
  {
    this.isPrivate = paramBoolean;
  }
  
  public void setRoundTime(int paramInt)
  {
    this.RoundTime = paramInt;
  }
  
  public void setMapCycleList(String paramString) {
    this.MapCycleList = paramString;
  }
  
  public String getName()
  {
    return this.Name;
  }
  
  public String getCreatorName()
  {
    return this.CreatorName;
  }
  
  public String getMapID()
  {
    return this.MapID;
  }
  
  public int getCycleMode()
  {
    return this.CycleMode;
  }
  
  public Boolean getPrivacy()
  {
    return this.isPrivate;
  }
  
  public Boolean getNeedsPass()
  {
    return this.requiresPass;
  }
  
  public int getCurrentRoundTime()
  {
    return this.RoundTime;
  }
  
  public int getStorageKey()
  {
    return this.StorageKey;
  }
  
  public String getMapCycleList()
  {
    if (this.MapCycleList != null)
      return this.MapCycleList;
    return null;
  }
  
  public void setStorageKey(int paramInt) {
    this.StorageKey = paramInt;
  }
  
  public void setNeedsPass(Boolean paramBoolean)
  {
    this.requiresPass = paramBoolean;
  }
  
    public void killRoom()
	{
		this.CR.ClientsLock.writeLock().lock();
		try
		{
			for (StickClient SC : this.CR.getAllClients())
			{
				SC.getIoSession().close(false);
			}
		Main.getLobbyServer().getRoomRegistry().deRegisterRoom(Main.getLobbyServer().getRoomRegistry().GetRoomFromName(Name));
		}
		finally {
			this.CR.ClientsLock.writeLock().unlock();
		}
	}
  
  public LinkedHashMap<String, StickClient> getVIPs()
  {
    return this.VIPs;
  }
  


  class OnTimedEvent
    implements Runnable
  {
    OnTimedEvent() {}
    


    public void run()
    {
      if (StickRoom.this.CR.getAllClients().size() == 0)
      {
        Main.getLobbyServer().getRoomRegistry().deRegisterRoom(Main.getLobbyServer().getRoomRegistry().GetRoomFromName(StickRoom.this.Name));
        
        try
        {
          StickRoom localStickRoom = Main.getLobbyServer().getRoomRegistry().GetRoomFromName(StickRoom.this.Name);
          if (localStickRoom != null)
            Main.getLobbyServer().getRoomRegistry().GetRoomFromName(StickRoom.this.Name).finalize();
          finalize();
        } catch (Throwable localThrowable) {
          System.out.println("There was an error deleting room " + StickRoom.this.Name);
          localThrowable.printStackTrace();
        }
        return;
      }
      
      if (StickRoom.this.RoundTime > 0) {
        StickRoom.this.RoundTime = (StickRoom.this.RoundTime - 1);
      } else
        StickRoom.this.RoundTime = 330;
      if (StickRoom.this.RoundTime == 31)
      {
        updateStats(getWinner());
      }
    }
    


    private StickClient getWinner()
    {
      int i = -1;
      Object localObject1 = null;
      StickRoom.this.CR.ClientsLock.readLock().lock();
      
      try
      {
        for (StickClient localStickClient : StickRoom.this.CR.getAllClients())
        {
          if (i == -1)
          {
            localObject1 = localStickClient;
            i = localStickClient.getGameKills();

          }
          else if ((localStickClient != null) && (localStickClient.getGameKills() > i))
          {
            localObject1 = localStickClient;
            i = localStickClient.getGameKills();
          }
        }
      } finally {
        StickRoom.this.CR.ClientsLock.readLock().unlock();
      }
      if (localObject1 == null)
      {
        System.out.println("Winner was null - setting winner as blank client");
        return new StickClient();
      }
      ((StickClient)localObject1).incrementGameWins();
      return (StickClient)localObject1;
    }
    
    private void updateStats(StickClient paramStickClient)
    {
      StickRoom.this.CR.ClientsLock.readLock().lock();
      try
      {
        for (StickClient localStickClient : StickRoom.this.CR.getAllClients())
        {
          try
          {
            if ((!localStickClient.getQuickplayStatus().booleanValue()) && (localStickClient.getDbID() != -1))
            {
              int i = 0;
              if (localStickClient.equals(paramStickClient))
                i = 1;
              PreparedStatement localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("UPDATE `users` SET `rounds` = `rounds` + 1, `kills` = `kills` + ?, `deaths` = `deaths` + ?, `wins` = `wins` + ? WHERE `UID` = ?");
              
              localPreparedStatement.setInt(1, localStickClient.getGameKills());
              localPreparedStatement.setInt(2, localStickClient.getGameDeaths());
              localPreparedStatement.setInt(3, i);
              localPreparedStatement.setInt(4, localStickClient.getDbID());
              localPreparedStatement.executeUpdate();
              
              localStickClient.setGameKills(0);
              localStickClient.setGameDeaths(0);
              System.out.println("Updated stats for user: " + localStickClient.getName());
            }
          }
          catch (Exception localException)
          {
            System.out.println("There was an error updating round stats for user " + localStickClient.getName());
            localException.printStackTrace();
          }
        }
      }
      finally
      {
        StickRoom.this.CR.ClientsLock.readLock().unlock();
      }
    }
  }
  
  public void finalize() throws Throwable
  {
    super.finalize();
  }
}