private void SendJoinGameMessage(ServerGameInfo sgi, short id) { GameInfoMessage msg = MessageFactory.Create(MessageType.GameParams) as GameInfoMessage; msg.GameInfo = sgi.GetGameInfo(); SendMessage(msg, id); }
public void TestDecode() { byte[] bytes = { 1, 2, //message PID 3, 4, //message Seq 5, 6, //conv PID 7, 8, //conv Seq 0, 2, //message type 0, 1, //game id 0, 2, //user id 0, 10, 0, 49, 0, 46, 0, 50, 0, 46, 0, 51, //GM address 0, 10, 0, 52, 0, 46, 0, 53, 0, 46, 0, 54 //UA address }; GameInfoMessage gim = GameInfoMessage.Decode(bytes); Assert.AreEqual(gim.GameId, 1); Assert.AreEqual(gim.UserId, 2); Assert.AreEqual(gim.msgId.Pid, 258); Assert.AreEqual(gim.msgId.Seq, 772); Assert.AreEqual(gim.convId.Pid, 1286); Assert.AreEqual(gim.convId.Seq, 1800); Assert.AreEqual(gim.GMAddress, "1.2.3"); Assert.AreEqual(gim.UAAddress, "4.5.6"); }
private void setupOnConnected(GameInfoMessage msg) { gameState.empireId = msg.empireId; gameState.empires = msg.empires; // TODO: set up rendering and stuff clearColor = new Color(225, 225, 225); findEntity("loading").destroy(); // set up the board var boardEntity = createEntity("board"); gameState.map = new MapRef(msg.mapSize); var board = boardEntity.addComponent(new GameBoard(gameState)); board.pawnMove = (pawn, dest) => { client.sendMessageAsync(new MovePawnMessage { pawn = pawn, dest = dest }); }; // setup navigator camera camera.setMaximumZoom(2f); camera.setMinimumZoom(0.1f); camera.setZoom(0); var navigator = camera.addComponent(new Navigator()); camera.setUpdateOrder(int.MaxValue); }
private ResultType CreateGame(GameInfoMessage msg) { ResultType result = _server.GameManager.IsCreateGamePossible(msg.GameInfo); SendMessage(Utils.CreateResultMessage(ResponseType.CreateGame, result), msg.SenderId); if (ResultType.Successful == result) { _server.GameManager.CreateGameNoError(msg.GameInfo); result = _server.GameManager.IsJoinGamePossible(msg.GameInfo.Name, _server.GetPlayer(msg.SenderId)); if (result == ResultType.Successful) { _server.GameManager.JoinGame(msg.GameInfo.Name, _server.GetPlayer(msg.SenderId)); } else { InfoLog.WriteError("Join not possible after create!"); } } return(result); /*ResultType resultType = _server.GameManager.CreateGame(msg.GameInfo); * if (resultType == ResultType.Successful) { * ResultType joinResult = _server.GameManager.JoinGame(msg.GameInfo.Name, _server.GetPlayer(msg.SenderId)); * if (joinResult != ResultType.Successful) { * InfoLog.WriteError("Join not possible after create!"); * return joinResult; * } * } * return resultType;*/ }
public static Message CreateCreateGameMessage() { GameInfoMessage gim = MessageFactory.Create(MessageType.CreateGame) as GameInfoMessage; GameInfo _gameInfo = new GameInfo(); _gameInfo.Name = _gameName; _gameInfo.MapName = "mala.map"; _gameInfo.MaxPlayerNumber = (short)clientNo; _gameInfo.GameType = GameType.Public; gim.GameInfo = _gameInfo; return(gim); }
public void UserApp_JoinGameConversation_Test() { TestAppWorker testAppWorker = new TestAppWorker(); testAppWorker.StartTest(); var fakeRegistry = new UdpCommunicator() { MinPort = 10000, MaxPort = 10999, Timeout = 1000, EnvelopeHandler = ProcessEnvelope1 }; fakeRegistry.Start(); IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Loopback, fakeRegistry.Port); Message msg1 = new RequestGameMessage(); Envelope env = new Envelope(msg1, targetEndPoint); Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId)); testAppWorker.commFacility.Process(env); Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId)); Thread.Sleep(100); Assert.AreNotSame(msg1, _lastIncomingEnvelope1); Assert.IsNotNull(_lastIncomingEnvelope1); Assert.IsNotNull(_lastIncomingEnvelope1.message); Assert.AreEqual(msg1.msgId, _lastIncomingEnvelope1.message.msgId); Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId); RequestGameMessage msg2 = _lastIncomingEnvelope1.message as RequestGameMessage; Assert.IsNotNull(msg2); GameInfoMessage msg3 = new GameInfoMessage(msg1.convId, 1, 1, "GMAddress", "UAAddress"); Assert.AreNotSame((IPEndPoint)fakeRegistry._myUdpClient.Client.LocalEndPoint, _lastIncomingEnvelope1.remoteEndPoint); Envelope env2 = new Envelope(msg3, _lastIncomingEnvelope1.remoteEndPoint); fakeRegistry.Send(env2); Thread.Sleep(100); Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId)); fakeRegistry.Stop(); testAppWorker.StopTest(); }
public void SendGameInfoMessage(RegistryData.GameInfo gameInfo) { MessageId currentConvId = FirstEnvelope.message.convId; IPEndPoint userEndPoint = FirstEnvelope.remoteEndPoint; gameInfo.Players++; Message msg = new GameInfoMessage(currentConvId, gameInfo.GameId, gameInfo.Players, gameInfo.RemoteEndPoint.ToString(), userEndPoint.ToString()); Envelope userEnv = new Envelope(msg, userEndPoint); Envelope gameManagerEnv = new Envelope(msg, gameInfo.RemoteEndPoint); commFacility.Send(userEnv); commFacility.Send(gameManagerEnv); }
public void Registry_JoinGameConversation_Test() { IPEndPoint registryEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000); RegistryData registryData = new RegistryData(); TestAppWorker testAppWorker = new TestAppWorker(registryData, registryEndPoint); testAppWorker.StartTest(); var comm1 = new UdpCommunicator() { MinPort = 10000, MaxPort = 10999, Timeout = 1000, EnvelopeHandler = ProcessEnvelope1 }; comm1.Start(); Message msg1 = new RequestGameMessage(); Envelope env = new Envelope(msg1, registryEndPoint); Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId)); Assert.IsNull(registryData.GetAvailableGameManager()); comm1.Send(env); Thread.Sleep(1000); /* * //The conversation happens to fast. After the sleep, the conv is already gone * Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId)); */ Assert.AreNotSame(msg1, _lastIncomingEnvelope1); Assert.IsNotNull(_lastIncomingEnvelope1); Assert.IsNotNull(_lastIncomingEnvelope1.message); Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId); GameInfoMessage msg2 = _lastIncomingEnvelope1.message as GameInfoMessage; Assert.IsNotNull(msg2); Assert.IsNotNull(registryData.GetAvailableGameManager()); Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId)); //-------------STOPPING------------// testAppWorker.StopTest(); comm1.Stop(); }
public void TestGameInfoMessage() { GameInfoMessage origMessage = new GameInfoMessage(MessageId.Create(), 1, 2, "1.2.3", "4.5.6"); byte[] bytes = origMessage.Encode(); GameInfoMessage decodedMessage = GameInfoMessage.Decode(bytes); Assert.AreEqual(origMessage.MessageType, 2, "Incorrect MessageType"); Assert.AreEqual(origMessage.MessageType, decodedMessage.MessageType, "MessageType did not match"); Assert.AreEqual(origMessage.msgId, decodedMessage.msgId, "msgId did not match"); Assert.AreEqual(origMessage.convId, decodedMessage.convId, "convId did not match"); Assert.AreEqual(origMessage.GameId, decodedMessage.GameId, "GameId did not match"); Assert.AreEqual(origMessage.UserId, decodedMessage.UserId, "UserId did not match"); Assert.AreEqual(origMessage.GMAddress, decodedMessage.GMAddress, "GMAddress did not match"); Assert.AreEqual(origMessage.UAAddress, decodedMessage.UAAddress, "UAAddress did not match"); }
public void TestDecodeBadByteArray() { byte[] bytes = { 1, 2, //message PID 3, 4, //message Seq 5, 6, //conv PID 7, 8, //conv Seq 0, 2, //message type 0, 1, //game id 0, //user id missing byte 0, 10, 0, 49, 0, 46, 0, 50, 0, 46, 0, 51, //GM address 0, 10, 0, 52, 0, 46, 0, 53, 0, 46, 0, 54 //UA address }; GameInfoMessage gim = (GameInfoMessage)Message.DecodeMessage(bytes); Assert.AreEqual(gim, null, "Attempting to decode a bad byte array did not return null."); }
private void ProcessCreateGame(GameInfoMessage msg) { Player player = _server.GetPlayer(msg.SenderId); InfoLog.WriteInfo(string.Format(ProcessStringFormat, "Create game", msg.SenderId), EPrefix.ServerProcessInfo); MenuState state = PlayerStateMachine.Transform(player.State, MenuAction.GameJoinEntry); ResultMessage resMsg = Utils.CreateResultMessage(ResponseType.CreateGame, ResultType.Unsuccesful); ResultType result = ResultType.Unsuccesful; if (state == MenuState.GameJoin) { result = CreateGame(msg); if (result == ResultType.Successful) { lock (player.SyncRoot) player.State = state; } } //resMsg.Result = (byte)result; //SendMessage(resMsg, msg.SenderId); }
public override bool BeforeInvoke(InvocationInfo info, out object returnValue) { // we can obtain the BattleMode instance from this call if (info.targetMethod.Equals("handleMessage")) { if (bm == null) { bm = (BattleMode)info.target; } Message m = (Message)info.arguments[0]; if (m is GameInfoMessage) { GameInfoMessage gm = (GameInfoMessage)m; if (new GameType(gm.gameType).isMultiplayer()) // just multiplayer matches { try { String opponentName = getOpponentName(gm); // now use the api to get the player's data WebClientTimeOut wc = new WebClientTimeOut(); wc.TimeOut = 3000; wc.DownloadStringCompleted += (sender, e) => { parseJSONResult(e.Result, opponentName); }; wc.DownloadStringAsync(new Uri("http://a.scrollsguide.com/player?fields=all&name=" + opponentName)); } catch // could not get information { } } } } returnValue = null; return(false); }
private String getOpponentName(GameInfoMessage gm) { return (gm.color == TileColor.white) ? gm.black : gm.white; }
private String getOpponentName(GameInfoMessage gm) { return((gm.color == TileColor.white) ? gm.black : gm.white); }
public void TestEncode() { GameInfoMessage gim = new GameInfoMessage(MessageId.Create(), 1, 2, "1.2.3", "4.5.6"); byte[] bytes = gim.Encode(); byte[] messagePIdBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(gim.msgId.Pid)); byte[] messageSeqBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(gim.msgId.Seq)); byte[] convPIdBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(gim.convId.Pid)); byte[] convSeqBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(gim.convId.Seq)); int currByte = 0; //MessageId //pid Assert.AreEqual(messagePIdBytes[0], bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); Assert.AreEqual(messagePIdBytes[1], bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); //seq Assert.AreEqual(messageSeqBytes[0], bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); Assert.AreEqual(messageSeqBytes[1], bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); //ConversationId //pid Assert.AreEqual(convPIdBytes[0], bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); Assert.AreEqual(convPIdBytes[1], bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); //seq Assert.AreEqual(convSeqBytes[0], bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); Assert.AreEqual(convSeqBytes[1], bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); //short MessageType Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); Assert.AreEqual(2, bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); //short GameId Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); Assert.AreEqual(1, bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); //short UserId Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); Assert.AreEqual(2, bytes[currByte], $"bytes[{currByte++}] did not match asserted value"); //string GMAddress Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(10, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(49, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(46, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(50, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(46, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(51, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); //string UAAddress Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(10, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(52, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(46, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(53, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(46, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(0, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); Assert.AreEqual(54, bytes[currByte], $"bytes[{currByte++}] did note match asserted value"); }
public void ProcessMessage(Message message) { handlerSuspender.WaitOne(); switch (message.Type) { case MessageType.Result: { ResultMessage result = message as ResultMessage; switch ((ResponseType)result.ResponseType) { case ResponseType.Login: if (LoginRequestReply != null) { LoginRequestReply(this, new RequestReplyEventArgs(!Convert.ToBoolean(result.Result), ((ResultType)(result.Result)).ToString())); } break; case ResponseType.Register: if (RegisterRequestReply != null) { RegisterRequestReply(this, new RequestReplyEventArgs(!Convert.ToBoolean(result.Result), ((ResultType)(result.Result)).ToString())); } break; case ResponseType.Remind: if (RemindRequestReply != null) { RemindRequestReply(this, new RequestReplyEventArgs(!Convert.ToBoolean(result.Result), ((ResultType)(result.Result)).ToString())); } break; case ResponseType.CreateGame: if (CreateGameRequestReply != null) { CreateGameRequestReply(this, new RequestReplyEventArgs(!Convert.ToBoolean(result.Result), ((ResultType)(result.Result)).ToString())); } break; case ResponseType.JoinGame: if (JoinGameRequestReply != null) { JoinGameRequestReply(this, new RequestReplyEventArgs(!Convert.ToBoolean(result.Result), ((ResultType)(result.Result)).ToString())); } break; case ResponseType.StartGame: if (StartGameRequestReply != null) { StartGameRequestReply(this, new RequestReplyEventArgs(!Convert.ToBoolean(result.Result), ((ResultType)(result.Result)).ToString())); } break; } } break; case MessageType.IdInformation: { NumericMessage numericMessage = message as NumericMessage; ClientPlayerInfo.SenderId = (short)numericMessage.Number; } break; case MessageType.ChatUsers: { ChatUsersMessage chatMessage = message as ChatUsersMessage; switch ((MessageOperation)chatMessage.Option) { case MessageOperation.Add: if (NewChatUsers != null) { NewChatUsers(this, new ChatEventArgs(chatMessage.ChatUsers.ToArray())); } break; case MessageOperation.Remove: if (DeleteChatUsers != null) { DeleteChatUsers(this, new ChatEventArgs(chatMessage.ChatUsers.ToArray())); } break; case MessageOperation.List: if (ResetChatUsers != null) { ResetChatUsers(this, new ChatEventArgs(chatMessage.ChatUsers.ToArray())); } break; } } break; case MessageType.ChatText: if (ChatTextReceive != null) { TextMessage textMessage = message as TextMessage; ChatTextReceive(this, new ChatEventArgs(textMessage.Text)); } break; case MessageType.PlayerInfoResponse: if (PlayerInfoRequestReply != null) { PlayerInfoMessage playerInfoMessage = message as PlayerInfoMessage; PlayerInfoRequestReply(this, new RequestReplyEventArgs(true, playerInfoMessage.PlayerData.ToString())); } break; case MessageType.Games: { GamesMessage gamesMessage = message as GamesMessage; switch ((MessageOperation)gamesMessage.Operation) { case MessageOperation.Add: if (NewGamesInfo != null) { NewGamesInfo(this, new GameEventArgs(gamesMessage.ListGameInfo.ToArray())); } break; case MessageOperation.Remove: if (DeleteGamesInfo != null) { DeleteGamesInfo(this, new GameEventArgs(gamesMessage.ListGameInfo.ToArray())); } break; case MessageOperation.List: if (ResetGamesInfo != null) { ResetGamesInfo(this, new GameEventArgs(gamesMessage.ListGameInfo.ToArray())); } break; } } break; case MessageType.GameParams: { GameInfoMessage gameInfoMessage = message as GameInfoMessage; ClientPlayerInfo.GameInfo = gameInfoMessage.GameInfo; if (GameParamsRequestReply != null) { GameParamsRequestReply(this, new RequestReplyEventArgs(true, gameInfoMessage.GameInfo.Description)); } } break; case MessageType.Players: { PlayersMessage playersMessage = message as PlayersMessage; switch ((MessageOperation)playersMessage.Operation) { case MessageOperation.Add: ClientPlayerInfo.Enemies.Add(playersMessage.PlayerList.ToArray()); if (NewPlayers != null) { NewPlayers(this, new PlayerEventArgs(playersMessage.PlayerList.ToArray())); } break; case MessageOperation.Remove: ClientPlayerInfo.Enemies.Remove(playersMessage.PlayerList.ToArray()); if (DeletePlayers != null) { DeletePlayers(this, new PlayerEventArgs(playersMessage.PlayerList.ToArray())); } break; case MessageOperation.Modify: { PlayerInfo[] players = playersMessage.PlayerList.ToArray(); for (int i = 0; i < players.Length; i++) { if (ClientPlayerInfo.Player.Id == players[i].Id) { ClientPlayerInfo.Player = players[i]; break; } } ClientPlayerInfo.Enemies.Modify(players); if (UpdatePlayers != null) { UpdatePlayers(this, new PlayerEventArgs(players)); } } break; case MessageOperation.List: ClientPlayerInfo.Enemies.Clear(); ClientPlayerInfo.Enemies.Add(playersMessage.PlayerList.ToArray()); if (ResetPlayers != null) { ResetPlayers(this, new PlayerEventArgs(playersMessage.PlayerList.ToArray())); } break; } } break; default: InfoLog.WriteInfo("MenuMessageHandler received unknown message type: " + message.Type, EPrefix.ClientInformation); break; } handlerSuspender.Release(); }