Пример #1
0
        Random random = new Random(); //random number generator

        public GameHandler(GameCons network, GameCons network2, String playerA, String playerB, String ipA, String ipB)
        {
            //set game active and initialize variables
            active        = true;
            this.playerA  = playerA;
            this.playerB  = playerB;
            versus        = playerA + " vs " + playerB;
            this.network  = network;
            this.network2 = network2;
            message       = new MessageHandler();
            this.ipA      = ipA;
            this.ipB      = ipB;
            resetBall();
            //initialize timers
            timerSend          = new System.Windows.Forms.Timer();
            timerSend.Enabled  = true;
            timerSend.Interval = 35;
            timerSend.Tick    += new EventHandler(send);

            timerRecieve          = new System.Windows.Forms.Timer();
            timerRecieve.Enabled  = true;
            timerRecieve.Interval = 25;
            timerRecieve.Tick    += new EventHandler(recieve);

            gameTime          = new System.Windows.Forms.Timer();
            gameTime.Enabled  = false;
            gameTime.Interval = 40;
            gameTime.Tick    += new EventHandler(gameTime_Tick);
            ballForceY        = random.Next(-5, 5);
        }
Пример #2
0
        public void processMessage(String message)
        {
            if (message != null)                                    //if there is a message
            {
                String temp = message.Substring(0, 3);              //Take message code
                message = message.Substring(4, message.Length - 4); //remove message code from message

                if (temp.Equals("105"))                             //Server Discovery
                {
                    String[] temp2 = message.Split(' ');
                    String   IP    = temp2[0];
                    System.Diagnostics.Debug.WriteLine(IP);
                    network.sendUDP(generateMessage("106", NetCons.IPString), IP); //Return server IP to sender
                }

                if (temp.Equals("100"))                  //Join lobby
                {
                    String[] temp2 = message.Split(' '); //Separate message
                    String   IP    = temp2[1];
                    String   alias = temp2[0];
                    if (player.FirstOrDefault(o => o.alias.Equals(temp2[0])) == null) //Checks alias not taken
                    {
                        player.Add(new playerInfo(IP, alias));                        //Adds to player base
                        bindPlayers.ResetBindings(false);                             //Updates list
                    }
                }

                else if (temp.Equals("110"))                                              //Leave lobby
                {
                    var tempPlayer = player.FirstOrDefault(o => o.alias.Equals(message)); //finds record which matches alias
                    if (tempPlayer != null)                                               //if it has found a player / stops duplicate message causing issue
                    {
                        network.broadcastUDP(generateMessage("205", tempPlayer.alias));   //notify clients
                        player.Remove(tempPlayer);                                        //deletes the record
                        bindPlayers.ResetBindings(false);                                 //update list
                    }
                }

                else if (temp.Equals("120"))                                                  //game invite
                {
                    String[] tempAliases = message.Split();                                   //Split message
                    String   tempA       = tempAliases[0];
                    String   tempB       = tempAliases[1].Trim();                             //Trim any trailing space
                    var      infoB       = player.FirstOrDefault(o => o.alias.Equals(tempB)); //get details to send to
                    network.sendUDP(generateMessage("210", tempA, tempB), infoB.IP);          //send game invite to recipient
                }
                else if (temp.Equals("130"))                                                  //game accept
                {
                    String[] tempInfo = message.Split();
                    String   tempA    = tempInfo[0];
                    String   tempB    = tempInfo[1].Trim();
                    var      infoA    = player.FirstOrDefault(o => o.alias.Equals(tempA)); //find records
                    var      infoB    = player.FirstOrDefault(o => o.alias.Equals(tempB));
                    //Get game ports
                    int      portA = getGamePort();
                    int      portB = getGamePort();
                    int      portC = getGamePort();
                    int      portD = getGamePort();
                    GameCons net   = new GameCons(portA, portB); //Set up GameCons instances
                    GameCons net2  = new GameCons(portC, portD);
                    //Notify clients to start games with all relevant data
                    network.sendUDP(generateMessage("320", "A", portA.ToString(), portB.ToString(), tempB), infoA.IP);
                    network.sendUDP(generateMessage("320", "B", portC.ToString(), portD.ToString(), tempA), infoB.IP);
                    //Create game instance and add to list
                    game.Add(new GameHandler(net, net2, tempA, tempB, infoA.IP, infoB.IP));
                    bindGames.ResetBindings(false); //update listbox
                }
                else if (temp.Equals("140"))        //broadcast message to chat
                {
                    String[] tempChat    = message.Split();
                    String   tempA       = tempChat[0];
                    int      tempInt     = tempA.Length + 1;                          //find start index of message for substring
                    String   tempMessage = message.Substring(tempInt);
                    network.broadcastUDP(generateMessage("220", tempA, tempMessage)); //broadcast to all clients
                }
                else if (temp.Equals("150"))                                          //private message
                {
                    String[] tempChat    = message.Split();
                    String   tempA       = tempChat[0];
                    String   tempB       = tempChat[1].Trim();
                    int      tempInt     = tempA.Length + tempB.Length + 2;                   //find start index of message for substring
                    String   tempMessage = message.Substring(tempInt);
                    var      infoB       = player.FirstOrDefault(o => o.alias.Equals(tempB)); //get details to send to
                    System.Diagnostics.Debug.WriteLine(infoB.IP);
                    //Send to recipient
                    network.sendUDP(generateMessage("230", tempA, tempMessage), infoB.IP);
                }
                else if (temp.Equals("160")) //begin stream
                {
                    String[] tempChat = message.Split();
                    String   IP       = tempChat[0];
                    String   tempA    = tempChat[1].Trim();
                    String   tempB    = tempChat[2].Trim();
                    //find game
                    var getInstance = game.FirstOrDefault(o => o.playerA.Equals(tempA) && o.playerB.Equals(tempB)); //get details to send to
                    //Start streaming to senders IP
                    getInstance.streamMatch(IP);
                }
            }
        }