public void Connect(TcpClient _socket) { socket = _socket; socket.ReceiveBufferSize = dataBufferSize; socket.SendBufferSize = dataBufferSize; stream = socket.GetStream(); receivedData = new Packet(); receiveBuffer = new byte[dataBufferSize]; stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null); ServerSend.Welcome(id, "You have connected to the server, welcome!"); }
//TODO: Server security, make "hit verify" public static void PlayerDamageReceived(int _fromClient, Packet _packet) { //TODO: //int _itemId = _packet.ReadInt(); //then use the ID of the item in server dictionary to get damage //weapon can also be used to tell client what damage effect to play //HACK: (for testing): int _playerHit = _packet.ReadInt(); float _damageDealt = _packet.ReadFloat(); Console.WriteLine($"Player {_fromClient} ({Server.clients[_fromClient].player.username}) hit player {_playerHit} ({Server.clients[_playerHit].player.username}) and dealt {_damageDealt} damage."); Server.clients[_playerHit].player.health -= _damageDealt; ///<summary>Tell ServerSend to update their health, but do not pass on data directly</summary> ServerSend.PlayerDamage(_playerHit); }
public void SendIntoGame(string _playerName) { player = new Player(id, _playerName, new Vector3(0, 0, 0)); foreach (Client _client in Server.clients.Values) { if (_client.player != null) { if (_client.id != id) { ServerSend.SpawnPlayer(id, _client.player); } } } foreach (Client _client in Server.clients.Values) { if (_client.player != null) { ServerSend.SpawnPlayer(_client.id, player); } } }
public static async void InterpCommand() //TODO: Make a thread call this? { while (true) { await Task.Run(() => { //await ReadConsoleAsync(); Console.Write("> "); string consoleInput = Console.ReadLine(); string[] consoleParts = consoleInput.Split(' '); switch (consoleParts[0].ToLower()) { case "help": Console.WriteLine("Commands:\nAnnounce\nAdmin\nGive\nHealth\nDisconnect\nExit"); break; case "announce": ServerSend.TextChat(0, consoleInput.Replace("announce", "Server:"), new Colour(255, 0, 230, 255)); break; case "admin": //Can only be issued by server, no packet //TODO: give player by ID admin perms (/give command, change gamemode). break; case "give": break; case "health": ///<summary> See the health of a player by ID. </summary> if (consoleParts.Count() <= 2) { try { Console.WriteLine($"Health of player {Server.clients[int.Parse(consoleParts[1])].player.username} is: {Server.clients[int.Parse(consoleParts[1])].player.health}"); } catch {} ///<note> Hack to prevent crash on exception</note> } ///<summary> Set player health. </summary> else { try { Server.clients[int.Parse(consoleParts[1])].player.health = int.Parse(consoleParts[2]); } catch {} } break; case "disconnect": //TODO: Disconnect player by ID from server. break; case "exit": //TODO: disconnect all players first. throw new Exception("\nServer Shutdown Called!"); default: if (consoleInput != "") { Console.WriteLine("Invalid command. Run 'help' to view list of commands."); } break; } }); } }