Example #1
0
        public static Command eatPelletCommand(long ts, Player p1, int oldPelletID, Pellet newPellet)
        {
            Command newCommand = new Command();
            newCommand.timeStamp = ts;
            newCommand.cType = CType.EatPellet;
            newCommand.message = newCommand.cType + ":" + ts + ":" + p1.playerNumber + ":" + p1.size + ":" + oldPelletID + ":" + newPellet.id + ":" + newPellet.x + ":" + newPellet.y + ":" + newPellet.size + ";";

            return newCommand;
        }
Example #2
0
            //server loop that checks messages from clients
            public void loop()
            {
                //always run this loop
                while (true)
                {
                    try
                    {
                        //if all connected clients are read, we can start the game
                        if (numberOfClients > 1 && ClientsReady(clientArray, clientArray.First))
                        {
                            //create all the pellets on the server
                            for (int i = 0; i < numberOfPellets; i++)
                            {
                                pelletArray[i] = new Pellet(i);
                            }

                            foreach (Client c in clientArray)
                            {
                                c.clientReady = false;

                                //let all the clients know about each other
                                foreach (Client c2 in clientArray)
                                {

                                    c2.sw.WriteLine("connected\\" + c.clientNumber);

                                }

                                //let all the clients know about the pellets
                                for (int j = 0; j < numberOfPellets; j++)
                                {
                                    Pellet pellet = pelletArray[j];
                                    c.sw.WriteLine("spawnPellet\\" + pellet.positionX + "\\" + pellet.positionY + "\\" + pellet.pelletID);
                                }

                                //now that all the clients have the initial game state
                                //each client can start playing
                                c.sw.WriteLine("start");
                            }

                            //now that the players have started playing
                            //the server should now start checking if any collisions have occured
                            //collisionThread.Start();
                            commandClock.Start();
                            playing = true;
                        }

                        //check each client in the array
                        //if they have commands waiting in the queue, dequeue and execute that command

                        foreach (Client client in clientArray)
                        {
                            //the server must check if it has recieved any commands from the clients
                            if (client.commandQueue.Count > 0)
                            {
                                //each command is a string that will be delimited by \\
                                string command = client.commandQueue.Dequeue();

                                //each message will be broken into an array of strings
                                string[] tokens = command.Split(new string[] { "\\" }, StringSplitOptions.None);

                                /*
                                 *the commands are as follows
                                 *play - tells the server that the client is ready to start playing the game
                                 *userInfo - tells the server that the client is sending login information
                                 *      -check sthe database and either logins the clients, creates new user info,
                                 *       denies the login attemps
                                 *velocity - broadcasts the sending client's updated velocity
                                 *position - broadcasts the sending client's updated position
                                 *score - broadcasts the correct scores to each client
                                 *lag - adds a delay when sending messages between client and server (for testing purposes)
                                 */

                                if (tokens[0].Equals("play"))
                                {
                                    if (playing)
                                    {
                                        loadGameState(client);
                                    }
                                    else
                                    {
                                        client.clientReady = true;
                                        Console.WriteLine("client " + client.clientNumber + " is ready");
                                    }
                                }

                                //
                                else if (tokens[0].Equals("userInfo"))
                                {

                                    if (!loginNames.Contains(tokens[1]))
                                    {

                                        if (dm.existsInTable(tokens[1]))
                                        {
                                            if (tokens[2].Equals(dm.getUserPassword(tokens[1])))
                                            {
                                                client.sw.WriteLine("loginSucceed\\" + tokens[1]);
                                                Console.WriteLine(tokens[1] + " has logged in");
                                            }
                                            else
                                            {
                                                client.sw.WriteLine("loginFail");
                                                Console.WriteLine(tokens[1] + " entered incorrect password");
                                            }
                                        }

                                        else
                                        {
                                            dm.insertIntoPlayer(tokens[1], tokens[2]);
                                            loginNames.Add(tokens[1]);
                                            client.sw.WriteLine("loginSucceed\\" + tokens[1]);
                                            client.clientName = tokens[1];
                                            dm.insertIntoHighScores(client.clientName, client.score);
                                            dm.printTable();
                                            dm.printHighTable();
                                            dm.sendPacket("add", client.clientName, client.score);
                                        }

                                    }
                                    else
                                    {
                                        client.sw.WriteLine("alreadyLoggedIn\\" + tokens[1]);
                                    }

                                }

                                 //if the velocity of a client has changed, they send it to the server
                                //the server broadcasts this velocity to all other clients
                                else if (tokens[0].Equals("velocity"))
                                {
                                    if (tokens[1].Equals("x"))
                                    {
                                        client.xVelocity = float.Parse(tokens[2]);
                                        client.yVelocity = 0;
                                    }
                                    else
                                    {
                                        client.yVelocity = float.Parse(tokens[2]);
                                        client.xVelocity = 0;
                                    }

                                    foreach (Client c in clientArray)
                                    {
                                        if (c.clientNumber != client.clientNumber)
                                        {
                                            c.sw.WriteLine("velocity\\" + client.clientNumber + "\\" + client.xVelocity + "\\" + client.yVelocity);

                                        }
                                    }

                                }

                                else if (tokens[0].Equals("position"))
                                {
                                    if (tokens[1].Equals("x"))
                                    {
                                        client.updatePosition(float.Parse(tokens[2]), client.whalePositionY);
                                    }
                                    else if (tokens[1].Equals("y"))
                                    {
                                        client.updatePosition(client.whalePositionX, float.Parse(tokens[2]));
                                    }

                                    foreach (Client c in clientArray)
                                    {
                                        if (c.clientNumber != client.clientNumber)
                                            c.sw.WriteLine("position\\" + client.clientNumber + "\\" + client.whalePositionX + "\\" + client.whalePositionY);
                                    }

                                    CheckCollisions();
                                }

                                else if (tokens[0].Equals("lag"))
                                {
                                    DateTime dt = NTPTime.getNTPTime(ref uniClock);
                                    dt.AddMinutes(uniClock.Elapsed.Minutes);
                                    dt.AddSeconds(uniClock.Elapsed.Seconds);
                                    dt.AddMilliseconds(uniClock.ElapsedMilliseconds);

                                    long ticks = dt.Ticks;

                                    //client1.sw.WriteLine("lag\\" + ticks);
                                }
                                else if (tokens[0].Equals("logout"))
                                {
                                    serverRef.RemoveClient(client);
                                    //Console.WriteLine("Name about to be removed {0}", tokens[1]);
                                    //loginNames.Remove(tokens[1]);
                                    //dm.deleteFromHighScores(client.clientName);
                                    //dm.sendPacket("remove", client.clientName, client.score);

                                }
                                else if (tokens[0].Equals("disconnect"))
                                {
                                    dm.sendPacket("remove", client.clientName, client.score);
                                }
                                else
                                {
                                    //do nothing
                                }

                            }
                        }

                    }
                    catch (Exception e)
                    {

                        Console.WriteLine(e.StackTrace);
                        Console.WriteLine(e.Message);

                    }
                }
            }
Example #3
0
        public static Command startGameCommand(long ts, Player p1, Player p2, float x1, float y1, float x2, float y2, int d1, int d2, Pellet[] pelletList)
        {
            Command newCommand = new Command();
            newCommand.timeStamp = ts;
            newCommand.cType = CType.StartGame;
            newCommand.message = newCommand.cType + ":" + 0 + ":" + p1.playerNumber + ":" + x1 + ":" + y1 + ":" + d1 + ":" + p2.playerNumber + ":" + x2 + ":" + y2 + ":" + d2;
            for (int i = 0; i < pelletList.Length; ++i)
            {
                newCommand.message += ":" + pelletList[i].id + ":" + pelletList[i].x + ":" + pelletList[i].y + ":" + pelletList[i].size;
            }

            newCommand.message += ";";

            return newCommand;
        }
Example #4
0
        public static Command startGameInProgressCommand(long ts, Player[] playerList, Pellet[] pelletList)
        {
            Command newCommand = new Command();
            newCommand.timeStamp = ts;
            newCommand.cType = CType.StartGame;
            newCommand.message = newCommand.cType + ":" + 1 + ":" + playerList.Length;

            for (int i = 0; i < playerList.Length; ++i )
            {
                newCommand.message += ":" + playerList[i].playerNumber + ":" + playerList[i].x + ":" + playerList[i].y + ":" + playerList[i].dir + ":" + playerList[i].size;
            }
            newCommand.message += ":" + pelletList.Length;

            for (int i = 0; i < pelletList.Length; ++i)
            {
                newCommand.message += ":" + pelletList[i].id + ":" + pelletList[i].x + ":" + pelletList[i].y + ":" + pelletList[i].size;
            }

            newCommand.message += ";";

            return newCommand;
        }
Example #5
0
 public void spawnPellet(int index)
 {
     pelletList[index] = new Pellet(pelletNumber);
     pelletNumber += 1;
 }