Example #1
0
        public void disconnect()
        {
            if (!disconnected)
            {
                Console.Out.WriteLine("Attempting to disconnect...");

                logMessage("Sending disconnect message...");
                MwnpMessage disconnect = new MwnpMessage(new int[] { netId, 0 }, "MWNL2_DISCONNECT", null);
                try
                {
                    messenger?.sendMessage(disconnect);
                }
                finally { }

                logMessage("Ending listener...");
                listener?.end();

                logMessage("Ending messenger...");
                messenger?.end();

                logStream.Close();
                logStream = null;

                disconnected = true;
                Console.Out.WriteLine("Disconnect complete.");

                BasicGameInfo gameInfo = (BasicGameInfo)(env?.getGameInfo());
                Console.Out.WriteLine("\n**GAME STATISTICS**");
                Console.Out.WriteLine("   Last round score: {0}\n", gameInfo?.getScore());
                Console.Out.WriteLine("   Best round score: {0}\n", gameInfo?.getBestScore());
                Console.Out.WriteLine("   Game high score: {0}\n", gameInfo?.getBestScore());
                Console.Out.WriteLine("   Number of times destroyed: {0}\n", gameInfo?.getNumDeaths());
            }
        }
Example #2
0
        private MwnpMessage getMessage(int length)
        {
            StringBuilder builder = new StringBuilder("[");

            for (int i = 0; i < length - 1; i++)
            {
                int currByte = inStream.ReadByte();
                if (currByte == -1)
                {
                    return(null);
                }

                builder.Append((char)currByte);
            }
            string message = builder.ToString();

            return(MwnpMessage.parseMessage(message));
        }
Example #3
0
        public void sendMessage(MwnpMessage msg)
        {
            if (sock.Connected)
            {
                string msgstring = msg.toJsonstring();
                outStream.Write(msgstring);
                outStream.Flush();

                Console.Out.WriteLine(msg);
                if (LOGGING)
                {
                    printMessage(msg);
                }
            }
            else
            {
                Console.Error.WriteLine("Failed to send message -- output socket closed.");
                logStream.WriteLine("Failed to send message -- output socket closed.");
            }
        }
Example #4
0
        private void run()
        {
            while (running)
            {
                try
                {
                    int msgSize = getMessageLength();
                    if (msgSize == -1)
                    {
                        return;
                    }

                    MwnpMessage msg = getMessage(msgSize);

                    if (msg == null)
                    {
                        return;
                    }

                    client.parseMessage(msg);

                    if (LOGGING)
                    {
                        printMessage(msg);
                    }

                    Thread.Sleep(1);
                }
                catch (IOException ex)
                {
                    Console.Error.WriteLine("Server read error...");
                    Console.Error.WriteLine(ex.ToString());
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.ToString());
                }
            }
        }
Example #5
0
 private void printMessage(MwnpMessage message)
 {
     logStream.Write("Message sent from {0} to {1} - \r\n", message.getSenderId(), message.getReceiverId());
     logStream.WriteLine(message);
 }
Example #6
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();
            }
        }
Example #7
0
 private void printMessage(MwnpMessage message)
 {
     (logStream ?? Console.Out).Write("Message received from {0} intended for {1} - \r\n", message.getSenderId(), message.getReceiverId());
     (logStream ?? Console.Out).WriteLine(message.ToString());
 }