/// <summary> /// Wymiana danych (klient - serwer). Podstawowa funkcja /// w ktorej obslugujemy danego klienta /// </summary> private void UpdateData() { while (true) { try { BinaryReader odbr = new BinaryReader(_clientSocket.GetStream()); int howMuchreceive = odbr.ReadInt32(); String ReceivedData; Byte[] data = new Byte[howMuchreceive]; Int32 sData; sData = _clientSocket.GetStream().Read(data, 0, data.Length); ReceivedData = Encoding.ASCII.GetString(data, 0, sData); if (ReceivedData[0] == '?') // Obaj przegrali { Console.WriteLine("Client {0} disconnected", lastId.ToString()); if ((lastId == _clientSockets[0].Id && _clientSockets[0].Id < _clientSockets[1].Id) || (lastId == _clientSockets[1].Id && _clientSockets[0].Id > _clientSockets[1].Id)) { ZeroArray(tabA, tabPos); } if ((lastId == _clientSockets[1].Id && _clientSockets[0].Id < _clientSockets[1].Id) || (lastId == _clientSockets[0].Id && _clientSockets[0].Id > _clientSockets[1].Id)) { ZeroArray(tabB, tabPos); } tabPos.Clear(); _clientSocket.GetStream().Close(); _clientSocket.Close(); _clientSockets.RemoveAt(GetIndex(_clientSockets, lastId)); _clientSocket.Dispose(); break; } if (ReceivedData[0] == '!') // Ktorys przegral badz sie rozlaczyl { if (_clientSockets.Count > 1) { for (int i = 0; i < _clientSockets.Count; i++) { if (_clientSockets[i].Socket != _clientSocket) { SendDataToClient("K" + "&", _clientSockets[i].Socket); } } } if (_clientSockets.Count > 1) { if ((lastId == _clientSockets[0].Id && _clientSockets[0].Id < _clientSockets[1].Id) || (lastId == _clientSockets[1].Id && _clientSockets[0].Id > _clientSockets[1].Id)) { ZeroArray(tabA, tabPos); } if ((lastId == _clientSockets[1].Id && _clientSockets[0].Id < _clientSockets[1].Id) || (lastId == _clientSockets[0].Id && _clientSockets[0].Id > _clientSockets[1].Id)) { ZeroArray(tabB, tabPos); } } else { if (lastId == 1) { ZeroArray(tabA, tabPos); } else { ZeroArray(tabB, tabPos); } } tabPos.Clear(); Console.WriteLine("Client {0} disconnected", lastId.ToString()); _clientSocket.GetStream().Close(); _clientSocket.Close(); _clientSockets.RemoveAt(GetIndex(_clientSockets, lastId)); //_clientSocket.Dispose(); break; } if (ReceivedData[0] == 'F') // Pozycje jedzenia { RandInfoAboutFood(); for (int i = 0; i < _clientSockets.Count; i++) { string dataAboutFood = "F#" + _typeOfFood.ToString() + "@" + _foodX.ToString() + "," + _foodY.ToString() + "&"; SendDataToClient(dataAboutFood, _clientSockets[i].Socket); } } if (ReceivedData[0] == 'S') // Wspolrzedne weza { if (_clientSockets.Count > 1) { for (int i = 0; i < _clientSockets.Count; i++) { if (_clientSockets[i].Socket != _clientSocket) { string toSend = "S#" + lastId.ToString() + "*" + ReceivedData.Remove(0, 2); SendDataToClient(toSend, _clientSockets[i].Socket); } } if ((lastId == _clientSockets[0].Id && _clientSockets[0].Id < _clientSockets[1].Id) || (lastId == _clientSockets[1].Id && _clientSockets[0].Id > _clientSockets[1].Id)) { ZeroArray(tabA, tabPos); } if ((lastId == _clientSockets[1].Id && _clientSockets[0].Id < _clientSockets[1].Id) || (lastId == _clientSockets[0].Id && _clientSockets[0].Id > _clientSockets[1].Id)) { ZeroArray(tabB, tabPos); } tabPos.Clear(); string[] split1 = ReceivedData.Split('&'); string[] split2 = split1[0].Split('$'); string[] positions = split2[1].Split(';'); foreach (var el in positions) { string[] tmp = el.Split(','); if (tmp.Length > 1) { tabPos.Add(Int32.Parse(tmp[0]) / 10); tabPos.Add(Int32.Parse(tmp[1]) / 10); } } if ((lastId == _clientSockets[0].Id && _clientSockets[0].Id < _clientSockets[1].Id) || (lastId == _clientSockets[1].Id && _clientSockets[0].Id > _clientSockets[1].Id)) { UpdateArray(tabA, tabPos); } if ((lastId == _clientSockets[1].Id && _clientSockets[0].Id < _clientSockets[1].Id) || (lastId == _clientSockets[0].Id && _clientSockets[0].Id > _clientSockets[1].Id)) { UpdateArray(tabB, tabPos); } //if (lastId == _clientSockets[0].Id) //{ // UpdateArray(tabA, tabPos); //} //else UpdateArray(tabB, tabPos); if (CheckCollsion(tabA, tabB) == true) { for (int k = 0; k < _clientSockets.Count; k++) { if (_clientSockets[k].Socket != _clientSocket) { SendDataToClient("C&", _clientSockets[k].Socket); } } } } } if (ReceivedData[0] == 'P') //Wyslanie punktow { if (_clientSockets.Count > 1) { for (int i = 0; i < _clientSockets.Count; i++) { if (_clientSockets[i].Socket != _clientSocket) { string toSend2 = ReceivedData + "&"; SendDataToClient(toSend2, _clientSockets[i].Socket); } } } } if (_clientSockets.Count == 1) { string toSendIfOne = lastId.ToString() + "*" + "0"; SendDataToClient(toSendIfOne, _clientSocket); } } catch (Exception ex) { Console.WriteLine(" >> " + ex.ToString()); } } }