200行一个方法,到处充斥着全局变量
真是个神仙啊!
first:
干掉全局变量,164行
second:
100行
真是个神仙啊!
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;
}