Exemplo n.º 1
0
        /// <summary>
        /// there are four steps for the server
        /// (1) read xml to get necessary information
        /// (2)initalize a new world and add info to the new world
        /// (3)uisng serverawaitClientloop to awit client join in
        /// (4)using update method to send update info to the client
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            xmlRead();
            watch = new Stopwatch();
            list  = new LinkedList <SocketState>();
            watch.Start();
            liveStar = new Stopwatch();
            liveStar.Start();
            theworld = new world();
            sta.setLoc(new Vector2D(200, -200));
            star ss = new star(1, new Vector2D(200, 200), 0.01);

            theworld.addStar(sta);
            theworld.addStar(ss);
            theworld.setSize(UniverSize);
            theworld.setRespawn(RespawnRate);
            theworld.setFrame(MSperFrame);
            NController.ServerAwaitClientLoop(HandleNewClient);
            Console.WriteLine("our server start, and client could join in now");
            //create a new thread to update the world and send the updated info to the client
            Task task = new Task(() =>
            {
                while (true)
                {
                    update();
                }
            });

            task.Start();
            Console.Read();
        }
Exemplo n.º 2
0
        public void TestProjectileMethod()
        {
            world world1 = new world();

            projectile p = new projectile(0, new Vector2D(0, 0), new Vector2D(0, 0), true, 1);
            star       s = new star(0, new Vector2D(0, 0), 1);

            world1.addStar(s);
            String     A  = p.ToString();
            projectile s1 = JsonConvert.DeserializeObject <projectile>(A);

            world1.setFrame(50);
            world1.setRespawn(300);
            world1.setSize(750);
            world1.update();
            Assert.AreEqual(p.getID(), 0);
            Assert.AreEqual(new Vector2D(0, 0), p.getloc());
            Assert.AreEqual(new Vector2D(0, 0), p.getdir());
            Assert.AreEqual(p.checkAlive(), true);
            Assert.AreEqual(p.getOwner(), 1);
            p = new projectile(0, new Vector2D(0, 0), new Vector2D(0, 0), true, 1);
            p.update(750, world1.getStar().Values);
            p.die();

            projectile p1 = new projectile(1, new Vector2D(0, 376), new Vector2D(0, 0), true, 2);

            p1.update(750, world1.getStar().Values);
        }
Exemplo n.º 3
0
        public void TestStarMethod()
        {
            world world1 = new world();
            star  s      = new star(0, new Vector2D(200, 200), 1);

            world1.addStar(s);
            world1.setFrame(50);
            world1.setRespawn(300);
            world1.setSize(750);
            world1.update();
            s.update(51);
            s.update(102);
            s.update(160);
            s.update(230);
        }
Exemplo n.º 4
0
        public void TestShipMethod()
        {
            world world1 = new world();
            star  s      = new star(0, new Vector2D(200, 200), 1);

            world1.addStar(s);
            world1.setFrame(50);
            world1.setRespawn(300);
            world1.setSize(750);
            Ship ship = world1.generateShip("ship");

            ship.hpdecrease();
            Assert.AreEqual(ship.getHp(), 4);
            ship.getDeath();
            Assert.AreEqual(ship.getDeath(), 0);
        }
Exemplo n.º 5
0
        public void TestStar2Method()
        {
            world  world1 = new world();
            star   s      = new star(0, new Vector2D(0, 200), 1);
            String A      = s.ToString();
            star   s1     = JsonConvert.DeserializeObject <star>(A);

            world1.addStar(s);
            world1.setFrame(50);
            world1.setRespawn(300);
            world1.setSize(750);
            world1.update();
            s.update(51);
            s.update(102);
            s.update(160);
            s.update(230);
        }
Exemplo n.º 6
0
        public void TestStar1Method()
        {
            world world1 = new world();
            star  s      = new star(0, new Vector2D(0, -200), 1);

            s.setLoc(new Vector2D(0, 0));
            Assert.AreEqual(new Vector2D(0, 0), s.getloc());
            world1.addStar(s);
            world1.setFrame(50);
            world1.setRespawn(300);
            world1.setSize(750);
            world1.update();
            s.update(51);
            s.update(102);
            s.update(160);
            s.update(230);
        }
Exemplo n.º 7
0
        public void TestWorldMethod()
        {
            world      world1 = new world();
            star       s      = new star(0, new Vector2D(0, 200), 1);
            projectile p1     = new projectile(1, new Vector2D(0, 376), new Vector2D(0, 0), true, 2);
            Ship       ship   = new Ship(1, new Vector2D(0, 0), new Vector2D(0, 0), false, "s", 5, 0);
            String     A      = s.ToString();
            star       s1     = JsonConvert.DeserializeObject <star>(A);

            world1.addStar(s);
            world1.setFrame(0);
            world1.addShip(ship);
            world1.Fire(1);

            world1.addproj(p1);
            world1.setRespawn(300);
            world1.setSize(750);

            Assert.AreEqual(world1.getProj().Values.Count, 1);
            Assert.AreEqual(world1.getShip().Values.Count, 1);
            Assert.AreEqual(world1.getStar().Values.Count, 1);
            ship = new Ship(1, new Vector2D(0, 0), new Vector2D(0, 0), true, "s", 5, 0);
            for (int i = 0; i <= 4; i++)
            {
                world1.getShip()[1].hpdecrease();
            }
            world1.respawn(ship);
            world1.addLostID(1);
            world1.cleanup();
            world1.removeProjectile(p1);
            world1.removeStar(s);
            world1.removeShip(ship);
            Assert.AreEqual(world1.getShip().Values.Count, 0);
            Assert.AreEqual(world1.getShip().Values.Count, 0);
            Assert.AreEqual(world1.getStar().Values.Count, 0);
            world1.update();
            s.update(51);
            s.update(102);
            s.update(160);
            s.update(230);
        }
Exemplo n.º 8
0
        public void TestShipMethod1()
        {
            world world1 = new world();
            star  s      = new star(0, new Vector2D(200, 200), 1);

            world1.addStar(s);
            world1.setFrame(50);
            world1.setRespawn(300);
            world1.setSize(750);
            Ship ship = world1.generateShip("ship");

            ship.hpdecrease();
            //    Assert.AreEqual(ship.getHp(), 4);
            ship.getDeath();
            //  Assert.AreEqual(ship.getDeath(), 0);

            Assert.AreEqual(ship.getName(), "ship");
            Assert.AreEqual(ship.getScore(), 0);
            Assert.AreEqual(ship.checkFire(10, 15), false);
            Assert.AreEqual(ship.checkFire(16, 15), true);
        }
Exemplo n.º 9
0
        public void TestShipMethod3()
        {
            world world1 = new world();
            star  s      = new star(0, new Vector2D(200, 200), 1);

            world1.addStar(s);
            world1.setFrame(50);
            world1.setRespawn(300);
            world1.setSize(750);
            Ship ship = world1.generateShip("ship");

            ship.hpdecrease();
            //    Assert.AreEqual(ship.getHp(), 4);
            ship.getDeath();
            //  Assert.AreEqual(ship.getDeath(), 0);

            Assert.AreEqual(ship.getName(), "ship");
            Assert.AreEqual(ship.getScore(), 0);
            Assert.AreEqual(ship.checkFire(10, 15), false);
            Assert.AreEqual(ship.checkFire(16, 15), true);
            Assert.AreEqual(ship.getThrust(), false);
            Assert.AreEqual(ship.getID(), 1);
            for (int i = 0; i <= 4; i++)
            {
                ship.hpdecrease();
            }
            Assert.AreEqual(ship.checkFire(16, 1), false);


            ship.doOperate('L');
            ship.doOperate('R');
            ship.refresh();
            ship.doOperate('T');
            ship.refresh();
            ship.getdir();
            ship.increaseScore();
            Assert.AreEqual(ship.getScore(), 1);
            ship.respawn();
            world1.update();
        }
Exemplo n.º 10
0
        public void TestworldMethod1()
        {
            world      world1 = new world();
            star       s      = new star(0, new Vector2D(0, -200), 1);
            Ship       ship   = new Ship(1, new Vector2D(0, 0), new Vector2D(0, 0), false, "s", 5, 0);
            projectile p1     = new projectile(1, new Vector2D(0, 10), new Vector2D(0, 0), true, 2);

            world1.addproj(p1);
            world1.addShip(ship);

            s.setLoc(new Vector2D(0, 0));
            Assert.AreEqual(new Vector2D(0, 0), s.getloc());
            world1.addStar(s);
            world1.setFrame(50);
            world1.setRespawn(300);
            world1.setSize(750);
            world1.update();
            s.update(51);
            s.update(102);
            s.update(160);
            s.update(230);
        }
Exemplo n.º 11
0
        //get the content information of the current world from the server
        // and using the JSon to get the objects
        private void ReceiveWorld(SocketState state)
        {
            string totalData = state.sb.ToString();

            string[] parts = Regex.Split(totalData, @"(?<=[\n])");


            // Loop until we have processed all messages.
            // We may have received more than one.
            Ship       sp = null;
            star       st = null;
            projectile pj = null;

            foreach (string p in parts)
            {
                // Ignore empty strings added by the regex splitter
                if (p.Length == 0)
                {
                    continue;
                }
                // The regex splitter will include the last string even if it doesn't end with a '\n',
                // So we need to ignore it if this happens.
                if (p[p.Length - 1] != '\n')
                {
                    break;
                }
                if (p[0] == '{' && p[p.Length - 2] == '}')
                {
                    JObject jsonObject = JObject.Parse(p);
                    JToken  tokenShip  = jsonObject["ship"]; //gte the ship object from the server
                    JToken  tokenStar  = jsonObject["star"]; // get the star object from the server
                    JToken  tokenProj  = jsonObject["proj"]; // get the projectiles from the server
                    if (tokenShip != null)
                    {
                        sp = JsonConvert.DeserializeObject <Ship>(p);// if ship is not null,get the ship
                    }
                    if (tokenStar != null)
                    {
                        st = JsonConvert.DeserializeObject <star>(p);// if the star is not null, get the star
                    }
                    if (tokenProj != null)
                    {
                        pj = JsonConvert.DeserializeObject <projectile>(p);// if the projectile is not null, get the projectile
                    }
                    //avoid the race condition
                    lock (theworld)
                    {
                        if (sp != null)
                        {
                            if (sp.getHp() > 0)
                            {
                                theworld.addShip(sp);// if the hp is greater than 0, add to the current world
                                if (ships.Contains(sp.getID()) == false)
                                {
                                    ships.Add(sp.getID());
                                    handlescore(sp);
                                }
                            }
                            else
                            {
                                handledie(sp);
                                ships.Remove(sp.getID());
                                theworld.removeShip(sp);//remove the destoryed ship
                            }
                        }

                        if (pj != null)
                        {
                            // if the projectile is still alive, add it to the projectils group
                            if (pj.checkAlive() == true)
                            {
                                theworld.addproj(pj);
                            }
                            // if projectile is not alive, remove it from the projectile group
                            else
                            {
                                theworld.removeProjectile(pj);
                            }
                        }
                        //check the star then add to the star group

                        if (st != null)
                        {
                            theworld.addStar(st);
                        }
                    }
                }



                // Then remove it from the SocketState's growable buffer
                state.sb.Remove(0, p.Length);

                frametick();
            }

            state.callMe = ReceiveWorld;
            Handlekey();
            NController.GetData(state);
        }