Work on spectator mode - joining as spectator now possible (on server side).

This commit is contained in:
lotodore
2013-06-29 16:23:10 +02:00
parent cbd6b40f85
commit ef489dae8a
12 changed files with 732 additions and 118 deletions
+189 -22
View File
@@ -10182,6 +10182,20 @@ public final class ProtoBuf {
* <code>required .NetGameInfo gameInfo = 6;</code>
*/
de.pokerth.protocol.ProtoBuf.NetGameInfo getGameInfo();
// repeated uint32 spectatorIds = 7 [packed = true];
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
java.util.List<java.lang.Integer> getSpectatorIdsList();
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
int getSpectatorIdsCount();
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
int getSpectatorIds(int index);
}
/**
* Protobuf type {@code GameListNewMessage}
@@ -10284,6 +10298,27 @@ public final class ProtoBuf {
bitField0_ |= 0x00000010;
break;
}
case 56: {
if (!((mutable_bitField0_ & 0x00000040) == 0x00000040)) {
spectatorIds_ = new java.util.ArrayList<java.lang.Integer>();
mutable_bitField0_ |= 0x00000040;
}
spectatorIds_.add(input.readUInt32());
break;
}
case 58: {
int length = input.readRawVarint32();
int limit = input.pushLimit(length);
if (!((mutable_bitField0_ & 0x00000040) == 0x00000040) && input.getBytesUntilLimit() > 0) {
spectatorIds_ = new java.util.ArrayList<java.lang.Integer>();
mutable_bitField0_ |= 0x00000040;
}
while (input.getBytesUntilLimit() > 0) {
spectatorIds_.add(input.readUInt32());
}
input.popLimit(limit);
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
@@ -10295,6 +10330,9 @@ public final class ProtoBuf {
if (((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
playerIds_ = java.util.Collections.unmodifiableList(playerIds_);
}
if (((mutable_bitField0_ & 0x00000040) == 0x00000040)) {
spectatorIds_ = java.util.Collections.unmodifiableList(spectatorIds_);
}
makeExtensionsImmutable();
}
}
@@ -10418,6 +10456,30 @@ public final class ProtoBuf {
return gameInfo_;
}
// repeated uint32 spectatorIds = 7 [packed = true];
public static final int SPECTATORIDS_FIELD_NUMBER = 7;
private java.util.List<java.lang.Integer> spectatorIds_;
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public java.util.List<java.lang.Integer>
getSpectatorIdsList() {
return spectatorIds_;
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public int getSpectatorIdsCount() {
return spectatorIds_.size();
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public int getSpectatorIds(int index) {
return spectatorIds_.get(index);
}
private int spectatorIdsMemoizedSerializedSize = -1;
private void initFields() {
gameId_ = 0;
gameMode_ = de.pokerth.protocol.ProtoBuf.NetGameMode.netGameCreated;
@@ -10425,6 +10487,7 @@ public final class ProtoBuf {
playerIds_ = java.util.Collections.emptyList();
adminPlayerId_ = 0;
gameInfo_ = de.pokerth.protocol.ProtoBuf.NetGameInfo.getDefaultInstance();
spectatorIds_ = java.util.Collections.emptyList();
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
@@ -10484,6 +10547,13 @@ public final class ProtoBuf {
if (((bitField0_ & 0x00000010) == 0x00000010)) {
output.writeMessage(6, gameInfo_);
}
if (getSpectatorIdsList().size() > 0) {
output.writeRawVarint32(58);
output.writeRawVarint32(spectatorIdsMemoizedSerializedSize);
}
for (int i = 0; i < spectatorIds_.size(); i++) {
output.writeUInt32NoTag(spectatorIds_.get(i));
}
}
private int memoizedSerializedSize = -1;
@@ -10526,6 +10596,20 @@ public final class ProtoBuf {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(6, gameInfo_);
}
{
int dataSize = 0;
for (int i = 0; i < spectatorIds_.size(); i++) {
dataSize += com.google.protobuf.CodedOutputStream
.computeUInt32SizeNoTag(spectatorIds_.get(i));
}
size += dataSize;
if (!getSpectatorIdsList().isEmpty()) {
size += 1;
size += com.google.protobuf.CodedOutputStream
.computeInt32SizeNoTag(dataSize);
}
spectatorIdsMemoizedSerializedSize = dataSize;
}
memoizedSerializedSize = size;
return size;
}
@@ -10629,6 +10713,8 @@ public final class ProtoBuf {
bitField0_ = (bitField0_ & ~0x00000010);
gameInfo_ = de.pokerth.protocol.ProtoBuf.NetGameInfo.getDefaultInstance();
bitField0_ = (bitField0_ & ~0x00000020);
spectatorIds_ = java.util.Collections.emptyList();
bitField0_ = (bitField0_ & ~0x00000040);
return this;
}
@@ -10677,6 +10763,11 @@ public final class ProtoBuf {
to_bitField0_ |= 0x00000010;
}
result.gameInfo_ = gameInfo_;
if (((bitField0_ & 0x00000040) == 0x00000040)) {
spectatorIds_ = java.util.Collections.unmodifiableList(spectatorIds_);
bitField0_ = (bitField0_ & ~0x00000040);
}
result.spectatorIds_ = spectatorIds_;
result.bitField0_ = to_bitField0_;
return result;
}
@@ -10708,6 +10799,16 @@ public final class ProtoBuf {
if (other.hasGameInfo()) {
mergeGameInfo(other.getGameInfo());
}
if (!other.spectatorIds_.isEmpty()) {
if (spectatorIds_.isEmpty()) {
spectatorIds_ = other.spectatorIds_;
bitField0_ = (bitField0_ & ~0x00000040);
} else {
ensureSpectatorIdsIsMutable();
spectatorIds_.addAll(other.spectatorIds_);
}
}
return this;
}
@@ -11020,6 +11121,72 @@ public final class ProtoBuf {
return this;
}
// repeated uint32 spectatorIds = 7 [packed = true];
private java.util.List<java.lang.Integer> spectatorIds_ = java.util.Collections.emptyList();
private void ensureSpectatorIdsIsMutable() {
if (!((bitField0_ & 0x00000040) == 0x00000040)) {
spectatorIds_ = new java.util.ArrayList<java.lang.Integer>(spectatorIds_);
bitField0_ |= 0x00000040;
}
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public java.util.List<java.lang.Integer>
getSpectatorIdsList() {
return java.util.Collections.unmodifiableList(spectatorIds_);
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public int getSpectatorIdsCount() {
return spectatorIds_.size();
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public int getSpectatorIds(int index) {
return spectatorIds_.get(index);
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public Builder setSpectatorIds(
int index, int value) {
ensureSpectatorIdsIsMutable();
spectatorIds_.set(index, value);
return this;
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public Builder addSpectatorIds(int value) {
ensureSpectatorIdsIsMutable();
spectatorIds_.add(value);
return this;
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public Builder addAllSpectatorIds(
java.lang.Iterable<? extends java.lang.Integer> values) {
ensureSpectatorIdsIsMutable();
super.addAll(values, spectatorIds_);
return this;
}
/**
* <code>repeated uint32 spectatorIds = 7 [packed = true];</code>
*/
public Builder clearSpectatorIds() {
spectatorIds_ = java.util.Collections.emptyList();
bitField0_ = (bitField0_ & ~0x00000040);
return this;
}
// @@protoc_insertion_point(builder_scope:GameListNewMessage)
}
@@ -16272,23 +16439,23 @@ public final class ProtoBuf {
com.google.protobuf.ByteString
getPasswordBytes();
// optional bool autoLeave = 3;
// optional bool autoLeave = 3 [default = false];
/**
* <code>optional bool autoLeave = 3;</code>
* <code>optional bool autoLeave = 3 [default = false];</code>
*/
boolean hasAutoLeave();
/**
* <code>optional bool autoLeave = 3;</code>
* <code>optional bool autoLeave = 3 [default = false];</code>
*/
boolean getAutoLeave();
// optional bool spectateOnly = 4;
// optional bool spectateOnly = 4 [default = false];
/**
* <code>optional bool spectateOnly = 4;</code>
* <code>optional bool spectateOnly = 4 [default = false];</code>
*/
boolean hasSpectateOnly();
/**
* <code>optional bool spectateOnly = 4;</code>
* <code>optional bool spectateOnly = 4 [default = false];</code>
*/
boolean getSpectateOnly();
}
@@ -16441,33 +16608,33 @@ public final class ProtoBuf {
}
}
// optional bool autoLeave = 3;
// optional bool autoLeave = 3 [default = false];
public static final int AUTOLEAVE_FIELD_NUMBER = 3;
private boolean autoLeave_;
/**
* <code>optional bool autoLeave = 3;</code>
* <code>optional bool autoLeave = 3 [default = false];</code>
*/
public boolean hasAutoLeave() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>optional bool autoLeave = 3;</code>
* <code>optional bool autoLeave = 3 [default = false];</code>
*/
public boolean getAutoLeave() {
return autoLeave_;
}
// optional bool spectateOnly = 4;
// optional bool spectateOnly = 4 [default = false];
public static final int SPECTATEONLY_FIELD_NUMBER = 4;
private boolean spectateOnly_;
/**
* <code>optional bool spectateOnly = 4;</code>
* <code>optional bool spectateOnly = 4 [default = false];</code>
*/
public boolean hasSpectateOnly() {
return ((bitField0_ & 0x00000008) == 0x00000008);
}
/**
* <code>optional bool spectateOnly = 4;</code>
* <code>optional bool spectateOnly = 4 [default = false];</code>
*/
public boolean getSpectateOnly() {
return spectateOnly_;
@@ -16826,22 +16993,22 @@ public final class ProtoBuf {
return this;
}
// optional bool autoLeave = 3;
// optional bool autoLeave = 3 [default = false];
private boolean autoLeave_ ;
/**
* <code>optional bool autoLeave = 3;</code>
* <code>optional bool autoLeave = 3 [default = false];</code>
*/
public boolean hasAutoLeave() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>optional bool autoLeave = 3;</code>
* <code>optional bool autoLeave = 3 [default = false];</code>
*/
public boolean getAutoLeave() {
return autoLeave_;
}
/**
* <code>optional bool autoLeave = 3;</code>
* <code>optional bool autoLeave = 3 [default = false];</code>
*/
public Builder setAutoLeave(boolean value) {
bitField0_ |= 0x00000004;
@@ -16850,7 +17017,7 @@ public final class ProtoBuf {
return this;
}
/**
* <code>optional bool autoLeave = 3;</code>
* <code>optional bool autoLeave = 3 [default = false];</code>
*/
public Builder clearAutoLeave() {
bitField0_ = (bitField0_ & ~0x00000004);
@@ -16859,22 +17026,22 @@ public final class ProtoBuf {
return this;
}
// optional bool spectateOnly = 4;
// optional bool spectateOnly = 4 [default = false];
private boolean spectateOnly_ ;
/**
* <code>optional bool spectateOnly = 4;</code>
* <code>optional bool spectateOnly = 4 [default = false];</code>
*/
public boolean hasSpectateOnly() {
return ((bitField0_ & 0x00000008) == 0x00000008);
}
/**
* <code>optional bool spectateOnly = 4;</code>
* <code>optional bool spectateOnly = 4 [default = false];</code>
*/
public boolean getSpectateOnly() {
return spectateOnly_;
}
/**
* <code>optional bool spectateOnly = 4;</code>
* <code>optional bool spectateOnly = 4 [default = false];</code>
*/
public Builder setSpectateOnly(boolean value) {
bitField0_ |= 0x00000008;
@@ -16883,7 +17050,7 @@ public final class ProtoBuf {
return this;
}
/**
* <code>optional bool spectateOnly = 4;</code>
* <code>optional bool spectateOnly = 4 [default = false];</code>
*/
public Builder clearSpectateOnly() {
bitField0_ = (bitField0_ & ~0x00000008);
+2 -1
View File
@@ -40,7 +40,8 @@ import org.junit.runners.Suite;
RunRankingGameTest.class,
RejoinGameTest.class,
RejoinMultiGameTest.class,
SeatStateTest.class
SeatStateTest.class,
SpectatorJoinTest.class
})
public class AllTests {
public static void main(String[] args)
@@ -0,0 +1,262 @@
/* PokerTH automated tests.
Copyright (C) 2013 Lothar May
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.pokerth.test;
import static org.junit.Assert.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Test;
import de.pokerth.protocol.ProtoBuf.NetGameInfo;
import de.pokerth.protocol.ProtoBuf.PokerTHMessage;
import de.pokerth.protocol.ProtoBuf.StartEventAckMessage;
import de.pokerth.protocol.ProtoBuf.NetGameInfo.EndRaiseMode;
import de.pokerth.protocol.ProtoBuf.NetGameInfo.NetGameType;
import de.pokerth.protocol.ProtoBuf.PokerTHMessage.PokerTHMessageType;
public class SpectatorJoinTest extends TestBase {
@Test
public void testSpectatorJoinGameBeforeStart() throws Exception {
Guid firstPlayerSession = new Guid();
int firstPlayerId = userInit(sock, AuthUser, AuthPassword, null, firstPlayerSession);
// Waiting for player list update.
PokerTHMessage msg;
msg = receiveMessage();
if (!msg.hasPlayerListMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
Collection<Integer> l = new ArrayList<Integer>();
String gameName = AuthUser + " rejoin game";
NetGameInfo gameInfo = createGameInfo(NetGameType.normalGame, 5, 7, 5, EndRaiseMode.doubleBlinds, 0, 50, gameName, l, 10, 0, 11, 10000);
sendMessage(createGameRequestMsg(
gameInfo,
"",
false));
// Game list update (new game)
msg = receiveMessage();
if (!msg.hasGameListNewMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
// Join game ack.
msg = receiveMessage();
if (!msg.hasJoinGameAckMessage()) {
failOnErrorMessage(msg);
fail("Could not create game!");
}
int gameId = msg.getJoinGameAckMessage().getGameId();
// Game list update (player joined).
msg = receiveMessage();
if (!msg.hasGameListPlayerJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
// Let a spectator join.
Socket spectatorSock = new Socket("localhost", 7234);
int spectatorId = userInit(spectatorSock, "test20", "test20");
msg = receiveMessage(spectatorSock);
if (!msg.hasPlayerListMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
msg = receiveMessage(spectatorSock);
if (!msg.hasGameListNewMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
msg = receiveMessage(spectatorSock);
if (!msg.hasPlayerListMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
// Player List should also be updated for first player.
msg = receiveMessage();
if (!msg.hasPlayerListMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
sendMessage(joinGameRequestMsg(gameId, "", false, true), spectatorSock);
msg = receiveMessage(spectatorSock);
if (!msg.hasJoinGameAckMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
assertTrue(msg.getJoinGameAckMessage().getSpectateOnly());
msg = receiveMessage(spectatorSock);
if (!msg.hasGamePlayerJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
assertEquals(firstPlayerId, msg.getGamePlayerJoinedMessage().getPlayerId());
// TODO Spectator joined message is missing here!
msg = receiveMessage(spectatorSock);
if (!msg.hasGameListSpectatorJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
assertEquals(gameId, msg.getGameListSpectatorJoinedMessage().getGameId());
assertEquals(spectatorId, msg.getGameListSpectatorJoinedMessage().getPlayerId());
// Spectator should be visible for first player.
msg = receiveMessage();
if (!msg.hasGameSpectatorJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
msg = receiveMessage();
if (!msg.hasGameListSpectatorJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
// Let 9 additional clients join.
Socket s[] = new Socket[9];
int playerId[] = new int[9];
for (int i = 0; i < 9; i++) {
s[i] = new Socket("localhost", 7234);
String username = "test" + (i+1);
String password = username;
playerId[i] = userInit(s[i], username, password);
// Waiting for player list update.
do {
msg = receiveMessage(s[i]);
} while (msg.hasPlayerListMessage());
if (!msg.hasGameListNewMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
assertEquals(1, msg.getGameListNewMessage().getSpectatorIdsCount());
assertEquals(spectatorId, msg.getGameListNewMessage().getSpectatorIds(0));
do {
msg = receiveMessage(s[i]);
} while (msg.hasGameListPlayerJoinedMessage() || msg.hasGamePlayerJoinedMessage());
sendMessage(joinGameRequestMsg(gameId, "", false), s[i]);
do {
msg = receiveMessage(s[i]);
failOnErrorMessage(msg);
} while (!msg.hasJoinGameAckMessage() && !msg.hasJoinGameFailedMessage());
if (!msg.hasJoinGameAckMessage()) {
fail("User " + username + " could not join ranking game.");
}
// The player should have joined the game.
msg = receiveMessage();
if (!msg.hasPlayerListMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
msg = receiveMessage();
if (!msg.hasGamePlayerJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
msg = receiveMessage();
if (!msg.hasGameListPlayerJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
// The spectator should also receive the updates.
msg = receiveMessage(spectatorSock);
if (!msg.hasPlayerListMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
msg = receiveMessage(spectatorSock);
if (!msg.hasGamePlayerJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
msg = receiveMessage(spectatorSock);
if (!msg.hasGameListPlayerJoinedMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
}
// Server should automatically send start event.
msg = receiveMessage();
if (!msg.hasStartEventMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
for (int i = 0; i < 9; i++) {
do {
msg = receiveMessage(s[i]);
failOnErrorMessage(msg);
} while (!msg.hasStartEventMessage());
}
// Acknowledge start event.
StartEventAckMessage startAck = StartEventAckMessage.newBuilder()
.setGameId(gameId)
.build();
msg = PokerTHMessage.newBuilder()
.setMessageType(PokerTHMessageType.Type_StartEventAckMessage)
.setStartEventAckMessage(startAck)
.build();
sendMessage(msg);
for (int i = 0; i < 9; i++) {
sendMessage(msg, s[i]);
}
// Game list update (game now running).
msg = receiveMessage(spectatorSock);
if (!msg.hasGameListUpdateMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
msg = receiveMessage(spectatorSock);
if (!msg.hasGameStartInitialMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
// Spectator should receive a hand start message without cards.
msg = receiveMessage(spectatorSock);
if (!msg.hasHandStartMessage()) {
failOnErrorMessage(msg);
fail("Invalid message: " + msg.getMessageType());
}
assertFalse(msg.getHandStartMessage().hasEncryptedCards());
assertFalse(msg.getHandStartMessage().hasPlainCards());
assertEquals(gameId, msg.getHandStartMessage().getGameId());
for (int i = 0; i < 9; i++) {
s[i].close();
}
spectatorSock.close();
}
}
+5
View File
@@ -289,9 +289,14 @@ public abstract class TestBase {
}
public PokerTHMessage joinGameRequestMsg(int gameId, String password, boolean autoLeave) {
return joinGameRequestMsg(gameId, password, autoLeave, false);
}
public PokerTHMessage joinGameRequestMsg(int gameId, String password, boolean autoLeave, boolean spectateOnly) {
JoinExistingGameMessage.Builder joinBuilder = JoinExistingGameMessage.newBuilder();
joinBuilder.setGameId(gameId);
joinBuilder.setAutoLeave(autoLeave);
joinBuilder.setSpectateOnly(spectateOnly);
if (!password.isEmpty()) {
joinBuilder.setPassword(password);
}