⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 user.java

📁 使用工具jublider开发的一个聊天室实现基本功能,
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            tsb.append (l % 60);
            tsb.append (" Minuten");
         } else if (l == 1) {
            tsb.append ("einer Minute");
         } else {
            tsb.append (l);
            tsb.append (" Minuten");
         }
         return tsb.toString ();
      }
      return userProps.get (k);
   }

	/**
	 * set the property of this user to the value given
	 * @param k the property to set for this user
	 * @param v the value for this property of this user
	 */
   public void setProperty (String k, Object v) {
      userProps.put (k, v);
   }

	/**
	 * retuns the timestamp this user logged in
	 * @return the timestamp
	 */
   public long getSessionStart () {
      return sessionStart;
   }

	/**
	 * set the HTTP/1.1 capability of this user
	 * @param b true if this users client is HTTP/1.1 capable
	 */
   public void setHTTP11 (boolean b) {
      isHTTP11 = b;
   }

   /**
    * get the name of this user
    * @return string with username
    */
   public String getName () {
      return (name);
   }

	/**
	 * adds a freind relation for this user
	 * @param fname the name of the friend
	 */
   public void addFriend (String fname) {
      if (friendsList.contains (fname)) return;
      UserManager.mgr.addFriendship (this, fname);
      friendsList.addElement (fname);
   }
   /**
    * returns the number of friends of this user
    * @return the number of freinds of this user
    */
   public int numberOfFriends () {
      return friendsList.size ();
   }
   
   public boolean isFriend (User u) {
       return isFriend (u.name);
   }
   public boolean isFriend (String name) {
       String n = name.trim().toLowerCase();
       return this.friendsList.contains(n);
   }
   
   /**
    * returns an Enumeration containing all friends of this user.
    * @return Enumeration contaioning all friends of this user
    */
   public Enumeration friends () {
      return friendsList.elements ();
   }

	/**
	 * give a message when the user-object gets finalized by the garbage-collector
   public void finalize () {
      Server.log ("[User " + name + "]", "got finalized **************", Server.MSG_STATE, Server.LVL_VERBOSE);
   }
     */
   
	public boolean equals (User u) {
		if (u==null) return false;
		if (id != null && id.equals(u.getID ())) return true;
		if (name.equalsIgnoreCase (u.getName ())) return true;
		return (false);
   }

   public int hashCode () {
      if (hashCode != Integer.MIN_VALUE)
         return hashCode;
      if (id != null)
         hashCode = id.hashCode();
      else
         hashCode = name.toLowerCase().hashCode ();
      return (hashCode);
   }

	/**
	 * returns the number of questions this user has asked within this group
	 * @return the number of questions this user has asked within this group
	 */
   public int getQuestionCounter () {
      return questionCounter;
   }
   /**
    * increments the questioncounter of this user
    */
   public void incrementQuestionCounter () {
      questionCounter++;
   }
   
   /**
    * returns true if this userobject is logged out
    * @return true if this userobject is logged out
    */
   public boolean isLoggedOut () {
   	  return state==LOGGED_OUT;
   }
   
   /**
    * returns true if this userobject is logged in
    * @return true if this userobject is logged in
    */
   public boolean isLoggedIn () {
      return state==LOGGED_IN;
   }

   /**
    * stores the last user to which this user sent a private-message
    * @param pu the user who sent the private-message to this user 
    */
   public void setPrivateUser (User pu) {
      this.ownPrivateUser = pu;
   }
   
   /**
    * returns the stored user to which this user sent a private message last time
    * @return the stored user to which this user sent a private message last time
    */
   public User getPrivateUser () {
      return ownPrivateUser;
   }
   
   /**
	* stores the last user which sent a private-message to this user
	* @param pu the user who sent the private-message to this user 
	*/
   public void setForeignPrivateUser (User pu) {
	  this.foreignPrivateUser = pu;
   }

   /**
	* returns the stored user which sent a private message to this user last time
	* @return the stored user which sent a private message to this user last time
	*/
   public User getForeignPrivateUser () {
	  return foreignPrivateUser;
   }
 
    public boolean usrMayWhisper(User u) {
        if (this.grp==null)
            return true;
        if (this.hasRight(IUserStates.IS_GUEST) 
		    && this.grp.hasState(IGroupState.SND_PRF_GUEST))
            return false;
        if (this.hasRight(IUserStates.ROLE_GOD)
            && this.grp.hasState(IGroupState.SND_PRF_GOD))
                return false;
        if (this.hasRight(IUserStates.IS_MODERATOR)
            && this.grp.hasState(IGroupState.SND_PRF_MODERATOR))
                return false;
        if (this.hasRight(IUserStates.ROLE_VIP)
            && this.grp.hasState(IGroupState.SND_PRF_VIP))
                return false;
        if (this.hasRight(IUserStates.ROLE_USER)
            && this.grp.hasState(IGroupState.SND_PRF_USER))
                return false;
        return true;
    }
   
	/**
	 * checks the references of this user and unsets them if
	 * the user referd to is logged out
	 */
	public void checkReferences() {
		if (foreignPrivateUser != null && foreignPrivateUser.isLoggedOut())
			foreignPrivateUser = null;
		if (ownPrivateUser != null && ownPrivateUser.isLoggedOut()) {
            User opu = UserManager.mgr.getUserByName(ownPrivateUser.name);
            if (opu != null)
                ownPrivateUser = opu;
        }
		if (invitedBy != null && invitedBy.isLoggedOut()) {
			invitedBy = null;
			invitedTo = null;
		}
	}

	/**
	 * Stores the message given by the command /away do display it, when the user
	 * returns from away-state (or when /w is called for this user...)
	 * @param param
	 */
	public void setAwayMessage(String param) {
		awayMessage = param;
	}
	/**
	 * returns the stored awaymessage
	 * @return the stored awaymessage
	 */
	public String getAwayMessage () {
		return awayMessage == null ? "" : awayMessage;
	}
    
    /**
     * calculate the chattime including the current session
     * @return the calculated chattime
     */
    public long getChattime () {
        Object ct = getProperty ("chattime");
        if (ct == null) {
            return ((lastActive () - getSessionStart ()) - awayTime ()) / 1000; 
        }
        return (((lastActive () - getSessionStart ()) - awayTime ()) / 1000) + ((Long) ct).longValue ();
    }
    
    public void setFriendsNotification (short mode) {
        this.friendNotification=mode;
    }
    public short notifyFriends() {
        return this.friendNotification;
    }
    
    
    /**
     * set the custom-title
     * @param et the custom-title
     */
    public void setCustomTitle (String et) {
        customTitle = et;
    }
    
    /**
     * get the custom-title which was fetched from db on login
     * @return custom-title
     */
    public String getCustomTitle () {
        return customTitle;
    }

    public synchronized long nextCheck() {
        long lowestValue = lastActive + (away ? Server.srv.USER_AWAY_TIMEOUT : Server.srv.USER_TIMEOUT);
        if ((state==LOGGING_OUT || state==SCHEDULED_FOR_REMOVAL)
            && removeWhen != 0 && removeWhen < lowestValue)
            lowestValue = removeWhen;
        return lowestValue;
    }

    /**
     * returns true, if this user is valid. Validity is determined in a "soft" way,
     * meaning, that users, which don't have a connection at the moment,
     * will also be checked for their removeWhen-Timestamp and will stay valid, until
     * their removeWhen-Timestamp is reached.
     * @return true if valid, false if not
     */
    public synchronized boolean check(long now) {
        switch (state) {
            case LOGGING_IN:
                long laDiff = now-this.lastActive;
                long seDiff = now-this.sessionStart;
                if (laDiff < Server.srv.USER_REMOVE_SCHEDULE_TIME*10
                    || seDiff < Server.srv.USER_REMOVE_SCHEDULE_TIME*10)
                    return true;
                else
                    return false;
            case SCHEDULED_FOR_REMOVAL:
                if (this.removeWhen==0) {
                    state=LOGGED_IN;
                    return true;
                }
                return this.removeWhen > now;
            case SENDING_QUIT_MESSAGE:
                return this.removeWhen > now;
            case LOGGING_OUT:
                return this.removeWhen > now;
        }
        long tot = lastActive + (away 
                        ? Server.srv.USER_AWAY_TIMEOUT 
                        : Server.srv.USER_TIMEOUT);
        if (this.hasRight(IUserStates.ROLE_VIP)) {
            if (Server.srv.VIP_TIMEOUT<0)
                tot = Long.MAX_VALUE;
            else if (Server.srv.VIP_TIMEOUT>0)
                tot = lastActive + Server.srv.VIP_TIMEOUT;
        }
        if (grp==null 
            || !grp.isValid() 
            || tot <=now) {
            this.sendQuitMessage(false);
        } else if (sk == null 
            || !sk.isValid() 
            || !sk.channel().isOpen())
            this.scheduleToRemove();
        return true;
    }

    public String toString() {
        StringBuffer tsb = new StringBuffer ("[User ");
        tsb.append (name);
        tsb.append (" [cookie=");
        tsb.append (cookie);
        tsb.append (" / state=");
        tsb.append (state);
        tsb.append (" / grp: ");
        tsb.append (grp);
        tsb.append (" / sk: ");
        if (sk!=null) {
            tsb.append ("true valid? ");
            tsb.append (sk.isValid());
            tsb.append (" open? ");
            tsb.append (sk.channel().isOpen());
        } else {
            tsb.append ("false");
        }
        tsb.append ("]]");
        return tsb.toString();
   }

    public void finalize() {
        if (Server.TRACE_CREATE_AND_FINALIZE)
            Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
    }
    
    /**
     * Compares the Roles of two users:<br><br>
     * returns:<br>
     *  2,3 not the same user and u is lower than 'this'<br>
     * -1 u is lower than 'this'<br>
     *  0 u is equal with 'this'<br>
     *  1 u is higher than 'his'<br>
     * @param u
     * @return
     */
    public int compareRoleTo(User u) {
    	int defaultright = this.getDefaultPermissionMap();
    	if (this.hasRole(IUserStates.ROLE_GOD)) {
            if (u.hasRole(IUserStates.ROLE_GOD))
                return 0;
        } else if (this.hasRole(IUserStates.ROLE_VIP)) {
            if (u.hasRole(IUserStates.ROLE_GOD))
                return 1;
            else if (u.hasRole(IUserStates.ROLE_VIP))
                return 0;
            else if (u.hasRole(IUserStates.ROLE_USER))
                return 2;
        } else if (this.hasRole(IUserStates.ROLE_USER)) {
        	if (!u.equals(this) && u.hasRole(IUserStates.ROLE_USER) && !u.hasRight(defaultright)) 
            	return 2;
        	if (!u.equals(this) && u.hasRole(IUserStates.ROLE_VIP) && !u.hasRight(defaultright)) 
            	return 3;
        	if (u.hasRole(IUserStates.ROLE_GOD))
                return 1;
            if (u.hasRole(IUserStates.ROLE_VIP))
                return 1;
            else if (u.hasRole(IUserStates.ROLE_USER))
                return 0;
        } else {
            // this must be an asshole
        	if (u.hasRole(IUserStates.ROLE_ASSHOLE) && defaultright == IUserStates.ROLE_USER )
                return 2;
            if (u.hasRole(IUserStates.ROLE_ASSHOLE))
                return 0;
            else
                return 1;
        }
        return -1;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -