public void ParseMessage(Message message) { using (DarkRiftReader reader = message.GetReader()) { ushort incomingClientId = reader.ReadUInt16(); if (incomingClientId == _characterFacade.Id) { float vertical = reader.ReadSingle(); float horizontal = reader.ReadSingle(); bool primaryAction = reader.ReadBoolean(); bool secondaryAction = reader.ReadBoolean(); float directionX = reader.ReadSingle(); float directionY = reader.ReadSingle(); Vector2 direction = new Vector2(directionX, directionY); float positionX = reader.ReadSingle(); float positionY = reader.ReadSingle(); Vector2 position = new Vector2(positionX, positionY); _controlState.Vertical = vertical; _controlState.Horizontal = horizontal; _controlState.PrimaryAction = false; _controlState.SecondaryAction = secondaryAction; _controlState.Direction = direction; _controlState.Position = position; } } _timer = 0.0f; }
public static bool?ReadNullableBoolean(this DarkRiftReader reader) { bool hasValue = reader.ReadBoolean(); bool?b = null; if (hasValue) { b = reader.ReadBoolean(); } return(b); }
/// <summary> /// Updates the texts in the scene based on the message that is received. /// </summary> /// <param name="updateGameStateMessage"></param> private void UpdateGameEndMessage(Message updateGameStateMessage) { using (DarkRiftReader reader = updateGameStateMessage.GetReader()) { bool isWin = reader.ReadBoolean(); _winText.enabled = isWin; _loseText.enabled = !isWin; } }
public static Quaternion?ReadNullableQuaternion(this DarkRiftReader reader) { bool hasValue = reader.ReadBoolean(); Quaternion?q = null; if (hasValue) { q = new Quaternion(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } return(q); }
void DeserialiseAnim(object data) { if (data is DarkRiftReader) { using (DarkRiftReader reader = (DarkRiftReader)data) { plValues.running = reader.ReadBoolean(); plValues.grounded = reader.ReadBoolean(); plValues.state = reader.ReadInt32(); plValues.speed = reader.ReadSingle(); plValues.hor = reader.ReadSingle(); plValues.ver = reader.ReadSingle(); } } else { Debug.LogError("Should have recieved a DarkRiftReciever but didn't! (Got: " + data.GetType() + ")"); transform.position = transform.position; } }
public static Vector3?ReadNullableVector3(this DarkRiftReader reader) { bool hasValue = reader.ReadBoolean(); Vector3?v = null; if (hasValue) { v = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } return(v); }
public static float?ReadNullableSingle(this DarkRiftReader reader) { bool hasValue = reader.ReadBoolean(); float?b = null; if (hasValue) { b = reader.ReadSingle(); } return(b); }
public static double?ReadNullableDouble(this DarkRiftReader reader) { bool hasValue = reader.ReadBoolean(); double?b = null; if (hasValue) { b = reader.ReadDouble(); } return(b); }
void PlayerDetails(MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { using (DarkRiftReader reader = message.GetReader()) { Player player = players[e.Client]; player.PositionX = reader.ReadSingle(); player.PositionY = reader.ReadSingle(); player.PositionZ = reader.ReadSingle(); player.AnimatorIsWalking = reader.ReadBoolean(); player.AnimatorIsRunning = reader.ReadBoolean(); player.AnimatorIsBackWalking = reader.ReadBoolean(); player.AnimatorIsRightWalking = reader.ReadBoolean(); player.AnimatorIsLeftWalking = reader.ReadBoolean(); player.AnimatorIsDirectRightWalking = reader.ReadBoolean(); player.AnimatorIsDirectLeftWalking = reader.ReadBoolean(); player.AnimatorIsJumpRun = reader.ReadBoolean(); player.RotationX = reader.ReadSingle(); player.RotationY = reader.ReadSingle(); player.RotationZ = reader.ReadSingle(); player.RotationW = reader.ReadSingle(); using (DarkRiftWriter writer = DarkRiftWriter.Create()) { writer.Write(player.ID); writer.Write(player.PositionX); writer.Write(player.PositionY); writer.Write(player.PositionZ); writer.Write(player.AnimatorIsWalking); writer.Write(player.AnimatorIsRunning); writer.Write(player.AnimatorIsBackWalking); writer.Write(player.AnimatorIsRightWalking); writer.Write(player.AnimatorIsLeftWalking); writer.Write(player.AnimatorIsDirectRightWalking); writer.Write(player.AnimatorIsDirectLeftWalking); writer.Write(player.AnimatorIsJumpRun); writer.Write(player.RotationX); writer.Write(player.RotationY); writer.Write(player.RotationZ); writer.Write(player.RotationW); message.Serialize(writer); } foreach (IClient c in ClientManager.GetAllClients().Where(x => x != e.Client)) { c.SendMessage(message, e.SendMode); } } } }
public static void DeserialisePokemonAnimBool(object data, Animator animator) { if (data is DarkRiftReader) { using (DarkRiftReader reader = (DarkRiftReader)data) animator.SetBool(reader.ReadString(), reader.ReadBoolean()); } else { Debug.LogError("Should have recieved a DarkRiftReciever but didn't! (Got: " + data.GetType() + ")"); } }
public void ReadBooleanTest() { // GIVEN a buffer of serialized data mockMessageBuffer.Setup(m => m.Buffer).Returns(new byte[] { 1 }); mockMessageBuffer.Setup(m => m.Offset).Returns(0); mockMessageBuffer.Setup(m => m.Count).Returns(1); // WHEN I read a boolean from the reader bool result = reader.ReadBoolean(); // THEN the value is as expected Assert.AreEqual(true, result); }
private void HandleIsPlayerReady(Message message) { ushort id; bool isReady; using (DarkRiftReader reader = message.GetReader()) { id = reader.ReadUInt16(); isReady = reader.ReadBoolean(); } _lobbyStateManager.SetReady(id, isReady); _lobbyMessageSender.SendUpdateLobbyMessage(); }
private void ParseUpdateLobbyMessage(Message message) { Debug.Log("Updated Lobby"); _connectedPlayers.Clear(); using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { ushort id = reader.ReadUInt16(); bool ready = reader.ReadBoolean(); string nickname = reader.ReadString(); _lobbyManager.AddPlayerToLobby(id, nickname, ready); _connectedPlayers.Add(id); } _lobbyManager.CheckDisconnectedPlayers(_connectedPlayers); } }
void OnPlayerReadyMessage(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.PlayerSetReadyTag) { using (DarkRiftReader reader = message.GetReader()) { bool isReady = reader.ReadBoolean(); // Update player ready status and check if all players are ready players[e.Client].isReady = isReady; CheckAllReady(); } } } }
private void ClientMessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { if (message.Tag == (ushort)ChessEnums.MessageTags.PlayerReady) { if (reader.Length % 2 != 0) { Logger.Error("Received malformed ready packet."); return; } ushort id = reader.ReadUInt16(); Logger.Log("Player Ready: " + id, LogType.Info); readyPlayers.Add(id); if (readyPlayers.Count == 2) { DeliverColorsAndStartGame(); } } else if (message.Tag == (ushort)ChessEnums.MessageTags.MovePiece) { char originColumn = reader.ReadChar(); char originRow = reader.ReadChar(); char targetColumn = reader.ReadChar(); char targetRow = reader.ReadChar(); SendMovementToClient(originColumn, originRow, targetColumn, targetRow, ClientManager.GetClient(ingamePlayers.Find(x => x != e.Client.ID)).ID); if (playerColors[e.Client.ID] == ChessEnums.Colors.White) { timer.WhiteMoved(); } else { timer.BlackMoved(); } } else if (message.Tag == (ushort)ChessEnums.MessageTags.PieceEaten) { char column = reader.ReadChar(); char row = reader.ReadChar(); Logger.Log("Eaten: " + column + row, LogType.Info); SendEatenPieceToClient(column, row, ClientManager.GetClient(ingamePlayers.Find(x => x != e.Client.ID)).ID); } else if (message.Tag == (ushort)ChessEnums.MessageTags.WinnerMessage) { timer.EndTimer(reader.ReadBoolean()); } else if (message.Tag == (ushort)ChessEnums.MessageTags.MoveReplace) { char originColumn = reader.ReadChar(); char originRow = reader.ReadChar(); char targetColumn = reader.ReadChar(); char targetRow = reader.ReadChar(); ushort type = reader.ReadUInt16(); SendMovementReplaceToClient(originColumn, originRow, targetColumn, targetRow, type, ClientManager.GetClient(ingamePlayers.Find(x => x != e.Client.ID)).ID); if (playerColors[e.Client.ID] == ChessEnums.Colors.White) { timer.WhiteMoved(); } else { timer.BlackMoved(); } } } }
void DarkRift_MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { if (OverallStateManager.Instance.OverallState == OverallState.IN_GAME) { if (ClientGameManager.Instance.ClientState == GameStates.GAME_IN_PROGRESS || ClientGameManager.Instance.ClientState == GameStates.WAITING_FOR_PLAYERS) { //game is in progress if (message.Tag == NetworkTags.EntityUpdate) { ClientEntityManager entityManager = ClientEntityManager.Instance; while (reader.Position < reader.Length) { string entityId = reader.ReadString(); string entityTypeId = reader.ReadString(); Entity entity = entityManager.GetEntity(entityId); if (entity == null) { entity = entityManager.CreateEntity(entityTypeId); reader.ReadSerializableInto(ref entity); //must populate entity before registering since registration depends on entity data entityManager.RegisterEntity(entity); } else { reader.ReadSerializableInto(ref entity); } } } else if (message.Tag == NetworkTags.PlayerEventUpdate) { ClientEntityManager entityManager = ClientEntityManager.Instance; while (reader.Position < reader.Length) { string playerEventId = reader.ReadString(); string playerEventTypeId = reader.ReadString(); PlayerEvent playerEvent = entityManager.GetPlayerEvent(playerEventId); if (playerEvent == null) { playerEvent = entityManager.CreatePlayerEvent(playerEventTypeId); reader.ReadSerializableInto(ref playerEvent); //must populate event before registering since registration depends on event data entityManager.RegisterPlayerEvent(playerEvent); } else { reader.ReadSerializableInto(ref playerEvent); } } } else if (message.Tag == NetworkTags.EntityDeath) { ClientEntityManager.Instance.HandleEntityDeath(reader.ReadString()); } else if (message.Tag == NetworkTags.EntityDespawn) { ClientEntityManager.Instance.HandleEntityDespawn(reader.ReadString()); } else if (message.Tag == NetworkTags.PlayerEventEnd) { ClientEntityManager.Instance.HandlePlayerEventEnd(reader.ReadString()); } else if (message.Tag == NetworkTags.CapturePoint) { while (reader.Position < reader.Length) { CapturePoint capturePoint = ClientGameManager.Instance.CapturePoints[reader.ReadUInt16()]; reader.ReadSerializableInto(ref capturePoint); } } else if (message.Tag == NetworkTags.GameState) { ClientGameManager.Instance.UpdateGameState(reader.ReadSerializable <GameState>(), reader.ReadBoolean()); } } if (message.Tag == NetworkTags.ChatMessage) { UIManager.Instance.AddChatMessage(ClientGameManager.Instance.GameState.GetPlayer(reader.ReadString()), reader.ReadString()); } else if (message.Tag == NetworkTags.UnitList) { List <PlayerUnit> playerUnits = new List <PlayerUnit>(); while (reader.Position < reader.Length) { playerUnits.Add(reader.ReadSerializable <PlayerUnit>()); } UIManager.Instance.OnUnitListReceived(playerUnits); } else if (message.Tag == NetworkTags.PlayerJoined) { ClientGameManager.Instance.OnPlayerJoined(reader.ReadSerializable <Player>()); } else if (message.Tag == NetworkTags.PlayerLeft) { ClientGameManager.Instance.OnPlayerLeft(reader.ReadSerializable <Player>()); } } else if (OverallStateManager.Instance.OverallState != OverallState.IN_GAME) { if (waitingToJoin) { if (message.Tag == NetworkTags.GameState) { waitingToJoin = false; OverallStateManager.Instance.LoadGame(reader.ReadSerializable <GameState>(), reader.ReadBoolean()); } } else { if (message.Tag == NetworkTags.FullUnitList) { List <PlayerUnit> playerUnits = new List <PlayerUnit>(); while (reader.Position < reader.Length) { playerUnits.Add(reader.ReadSerializable <PlayerUnit>()); } MainMenuManager.Instance.OnUnitListReceived(playerUnits); } } } if (message.Tag == NetworkTags.Connection) { //Response to join request indicating if our matchmaking ticket was valid if (reader.ReadBoolean()) { IsConnectedToServer = true; //Notify anyone interested that we've completed joining a server Debug.Log("PlayFab: Ticket accepted by server, join completed."); ServerJoinSuccess?.Invoke("PlayFab: Ticket accepted by server, join completed."); } else { Debug.LogError("PlayFab: Ticket rejected by server, disconnecting."); ServerJoinFailure?.Invoke("PlayFab: Ticket rejected by server, disconnecting."); client.Disconnect(); } } } }
void Client_MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { using (DarkRiftReader r = message.GetReader()) { if (message.Tag == UDRMS_Tags.connectedToMS) { //TODO: Login MenuCanvas_Controller.Instance.ActiveLogin(); } if (message.Tag == UDRMS_Tags.LoginInfo) { bool canLogin = r.ReadBoolean(); if (canLogin) { Player_Manager.Instance.SetState(ClientState.AtLobbyScreen); } else { //TODO: Refresh Login } } if (message.Tag == UDRMS_Tags.getLobbyMatchs) { MenuCanvas_Controller.Instance.ClearMatchLobbys_Lobby(); while (r.Position < r.Length) { ushort matchID = r.ReadUInt16(); ushort matchOwnerID = r.ReadUInt16(); ushort actualPlayers = r.ReadUInt16(); ushort maxPlayersInMatch = r.ReadUInt16(); MenuCanvas_Controller.Instance.AddMatchLobby_Lobby(matchID); } } if (message.Tag == UDRMS_Tags.createLobbyMatch) { } if (message.Tag == UDRMS_Tags.getLobbyMatchInfo) { } //Connectou-se ao Lobby da Partida if (message.Tag == UDRMS_Tags.connectLobbyMatch) { bool canJoin = r.ReadBoolean(); ushort matchID = r.ReadUInt16(); if (canJoin) { //TODO: Move to Room Player_Manager.Instance.SetState(ClientState.AtLobbyMatchScreen); } } } } }
private void Client_MessageReceived(object sender, MessageReceivedEventArgs e) { try { using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { OpCodes opCode = (OpCodes)message.Tag; if (opCode != OpCodes.AuthenticationResponse && pendingConnections.Contains(e.Client.ID)) { pendingConnections.Remove(e.Client.ID); LeaveRoom(e.Client.ID); e.Client.Disconnect(); return; } switch (opCode) { case OpCodes.UpdateRoomData: string extraData = reader.ReadString(); int newMaxPlayers = reader.ReadInt32(); UpdateRoomData(e, extraData, newMaxPlayers); break; case OpCodes.AuthenticationResponse: if (reader.ReadString() == authKey) { pendingConnections.Remove(e.Client.ID); using (DarkRiftWriter writer = DarkRiftWriter.Create()) { using (Message sendAuthed = Message.Create((ushort)OpCodes.Authenticated, writer)) e.Client.SendMessage(sendAuthed, SendMode.Reliable); } } else { pendingConnections.Remove(e.Client.ID); LeaveRoom(e.Client.ID); e.Client.Disconnect(); } break; case OpCodes.RequestID: SendClientID(e); break; case OpCodes.CreateRoom: CreateRoom(e, reader.ReadInt32(), reader.ReadString(), reader.ReadBoolean(), reader.ReadString(), reader.ReadBoolean(), reader.ReadString()); break; case OpCodes.JoinServer: ushort hostID = reader.ReadUInt16(); bool useRelay = reader.ReadBoolean(); string clientLocalIP = reader.ReadString(); JoinRoom(e, hostID, useRelay, clientLocalIP); break; case OpCodes.SendData: byte[] readBuffer = readBuffers.Rent(reader.ReadInt32()); reader.ReadBytesInto(readBuffer, 0); ProcessData(e, reader, readBuffer, readBuffer.Length); break; case OpCodes.LeaveRoom: LeaveRoom(e.Client); break; case OpCodes.KickPlayer: ushort clientID = reader.ReadUInt16(); LeaveRoom(clientID, e.Client.ID); break; case OpCodes.RequestServers: SendServerList(e); break; } } } catch { // Do disconnect/kick maybe later if they do be acting up. } }
void DeserialisePosRot(object data) { //Here we decode the stream, the data will arrive as a DarkRiftReader so we need to cast to it //and then read the data off in EXACTLY the same order we wrote it. if (data is DarkRiftReader) { //Cast in a using statement because we are using streams and therefore it //is important that the memory is deallocated afterwards, you wont be able //to use this more than once though. using (DarkRiftReader reader = (DarkRiftReader)data) { //Then read and //update is for this object try { //Read boolean to set isKinematic RigidBody rb.isKinematic = reader.ReadBoolean(); if (DEBUG) { Debug.Log("isKinematic Readed"); } transform.position = new Vector3( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle() ); if (DEBUG) { Debug.Log("Position Readed"); } transform.rotation = new Quaternion( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle() ); if (DEBUG) { Debug.Log("ObjectID: " + ObjectID.ToString()); Debug.Log("Data recieved:" + transform.position.ToString("F4") + " " + transform.rotation.ToString("F6")); } } catch (KeyNotFoundException) { //Probably not aware of them yet! } } } else { Debug.LogError("Should have recieved a DarkRiftReciever but didn't! (Got: " + data.GetType() + ")"); transform.position = transform.position; transform.rotation = transform.rotation; } }
public static void DeserialisePokemonData(object data, ushort senderID, Trainer trainer) { if (data is DarkRiftReader) { using (DarkRiftReader reader = (DarkRiftReader)data) { string[] strings = reader.ReadStrings(); int[] ints = reader.ReadInt32s(); PokemonData pokemonData = new PokemonData(strings[0], strings[1], strings[2], ints[0], ints[1], ints[2], ints[3], ints[4], ints[5], ints[6], ints[7], ints[8], ints[9], ints[10], ints[11], ints[12], ints[13], ints[14], ints[15], ints[16], ints[17], ints[18], ints[19], ints[20], ints[21], ints[22], ints[23], ints[24], ints[25], ints[26], ints[27], ints[28], ints[29], ints[30], ints[31], ints[32], reader.ReadBoolean()); double[] doubles = reader.ReadDoubles(); GameObject clone = Object.Instantiate(Resources.Load("Prefabs/Pokemon/" + pokemonData.pokemonName), new Vector3((float)doubles[0], (float)doubles[1], (float)doubles[2]), Quaternion.identity) as GameObject; PokemonComponents components = clone.GetComponent <PokemonComponents>(); components.pokemon.trainerName = strings[3]; components.pokemon.networkID = senderID; components.pokemon.nickname = pokemonData.nickname; components.pokemon.fromTrade = pokemonData.fromTrade; components.pokemon.level = pokemonData.level; components.pokemon.gender = (Genders)pokemonData.gender; components.pokemon.nature = (Natures)pokemonData.nature; components.hpPP.curMaxHP = pokemonData.curMaxHP; components.hpPP.curMaxPP = pokemonData.curMaxPP; components.stats.curMaxATK = pokemonData.curMaxATK; components.stats.curMaxDEF = pokemonData.curMaxDEF; components.stats.curMaxSPATK = pokemonData.curMaxSPATK; components.stats.curMaxSPDEF = pokemonData.curMaxSPDEF; components.stats.curMaxSPD = pokemonData.curMaxSPD; components.hpPP.curHP = pokemonData.curHP; components.hpPP.curPP = pokemonData.curPP; components.stats.curATK = pokemonData.curATK; components.stats.curDEF = pokemonData.curDEF; components.stats.curSPATK = pokemonData.curSPATK; components.stats.curSPDEF = pokemonData.curSPDEF; components.stats.curSPD = pokemonData.curSPD; components.hpPP.hpEV = pokemonData.hpEV; components.hpPP.ppEV = pokemonData.ppEV; components.stats.atkEV = pokemonData.atkEV; components.stats.defEV = pokemonData.defEV; components.stats.spatkEV = pokemonData.spatkEV; components.stats.spdefEV = pokemonData.spdefEV; components.stats.spdEV = pokemonData.spdEV; components.hpPP.hpIV = pokemonData.hpIV; components.hpPP.ppIV = pokemonData.ppIV; components.stats.atkIV = pokemonData.atkIV; components.stats.defIV = pokemonData.defIV; components.stats.spatkIV = pokemonData.spatkIV; components.stats.spdefIV = pokemonData.spdefIV; components.stats.spdIV = pokemonData.spdIV; components.pokemon.curEXP = pokemonData.curEXP; components.pokemon.equippedItem = pokemonData.equippedItem; components.pokemon.id = pokemonData.id; trainer.AssignPokemon(components.pokemon); components.pokemon.SetupExistingPokemon(); } } else { Debug.LogError("Should have recieved a DarkRiftReciever but didn't! (Got: " + data.GetType() + ")"); } }