shalbert     Design

Hal Danger · United States · male · registered since 1998 · last online - today

recursion

Other ·

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

// Written by shalbert

public class ThreatSystem {
   
   // Create some instance data variables
   private ArrayList<OffendingUser> OffendersList;
   private long L1_DURATION_IN_MS = 0;
   private long L2_DURATION_IN_MS = 0;
   private long L3_DURATION_IN_MS = 0;
   private long L4_DURATION_IN_MS = 0;
   
   public ThreatSystem(int l1Seconds, int l2Seconds, int l3Seconds, int l4Seconds){
      OffendersList = new ArrayList<OffendingUser>();
      L1_DURATION_IN_MS = (long) l1Seconds * 1000; 
      L2_DURATION_IN_MS = (long) l2Seconds * 1000;
       L3_DURATION_IN_MS = (long) l3Seconds * 1000;
      L4_DURATION_IN_MS = (long) l4Seconds * 1000;
   } 
   
   public boolean threatHasOccured(String user){
      //System.out.println("L1 Duration: " + L1_DURATION_IN_MS);
      // First check to see if user exists at all...
      if (doesUserExists(user)){
         //Get the user
         OffendingUser thisoffender = getUser(user);
         // Now, let's see what level he should be at now.
         if (performLevelShift(thisoffender, true)) {
            
            return true;
         }
         //System.out.println("User " + thisoffender.getName() + " level is now " + thisoffender.getThreatLevel());
      }else{
         // User does not exists, so add him.
         addOffendingUser(user);
         OffendingUser thisoffender = getUser(user);
         //System.out.println("1User " + thisoffender.getName() + " level is now " + thisoffender.getThreatLevel());
      }
      return false;
   }
   
   public int getThreatLevelOfUser(String user){
      if (doesUserExists(user)){
         OffendingUser thisoffender = getUser(user);
         performLevelShift(thisoffender, false);
         return thisoffender.getThreatLevel();
      }
      return 0;
      
      
   }
   
   private boolean doesUserExists(String user){
      Iterator<OffendingUser> iterator = OffendersList.iterator();
      while(iterator.hasNext()) {
         OffendingUser t = iterator.next();
         if (t.getName().equals(user)){
            return true;
         }
      }
      return false;
   }
   
   private OffendingUser getUser(String user){
      Iterator<OffendingUser> iterator = OffendersList.iterator();
      while(iterator.hasNext()) {
         OffendingUser tmpuser = ((OffendingUser) iterator.next());
         if (tmpuser.getName().equals(user)){
            return tmpuser;
         }
         
      }
      return null;
   }
   
   private void addOffendingUser(String user){
      OffendingUser tmpuser = new OffendingUser(user, "");
      OffendersList.add(tmpuser);
   }
   
   private boolean performLevelShift(OffendingUser thisuser, boolean hasOffended){
      // 
      if (thisuser.getThreatLevel() == 1){
         //Check to make sure he isn't expired
         if ((new Date().getTime() - thisuser.getTimeStamp()) >  L1_DURATION_IN_MS){
            System.out.println("Level 1 Check: " + thisuser.getTimeStamp());
            // He's completely expired, so we just keep him here and reset timestamp.
            thisuser.resetTimeStamp();
         }else{
            if (hasOffended){
               thisuser.upThreatLevel();
               thisuser.resetTimeStamp();
            }
         }
      }else if (thisuser.getThreatLevel() == 2){
         if ((new Date().getTime() - thisuser.getTimeStamp()) >  L2_DURATION_IN_MS){
            //System.out.println("Level 2 Check: CT: " + new Date().getTime() + "UserTime: " + thisuser.getTimeStamp() + " Difference: " + (new Date().getTime() - thisuser.getTimeStamp()));
            // He's expired in this level, so we do some modifications...
            thisuser.lowerTimeStamp(L2_DURATION_IN_MS);
            thisuser.lowerThreatLevel();
            //recursion!
            performLevelShift(thisuser, hasOffended);
         }else{
            if (hasOffended){
               thisuser.upThreatLevel();
               thisuser.resetTimeStamp();
            }
         }
         
      }else if (thisuser.getThreatLevel() == 3){
         if ((new Date().getTime() - thisuser.getTimeStamp()) >  L3_DURATION_IN_MS){
            //System.out.println("Level 3 Check: CT: " + new Date().getTime() + "UserTime: " + thisuser.getTimeStamp() + " Difference: " + (new Date().getTime() - thisuser.getTimeStamp()));
            // He's expired in this level, so we do some modifications...
            thisuser.lowerTimeStamp(L3_DURATION_IN_MS);
            thisuser.lowerThreatLevel();
            //recursion!
            performLevelShift(thisuser, hasOffended);
         }else{
            if (hasOffended){
               thisuser.upThreatLevel();
               thisuser.resetTimeStamp();
            }
         }
         
      }else if (thisuser.getThreatLevel() == 4){
         if ((new Date().getTime() - thisuser.getTimeStamp()) >  L4_DURATION_IN_MS){
            //System.out.println("Level 4 Check: CT: " + new Date().getTime() + "UserTime: " + thisuser.getTimeStamp() + " Difference: " + (new Date().getTime() - thisuser.getTimeStamp()));
            // He's expired in this level, so we do some modifications...
            thisuser.lowerTimeStamp(L4_DURATION_IN_MS);
            thisuser.lowerThreatLevel();
            //recursion!
            performLevelShift(thisuser, hasOffended);
         }else{
            if (hasOffended){
               thisuser.upThreatLevel();
               thisuser.resetTimeStamp();
            }
            //System.out.println("Level 5, you are auto-banend");
            return true;
         }
      }else if (thisuser.getThreatLevel() == 5){
         // You don't get off here... unless removed manually.
         //System.out.println("Level 5, you are auto-banend");
         return true;
         
      }
      
      
      return false;
      
   }
   
   
   // Create a sub-class to represent our offending user (the bad guy!)
   private class OffendingUser {
      
      private String name = "#";
      private String channel = "";
      private int threatlevel;
      private long timeStamp;
      
      private OffendingUser(String tname, String tchannel) {
         //instantiate date
         name = tname;
         channel = tchannel;
         threatlevel = 1;
         timeStamp = new Date().getTime();
      }
      
      public String getName(){
         return name;
      }
      
      public int getThreatLevel() {
         return threatlevel;
      }
      
      public long getTimeStamp()
      {
         return timeStamp;
      }
      
      public void resetTimeStamp(){
         timeStamp = new Date().getTime();
      }
      
      public void upThreatLevel(){
         threatlevel++;
      }
      
      public void lowerThreatLevel(){
         threatlevel--;
      }
      
      public void lowerTimeStamp(long amount){
         timeStamp = timeStamp + amount;
      }
      
   }
   

}
January 18th, 2012 21:23

ChatManager class

Other ·

package com.shalsoft.chatfoo;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ChatManager {
   private ArrayList<Channel> ChannelList = new ArrayList<Channel>();
   private ArrayList<Dialog> DialogList = new ArrayList<Dialog>();
   private Shell theShell;
   private CTabFolder theCTabFolder;
   
   public ChatManager(Shell parent, CTabFolder thistabfolder) {
      theShell = parent;
      theCTabFolder = thistabfolder;
   }
   
   public void addDialog(String someuser){
      Dialog myDialog = new Dialog(theShell, this, theCTabFolder, someuser);
      myDialog.open();
      DialogList.add(myDialog);
      
   }
   
   public void addChannel(String somechannel) {
        Channel myChannel = new Channel(theShell, this, theCTabFolder, somechannel);
        myChannel.open();
        ChannelList.add(myChannel);
   }
   
   public boolean removeChannel(String somechannel){
      boolean foundchannel = false;
      for(int i=0;i < ChannelList.size();i++)
      {
         Channel tmpchannel = ChannelList.get(i);
         if (tmpchannel.getChannelName().equals(somechannel)){
            System.out.println("remove channel " + somechannel + "##");
            ChannelList.remove(i);
            tmpchannel.getTabItem().dispose();
            foundchannel = true;
         }
      }
      
      return foundchannel;
   }
   
   public boolean removeDialog(String someuser){
      boolean founduser = false;
      for(int i=0;i < DialogList.size();i++)
      {
         Dialog tmpdialog = DialogList.get(i);
         if (tmpdialog.getUserName().equals(someuser)){
   
            DialogList.remove(i);
            tmpdialog.getTabItem().dispose();
            founduser = true;
         }
      }
      
      return founduser;
   }
   
   public Channel getChannel(String somechannel){
      Channel tmpchannel = null;
      for(int i=0;i < ChannelList.size();i++)
      {

         if (ChannelList.get(i).getChannelName().equals(somechannel)){
            tmpchannel = ChannelList.get(i);
         }
      }
      return tmpchannel;
   }
   
   public Dialog getDialog(String someuser){
      Dialog tmpdialog = null;
      for(int i=0;i < DialogList.size();i++)
      {

         if (DialogList.get(i).getUserName().equals(someuser)){
            tmpdialog = DialogList.get(i);
         }
      }
      return tmpdialog;
   }
   
   public String getAllChannels(){
      String allchannels = "";
      for(int i=0;i < ChannelList.size();i++)
      {
         if (allchannels.equals("")){
            allchannels = ChannelList.get(i).getChannelName(); 
         }else{
            allchannels = allchannels + ", " + ChannelList.get(i).getChannelName(); 
         }
      }
      
      return allchannels;
   }
   
   public void removeAllChannels(){
      ArrayList<Channel> TempChannelList = new ArrayList<Channel>(ChannelList);
      
      for(int i=0;i < TempChannelList.size();i++)
      {
            Channel tmpchannel = TempChannelList.get(i);
            tmpchannel.getTabItem().dispose();
      }
      System.out.println("All Channels: " + getAllChannels());
      ChannelList.clear();
      
   }
   
   public String getAllDialogs(){
      String alldialogs = "";
      ArrayList<Dialog> TempDialList = new ArrayList<Dialog>(DialogList);
      for(int i=0;i < TempDialList.size();i++)
      {
         if (alldialogs.equals("")){
            alldialogs = TempDialList.get(i).getUserName(); 
         }else{
            alldialogs = alldialogs + ", " + TempDialList.get(i).getUserName(); 
         }
      }
      
      return alldialogs;
   }
   
   public void removeAllDialogs(){
      for(int i=0;i < DialogList.size();i++)
      {
         Dialog tmpdialog = DialogList.get(i);
         tmpdialog.getTabItem().dispose();
      }
      DialogList.clear();
   }
   
   public boolean channelAlreadyOpen(String somechannel) {
      boolean foundchannel = false;
      for(int i=0;i < ChannelList.size();i++)
      {
         Channel tmpchannel = ChannelList.get(i);
         if (tmpchannel.getChannelName().equals(somechannel)){
            foundchannel = true;
         }
      }      
      return foundchannel;
   }
   
   public boolean dialogAlreadyOpen(String someuser) {
      boolean founddialog = false;
      for(int i=0;i < DialogList.size();i++)
      {
         Dialog tmpdialog = DialogList.get(i);
         if (tmpdialog.getUserName().equals(someuser)){
            founddialog = true;
         }
      }      
      return founddialog;
   }


   

}
November 29th, 2011 23:30

Tags:  ·  ·  ·  ·  ·

I client's timer thread - I only have one, and I use mod operator to trigger other events.

Computer ·

      public void startTimerThread() {
         new Thread() {
            public void run(){
               try {   
                  mytimerThread = Thread.currentThread();
                  int tc = 0;
                  int myPingTimer = 10;
                  int msgTimer = 30;
                  int netStatsTimer = 3;
                  while(!(Thread.currentThread().isInterrupted())){
                     Thread.sleep(1000);
                     tc++;
                     if (tc % myPingTimer == 0){
                        send("Jp");
                        responsetime = new Date().getTime();
                     }
                     if (tc % msgTimer == 0){
                        thedisplay.asyncExec(
                              new Runnable() {
                                     public void run(){
                                       try {
                                         try {
                                            ChatFoo.loadMsgInfo(mywebhelper.getMsgInfo());
                                         } catch (ParseException e) {
                                            e.printStackTrace();
                                         }
                                    } catch (IOException e) {
                                       e.printStackTrace();
                                    }
                                }
                          });
                     }
                     if (tc % netStatsTimer == 0){
                        thedisplay.asyncExec(
                                 new Runnable() {
                                        public void run(){
                                          ChatFoo.updateDataIn("Data in: " + datain + " bytes");
                                          ChatFoo.updateDataOut("Data out: " + dataout + " bytes");
                                   }
                             });
                     }
                  }
               } catch (IOException e) {
                  e.printStackTrace();
               } catch (InterruptedException e) {
                  //e.printStackTrace();
               }
            }
         }.start();   
      }   
}
November 23rd, 2011 17:07

Tags:  ·  ·  ·  ·

Sums it up...

Other · · 1 comment


Haha
April 7th, 2011 19:25

Time to pimp out my profile

Other · · 3 comments

I'm working on (arguably way to much) some cool profile design with the all the new options the site has. Let's see if I can really pimp out my profile!

April 20th, 2010 21:30

unix one-liner of the day

Other · · 1 comment

Here is my unix 1 liner of the day. I wrote this to copy only the unique subset of a file name into a temporary directory.

THISID=""; for I in `ls -1 *.pdf`; do if [ "$THISID" != `echo $I | sed 's/_client.*//'` ]; then cp $I /export/home/hplunket/print_vendor_tmp/; fi; THISID=`echo $I | sed 's/_client.*//'`; done
October 21st, 2008 06:07

JustMe Screen Shot

Other ·

Two people asked today if they could "see" JustMe. I said, what do you mean? He's in the channel. They said they wanted to see a screen shot. So, here you go...

http://www.halplunkett.com/justme/justme-screenshot.jpg

August 29th, 2008 23:03

July 25th, 2008

Other · · 3 comments

Dark Knight is awesome. Go see it.

July 25th, 2008 23:37
Page 1 of 2
1 2 · 
Show entries

Year 2012 (1)
  January (1)
 
Year 2011 (3)
Year 2010 (1)
Year 2008 (5)

Subscribe

RSS 2.0

Search blog
(only public entries)

Do you want to blog, too?
Blog for free at spinchat.com
This page is a personal homepage hosted on spinchat.com. The responsibility lies with the user.
spinchat.com is a large online-community with chat, blogs, boards, online games and much more.
Spinchat.com: meet new friends

Imprint · Privacy policy · Sitemap