void Handler_OnReadyReceived(NetworkMessage netMsg) { PTMessage msg = netMsg.ReadMessage <PTMessage>(); PTDebug("OnReady: " + msg.ToString(true)); PTPlayer player = PTPlayer.Find(msg.senderName); if (player != null) { players[player] = true; } if (OnReadyReceived != null) { OnReadyReceived(msg); } ConfirmReadyReceived(netMsg); }
void Handler_OnConnected(NetworkMessage netMsg) { PTMessage msg = netMsg.ReadMessage <PTMessage>(); PTDebug("OnConnected: " + msg.ToString(true)); PTPlayer player = PTPlayer.Find(netMsg.conn.connectionId); if (player != null) { players[player] = false; } else { PTPlayer newPlayer = new PTPlayer(msg.senderName, netMsg.conn.connectionId, null); players.Add(newPlayer, false); } if (isTableTop) { if (!acceptNewPlayer || players.Count >= maximumPlayer) { //put the newly connected player in the wait queue KeyValuePair <string, int> newPlayer = new KeyValuePair <string, int>(msg.senderName, netMsg.conn.connectionId); waitQueue.Enqueue(newPlayer); } //Tell the sender client that this server has been connected PTMessage msgFromServer = new PTMessage(); msgFromServer.senderName = playerName; NetworkServer.SendToClient(netMsg.conn.connectionId, (short)PTEvent.Connect, msgFromServer); } else { } //Invoke OnConnected Event if (OnConnected != null) { OnConnected(msg); } }
public static void Send(PTPlayer player, byte[] data) { //This method is the parent method for other server side overloads //Corner case if (singleton == null || !NetworkServer.active || player == null) { Debug.Log("Error in PTManager.Send(PTPlayer player, byte[] data): Check singleton, server settings or PTPlayer instance"); return; } //create pt message to send PTMessage msg = new PTMessage(); msg.senderName = playerName; msg.data = data; //Send the pt message NetworkServer.SendToClient(player.connectionId, (short)PTEvent.Data, msg); singleton.PTDebug("Sent to " + player + ": " + msg.ToString(true)); }
private IEnumerator InstantiatePlayerTT(PTPlayer ptPlayer) { //paramaters float awayFromCenter = 0.6f; float currY = 7.5f; //random init position Vector3 currPos = Vector3.zero; while (true) { yield return(new WaitForEndOfFrame()); float currX = UnityEngine.Random.Range(X_MIN, X_MAX); float currZ = UnityEngine.Random.Range(Z_MIN, Z_MAX); if ((currX > X_MAX * awayFromCenter || currX < X_MIN * awayFromCenter) && (currZ > Z_MAX * awayFromCenter || currZ < Z_MIN * awayFromCenter)) { currPos = new Vector3(currX, currY, currZ); break; } } //instantiate game object PTMono_PlayerTT newPlayer = Instantiate(ptMonoPlayer.gameObject, currPos, ptMonoPlayer.transform.rotation).GetComponent <PTMono_PlayerTT>(); //Assign the ptPlayer to the new player game object newPlayer.ptPlayer = ptPlayer; //Set name int countPresetName = Enum.GetNames(typeof(PresetName)).Length; newPlayer.name = newPlayer.ptPlayer == null?General.RandName() + "(Bot)" : newPlayer.ptPlayer.name; while (true) { yield return(new WaitForEndOfFrame()); bool hasSameName = false; foreach (PTMono_PlayerTT player in players) { if (!player.Equals(newPlayer) && player.name == newPlayer.name) { hasSameName = true; print("while InstantiatePlayerTT, same name detected:" + player.name); break; } } if (hasSameName) { newPlayer.name = newPlayer.ptPlayer == null?General.RandName() + "(Bot)" : newPlayer.ptPlayer.name; } else { break; } } //wait until newplayer is instantiated successfully yield return(new WaitUntil(() => newPlayer != null)); }
public static void Send <T>(string playerName, T data) { PTPlayer player = PTPlayer.Find(playerName); Send <T>(player, data); }