private static void HandleLogin(int connectionId, byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); int packageID = buffer.ReadInteger(); string username = buffer.ReadString(); string password = buffer.ReadString(); var mySQLConnection = new MySqlConnection(MySQL.CreateConnectionString()); try { mySQLConnection.Open(); Console.WriteLine("Player {0} Succesfully connected to MySQL Server '{1}'", username, mySQLConnection.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw; } //ArrayList allCards = Database.TakeAllCardsWithInformation(mySQLConnection); //TODO: REWORK WHEN CHANGE HEAL ITEM LOGIC mySQLConnection.Close(); ServerTCP.PACKET_SendAllCards(connectionId); //if (!Database.AccountExist(username)) //{ // ServerTCP.PACKET_AlertMsg(connectionId, "Account doesn't exist!"); // return; //} //if (!Database.PasswordOK(username, password)) //{ // ServerTCP.PACKET_AlertMsg(connectionId, "Password is incorrect!"); // return; //} Console.WriteLine("Player '{0}' succesfully logged into his account", username); ServerTCP.PACKET_LoadMenu(connectionId, username); }
private static void ClientConnectCallback(IAsyncResult result) { TcpClient tempClient = serverSocket.EndAcceptTcpClient(result); serverSocket.BeginAcceptTcpClient(new AsyncCallback(ClientConnectCallback), null); for (int i = 1; i < Constants.MAX_PLAYERS; i++) { if (clientObjects[i].socket == null) { clientObjects[i] = new ClientObject(tempClient, i); Console.WriteLine("Incoming Connection from {0}", clientObjects[i].socket.Client.RemoteEndPoint.ToString()); ServerTCP.PACKET_WelcomeMsg(i, "Welcome to my Server"); return; } } }
//TODO Rework Logic for random cards for EACH Player from DB private void SendCards() { //TODO CHANGE, NOW WORKS ONLY FOR ATTACK CARDS ArrayList cards = new ArrayList(); Random rand = new Random(); //Add Attack Card // TODO: GET BACK, DEBAGGING: cards.Add(Constants.attackCards.ElementAt(rand.Next(0, Constants.attackCards.Count)).Key); //cards.Add(36); //Add Heal Card cards.Add(Constants.healCards.ElementAt(rand.Next(0, Constants.healCards.Count)).Key); //Add Item Card cards.Add(Constants.itemCards.ElementAt(rand.Next(0, Constants.itemCards.Count)).Key); ServerTCP.PACKET_SendCards(p1.connectionID, cards); ServerTCP.PACKET_SendCards(p2.connectionID, cards); }
public void InitializeMatch() { ServerTCP.PACKET_LoadMatch(p1.connectionID, matchID); ServerTCP.PACKET_LoadMatch(p2.connectionID, matchID); }
private void SendResults() { int p1SelectedCardID = p1.selectedCardID; int p2SelectedCardID = p2.selectedCardID; //Send Info to each player in format: playerHealth, EnemyHealth, PlayerBullets, EnemyBullets, PlayerCard, EnemyCard //Later add: PlayerAction, EnemyAction for animation //for player1: ByteBuffer buffer = new ByteBuffer(); buffer.WriteInteger(p1.Health); buffer.WriteInteger(p2.Health); buffer.WriteInteger(p1.Bullets); buffer.WriteInteger(p2.Bullets); buffer.WriteInteger(p1.results.accuracy); buffer.WriteInteger(p2.results.accuracy); //Send Active Effects //For Player buffer.WriteInteger(p1.effects.Count); //Write Number of Effects foreach (var eff in p1.effects.Keys) { buffer.WriteInteger(eff); buffer.WriteInteger(p1.effects[eff].Item1); //Value buffer.WriteInteger(p1.effects[eff].Item2); //Duration Time } //And for opponent buffer.WriteInteger(p2.effects.Count); //Write Number of Effects foreach (var eff in p2.effects.Keys) { buffer.WriteInteger(eff); buffer.WriteInteger(p2.effects[eff].Item1); //Value buffer.WriteInteger(p2.effects[eff].Item2); //Duration Time } buffer.WriteInteger(p1SelectedCardID); buffer.WriteInteger(p2SelectedCardID); //for player2: ByteBuffer buffer2 = new ByteBuffer(); buffer2.WriteInteger(p2.Health); buffer2.WriteInteger(p1.Health); buffer2.WriteInteger(p2.Bullets); buffer2.WriteInteger(p1.Bullets); buffer2.WriteInteger(p2.results.accuracy); buffer2.WriteInteger(p1.results.accuracy); //For Player buffer2.WriteInteger(p2.effects.Count); //Write Number of Effects foreach (var eff in p2.effects.Keys) { buffer2.WriteInteger(eff); buffer2.WriteInteger(p2.effects[eff].Item1); //Value buffer2.WriteInteger(p2.effects[eff].Item2); //Duration Time } //For Opponent buffer2.WriteInteger(p1.effects.Count); //Write Number of Effects foreach (var eff in p1.effects.Keys) { buffer2.WriteInteger(eff); buffer2.WriteInteger(p1.effects[eff].Item1); //Value buffer2.WriteInteger(p1.effects[eff].Item2); //Duration Time } buffer2.WriteInteger(p2SelectedCardID); buffer2.WriteInteger(p1SelectedCardID); //Set Ready to false, for animations p1.Ready = false; p2.Ready = false; //deselect cards: //Sending: ServerTCP.PACKET_ShowResult(p1.connectionID, buffer.ToArray()); ServerTCP.PACKET_ShowResult(p2.connectionID, buffer2.ToArray()); }
static void Main(string[] args) { InitializeConsoleThread(); ServerHandleData.InitializePacketListener(); ServerTCP.InitializeServer(); }