神様!君に振り回されて死にそうだ

18945 ワード

200行の1つの方法で、グローバル変数があふれています.
本当に神様ですね.

public String payCart() {
		identity = getPubStrB();//    		
		SessionUtil sessionUtil = new SessionUtil();
		CookieUtil cookieUtil = new CookieUtil();
		int k=0,coinTotal=0,gameMoneyTotal=0,payStatus=0;		
		//    
		String [] tmpShopArr;
		String [] tmpArr;		
		strComm = getPubStrA();
		if(strComm==null || strComm.length()<3) return ERROR;
		tmpShopArr = strComm.split(";");
		shopAmountArr = new int[tmpShopArr.length];
		shopArr = new Shop[tmpShopArr.length];
		
		for(int p=0; p<tmpShopArr.length; p++){
			tmpArr = tmpShopArr[p].split("-");
			shopAmountArr[k] = Integer.parseInt(tmpArr[1]);
			detachedCriteria = DetachedCriteria.forClass(Shop.class);
			detachedCriteria.add(Restrictions.eq("id", Integer.parseInt(tmpArr[0])));
			shopArr[k] = (Shop)userDao.findAllByCriteria(detachedCriteria).get(0);
			k++;
		}
		
		detachedCriteria = DetachedCriteria.forClass(UserInfo.class);
		detachedCriteria.add(Restrictions.eq("userId", userCenter.getCUID()));
		userInfo = (UserInfo)userDao.findAllByCriteria(detachedCriteria).get(0);
		
		for(int i=0; i<shopArr.length; i++){
			shop = shopArr[i];
			if(shop.getAmount()<1){
				payStatus = 1;
				this.setActionMessage(shop.getGoodsName()+"   ");
				return SUCCESS;
			}
			if(shop.getGoodsType()==2){
				if(gameDao.getGameNum(userCenter.getCUID(),shop.getVarField1())>=0){
					this.setActionMessage("         ");
					payStatus = 1;
					return SUCCESS;
				}
			}
			if(shop.getPrice()>0 && shop.getPrice1()>0 ){//        
				if(shop.getBeneficiary()==0){
					if(userInfo.getVipOverTime().getTime()>new Date().getTime()){//    price1
						if(shop.getPrice1Type()==0){
							coinTotal = coinTotal + shop.getPrice1()*shopAmountArr[i];
							shop.setPayAmount(shop.getPrice1()*shopAmountArr[i]);
						}else{
							gameMoneyTotal = gameMoneyTotal + shop.getPrice1()*shopAmountArr[i];
							shop.setPayAmount(shop.getPrice1()*shopAmountArr[i]);
						}
					}else{//    price
						if(shop.getPriceType()==0){
							coinTotal = coinTotal + shop.getPrice()*shopAmountArr[i];
							shop.setPayAmount(shop.getPrice()*shopAmountArr[i]);
						}else{
							gameMoneyTotal = gameMoneyTotal + shop.getPrice()*shopAmountArr[i];
							shop.setPayAmount(shop.getPrice()*shopAmountArr[i]);
						}
					}	
				}else{//    price1
					if(shop.getPrice1Type()==0){
						coinTotal = coinTotal + shop.getPrice1()*shopAmountArr[i];
						shop.setPayAmount(shop.getPrice1()*shopAmountArr[i]);
					}else{
						gameMoneyTotal = gameMoneyTotal + shop.getPrice1()*shopAmountArr[i];
						shop.setPayAmount(shop.getPrice1()*shopAmountArr[i]);
					}
				}				
			}else if(shop.getPrice()>0 && shop.getPrice1()==0){//      price
				if(shop.getPriceType()==0){
					coinTotal = coinTotal + shop.getPrice()*shopAmountArr[i];
					shop.setPayAmount(shop.getPrice()*shopAmountArr[i]);
				}else{
					gameMoneyTotal = gameMoneyTotal + shop.getPrice()*shopAmountArr[i];
					shop.setPayAmount(shop.getPrice()*shopAmountArr[i]);
				}
			}else if(shop.getPrice()==0 && shop.getPrice1()>0){//      
				if(shop.getBeneficiary()==0){
					if(userInfo.getVipOverTime().getTime()>new Date().getTime()){//    price1
						if(shop.getPrice1Type()==0){
							coinTotal = coinTotal + shop.getPrice1()*shopAmountArr[i];
							shop.setPayAmount(shop.getPrice1()*shopAmountArr[i]);
						}else{
							gameMoneyTotal = gameMoneyTotal + shop.getPrice1()*shopAmountArr[i];
							shop.setPayAmount(shop.getPrice1()*shopAmountArr[i]);
						}
					}else{
						payStatus = 1;
						this.setActionMessage("     "+shop.getGoodsName());
						return SUCCESS;
					}	
				}else{//    price1
					if(shop.getPrice1Type()==0){
						coinTotal = coinTotal + shop.getPrice1()*shopAmountArr[i];
						shop.setPayAmount(shop.getPrice1()*shopAmountArr[i]);
					}else{
						gameMoneyTotal = gameMoneyTotal + shop.getPrice1()*shopAmountArr[i];
						shop.setPayAmount(shop.getPrice1()*shopAmountArr[i]);
					}
				}
			}
		}
		//     				
		if(payStatus==1)return SUCCESS;
		if(userInfo.getMoney()-coinTotal<0){
			this.setActionMessage("          ,   ");		
			return SUCCESS;
		}
		if(userInfo.getBankGameMoney()-gameMoneyTotal<0){
			this.setActionMessage("            ");		
			return SUCCESS;
		}
		
		//             
		int nday=0;
		for(int j=0; j<shopArr.length; j++){
			if(shopArr[j].getGoodsType()==4 && shopArr[j].getVarField1().length()>1 ){
				nday = Integer.parseInt(shopArr[j].getVarField1());
				if(nday>showDay)showDay = nday;
				showType = 1;
			}else if(shopArr[j].getGoodsType()==4){
				showType = 1;
			}
		}
		if(showType==1 && showDay==0){
			Date tday = new Date();
			if(userInfo.getShowOverTime()==null || userInfo.getShowOverTime().getTime()<tday.getTime()){
				this.setActionMessage("           ,     ");		
				return SUCCESS;
			}
		}
		
		buyShow = "";
		for(int j = 0; j < shopArr.length; j++){
			shop = shopArr[j];
			if(shop.getGoodsType()==0){//    
				buyGameProp(j);
			}else if(shop.getGoodsType()==1){//    
				buyProduct(j);
			}else if(shop.getGoodsType()==2){//    
				doGameZero(j);
			}else if(shop.getGoodsType()==3){//    
				payVip(j);
			}
			else if (shop.getGoodsType() == 4) {//show
				int showIndex = this.getShowIndex(shop);
				buyShow += "," + showIndex + "," + shop.getPic();
				
				payShow(j);	
			}
			shop.setAmount(shop.getAmount()-shopAmountArr[j]);
			gameDao.updateObject(shop);
		}
		
		if (buyShow.length() > 5) {
			Map<String, String> showMap =  this.getShowMap(buyShow, userCenter.getCIndexOf(12), cookieUtil.getCookie("show"));
			
			strComm="";
			myShow="";
			if (showMap.size() > 0) {				
				Object[] o = this.g(showMap);
				
				for(int i = 0; i < o.length; i++) {	
					String key = o[i].toString();
					strComm += "#" + key + "#" + showMap.get(key);
					myShow += "," + showMap.get(key);
				}
			}
			myShow = myShow.substring(1);
			strComm = strComm.substring(1);
			String rootPath = ServletActionContext.getServletContext().getRealPath("/");
			ImageUtil image = new ImageUtil();
			BufferedImage bufimg = image.showImage(myShow, rootPath + "show/");
			if (this.createShowImage(bufimg, myShow)) {
				cookieUtil.setCookie("show",strComm,-1);
				userInfo.setShowCustom(strComm);
				if (showDay > 0) {
					Date tday = new Date();
					GregorianCalendar gDay = new GregorianCalendar();		
					if(userInfo.getShowOverTime()==null || userInfo.getShowOverTime().getTime()<tday.getTime()){
						userInfo.setShowOverTime(tday);
					}
					gDay.setTimeInMillis(userInfo.getShowOverTime().getTime());
					gDay.add(Calendar.DATE,showDay);
					userInfo.setShowOverTime(gDay.getTime());
				}
				userInfo.setSelfImageShow(userInfo.getIfSelfImage());
				userCenter.reSaveCID(12,strComm);
				
				strComm = "show";
			}
			else {
				logger.debug(userCenter.getCUID() + " no pic");
				this.setActionMessage("SHOW    ");
				return SUCCESS;
			}
		}
		userInfo.setMoney(userInfo.getMoney()-coinTotal);
		userInfo.setBankGameMoney(userInfo.getBankGameMoney()-gameMoneyTotal);
		userDao.updateObject(userInfo);
		sessionUtil.setSession("cart",null,0);
		sessionUtil.setSession("preShow",null,0);
		cookieUtil.clearKey("GoodsSort");
		this.setActionMessage(strComm+"    。      :"+gameMoneyTotal + ",        ");
		return SUCCESS;
	}

first:
グローバル変数、164行をクリア

public String payCart() {
		/*
		 *    “  -  ”   
		 * e.g:
		 *    -  ;  -  
		 */
		String shopAndCountString = getPubStrA();
		if (shopAndCountString == null || shopAndCountString.length() < 3)
			return ERROR;
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass( UserInfo.class );
		detachedCriteria.add( Restrictions.eq("userId", userCenter.getUserID()) );
		UserInfo userInfo = (UserInfo) userDao.findAllByCriteria(detachedCriteria).get(0);
		/*
		 *      
		 */	 
		if (userInfo.getPlayServerId() > 0) {
			this.setActionMessage("      ,         ");
			return SUCCESS;
		}
		
		/*
		 *     
		 */		
		String[] idAndAmountArray = shopAndCountString.split(";");
		List<Integer> shopAmountList = this.getShopAmountList(idAndAmountArray);//    List
		List<Shop> shopList = this.getShopList(idAndAmountArray);//  List
		long gameMoneyTotal = 0;
		int showDay = 0;
		boolean hasShow = false;//   “   ”  
		
		for (int i = 0; i < shopList.size(); i++) {
			Shop shop = shopList.get(i);
			if (shop.getAmount() < 1) {
				this.setActionMessage(shop.getGoodsName() + "   ");
				return SUCCESS;
			}
			
			if (shop.getGoodsType() == 2) {
				if ( gamePointOverZero( userCenter.getUserID(), parseOldGameId(shop.getVarField1()) ) ) {
					this.setActionMessage("         ");
					return SUCCESS;
				}
			}
			
			long vipOverTime = userInfo.getVipOverTime().getTime(), nowTime = new Date().getTime();
			if (shop.getPrice() == 0 && shop.getPrice1() > 0) {//       
				if (shop.getBeneficiary() == 0) {
					if (vipOverTime <= nowTime) {//     price1
						this.setActionMessage("     " + shop.getGoodsName());
						return SUCCESS;
					}
				}
			}
			
			gameMoneyTotal = getPayGameMoneyTotal(shop, shopAmountList.get(i), vipOverTime > nowTime, gameMoneyTotal);
			
			if ( shop.getGoodsType() == 4 ) {
				hasShow = true;//   
				
				if ( shop.getVarField1().length() > 1 ) {
					int days = Integer.parseInt( shop.getVarField1() );//      
					showDay = (days > showDay) ? days : showDay;
				}
			} 
			
		}// end for
		
		if (userInfo.getGameMoney() - gameMoneyTotal < 0) {
			this.setActionMessage("         ");
			return SUCCESS;
		}

		//              
		if ( hasShow && showDay == 0 ) {
			if (userInfo.getShowOverTime() == null
					|| userInfo.getShowOverTime().getTime() < new Date().getTime()) {
				this.setActionMessage("           ,     ");
				return SUCCESS;
			}
		}

		StringBuilder payShowImages = new StringBuilder();
		for (int j = 0; j < shopList.size(); j++) {
			Shop shop = shopList.get(j);
			if (shop.getGoodsType() == 0) {//     
				buyGameProp( userInfo, shop );
			} 
			else if (shop.getGoodsType() == 1) {//     
				buyProduct( userInfo, shop );
			} 
			else if (shop.getGoodsType() == 2) {//     
				setupGamePointsToZero( userInfo, shop );
			} 
			else if (shop.getGoodsType() == 3) {//     
				payVip( userInfo, shop, shopAmountList.get(j) );
			} 
			else if (shop.getGoodsType() == 4) {// show
				payShowImages.append( "," + this.getShowIndex(shop) + "," + shop.getPic() );
				payShow( userInfo, shop );
			}
			shop.setAmount(shop.getAmount() - shopAmountList.get(j));
			gameDao.updateObject(shop);
		}
		
		CookieUtil cookieUtil = new CookieUtil();
		if (payShowImages.length() > 5) {
			Map<String, String> showMap = this.getShowMap(
					payShowImages.toString(),
					userCenter.getShow(), 
					cookieUtil.getCookie("show")
				);

			StringBuilder strComm = new StringBuilder();
			StringBuilder myShow = new StringBuilder();
			if (showMap.size() > 0) {
				Object[] o = this.g(showMap);

				for (int i = 0; i < o.length; i++) {
					String key = o[i].toString();
					strComm.append( "#" + key + "#" + showMap.get(key) );
					myShow.append( "," + showMap.get(key) );
				}
			}
			String showImages = myShow.substring(1);
			String showCustomString = strComm.substring(1);
			ImageUtil image = new ImageUtil();
			BufferedImage bufimg = image.showImage(
					showImages, 
					ServletActionContext.getServletContext().getRealPath("/") + "show/");
			if ( this.createShowImage(bufimg, showImages) ) {
				cookieUtil.setCookie("show", showCustomString, -1);
				userInfo.setShowCustom( showCustomString );
				if (showDay > 0) {
					Date tday = new Date();
					GregorianCalendar gDay = new GregorianCalendar();
					if (userInfo.getShowOverTime() == null
							|| userInfo.getShowOverTime().getTime() < tday.getTime()) {
						userInfo.setShowOverTime(tday);
					}
					gDay.setTimeInMillis(userInfo.getShowOverTime().getTime());
					gDay.add(Calendar.DATE, showDay);
					userInfo.setShowOverTime(gDay.getTime());
				}
				userInfo.setSelfImageShow(userInfo.getIfSelfImage());
				userCenter.reSaveCID(12, showCustomString);
			} 
			else {
				logger.debug(userCenter.getUserName() + " no pic");
				this.setActionMessage("SHOW    ");
				return SUCCESS;
			}
		}
		userInfo.setGameMoney( ( int )(userInfo.getGameMoney() - gameMoneyTotal) );
		userDao.updateObject(userInfo);
		
		SessionUtil sessionUtil = new SessionUtil();
		sessionUtil.setSession(SessionKey.cart.getValue(), null, 0);
		sessionUtil.setSession(SessionKey.show.getValue(), null, 0);
		
		cookieUtil.clearKey("GoodsSort");
		this.setActionMessage("    。      :" + gameMoneyTotal	+ ",        ");
		return SUCCESS;
	}

second:
100行

public String payCart() {
		/*
		 *    “  -  ”   
		 * e.g:
		 *    -  ;  -  
		 */
		String shopAndCountString = getPubStrA();
		if (shopAndCountString == null || shopAndCountString.length() < 3)
			return ERROR;
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass( UserInfo.class );
		detachedCriteria.add( Restrictions.eq("userId", userCenter.getUserID()) );
		UserInfo userInfo = (UserInfo) userDao.findAllByCriteria(detachedCriteria).get(0);
		/*
		 *      
		 */	 
		if (userInfo.getPlayServerId() > 0) {
			this.setActionMessage("      ,         ");
			return SUCCESS;
		}
		
		/*
		 *     
		 */		
		String[] idAndAmountArray = shopAndCountString.split(";");
		List<Integer> shopAmountList = this.getShopAmountList(idAndAmountArray);//    List
		List<Shop> shopList = this.getShopList(idAndAmountArray);//  List
		long gameMoneyTotal = 0;
		int showDay = 0;
		boolean hasShow = false;//   “   ”  
		
		for (int i = 0; i < shopList.size(); i++) {
			Shop shop = shopList.get(i);
			if (shop.getAmount() < 1) {
				this.setActionMessage(shop.getGoodsName() + "   ");
				return SUCCESS;
			}
			
			if (shop.getGoodsType() == 2) {
				if ( gamePointOverZero( userCenter.getUserID(), parseOldGameId(shop.getVarField1()) ) ) {
					this.setActionMessage("         ");
					return SUCCESS;
				}
			}
			
			long vipOverTime = userInfo.getVipOverTime().getTime(), nowTime = new Date().getTime();
			if (shop.getPrice() == 0 && shop.getPrice1() > 0) {//       
				if (shop.getBeneficiary() == 0) {
					if (vipOverTime <= nowTime) {//     price1
						this.setActionMessage("     " + shop.getGoodsName());
						return SUCCESS;
					}
				}
			}
			
			gameMoneyTotal = getPayGameMoneyTotal(shop, shopAmountList.get(i), vipOverTime > nowTime, gameMoneyTotal);
			
			if ( shop.getGoodsType() == 4 ) {
				hasShow = true;//   
				
				if ( shop.getVarField1().length() > 1 ) {
					int days = Integer.parseInt( shop.getVarField1() );//      
					showDay = (days > showDay) ? days : showDay;
				}
			} 
			
		}// end for
		
		if (userInfo.getGameMoney() - gameMoneyTotal < 0) {
			this.setActionMessage("         ");
			return SUCCESS;
		}

		//              
		if ( hasShow && showDay == 0 ) {
			if (userInfo.getShowOverTime() == null
					|| userInfo.getShowOverTime().getTime() < new Date().getTime()) {
				this.setActionMessage("           ,     ");
				return SUCCESS;
			}
		}

		CookieUtil cookieUtil = new CookieUtil();
		if ( ! createShowImage(shopList, shopAmountList, cookieUtil, userInfo, showDay) ) {
			logger.debug(userCenter.getUserName() + " no pic");
			this.setActionMessage("SHOW    ");
			return SUCCESS;
		}
		
		userInfo.setGameMoney( ( int )(userInfo.getGameMoney() - gameMoneyTotal) );
		userDao.updateObject(userInfo);
		
		SessionUtil sessionUtil = new SessionUtil();
		sessionUtil.setSession(SessionKey.cart.getValue(), null, 0);
		sessionUtil.setSession(SessionKey.show.getValue(), null, 0);
		
		cookieUtil.clearKey("GoodsSort");
		this.setActionMessage("    。      :" + gameMoneyTotal	+ ",        ");
		return SUCCESS;
	}