예제 #1
0
 public MwnpMessage(int[] ids, ShipCommand cmd)
 {
     this.ids     = ids;
     this.command = "SCMD";
     this.data    = cmd.getMessage();
 }
예제 #2
0
        public void parseMessage(MwnpMessage msg)
        {
            if (msg.getCommand().Equals("MWNL2_ASSIGNMENT"))
            {
                MwnpMessage dMsg = (MwnpMessage)msg;
                this.netId = (int)((Double)(dMsg.getData()));
            }
            else if (msg.getCommand().Equals("MWNL2_AC"))
            {
                Console.Error.WriteLine("Already connected from this IP address.  Exiting...");
                Environment.Exit(1);
            }
            else if (msg.getCommand().Equals("REQUEST"))
            {
                StringStringMap map       = (StringStringMap)msg.getData();
                int             numImages = int.Parse(map["IMAGELENGTH"]);
                int             width     = int.Parse(map["WORLDWIDTH"]);
                int             height    = int.Parse(map["WORLDHEIGHT"]);

                MwnpMessage.RegisterGameType(map["GAMENAME"]);

                // TODO: Some games may return extra data in the map, we should figure out how we want to expose this in the client
                RegistrationData data = ship.registerShip(numImages, width, height);
                data = new RegistrationData(data.getName(), data.getColor(), data.getImage());

                MwnpMessage response = new MwnpMessage(new int[] { netId, 0 }, data);
                messenger.sendMessage(response);
            }
            else if (msg.getCommand().Equals("ENV"))
            {
                env = (Environment <T>)msg.getData();

                // check for death
                int currShipId = env.getShipStatus().getId();
                if (shipId == -1)
                {
                    shipId = currShipId;
                }

                if (shipId != currShipId)
                {
                    // new id means ship has died; inform ship
                    ship.shipDestroyed(((BasicGameInfo)env.getGameInfo()).getLastDestroyedBy());
                    shipId = currShipId;
                }

                ShipCommand cmd = null;
                try
                {
                    cmd = ship.getNextCommand(env);
                }
                catch (Exception ex)
                {
                    // typically means ship has an exception; skip to the actual problem to avoid confusion
                    Console.Error.WriteLine("Exception thrown by getNextCommand: \n" + ex.ToString());
                    disconnect();
                }
                if (cmd == null)
                {
                    cmd = new IdleCommand(0.1);
                }
                MwnpMessage response = new MwnpMessage(new int[] { netId, 0 }, cmd);
                messenger.sendMessage(response);
            }
            else if (msg.getCommand().Equals("ERROR"))
            {
                logMessage(msg.getData().ToString());
                Console.Error.WriteLine(msg.getData().ToString());
                //ship.processError((ErrorData)msg.getData());
            }
            else if (msg.getCommand().Equals("MWNL2_DISCONNECT"))
            {
                this.disconnect();
            }
        }