java計時器的用法

package com.toplx.hiber.bussiness.athletics.altheticsServerModel;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.mina.common.IoSession;

import com.toplx.hiber.bussiness.athletics.broadList.ClientInforAthletics;
import com.toplx.hiber.bussiness.athletics.broadList.ClientInforUtilsAthletics;

/**
* 競技中的房間模型(每個房間有2個人)
* @author Administrator
*在設定的過程中,紅髮一定要對應攻擊狀態爲0
*/
public class RoomModel{


private String roomID;
private Integer redID;//紅方信息
private Integer blueID;//藍方信息
private int isStart;//是否開始0表示未開始1表示開始
private int blueIsReady;//藍方是否準備0表示未準備1表示準備

private int redTotal=100;//紅髮總進度
private int blueTotal=100;//藍方跟的總進度
private static int roundTime=20*1000;
private int attractMark=0;//0代表紅髮攻擊,1代表藍方攻擊
private int altheticsTime;
private String mapID;//地圖編號

public Timer timer;


public RoomModel(Integer roleID,Integer blueID,String roomID){
this.redID=roleID;
this.blueID=blueID;
this.roomID=roomID;
}

//房間計時器

public int getAttractMark() {
return attractMark;
}
public void setAttractMark(int attractMark) {
this.attractMark = attractMark;
}
public int getAltheticsTime() {
return altheticsTime;
}
public void setAltheticsTime(int altheticsTime) {
this.altheticsTime = altheticsTime;
}
/**
*
* 其他信息
* @return
*/
public Integer getRedID() {
return redID;
}
public void setRedID(Integer redID) {
this.redID = redID;
}
public Integer getBlueID() {
return blueID;
}
public void setBlueID(Integer blueID) {
this.blueID = blueID;
}
public int getIsStart() {
return isStart;
}
public void setIsStart(int isStart) {
this.isStart = isStart;
}


public int getRedTotal() {
return redTotal;
}
public void setRedTotal(int redTotal) {
this.redTotal = redTotal;
}

public int getBlueTotal() {
return blueTotal;
}
public void setBlueTotal(int blueTotal) {
this.blueTotal = blueTotal;
}
public int getRoundTime() {
return roundTime;
}
public void setRoundTime(int roundTime) {
this.roundTime = roundTime;
}
public int getBlueIsReady() {
return blueIsReady;
}
public void setBlueIsReady(int blueIsReady) {
this.blueIsReady = blueIsReady;
}



//以下是計時器協議3秒計時
public void startThreeTimer()
{
if(timer!=null){
timer.cancel();
}
timer=new Timer();
timer.schedule(new TimerTask(){

@Override
public void run() {
System.out.println("3秒倒計時完成,開始控制權分配");
//timer.cancel();
startContorlFenPei("3秒計時器分配控制權");

}

},3000);

}
//控制權計時20秒
public void startTwenty()
{
if(timer!=null){
timer.cancel();
}
timer=new Timer();
timer.schedule(new TimerTask(){

@Override
public void run() {
System.out.println("20秒倒計時完成,開始控制權分配");
startControlPortio("20秒計時器分配控制權");

}

},20000,20000);
}
//播放計時10秒
public void startTenSecond()
{
if(timer!=null){
timer.cancel();
}
timer=new Timer();
timer.schedule(new TimerTask(){

@Override
public void run() {
System.out.println("10秒播放時間完成,開始控制權分配");
//timer.cancel();
startContorlFenPei("10秒計時器分配控制權");
}

},10000);
}
//開始分配控制權
public void startContorlFenPei(String count)
{
startControlPortio(count);
startTwenty();
}

//控制權分配
public void startControlPortio(String count)
{

Object[] win=null;
Object[] lost=null;

IoSession redSession=ClientInforUtilsAthletics.getSessionByRoleID.get(this.redID);
IoSession blueSession=ClientInforUtilsAthletics.getSessionByRoleID.get(this.blueID);

ClientInforAthletics redIDalthRoleMessage = ClientInforUtilsAthletics.allAthleticsRoleMessage
.get(this.redID);
Integer redHealth=redIDalthRoleMessage.getRoleAthleticsDto().getHealth();
ClientInforAthletics blueIDalthRoleMessage= ClientInforUtilsAthletics.allAthleticsRoleMessage
.get(this.blueID);
Integer blueHealth=blueIDalthRoleMessage.getRoleAthleticsDto().getHealth();
System.out.println("當前血量"+redHealth+" "+blueHealth);

if(redHealth<=0){
timer.cancel();
HashMap<Object,Object> map=new HashMap<Object,Object>();

map.put("Event","S_Athletics_Over");
map.put("winID",blueID);
map.put("lostID",redID);
map.put("win",win);
map.put("lost",lost);
redSession.write(map);
blueSession.write(map);
}else if(blueHealth<=0){
timer.cancel();


HashMap<Object,Object> map=new HashMap<Object,Object>();
map.put("Event","S_Athletics_Over");
map.put("winID",redID);
map.put("lostID",blueID);
map.put("win",win);
map.put("lost",lost);
redSession.write(map);
blueSession.write(map);


}else{
if(attractMark==1){
System.out.println("開始紅髮攻擊");
attractMark=0;
HashMap<Object,Object> map=new HashMap<Object,Object>();
map.put("Event","S_Athletics_Control");
map.put("roleID",redID);
map.put("wind",0.5);

map.put("count",count);
redSession.write(map);
blueSession.write(map);


}
else{
System.out.println("開始藍髮攻擊");
attractMark=1;
HashMap<Object,Object> map=new HashMap<Object,Object>();
map.put("Event","S_Athletics_Control");
map.put("roleID",blueID);
map.put("wind",0.5);
map.put("count",count);
redSession.write(map);
blueSession.write(map);

}
}


}
public void stop(){
if(timer!=null){
timer.cancel();
}
}

public String getMapID() {
return mapID;
}
public void setMapID(String mapID) {
this.mapID = mapID;
}


public static void main(String args[]){


}

public String getRoomID() {
return roomID;
}

public void setRoomID(String roomID) {
this.roomID = roomID;
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章