// Checks if there is anything entered into the input field. public void LockInput(InputField input) { enterIP.SetActive(false); input.interactable = false; if (input.text.Length == 0) { Debug.Log("Text has been entered"); } connecting.gameObject.SetActive(true); if (CustomNetworkManager.Instance.Connect(input.text)) { //Debug.Log("Connection successful"); connected.gameObject.SetActive(true); button.GetComponent <Button>().interactable = true; input.text = ""; PlaceHolder.GetComponent <Text>().text = ""; DarkRiftAPI.SendMessageToServer(Roland.NetworkingTags.Server, Roland.NetworkingTags.ServerSubjects.GetNumOfPlayers, ""); } else { //Debug.Log("Connection failed"); notConnected.gameObject.SetActive(true); } connecting.gameObject.SetActive(false); }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.C)) { Debug.Log("Sending C"); DarkRiftAPI.SendMessageToServer(Roland.NetworkingTags.Server, Roland.NetworkingTags.ServerSubjects.SendMeSomething, null); } }
/// <summary> /// Login with the specified username and password. /// </summary> /// <param name="username">The username.</param> /// <param name="password">The password.</param> public static void Login(string username, string password) { //Stop people from simply spamming the server if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return; } //Build the data to send using (DarkRiftWriter writer = new DarkRiftWriter()) { writer.Write(username); writer.Write(SecurityHelper.GetHash(password, hashType)); //Choose the connection to use if (connection == null) { if (DarkRiftAPI.isConnected) { //Send via DarkRiftAPI DarkRiftAPI.SendMessageToServer( tag, loginSubject, writer ); BindIfNotBound(); } else { //Called if you try to login whilst not connected to a server Debug.LogError("[LoginPlugin] You can't login if you're not connected to a server! (Do you mean to use DarkRiftAPI?)"); } } else { if (connection.isConnected) { //Send via DarkRiftConnection connection.SendMessageToServer( tag, loginSubject, writer ); BindIfNotBound(); } else { //Called if you try to login whilst not connected to a server Debug.LogError("[LoginPlugin] You can't login if you're not connected to a server!"); } } } }
private void OnEnable() { ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback; //Network.player.ipAddress; string ip = GetIPAddress(); //Debug.Log(Network.player.ipAddress); display_ip_address.text += " " + ip; DarkRiftAPI.onData += Receive; DarkRiftAPI.Connect(ip); DarkRiftAPI.SendMessageToServer(Roland.NetworkingTags.Server, Roland.NetworkingTags.ServerSubjects.GetNumOfPlayers, 0); }
public void SendRestart() { CurrentPlayer.Instance.Money += BaseIncomeGold; //DarkRiftAPI.SendMessageToServer(NetworkingTags.Server, NetworkingTags.ServerSubjects.SetMoneyForPlayer, CurrentPlayer.Instance.Money); int money = CurrentPlayer.Instance.Money; Debug.Log("Sennding " + money); DarkRiftAPI.SendMessageToServer(NetworkingTags.Server, NetworkingTags.ServerSubjects.PlayerRestarting, money); CurrentPlayer.Instance.Restart(); ChangeScene("MultiplayerGame"); }
public void StartTimer() { ClientReady = !ClientReady; ReadyText.SetActive(ClientReady); if (ClientReady) { DarkRiftAPI.SendMessageToServer(NetworkingTags.Server, NetworkingTags.ServerSubjects.ClientReadyToPlay, ""); } else { DarkRiftAPI.SendMessageToServer(NetworkingTags.Server, NetworkingTags.ServerSubjects.ClientNotReady, ""); } }
/// <summary> /// Logs out the user from the server. /// </summary> public static void Logout() { //Build the data to send using (DarkRiftWriter writer = new DarkRiftWriter()) { writer.Write(0); //Choose the connection to use if (connection == null) { if (DarkRiftAPI.isConnected) { //Send via DarkRiftAPI DarkRiftAPI.SendMessageToServer( tag, logoutSubject, writer ); BindIfNotBound(); } else { //Called if you try to login whilst not connected to a server Debug.LogError("[LoginPlugin] You can't logout if you're not connected to a server! (Do you mean to use DarkRiftAPI?)"); } } else { if (connection.isConnected) { //Send via DarkRiftConnection connection.SendMessageToServer( tag, logoutSubject, writer ); BindIfNotBound(); } else { //Called if you try to login whilst not connected to a server Debug.LogError("[LoginPlugin] You can't logout if you're not connected to a server!"); } } } }
void Start() { if (!DarkRiftAPI.isConnected) { CustomNetworkManager.Instance.Connect("127.0.0.1"); } DarkRiftAPI.onDataDetailed += ReceiveData; currentPlayerCount = 0; thePlayerBoxes = new GameObject[thePlayerBoxesParent.childCount]; for (int i = 0; i < thePlayerBoxesParent.childCount; i++) { thePlayerBoxes[i] = thePlayerBoxesParent.GetChild(i).gameObject; thePlayerBoxes[i].SetActive(false); } DarkRiftAPI.SendMessageToAll(NetworkingTags.Room, NetworkingTags.RoomSubjects.JoinRoom, ""); DarkRiftAPI.SendMessageToServer(NetworkingTags.Server, NetworkingTags.ServerSubjects.ChangeStateToRoom, ""); }
void OnMouseDrag() { //When draged we need to tell everyone else what's happening so... if (DarkRiftAPI.isConnected) { //First get it's new position... Vector3 pos = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(10); //Move it to that position on our screen transform.position = new Vector3(pos.x, pos.y, 0f); //Then send it to the others if we've moved enough if (Vector3.Distance(lastPos, pos) > 0.05f) { DarkRiftAPI.SendMessageToServer(0, (ushort)cubeID, new Vector3(pos.x, pos.y, 0f)); lastPos = pos; } } }
/******************************************************************* * Method used for initialization ******************************************************************/ void Start() { // connect to the DarkRift server at IP and port DarkRiftAPI.Connect(IP, port); // assign onDataRecieved function to the onData event so it // runs every time data is recieved from the server. DarkRiftAPI.onData += onDataReceived; // send the request for item and note data to spawn on the level // server can only reply to a specific client so the client has to // send the first message. if (DarkRiftAPI.isConnected) { Debug.Log("Sending data request."); DarkRiftAPI.SendMessageToServer(0, (ushort)clientID, "RequestData"); } else { Debug.Log("Failed to connect to DarkRift Server!"); } }
void ReceiveData(ushort senderID, byte tag, ushort subject, object data) { //When any data is received it will be passed here, //we then need to process it if it's got a tag of 0 and, if //so, create an object. This is where you'd handle most adminy //stuff like that. //Ok, if data has a Controller tag then it's for us if (tag == NetworkingTags.Controller) { //If a player has joined tell them to give us a player //Also internally increase the amount of players. if (subject == NetworkingTags.ControllerSubjects.JoinMessage) { CurrentPlayer.Instance.AmountOfPlayers++; int color = (int)data; MinersBombMinersServerPlugin.MinersBombMinersServerPlugin.PlayerType player = (MinersBombMinersServerPlugin.MinersBombMinersServerPlugin.PlayerType)color; if (ourColor == MinersBombMinersServerPlugin.MinersBombMinersServerPlugin.PlayerType.None) { ourColor = player; } if (senderID != DarkRiftAPI.id) { DarkRiftAPI.SendMessageToID(senderID, NetworkingTags.Controller, NetworkingTags.ControllerSubjects.ReplyToJoin, ourColor); } else { DarkRiftAPI.SendMessageToServer(NetworkingTags.Server, NetworkingTags.ServerSubjects.GetMoneyForPlayer, DarkRiftAPI.id); } color -= 1; KillTrackSystem.Instance.AddPlayer(senderID, new PlayerStats()); ConnectDisconnect.instance.AddPlayer(color, senderID); int ThisPlayerColor = ConnectDisconnect.instance.GetPlayerColor(DarkRiftAPI.id); if (ThisPlayerColor != -1) { CurrentPlayer.Instance.HPIconSprite = StartingSprite[ThisPlayerColor]; } CurrentPlayer.Instance.UpdateHealthPointInGame(); } else if (subject == NetworkingTags.ControllerSubjects.SpawnPlayer) { } else if (subject == NetworkingTags.ControllerSubjects.ReadyToStartGame) { PlayerReady++; } else if (subject == NetworkingTags.ControllerSubjects.ReplyToJoin) { CurrentPlayer.Instance.AmountOfPlayers++; MinersBombMinersServerPlugin.MinersBombMinersServerPlugin.PlayerType player = (MinersBombMinersServerPlugin.MinersBombMinersServerPlugin.PlayerType)data; int color = (int)player; color -= 1; ConnectDisconnect.instance.AddPlayer(color, senderID); KillTrackSystem.Instance.AddPlayer(senderID, new PlayerStats()); } else if (subject == NetworkingTags.ControllerSubjects.YouWin) { CheckWinLose(WinLoseDraw.Win); } else if (subject == NetworkingTags.ControllerSubjects.Draw) { CheckWinLose(WinLoseDraw.Draw); } else if (subject == NetworkingTags.ControllerSubjects.YouLose) { CheckWinLose(WinLoseDraw.Lose); } else if (subject == NetworkingTags.ControllerSubjects.GameOver) { RestartButton.SetActive(true); } else if (subject == NetworkingTags.ControllerSubjects.GetMoneyForPlayer) { CurrentPlayer.Instance.Money = (int)data; } else if (subject == NetworkingTags.ControllerSubjects.StartGame) { List <MinersBombMinersServerPlugin.PacketUseTypeID> PacketPlayerData = (List <MinersBombMinersServerPlugin.PacketUseTypeID>)data; for (int i = 0; i < PacketPlayerData.Count; i++) { Vector2 SpawnPoint = Vector2.zero; RuntimeAnimatorController theColoredPlayer = PlayerAnimators[0]; switch (PacketPlayerData[i].thePlayerType) { case 1: theColoredPlayer = PlayerAnimators[0]; SpawnPoint = new Vector2(1, 1); break; case 2: SpawnPoint = new Vector2(theTileMap.size_x - 2, theTileMap.size_z - 2); theColoredPlayer = PlayerAnimators[1]; break; case 3: SpawnPoint = new Vector2(theTileMap.size_x - 2, 1); theColoredPlayer = PlayerAnimators[2]; break; case 4: SpawnPoint = new Vector2(1, theTileMap.size_z - 2); theColoredPlayer = PlayerAnimators[3]; break; default: Debug.LogWarning("No such player type found! Logged " + PacketPlayerData[i].thePlayerType); break; } GameObject clone; if (PacketPlayerData[i].client_id == DarkRiftAPI.id) { clone = Lean.LeanPool.Spawn(PlayerPrefab, theTileMap.ConvertTileToWorld(SpawnPoint), Quaternion.identity); Player thePlayer = clone.GetComponentInChildren <Player>(); thePlayer.player_id = PacketPlayerData[i].client_id; thePlayer.theController = this; thePlayer.theEquipments = CurrentPlayer.Instance.AmountOfEquipments; CurrentPlayer.Instance.ThePlayer = thePlayer; UiHolder theHolder = GetComponent <UiHolder>(); HealthBar healthBar = clone.GetComponentInChildren <HealthBar>(); healthBar.damageImage = theHolder.DamageHealth; } else { clone = Lean.LeanPool.Spawn(PlayerDummy, theTileMap.ConvertTileToWorld(SpawnPoint), Quaternion.identity); DummyPlayer thePlayer = clone.GetComponentInChildren <DummyPlayer>(); thePlayer.id = PacketPlayerData[i].client_id; } clone.GetComponentInChildren <Animator>().runtimeAnimatorController = theColoredPlayer; } StartCoroutine(StartCountdown()); } else if (subject == NetworkingTags.ControllerSubjects.DisconnectYou) { DarkRiftAPI.Disconnect(); } } else if (tag == NetworkingTags.Server) { if (subject == NetworkingTags.ServerSubjects.PlayerRestarting) { Debug.Log("Here data is " + data); } } }
/// <summary> /// Sends a command to the server, to be distributed to everyone in the player's room. /// </summary> public static void SendCommand(Command com) { //Be sure to convert the list to an array since DarkRift can't send lists DarkRiftAPI.SendMessageToServer((byte)NetworkTag.Game, (ushort)0, com.Serialized().ToArray()); }
void OnDespawn() { DarkRiftAPI.SendMessageToServer(NetworkingTags.Server, NetworkingTags.ServerSubjects.ILose, ""); OnDestroy(); }
/******************************************************************* * Method to determine what to do when the application stops. ******************************************************************/ void OnApplicationQuit() { // connect to the server DarkRiftAPI.Connect(IP, port); List <GameObject> goList = new List <GameObject> (); bool skip = false; // grab all the GameObjects in the level GameObject[] temp = (GameObject[])GameObject.FindObjectsOfType(typeof(GameObject)); // find the GameObjects that represent items and put them in the goList foreach (GameObject go in temp) { // if the gameObject has both ItemData and a SpriteRenderer then it's an item on the floor // inventory doesn't use the SpriteRenderer if (go.GetComponent <ItemData>() != null && go.GetComponent <SpriteRenderer>() != null) { // compare to the items in the itemData list that were sent from the server foreach (DataPacket item in itemData) { // if the position and id are the same as item sent from database then duplicate, don't send back if (item.x == go.transform.position.x && item.y == go.transform.position.y && item.itemNum == go.GetComponent <ItemData>().item.ID) { // set skip to true and break out of item comparison skip = true; break; } } // if skip is true don't put in list for database, and check next gameobject if (skip) { skip = false; continue; } goList.Add(go); //Debug.Log ("putting in LIST: " + go.name); } } // for every item in the goList, get the item data and location foreach (GameObject go in goList) { ItemData idata = go.GetComponent <ItemData>(); if (idata == null) { Debug.Log("ERROR!!! idata is null"); } Debug.Log("item going to database: " + idata.item.Title); // create a new DataPacket with the item data DataPacket dp = new DataPacket((byte)idata.item.ID, go.transform.position.x, go.transform.position.y); // send the item data to the server DarkRiftAPI.SendMessageToServer(1, (ushort)clientID, dp); } // notes list filled as notes are written by function addNote, just send them to DB. foreach (DataPacket packet in notes) { Debug.Log("note going to database: " + packet.note); // temporarily sending the note packets as a string, for some reason won't send the DataPacket as a note // but can send DataPacket as a note from server to client DarkRiftAPI.SendMessageToServer(2, (ushort)clientID, packet.note + "|" + packet.x + "|" + packet.y); } // disconnect from server DarkRiftAPI.Disconnect(); }
/// <summary> /// Sends a command to the server, to be distributed to everyone in the player's room. /// </summary> public static void SendCommand(byte[] data) { //Be sure to convert the list to an array since DarkRift can't send lists DarkRiftAPI.SendMessageToServer((byte)NetworkTag.Game, (ushort)0, data); }