added startMatch and joinGame

This commit is contained in:
betasteward 2015-06-17 13:53:39 -04:00
parent 19ad01ced8
commit 58e85df735
17 changed files with 220 additions and 46 deletions

View file

@ -96,6 +96,7 @@ import java.util.prefs.Preferences;
import static mage.client.dialog.PreferencesDialog.KEY_CONNECT_FLAG;
import mage.cards.repository.ExpansionInfo;
import mage.cards.repository.ExpansionRepository;
import mage.client.util.GameManager;
import mage.client.util.audio.AudioManager;
import mage.interfaces.ServerState;
import mage.view.ChatMessage;
@ -1477,6 +1478,21 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
showTableWaitingDialog(roomId, tableId, chatId, owner, tournament);
}
@Override
public void gameStarted(UUID gameId, UUID playerId) {
// try {
GameManager.getInstance().setCurrentPlayerUUID(playerId);
showGame(gameId, playerId);
logger.info("Game " + gameId + " started for player " + playerId);
// } catch (Exception ex) {
// handleException(ex);
// }
if (Plugins.getInstance().isCounterPluginLoaded()) {
Plugins.getInstance().addGamesPlayed();
}
}
}
class MagePaneMenuItem extends JCheckBoxMenuItem {

View file

@ -382,10 +382,11 @@ public final class GamePanel extends javax.swing.JPanel {
this.pnlReplay.setVisible(false);
this.gameChatPanel.clear();
this.gameChatPanel.connect(client.getGameChatId(gameId));
if (!client.joinGame(gameId)) {
UUID chatId = client.joinGame(gameId);
if (chatId == null) {
removeGame();
} else {
} else {
this.gameChatPanel.connect(chatId);
// play start sound
AudioManager.playYourGameStarted();
}
@ -415,10 +416,13 @@ public final class GamePanel extends javax.swing.JPanel {
this.pnlReplay.setVisible(false);
this.gameChatPanel.clear();
this.gameChatPanel.connect(client.getGameChatId(gameId));
if (!client.watchGame(gameId)) {
UUID chatId = client.watchGame(gameId);
if (chatId == null) {
removeGame();
}
else {
this.gameChatPanel.connect(chatId);
}
for (PlayAreaPanel panel : getPlayers().values()) {
panel.setPlayingMode(false);
}

View file

@ -91,10 +91,10 @@ public class CallbackClientImpl implements CallbackClient {
switch (callback.getMethod()) {
case "startGame":
{
TableClientMessage message = (TableClientMessage) callback.getData();
GameManager.getInstance().setCurrentPlayerUUID(message.getPlayerId());
gameStarted(message.getGameId(), message.getPlayerId());
break;
// TableClientMessage message = (TableClientMessage) callback.getData();
// GameManager.getInstance().setCurrentPlayerUUID(message.getPlayerId());
// gameStarted(message.getGameId(), message.getPlayerId());
// break;
}
case "startTournament":
{
@ -433,18 +433,18 @@ public class CallbackClientImpl implements CallbackClient {
// }
// }
protected void gameStarted(final UUID gameId, final UUID playerId) {
try {
frame.showGame(gameId, playerId);
logger.info("Game " + gameId + " started for player " + playerId);
} catch (Exception ex) {
handleException(ex);
}
if (Plugins.getInstance().isCounterPluginLoaded()) {
Plugins.getInstance().addGamesPlayed();
}
}
// protected void gameStarted(final UUID gameId, final UUID playerId) {
// try {
// frame.showGame(gameId, playerId);
// logger.info("Game " + gameId + " started for player " + playerId);
// } catch (Exception ex) {
// handleException(ex);
// }
//
// if (Plugins.getInstance().isCounterPluginLoaded()) {
// Plugins.getInstance().addGamesPlayed();
// }
// }
protected void draftStarted(UUID draftId, UUID playerId) {
try {

View file

@ -128,6 +128,11 @@ public class MultiConnectTest {
public void joinedTable(UUID roomId, UUID tableId, UUID chatId, boolean owner, boolean tournament) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void gameStarted(UUID gameId, UUID playerId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
public static void main(String[] argv) throws Exception {

View file

@ -362,15 +362,20 @@ public class Client {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public UUID getGameChatId(UUID gameId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// public UUID getGameChatId(UUID gameId) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
public UUID joinGame(UUID gameId) {
try {
return clientMessageHandler.joinGame(gameId);
} catch (Exception ex) {
logger.error("Error joining game", ex);
}
return null;
}
public boolean joinGame(UUID gameId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public boolean watchGame(UUID gameId) {
public UUID watchGame(UUID gameId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

View file

@ -36,6 +36,7 @@ import org.mage.network.handlers.server.ServerRequestHandler;
//import org.mage.network.handlers.server.TableMessageHandler;
import org.mage.network.interfaces.MageServer;
import org.mage.network.model.ChatMessageMessage;
import org.mage.network.model.GameStartedMessage;
import org.mage.network.model.InformClientMessage;
import org.mage.network.model.JoinedTableMessage;
import org.mage.network.model.MessageType;
@ -179,5 +180,11 @@ public class Server {
ch.writeAndFlush(new JoinedTableMessage(roomId, tableId, chatId, owner, tournament)).addListener(WriteListener.getInstance());
}
public void gameStarted(String sessionId, UUID gameId, UUID playerId) {
Channel ch = findChannel(sessionId);
if (ch != null)
ch.writeAndFlush(new GameStartedMessage(gameId, playerId)).addListener(WriteListener.getInstance());
}
}

View file

@ -18,6 +18,7 @@ import org.mage.network.model.ClientMessage;
import org.mage.network.model.CreateTableRequest;
import org.mage.network.model.GetRoomRequest;
import org.mage.network.model.JoinChatRequest;
import org.mage.network.model.JoinGameRequest;
import org.mage.network.model.JoinTableRequest;
import org.mage.network.model.LeaveChatRequest;
import org.mage.network.model.LeaveTableRequest;
@ -104,6 +105,12 @@ public class ClientMessageHandler extends SimpleChannelInboundHandler<ClientMess
return booleanQueue.take();
}
public UUID joinGame(UUID gameId) throws Exception {
uuidQueue.clear();
ctx.writeAndFlush(new JoinGameRequest(gameId)).addListener(WriteListener.getInstance());
return uuidQueue.take();
}
public void joinChat(UUID chatId) {
ctx.writeAndFlush(new JoinChatRequest(chatId)).addListener(WriteListener.getInstance());
}

View file

@ -23,4 +23,6 @@ public interface MageClient {
ServerState getServerState();
void joinedTable(UUID roomId, UUID tableId, UUID chatId, boolean owner, boolean tournament);
void gameStarted(UUID gameId, UUID playerId);
}

View file

@ -38,6 +38,7 @@ public interface MageServer {
void swapSeats(String sessionId, UUID roomId, UUID tableId, int seatNum1, int seatNum2);
boolean startMatch(String sessionId, UUID roomId, UUID tableId);
UUID joinGame(final UUID gameId, final String sessionId);
void pingTime(long milliSeconds, String sessionId);

View file

@ -0,0 +1,24 @@
package org.mage.network.model;
import java.util.UUID;
import org.mage.network.handlers.client.ClientMessageHandler;
/**
*
* @author BetaSteward
*/
public class GameStartedMessage extends ClientMessage {
private UUID gameId;
private UUID playerId;
public GameStartedMessage(UUID gameId, UUID playerId) {
this.gameId = gameId;
this.playerId = playerId;
}
@Override
public void handleMessage(ClientMessageHandler handler) {
handler.getClient().gameStarted(gameId, playerId);
}
}

View file

@ -0,0 +1,23 @@
package org.mage.network.model;
import java.util.UUID;
import org.mage.network.handlers.client.ClientMessageHandler;
/**
*
* @author BetaSteward
*/
public class JoinGameMessage extends ClientMessage {
private UUID chatId;
public JoinGameMessage(UUID chatId) {
this.chatId = chatId;
}
@Override
public void handleMessage(ClientMessageHandler handler) {
handler.receiveId(chatId);
}
}

View file

@ -0,0 +1,25 @@
package org.mage.network.model;
import io.netty.channel.ChannelHandlerContext;
import java.util.UUID;
import org.mage.network.handlers.WriteListener;
import org.mage.network.interfaces.MageServer;
/**
*
* @author BetaSteward
*/
public class JoinGameRequest extends ServerRequest {
private UUID gameId;
public JoinGameRequest(UUID gameId) {
this.gameId = gameId;
}
@Override
public void handleMessage(MageServer server, ChannelHandlerContext ctx) {
ctx.writeAndFlush(new JoinGameMessage(server.joinGame(gameId, ctx.channel().id().asLongText()))).addListener(WriteListener.getInstance());
}
}

View file

@ -0,0 +1,22 @@
package org.mage.network.model;
import org.mage.network.handlers.client.ClientMessageHandler;
/**
*
* @author BetaSteward
*/
public class StartMatchMessage extends ClientMessage {
private boolean success;
public StartMatchMessage(boolean success) {
this.success = success;
}
@Override
public void handleMessage(ClientMessageHandler handler) {
handler.receiveBoolean(success);
}
}

View file

@ -0,0 +1,27 @@
package org.mage.network.model;
import io.netty.channel.ChannelHandlerContext;
import java.util.UUID;
import org.mage.network.handlers.WriteListener;
import org.mage.network.interfaces.MageServer;
/**
*
* @author BetaSteward
*/
public class StartMatchRequest extends ServerRequest {
private UUID roomId;
private UUID tableId;
public StartMatchRequest(UUID roomId, UUID tableId) {
this.roomId = roomId;
this.tableId = tableId;
}
@Override
public void handleMessage(MageServer server, ChannelHandlerContext ctx) {
ctx.writeAndFlush(new StartMatchMessage(server.startMatch(ctx.channel().id().asLongText(), roomId, tableId))).addListener(WriteListener.getInstance());
}
}

View file

@ -514,20 +514,20 @@ public class Main implements MageServer {
// });
// }
//
// @Override
// public boolean startMatch(final String sessionId, final UUID roomId, final UUID tableId) throws MageException {
// if (!TableManager.getInstance().getController(tableId).changeTableState(TableState.STARTING)) {
// return false;
// }
@Override
public boolean startMatch(final String sessionId, final UUID roomId, final UUID tableId) {
if (!TableManager.getInstance().getController(tableId).changeTableState(TableState.STARTING)) {
return false;
}
// execute("startMatch", sessionId, new Action() {
// @Override
// public void execute() {
// UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
// TableManager.getInstance().startMatch(userId, roomId, tableId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableManager.getInstance().startMatch(userId, roomId, tableId);
// }
// });
// return true;
// }
return true;
}
//
// @Override
// public void startChallenge(final String sessionId, final UUID roomId, final UUID tableId, final UUID challengeId) throws MageException {
@ -704,16 +704,17 @@ public class Main implements MageServer {
// return null;
// }
//
// @Override
// public void joinGame(final UUID gameId, final String sessionId) throws MageException {
@Override
public UUID joinGame(final UUID gameId, final String sessionId) {
// execute("joinGame", sessionId, new Action() {
// @Override
// public void execute() {
// UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
// GameManager.getInstance().joinGame(gameId, userId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().joinGame(gameId, userId);
return GameManager.getInstance().getChatId(gameId);
// }
// });
// }
}
//
// @Override
// public void joinDraft(final UUID draftId, final String sessionId) throws MageException {
@ -1382,4 +1383,8 @@ public class Main implements MageServer {
server.joinedTable(sessionId, roomId, tableId, chatId, owner, tournament);
}
public void gameStarted(String sessionId, UUID gameId, UUID playerId) {
server.gameStarted(sessionId, gameId, playerId);
}
}

View file

@ -581,7 +581,7 @@ public class TableController {
if (!match.getPlayer(entry.getValue()).hasQuit()) {
User user = UserManager.getInstance().getUser(entry.getKey());
if (user != null) {
user.ccGameStarted(match.getGame().getId(), entry.getValue());
user.gameStarted(match.getGame().getId(), entry.getValue());
if (creator == null) {
creator = user.getName();

View file

@ -193,8 +193,9 @@ public class User {
Main.getInstance().joinedTable(sessionId, roomId, tableId, chatId, owner, tournament);
}
public void ccGameStarted(final UUID gameId, final UUID playerId) {
fireCallback(new ClientCallback("startGame", gameId, new TableClientMessage(gameId, playerId)));
public void gameStarted(final UUID gameId, final UUID playerId) {
// fireCallback(new ClientCallback("startGame", gameId, new TableClientMessage(gameId, playerId)));
Main.getInstance().gameStarted(sessionId, gameId, playerId);
}
public void ccDraftStarted(final UUID draftId, final UUID playerId) {
@ -298,7 +299,7 @@ public class User {
}
for (Entry<UUID, GameSessionPlayer> entry: gameSessions.entrySet()) {
ccGameStarted(entry.getValue().getGameId(), entry.getKey());
gameStarted(entry.getValue().getGameId(), entry.getKey());
entry.getValue().init();
GameManager.getInstance().sendPlayerString(entry.getValue().getGameId(), userId, "");
}