我在 sfs2x 中开发了一个宾果游戏。我只能给你一个指导,所以我希望它有所帮助。如果我理解这个问题,您希望在没有请求或事件触发命令的情况下发送命令。
创建一个命令类:包路径相对于您的 src 文件夹。
package com.rcras.bingo1;
public class Commands
{
/** User buys bingo cards */
public static final String CMD_BUY_CARDS = "bc";
/** Server sends Bingo */
public static final String CMD_BINGO = "bo";
/** User Calls Bingo */
public static final String CMD_CALL_BINGO = "bingo";
/** Get the time left till next game */
public static final String CMD_GET_TIMER = "get_timer";
/** No bingos left --- Game Over */
public static final String CMD_GAME_OVER= "gaov";
/** Ready to start a game */
public static final String CMD_READY = "ready";
/** Bingo Draw */
public static final String CMD_DRAW = "draw";
/** tell the client app the game is starting */
public static final String CMD_START = "start";
}
我没有包括我使用的所有命令。无需设置事件或请求处理程序,因为命令是从服务器端计时器事件发送的。
您将不得不编写一个“bingogame”类并从您的扩展中调用该类。我的目标是拥有多个同时运行游戏的房间。我使用 ConcurrentHashMap 跟踪游戏:
import java.util.concurrent.ConcurrentHashMap;
import com.rcras.bingo1.Commands;
public class Bingo1 extends SFSExtension {
List<User> DiamondRoomPlayers;
Timer timer;
private static int min =2;
private static int sec = 60;
/** Current games */
private ConcurrentHashMap<Integer,BingoGame> games = null;
public ConcurrentHashMap<Integer, BingoGame> getGames() {
return games;
}
public void startDiamondGame()
{
Zone thisZone = this.getParentZone();
Room room2 = thisZone.getRoomByName("Diamond");
BingoGame bingoGame = this.getGames().get(room2.getId());
if(bingoGame == null || (bingoGame != null && bingoGame.isStarted() == false))
{
DiamondRoomPlayers=room2.getUserList();
ISFSObject DiamondObj = new SFSObject();
send(Commands.CMD_START, DiamondObj, DiamondRoomPlayers);
BingoGame DiamondRoomGame = new BingoGame(this, room2);
getGames().put(room2.getId(),DiamondRoomGame);
DiamondRoomGame.setId(room2.getId()) ;
DiamondRoomGame.init();
}
}
BingoGame 类有一个触发抽奖的计时器
public class BingoGame {
public BingoGame(Bingo1 ext, Room room)
{
Draw= new int[75];
setPlayers(room.getUserList());
//players = room.getUserList();
thisRoom = room;
this.extension=ext;
//setters
this.setFirstBingo(true);
this.setStarted(false);
this.setSecondBingo(true);
this.setFirstPrize(1000);
this.setSecondPrize(500);
}
public void init()
{
if(players.size() > 0)
{
StartBingoGame();
}
else
{
System.out.println("NOT ENOUGH PLAYER IN THE" + thisRoom.getName() + " Room!" );
}
}
private void StartBingoGame()
{
timer = new Timer();
timer.schedule(new BingoDrawTask(),
0, //initial delay
1*3000); //draw a number every 3 seconds
setStarted(true); //setter for started variable
}
class BingoDrawTask extends TimerTask {
private int BingoNum;
private boolean isThere = true;
@Override
public void run() {
isThere = true;
Random BingoCall = new Random();
while( isThere == true)
{
BingoNum = BingoCall.nextInt(75) + 1;
System.out.println("Bingo Draw:" + BingoNum);
isThere = CheckDrawArray(BingoNum);
}
Draw[NunbersCalled]=BingoNum;
NunbersCalled = NunbersCalled + 1;
System.out.println(thisRoom.getName() + " Room: Draw Number: " + NunbersCalled);
//this should never happen but it's there for testing
if (NunbersCalled == 75)
{
System.out.println("STOP!!!" );
StopBingoGame();
}
// Empty obj
ISFSObject numObj = new SFSObject();
numObj.putInt("cn", NunbersCalled);
numObj.putInt("dn", BingoNum);
numObj.putUtfString("rn", thisRoom.getName());
/*This command 'pushes' the command to a player list named players
extension.send(Commands.CMD_DRAW, numObj, players);
System.out.println("Send ----" + BingoNum);
}
在客户端
添加事件监听器
sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);
添加处理程序
private function onExtensionResponse(evt:SFSEvent):void
{
var obj:SFSObject = evt.params.params as SFSObject;
if(evt.params.cmd == "draw")
{
//Handle draw - mostly handled by BingoCard class
}
}
我试图保持简短,但没有发生。我希望我已经给了你足够的代码来向你展示我是如何实现推送的?在我的“宾果游戏”中。