/// <summary> /// Adds the vote to the VoteMachine. /// </summary> /// <param name="stage_id">What stage did he or she vote?</param> /// <param name="who">Who sent the vote?</param> public void AcceptVote(TcpClientSocket who, byte stage_id) { if (playstate != 2) { return; } VoteMachine.Votes.Add(stage_id); if (VoteMachine.Votes.Count == this.players.Count) { VoteMachine.Clock.Enabled = true; } //Tell the players that this person voted and what they chose. foreach (TcpClientSocket Player in players) { var Write = Player.WriteBuffer; Write.Clear(); Write.WriteUByte(Player.HeaderId); Write.WriteUByte(Code.Vote); Write.WriteUByte(who.Slot); Write.WriteUByte(stage_id); Write.SendTcp(Player.DataStream); } }
public static void TcpDisconnect(TcpClientSocket myClient) { // Notify the client that the server has disconnected it from the server. myClient.WriteBuffer.Clear(); myClient.WriteBuffer.WriteUByte(Code.ConnectionEnd); myClient.WriteBuffer.WriteString(MainServer.KickMessage); myClient.WriteBuffer.SendTcp(myClient.DataStream); }
public void Link(TcpClientSocket obj) { Client = obj; Name = obj.Name; Tag = obj.Tag; ID = obj.ID; obj.ListSlot = this; }
public uint Join(TcpClientSocket NewGuy) { //Already in a room! if (NewGuy.CurrentRoom != null) { return(4000000002); } //Room is full! if ((players.Count + 1) > maxplayers) { return(4000000000); } //Room is Voting or in Battle! (let's not do spectator yet) if ((tag != 0 && playstate != 1) || (tag == 0 && playstate > 2)) { return(4000000001); } //Success! Tell all current players that a new one's coming in. foreach (TcpClientSocket Player in players) { var Write = Player.WriteBuffer; Write.Clear(); Write.WriteUByte(Player.HeaderId); Write.WriteUByte(Code.UpdatePlayers); Write.WriteBool(true); //A player has joined. Here's his or her name, icon, XP. Write.WriteString(NewGuy.Name); Write.WriteUByte(NewGuy.Tag); Write.WriteUByte(NewGuy.Icon); Write.WriteUInt(NewGuy.XP); Write.WriteUByte(NewGuy.Character); Write.WriteUByte(NewGuy.Team); Write.WriteDouble(NewGuy.ID); Write.SendTcp(Player.DataStream); } players.Add(NewGuy); NewGuy.CurrentRoom = this; NewGuy.Record = new Record(); //Recording for battles. NewGuy.Slot = (byte)(this.players.IndexOf(NewGuy) + 1); CmdSystem.AddLog(NewGuy.Name + " has joined room " + this.name + " (ID:" + this.id + ")"); return(this.id); }
/// <summary> /// Creates a new Room, with all default values. This is normally called by the server. /// </summary> public Room() { MainServer.RoomList.Add(this); this.id = ++MainServer.RoomNumb; this.host = null; this.name = "Server Room " + this.id; this.host_name = "System"; this.mode = 1; this.teams = false; this.minutes = 3; this.maxplayers = 8; this.maxteams = 2; this.tag = 0; CmdSystem.AddLog(this.name + " created."); players = new List <TcpClientSocket>(); MainServer.Event.RoomCreated(this); }
/// <summary> /// Changes the character of this player and notifies everyone. /// </summary> public void ChangeCharacter(TcpClientSocket who, byte toWhat) { if (playstate > 2) { return; } who.Character = toWhat; var Write = who.WriteBuffer; Write.WriteUByte(Code.ChangeCharacter); Write.WriteUByte(who.Slot); Write.WriteUByte(who.Character); foreach (TcpClientSocket Player in players) { Write.SendTcp(Player.DataStream); } }
/// <summary> /// Changes the team of this player and notifies everyone. /// </summary> public void ChangeTeam(TcpClientSocket who, byte toWhat) { if (playstate != 1 || !this.teams) { return; } who.Team = toWhat; var Write = who.WriteBuffer; Write.WriteUByte(Code.ChangeTeam); Write.WriteUByte(who.Slot); Write.WriteUByte(who.Team); foreach (TcpClientSocket Player in players) { Write.SendTcp(Player.DataStream); } }
/// <summary> /// Creates a new Room, which acts as a Server, or a Room, or a Party, for players to join. /// Automatically places the host in there too, because... empty rooms? Nahh. /// </summary> /// <param name="host">Hosting Player</param> /// <param name="name">Name of the Room, will be seen by other players</param> /// <param name="mode">The starting mode, which can be changed later</param> /// <param name="isTeam">Are teams involved?</param> /// <param name="BattleLength">Length of the battles, in minutes</param> /// <param name="MaxPlayers">Maximum number of players</param> /// <param name="NumbTeams">Number of allowed teams</param> public Room(TcpClientSocket host, String name, byte mode, bool isTeam, byte NumbTeams, byte BattleLength, byte MaxPlayers) { MainServer.RoomList.Add(this); this.id = ++MainServer.RoomNumb; this.host = host; this.host_name = host.Name; this.name = name; this.mode = mode; this.teams = isTeam; this.minutes = BattleLength; this.maxplayers = MaxPlayers; this.maxteams = NumbTeams; this.tag = 1; players = new List <TcpClientSocket>(); players.Add(host); host.CurrentRoom = this; host.Record = new Record(); //Recording for battles. host.Slot = 1; CmdSystem.AddLog(host.Name + " has created room " + this.name + " (ID:" + id + ")"); MainServer.Event.RoomCreated(this); }
public void SendMyInfo(TcpClientSocket ToWho) { var Write = ToWho.WriteBuffer; Write.Clear(); Write.WriteUByte(ToWho.HeaderId); Write.WriteUByte(Code.RequestRoomInformation); Write.WriteUInt(id); Write.WriteString(name); Write.WriteString(host_name); Write.WriteSByte(tag); Write.WriteUByte((byte)players.Count); Write.WriteUByte(maxplayers); Write.WriteUByte(mode); Write.WriteBool(teams); Write.WriteUByte(playstate); Write.WriteUByte(maxteams); Write.WriteUByte(minutes); Write.SendTcp(ToWho.DataStream); }
/// <summary> /// Sends every player's information, including Name, Icon, XP, ID, Char and Team. /// </summary> /// <param name="ToWhom">To who shall we send it to?</param> public void SendPlayerList(TcpClientSocket ToWhom) { var Write = ToWhom.WriteBuffer; Write.Clear(); Write.WriteUByte(ToWhom.HeaderId); Write.WriteUByte(Code.RequestPlayerList); foreach (TcpClientSocket Player in players) { Write.WriteBool(true); //A player has left. Here's his place, but you might have to request the list again. Write.WriteUByte(Player.Tag); Write.WriteUByte(Player.Icon); Write.WriteUInt(Player.XP); Write.WriteUByte(Player.Character); Write.WriteUByte(Player.Team); Write.WriteDouble(Player.ID); Write.WriteString(Player.Name); } Write.WriteBool(false); Write.SendTcp(ToWhom.DataStream); }
/// <summary> /// After battle, each player submits their stats. This method lets everyone else know. /// </summary> public void DistributeStats(TcpClientSocket who) { if (playstate != 5) { return; } var Write = who.WriteBuffer; Write.WriteUShort(who.Record.HitsGiven); Write.WriteUShort(who.Record.Kills); Write.WriteUShort(who.Record.Items); Write.WriteUShort(who.Record.HitsTaken); Write.WriteUShort(who.Record.Deaths); Write.WriteUShort(who.Record.CoinsGained); Write.WriteUShort(who.Record.CoinsLost); foreach (TcpClientSocket Player in players) { Write.SendTcp(Player.DataStream); } }
/// <summary> /// Accepts incoming client connections. /// </summary> public async void TcpAccept() { // While the server is online accept incoming client connections. while (Status == true) { // Attempt to process the code, if not successful, throw an exception and close the server. Thread.Sleep(10); try { // If a pending client connection is found, accept the client connection. if (ServerHandle.Pending() == true) { // If the client connection is accepted, setup the new clent. TcpClientSocket NewClient = new TcpClientSocket(BufferReadSize, BufferWriteSize, BufferAlignment, PacketHeader); NewClient.ClientHandle = await ServerHandle.AcceptTcpClientAsync(); CmdSystem.AddLog("New connection. Verifying..."); NewClient.ClientHandle.LingerState = new LingerOption(true, 0); NewClient.ClientHandle.NoDelay = true; NewClient.DataStream = NewClient.ClientHandle.GetStream(); // Add the client to the server's client socket list. ClientList.Add(NewClient.SocketId, NewClient); // Start running the client and processing data for it, be it sending or receiving data. ThreadPool.QueueUserWorkItem(myThread => ClientHandle(NewClient)); } } catch (Exception e) { CmdSystem.AddLog("===== ERROR ====="); CmdSystem.AddLog(e.Message); CmdSystem.AddLog("================="); } } TcpListenerClose(); }
/// <summary> /// Starts handling the new client. If the client connects after MaxClients is exceeded, the client is disconnected. /// </summary> /// <param name="myClient">The client socket class that is being handled.</param> public async void ClientHandle(TcpClientSocket myClient) { if (ClientList.Count < MaxClients) { // Attempt to process the code, if not successful, throw an exception and disconnect the client. try { // While the client is connected, process any data and respond accordingly. while (myClient.Connected == true) { // Only process data if data is available(sent from the client to the server). if (myClient.CurrentRoom != null) { Thread.Sleep(1); } else { Thread.Sleep(20); } // If a client-issued(not server-issued) disconnection has been detected close the client connection. // Usually this is an unexpected disconnection, a proper disconnection should have a client send a message/packet requesting disconnection. if (myClient.ClientHandle.Connected == false) { myClient.WriteBuffer.SetPeek(0); myClient.WriteBuffer.WriteUByte(PacketHeader); myClient.WriteBuffer.SendTcp(myClient.DataStream); myClient.Connected = myClient.ClientHandle.Connected; } if (myClient.DataStream.DataAvailable == true) { myClient.DCTimer.Enabled = false; myClient.DCTimer.Interval = 30000; // Clear the read and write buffers to respond to the newly received packet. myClient.ReadBuffer.Reset(); myClient.WriteBuffer.Reset(); // Copy the received data from the client's network stream to the client's read buffer. int PacketSize = myClient.ClientHandle.Available; await myClient.DataStream.ReadAsync(myClient.ReadBuffer.Buffer, 0, PacketSize); // Start reading the received data and respond accordingly. TcpPackets.TcpPacketRead(myClient, this); } else { //So we didn't get any data. Set the timer for 30 seconds to force-remove the player unless they respond again. myClient.DCTimer.Enabled = true; } } } catch (Exception e) { myClient.Connected = false; //if (myClient.UserImposedDisconnection == false) { CmdSystem.AddLog(myClient.Name + " has been disconnected."); Console.WriteLine("==="); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); Console.WriteLine("==="); } } } // If the client disconnects, run a method that happens upon client disconnection. /* * NOTE: If a client disconnections by itself you can NOT use this to send messages back to the client. * Even though you can't send a message back to the client, you can still do something else. Such as * displaying a console output or sending a message to other clients that this client disconnected. */ // You CAN send messages back to the client if the client is being disconnected by the server instead! // This is because the client is still connected to the server while the ClientDisconnect() method is run. // However, the client is disconnected AFTER the ClientDisconnect() method. if (myClient.CurrentRoom != null) { myClient.CurrentRoom.Remove(myClient); } TcpPackets.TcpDisconnect(myClient); // Remove the client from the client list when it disconnects. ClientList.Remove(myClient.SocketId); myClient.TcpClientClose(); if (myClient.ListSlot != null) { myClient.ListSlot.GoOffline(); } }
public void PlayerJoinedRoom(TcpClientSocket Player, Room Room) { }
public void PlayerLeftRoom(TcpClientSocket Player, Room Room) { }
/// <summary> /// Takes the player out of the room. /// </summary> /// <param name="Quitter">The Player to remove.</param> public byte Remove(TcpClientSocket Quitter) { if (Quitter.CurrentRoom == null) { return(0); } var place = players.IndexOf(Quitter) + 1; players.Remove(Quitter); try { foreach (TcpClientSocket Player in players) { var Write = Player.WriteBuffer; Write.Clear(); Write.WriteUByte(Player.HeaderId); Write.WriteUByte(Code.UpdatePlayers); Write.WriteBool(false); //A player has left. Here's his place, but you might have to request the list again. Write.WriteUByte((byte)place); Write.SendTcp(Player.DataStream); if (playstate < 3) { Player.Slot = (byte)(this.players.IndexOf(Player) + 1); } } } catch { } Quitter.CurrentRoom = null; Quitter.Record = null; Quitter.Slot = 0; CmdSystem.AddLog(Quitter.Name + " has left room " + this.name + " (ID:" + this.id + ")"); if (players.Count == 0) { if (tag == 0) { playstate = 1; } else { Dispose(); } } else if (players.Count == 1 && playstate > 1) { EnterLobby(); } else if (playstate > 1) { PlayerCompleted(); PlayerFinished(); PlayerReady(); if (playstate == 2 && VoteMachine.Votes.Count >= this.players.Count) { VoteMachine.Clock.Enabled = true; } } return(1); }
/// <summary> /// Read and process packets received from the client. /// </summary> /// <param name="myClient">Client socket class that is being handled.</param> // <param name="myClientList">List of clients from the server.</param> /// <param name="myServer">Server's handle(listener) socket class.</param> // <param name="myServerSocketId">Socket ID of the server.</param> public static void TcpPacketRead(TcpClientSocket myClient, TcpListenerSocket myServer) { byte myCheck = myClient.ReadBuffer.StartRead(0); var WriteBuff = myClient.WriteBuffer; var ReadBuff = myClient.ReadBuffer; var File = MainServer.SettingsFile; var ID = myClient.PlayerID.ToString(); // Check for packets by searching for packet headers. while (myCheck == myClient.HeaderId) { byte myPacketId = ReadBuff.Readu8(); switch (myPacketId) { /* To READ from the buffer... * float myFloat = myClient.ReadBuffer.Readf32(); * int myInteger = myClient.ReadBuffer.Readu32(); * string myString = myClient.ReadBuffer.ReadStr(); * * To WRITE to the buffer and send... * myClient.WriteBuffer.SetPeek( 0 ); * myClient.WriteBuffer.Writeu8( TcpNetwork.TcpPacketHeader ); * myClient.WriteBuffer.Writeu8( 254 ); * myClient.WriteBuffer.SendTcp( myClient.DataStream );*/ //They sent this to see if the connection works. Echo their packet back. case (Code.ConnectionBegin): { //Assigns the given PlayerID to this specific client. Also the name should've given their name. myClient.PlayerID = ReadBuff.Readf64(); myClient.PlayerName = ReadBuff.ReadStr(); //Tells them that they connected. WriteBuff.SetPeek(0); WriteBuff.Writeu8(myClient.HeaderId); WriteBuff.Writeu8(Code.ConnectionBegin); WriteBuff.WriteStr(MainServer.WelcomeMessage); CmdSystem.AddLog("BBLegacy Client Connected (" + myClient.PlayerName + " [" + myClient.PlayerID.ToString() + "])"); WriteBuff.SendTcp(myClient.DataStream); //Update their online status. File.SetValue(myClient.PlayerID.ToString(), "MenuStatus", Code.Status_AOnline); File.SetValue(myClient.PlayerID.ToString(), "Name", myClient.PlayerName); break; } case (Code.ConnectionEnd): { myClient.TcpClientClose(); CmdSystem.AddLog("Client " + myClient.SocketId.ToString() + " Requested Disconnection -- Granted."); break; } case (Code.SessionCreate): { //You're actually creating a session :D //It's just like joining one, but you make it, then join. byte RoomId = 0, Failed = 0; var RoomName = ReadBuff.ReadStr(); var RoomHostName = myClient.PlayerName; //Can't make one while inside one. if (myClient.isInSession) { Failed = 1; } else { for (byte i = 0; i < 255; i++) { if (!MainServer.SessionNumberList.Contains(i)) { //Found a free room. RoomId = i; MainServer.SessionNumberList.Add(i); break; } if (i == 255) { Failed = 2; //WHAT. there's more than 250 rooms? } } } WriteBuff.SetPeek(0); WriteBuff.Writeu8(myClient.HeaderId); WriteBuff.Writeu8(Code.SessionCreate); WriteBuff.Writeu8(Failed); WriteBuff.SendTcp(myClient.DataStream); break; } case (Code.SessionJoin): { //Request to Join a Room. Which room? This room! var id = ReadBuff.Readu8(); Session RoomId = MainServer.SessionList.Find(e => e.GetID() == id); //Cannot join a room while in another. if (myClient.isInSession) { WriteBuff.SetPeek(0); WriteBuff.Writeu8(myClient.HeaderId); WriteBuff.Writeu8(Code.SessionJoin); WriteBuff.Writeu8(0); WriteBuff.SendTcp(myClient.DataStream); break; } //Success, put them in the room... myClient.isInSession = true; myClient.CurrentSession = RoomId; //...and then tell the player. WriteBuff.SetPeek(0); WriteBuff.Writeu8(myClient.HeaderId); WriteBuff.Writeu8(Code.SessionJoin); WriteBuff.Writeu8(1); WriteBuff.SendTcp(myClient.DataStream); break; } case (Code.SessionLeave): { //Request to leave the room. Which room (To tell the players in that room he left)? var RoomId = ReadBuff.Readu8(); //Cannot leave a room when you're not in one. if (!myClient.isInSession) { WriteBuff.SetPeek(0); WriteBuff.Writeu8(myClient.HeaderId); WriteBuff.Writeu8(Code.SessionLeave); WriteBuff.Writeu8(0); WriteBuff.SendTcp(myClient.DataStream); break; } //Success, take them out the room... myClient.isInSession = false; myClient.CurrentSession = null; //...and then tell the player. WriteBuff.SetPeek(0); WriteBuff.Writeu8(myClient.HeaderId); WriteBuff.Writeu8(Code.SessionJoin); WriteBuff.Writeu8(1); WriteBuff.SendTcp(myClient.DataStream); //As well as the other players in that room. Session EveryoneElse = MainServer.SessionList.Find(e => e.GetID() == RoomId); //Loop through all connected clients to see which is in the room. { foreach (KeyValuePair <long, TcpClientSocket> ClientSocket in TcpListenerSocket.ClientList) { if (ClientSocket.Value.CurrentSession == EveryoneElse) { WriteBuff.SetPeek(0); WriteBuff.Writeu8(ClientSocket.Value.HeaderId); WriteBuff.Writeu8(Code.PlayerLeave); WriteBuff.Writeu8(1); WriteBuff.SendTcp(ClientSocket.Value.DataStream); } } } //Then tell each of them that the player left. break; } default: break; } // Back-up the read/peek position of the buffer and check for a secondary/merged packet. int myHeaderId = myClient.ReadBuffer.EndRead(false); myCheck = ( byte )((myHeaderId != -1) ? myHeaderId : ~myHeaderId); } }
public void PlayerCreatedRoom(TcpClientSocket Player, Room Room) { }
public void PlayerChangedChar(TcpClientSocket Player, byte Char) { }
public void PlayerDeath(TcpClientSocket Player, float Time) { }
public void PlayerScored(TcpClientSocket Player, short Score) { }
public void PlayerUsedItem(TcpClientSocket Player, byte Item) { }
public void PlayerChangedTeam(TcpClientSocket Player, byte Team) { }
/// <summary> /// Send packets to a client or process data for a client independantly of reading/receiving data. /// </summary> /// <param name="myClient">Client socket class that is being handled.</param> /// <param name="myServer">Server's handle(listener) socket class.</param> public static void TcpPacketSend(TcpClientSocket myClient, TcpListenerSocket myServer) { // Send messages to a client without receiving data. }
/// <summary> /// Read and process packets received from the client. /// </summary> /// <param name="Player">Client socket class that is being handled.</param> /// <param name="myServer">Server's handle(listener) socket class.</param> public static void TcpPacketRead(TcpClientSocket Player, TcpListenerSocket myServer) { byte myCheck = Player.ReadBuffer.StartRead(0); var buff = Player.DataStream; //Not all functions require a writeback, but it helps to have this ready. var Write = Player.WriteBuffer; var Read = Player.ReadBuffer; var File = MainServer.SettingsFile; var PLID = Player.ID.ToString(); // Check for packets by searching for packet headers. while (myCheck == Player.HeaderId) { Write.Clear(); Write.WriteUByte(Player.HeaderId); byte myPacketId = Read.ReadUByte(); switch (myPacketId) { /* To READ from the buffer... * float myFloat = Player.ReadBuffer.Readf32(); * int myInteger = Player.ReadBuffer.Readu32(); * string myString = Player.ReadBuffer.ReadStr(); * * To WRITE to the buffer and send... * Player.WriteBuffer.SetPeek( 0 ); * Player.WriteBuffer.Writeu8( TcpNetwork.TcpPacketHeader ); * Player.WriteBuffer.Writeu8( 254 ); * Player.WriteBuffer.SendTcp( Player.DataStream );*/ //They sent this to see if the connection works. Echo their packet back. case Code.ConnectionBegin: { //Assigns the given PlayerID to this specific client. Also the name should've given their name. Player.ID = Read.ReadDouble(); Player.Name = Read.ReadString(); Player.Icon = Read.ReadUByte(); Player.XP = Read.ReadUInt(); //Tells them that they connected. Write.WriteUByte(Code.ConnectionBegin); Write.WriteString(MainServer.WelcomeMessage); Write.WriteBool(MainServer.CanCreateRooms); CmdSystem.AddLog(Player.Name + " has connected (ID:" + Player.ID.ToString() + ")"); Write.SendTcp(Player.DataStream); //If they're admins... if (AdminList.isAdmin(Player.ID)) { Player.Tag = 2; } //If they're not on the list, put them on the list. //This also means that they're new players, 1st time joiners! var b = false; foreach (PlayerListItem i in MainServer.PlayerList) { if (i.ID == Player.ID) { b = true; Player.Tag = i.Tag; i.Link(Player); } } if (!b) { Player.ListSlot = new PlayerListItem(); } Player.ListSlot.Link(Player); Player.ListSlot.GoOnline(); MainServer.Event.PlayerConnected(Player); break; } case Code.ConnectionEnd: { Player.UserImposedDisconnection = true; Player.Connected = false; CmdSystem.AddLog(Player.Name + " has left"); if (Player.ListSlot != null) { Player.ListSlot.GoOffline(); } MainServer.Event.PlayerDisconnected(Player); break; } // == ADD MENU STATUS == //They want to join this room. Returns ID upon success, or a negative number upon failure. case Code.SessionJoin: { var id = Read.ReadUInt(); uint success = 4000000004; byte pnum = 0; String nm = " "; foreach (Room i in MainServer.RoomList) { if (i.id == id) { success = i.Join(Player); pnum = (byte)i.players.Count; nm = i.name; break; } } Write.Clear(); Write.WriteUByte(Player.HeaderId); Write.WriteUByte(Code.SessionJoin); Write.WriteUInt(success); Write.WriteUByte(pnum); byte plst = 1; if (Player.CurrentRoom != null) { plst = Player.CurrentRoom.playstate; } Write.WriteUByte(plst); Write.WriteString(nm); Write.SendTcp(buff); if (success < 4000000000) { Player.ListSlot.GoPlayingOnline(); } //Now that they know they joined, we can now auto-start the room if needed. foreach (Room i in MainServer.RoomList) { if (i.id == id) { if (i.tag == 0 && i.playstate <= 2) { if (i.players.Count > 1 && i.playstate == 1) { i.EnterVoteRoom(); } i.Teamify(); } } } MainServer.Event.PlayerJoinedRoom(Player, Player.CurrentRoom); break; } case Code.SessionLeave: { byte success = 1; Room rm = Player.CurrentRoom; if (Player.CurrentRoom == null) { success = 0; } else { success = Player.CurrentRoom.Remove(Player); } Write.WriteUByte(Code.SessionLeave); Write.WriteUByte(success); Write.SendTcp(Player.DataStream); if (success > 0) { Player.ListSlot.GoOnline(); MainServer.Event.PlayerLeftRoom(Player, rm); } break; } //Creates a session, returning room ID if the operation was successful. case Code.SessionCreate: { //Host, Name, Mode, Team, Team Number, Minutes, Max Players var rs1 = Read.ReadString(); var ub2 = Read.ReadUByte(); var bo3 = Read.ReadBool(); var ub4 = Read.ReadUByte(); var ub5 = Read.ReadUByte(); var ub6 = Read.ReadUByte(); var bo7 = Read.ReadBool(); var ub8 = Read.ReadUByte(); var ub9 = Read.ReadUByte(); var ub10 = Read.ReadUByte(); var ub11 = Read.ReadUByte(); var ub12 = Read.ReadUByte(); Write.WriteUByte(Code.SessionCreate); if (MainServer.CanCreateRooms && Player.CurrentRoom == null) { Room rm = new Room(Player, rs1, ub2, bo3, ub4, ub5, ub6); Write.WriteBool(true); Write.WriteUInt(rm.id); Write.WriteString(rm.name); rm.is_custom_items = bo7; rm.citm_1 = ub8; rm.citm_2 = ub9; rm.citm_3 = ub10; rm.citm_4 = ub11; rm.citm_5 = ub12; //So for special admins, we should highlight their room by making it a special tagged room. if (AdminList.isAdmin(Player.ID)) { rm.tag = 2; } Player.ListSlot.GoPlayingOnline(); } else { Write.WriteBool(false); } Write.SendTcp(buff); MainServer.Event.PlayerCreatedRoom(Player, Player.CurrentRoom); break; } //Not sent by client. case Code.UpdatePlayers: break; //Host-invoked room starting. case Code.StartRoom: { var rm = Player.CurrentRoom; if (rm == null || rm.tag == 0 || rm.players.IndexOf(Player) != 0 || rm.playstate != 1) { break; } rm.EnterVoteRoom(); break; } //A player changes his or her character in the lobby. Let the room tell the others. case Code.ChangeCharacter: { var c = Read.ReadUByte(); if (Player.CurrentRoom == null) { break; } Player.CurrentRoom.ChangeCharacter(Player, c); MainServer.Event.PlayerChangedChar(Player, Player.Character); break; } //A player changes his or her team. Let the room tell the others. case Code.ChangeTeam: { var t = Read.ReadUByte(); if (Player.CurrentRoom == null) { break; } Player.CurrentRoom.ChangeTeam(Player, t); MainServer.Event.PlayerChangedTeam(Player, Player.Team); break; } //The host changes these battle settings. Let their room update and then tell everyone. case Code.ChangeBattleSettings: { break; } //Not sent by client. case Code.RoomState: break; //The player requests the information of another player with this ID. case Code.PlayerInfo: { break; } //Simple packet to say that we're responding. Can be used for ping. case Code.IAmHere: { Write.WriteUByte(Code.IAmHere); Write.SendTcp(buff); break; } //The player wants a list of all open rooms. case Code.RequestRoomList: { Write.WriteUByte(Code.RequestRoomList); //Send just their IDs and a 0 at the end. The game will ask later. //The order will go like SERVER ROOMS; OPEN PLAYER ROOMS; CLOSED PLAYER ROOMS. //The extra bool reports their size (big or small). foreach (Room i in MainServer.RoomList) { if (i.visible && i.tag == 0) { Write.WriteUInt(i.id); Write.WriteBool(true); } } foreach (Room i in MainServer.RoomList) { if (i.visible && i.tag == 2 && i.playstate == 1) { Write.WriteUInt(i.id); Write.WriteBool(true); } } foreach (Room i in MainServer.RoomList) { if (i.visible && i.tag == 1 && i.playstate == 1) { Write.WriteUInt(i.id); Write.WriteBool(false); } } foreach (Room i in MainServer.RoomList) { if (i.visible && i.tag == 2 && i.playstate != 1) { Write.WriteUInt(i.id); Write.WriteBool(true); } } foreach (Room i in MainServer.RoomList) { if (i.visible && i.tag == 1 && i.playstate != 1) { Write.WriteUInt(i.id); Write.WriteBool(false); } } Write.WriteUInt(0); Write.SendTcp(buff); break; } //The player wants this specific room's information. case Code.RequestRoomInformation: { var rm = Read.ReadUInt(); var found = false; if (MainServer.RoomList.Count == 0) { break; } Write.WriteUByte(Code.RequestRoomInformation); foreach (Room i in MainServer.RoomList) { if (i.id == rm) { Write.WriteUInt(i.id); Write.WriteString(i.name); Write.WriteString(i.host_name); Write.WriteSByte(i.tag); Write.WriteUByte((byte)i.players.Count); Write.WriteUByte(i.maxplayers); Write.WriteUByte(i.mode); Write.WriteBool(i.teams); Write.WriteUByte(i.playstate); Write.WriteUByte(i.maxteams); Write.WriteUByte(i.minutes); found = true; break; } } if (found) { Write.SendTcp(buff); } else { Write.Clear(); } break; } //The player wants a list of all players. case Code.RequestPlayerList: { if (Player.CurrentRoom == null) { break; } Player.CurrentRoom.SendPlayerList(Player); break; } //Packet echo'ing. case Code.SendMe: { Write.WriteUByte(Code.SendMe); byte DataType = Read.ReadUByte(); int val; uint uval; float valf; double vald; Write.WriteUByte(DataType); switch (DataType) { case 1: val = Read.ReadUByte(); Write.WriteUByte((byte)val); CmdSystem.AddLog(Player.Name + " echo request: " + (byte)val); break; case 2: val = Read.ReadSByte(); Write.WriteSByte((sbyte)val); CmdSystem.AddLog(Player.Name + " echo request: " + (sbyte)val); break; case 3: val = Read.ReadUShort(); Write.WriteUShort((ushort)val); CmdSystem.AddLog(Player.Name + " echo request: " + (ushort)val); break; case 4: val = Read.ReadSShort(); Write.WriteSShort((short)val); CmdSystem.AddLog(Player.Name + " echo request: " + (short)val); break; case 5: uval = Read.ReadUInt(); Write.WriteUInt(uval); CmdSystem.AddLog(Player.Name + " echo request: " + uval); break; case 6: val = (int)Read.ReadSInt(); Write.WriteSInt(val); CmdSystem.AddLog(Player.Name + " echo request: " + val); break; case 7: valf = Read.ReadFloat(); Write.WriteFloat(valf); CmdSystem.AddLog(Player.Name + " echo request: " + valf); break; case 8: vald = Read.ReadDouble(); Write.WriteDouble(vald); CmdSystem.AddLog(Player.Name + " echo request: " + vald); break; default: Read.ReadUByte(); Write.WriteUByte(0); CmdSystem.AddLog(Player.Name + " blank echo request."); break; } Write.SendTcp(Player.DataStream); break; } //A player sends a vote to his room. case Code.Vote: { byte stg = Read.ReadUByte(); if (Player.CurrentRoom == null) { break; } Player.CurrentRoom.AcceptVote(Player, stg); break; } //The player has finished loading and is ready to start the battle. case Code.BattleReady: { if (Player.CurrentRoom == null) { break; } Player.CurrentRoom.PlayerReady(); break; } /* * NOW, for the next few codes, the server is simply a relayer to all * the other players. It doesn't really keep any of this data, which * we should in the future so we can keep some spam/hack control. */ //The player moved, let everyone else know. case Code.Movement: { if (Player.CurrentRoom == null) { break; } var ss1 = Read.ReadSShort(); var ss2 = Read.ReadSShort(); var ub3 = Read.ReadUByte(); Write.WriteUByte(Code.Movement); Write.WriteUByte(Player.Slot); Write.WriteSShort(ss1); Write.WriteSShort(ss2); Write.WriteUByte(ub3); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } break; } //The player stopped, let everyone else know. case Code.Stopment: { if (Player.CurrentRoom == null) { break; } var ss1 = Read.ReadSShort(); var ss2 = Read.ReadSShort(); Write.WriteUByte(Code.Stopment); Write.WriteUByte(Player.Slot); Write.WriteSShort(ss1); Write.WriteSShort(ss2); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } break; } //The player used an item, let everyone else know. case Code.Item: { if (Player.CurrentRoom == null) { break; } var ub1 = Read.ReadUByte(); var bo1 = Read.ReadBool(); var ss2 = Read.ReadSShort(); var ss3 = Read.ReadSShort(); var ff4 = Read.ReadFloat(); var ff5 = Read.ReadFloat(); Write.WriteUByte(Code.Item); Write.WriteUByte(Player.Slot); Write.WriteUByte(ub1); Write.WriteBool(bo1); Write.WriteSShort(ss2); Write.WriteSShort(ss3); Write.WriteFloat(ff4); Write.WriteFloat(ff5); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } MainServer.Event.PlayerUsedItem(Player, ub1); break; } //The player got hit, let everyone else know. case Code.Hit: { if (Player.CurrentRoom == null) { break; } var ub1 = Read.ReadUByte(); var ss2 = Read.ReadSShort(); var ss3 = Read.ReadSShort(); var ff4 = Read.ReadFloat(); var ff5 = Read.ReadFloat(); var ff6 = Read.ReadFloat(); Write.WriteUByte(Code.Hit); Write.WriteUByte(Player.Slot); Write.WriteUByte(ub1); Write.WriteSShort(ss2); Write.WriteSShort(ss3); Write.WriteFloat(ff4); Write.WriteFloat(ff5); Write.WriteFloat(ff6); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } MainServer.Event.PlayerHit(Player, ub1, ff6); break; } //The player got killed, let everyone else know. case Code.Death: { if (Player.CurrentRoom == null) { break; } var ss1 = Read.ReadSShort(); var ss2 = Read.ReadSShort(); var ff3 = Read.ReadFloat(); var ub4 = Read.ReadUByte(); Write.WriteUByte(Code.Death); Write.WriteUByte(Player.Slot); Write.WriteSShort(ss1); Write.WriteSShort(ss2); Write.WriteFloat(ff3); Write.WriteUByte(ub4); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } MainServer.Event.PlayerDeath(Player, ff3); break; } //The player got a point, let everyone else know. case Code.Score: { if (Player.CurrentRoom == null) { break; } var ub1 = Read.ReadUByte(); var ub2 = Read.ReadUByte(); var ss3 = Read.ReadSShort(); Write.WriteUByte(Code.Score); Write.WriteUByte(ub1); Write.WriteUByte(ub2); Write.WriteSShort(ss3); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } MainServer.Event.PlayerScored(Player, ss3); break; } //Absolute Score Update case Code.AbsoluteScore: { if (Player.CurrentRoom == null) { break; } var ss1 = Read.ReadSShort(); Write.WriteUByte(Code.AbsoluteScore); Write.WriteUByte(Player.Slot); Write.WriteSShort(ss1); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } break; } //The player dropped coins, let everyone else know. case Code.CoinDrop: { if (Player.CurrentRoom == null) { break; } var ub1 = Read.ReadUByte(); Write.WriteUByte(Code.CoinDrop); Write.WriteUByte(Player.Slot); Write.WriteUByte(ub1); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } break; } //The player dropped coins, let everyone else know. case Code.ItemDrop: { if (Player.CurrentRoom == null) { break; } var ss1 = Read.ReadSShort(); var ss2 = Read.ReadSShort(); var fl3 = Read.ReadFloat(); var ub4 = Read.ReadUByte(); var ub5 = Read.ReadUByte(); var ub6 = Read.ReadUByte(); Write.WriteUByte(Code.ItemDrop); Write.WriteUByte(Player.Slot); Write.WriteSShort(ss1); Write.WriteSShort(ss2); Write.WriteFloat(fl3); Write.WriteUByte(ub4); Write.WriteUByte(ub5); Write.WriteUByte(ub6); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } break; } //The player's purple spawned, let everyone else know. case Code.PurpleSpawn: { if (Player.CurrentRoom == null) { break; } var ss1 = Read.ReadSShort(); var ss2 = Read.ReadSShort(); Write.WriteUByte(Code.PurpleSpawn); Write.WriteUByte(Player.Slot); Write.WriteSShort(ss1); Write.WriteSShort(ss2); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } break; } //The player has finished, here are their stats. case Code.PlayerRecap: { if (Player.Record == null || Player.CurrentRoom == null) { break; } Player.Record.HitsGiven = Read.ReadUShort(); Player.Record.Kills = Read.ReadUShort(); Player.Record.Items = Read.ReadUShort(); Player.Record.HitsTaken = Read.ReadUShort(); Player.Record.Deaths = Read.ReadUShort(); Player.Record.CoinsGained = Read.ReadUShort(); Player.Record.CoinsLost = Read.ReadUShort(); Write.WriteUByte(Code.PlayerRecap); Write.WriteUByte(Player.Slot); Player.CurrentRoom.DistributeStats(Player); break; } //The player has to send their statistics for proper attack, defense, etc. case Code.PlayerStats: { if (Player.CurrentRoom == null) { break; } var ub1 = Read.ReadUByte(); var ub2 = Read.ReadUByte(); var ub3 = Read.ReadUByte(); var ub4 = Read.ReadUByte(); Write.WriteUByte(Code.PlayerStats); Write.WriteUByte(Player.Slot); Write.WriteUByte(ub1); Write.WriteUByte(ub2); Write.WriteUByte(ub3); Write.WriteUByte(ub4); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } break; } //They sent a Battle Complete packet and are now waiting for everyone else to finish. case Code.BattleEnd: { if (Player.CurrentRoom == null) { break; } Player.CurrentRoom.PlayerFinished(); break; } //They pressed the Next Battle button and are waiting for everyone else to do the same. case Code.BattleComplete: { if (Player.CurrentRoom == null) { break; } Player.CurrentRoom.PlayerCompleted(); break; } //They asked for an updated Server Message pack. case Code.ServerMessages: { Write.WriteUByte(Code.ServerMessages); Write.WriteString(MainServer.GoodbyeMessage); Write.WriteString(MainServer.DCMessage); Write.WriteString(MainServer.KickMessage); Write.WriteString(MainServer.InfoMessage); Write.SendTcp(Player.DataStream); break; } //Update their XP. Simple as that. case Code.UpdateXP: { Player.XP = Read.ReadUInt(); break; } //Yes or no? case Code.CanICreateRoomsYet: { Write.WriteUByte(Code.CanICreateRoomsYet); Write.WriteBool(MainServer.CanCreateRooms); Write.SendTcp(Player.DataStream); break; } // case Code.PlayerList: { Write.WriteUByte(Code.PlayerList); foreach (PlayerListItem i in MainServer.PlayerList) { if (i.Status == Code.Status_AOnline) { Write.WriteBool(true); Write.WriteDouble(i.ID); Write.WriteUByte(i.Tag); Write.WriteUByte(i.Status); Write.WriteString(i.Name); } } foreach (PlayerListItem i in MainServer.PlayerList) { if (i.Status == Code.Status_POnline) { Write.WriteBool(true); Write.WriteDouble(i.ID); Write.WriteUByte(i.Tag); Write.WriteUByte(i.Status); Write.WriteString(i.Name); } } foreach (PlayerListItem i in MainServer.PlayerList) { if (i.Status == Code.Status_Offline) { Write.WriteBool(true); Write.WriteDouble(i.ID); Write.WriteUByte(i.Tag); Write.WriteUByte(i.Status); Write.WriteString(i.Name); } } Write.WriteBool(false); Write.SendTcp(Player.DataStream); break; } case Code.PL_Single: { var id = Read.ReadDouble(); foreach (PlayerListItem i in MainServer.PlayerList) { if (i.ID == id) { Write.WriteUByte(Code.PL_Single); Write.WriteDouble(id); Write.WriteUByte(i.Status); Write.SendTcp(Player.DataStream); } } } break; //The player dropped coins, let everyone else know. case Code.ItemInvoke: { if (Player.CurrentRoom == null) { break; } var ub1 = Read.ReadUByte(); var ss2 = Read.ReadSShort(); var ss3 = Read.ReadSShort(); Write.WriteUByte(Code.ItemInvoke); Write.WriteUByte(Player.Slot); Write.WriteUByte(ub1); Write.WriteSShort(ss2); Write.WriteSShort(ss3); foreach (TcpClientSocket Others in Player.CurrentRoom.players) { if (!Others.Equals(Player)) { Write.SendTcp(Others.DataStream); } } break; } case Code.Command: var s = Read.ReadString(); Console.WriteLine("USER INVOKED COMMAND:"); Console.WriteLine(" " + s); Console.WriteLine(" by " + Player.Name); var done = true; if (Player.Tag != 2) { done = false; CmdSystem.AddLog("User missing permissions, access denied"); } else { CmdSystem.RunCmd(s); done = CmdSystem.LastCommandSuccessful; } Write.WriteUByte(Code.Command); Write.WriteBool(done); Write.WriteString(CmdSystem.LastLog); Write.SendTcp(Player.DataStream); break; default: break; } // Back-up the read/peek position of the buffer and check for a secondary/merged packet. int myHeaderId = Player.ReadBuffer.EndRead(false); myCheck = ( byte )((myHeaderId != -1) ? myHeaderId : ~myHeaderId); } }
public void PlayerHit(TcpClientSocket Player, byte Type, float Time) { }
public void PlayerDisconnected(TcpClientSocket Player) { }