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;
}

}

你可能感兴趣的:(java,apache,Mina)