Beispiel #1
0
        protected Message ProcessIncomingEnvelope(Envelope env)
        {
            if (env.Message.GetType() == typeof(PlayerIdMessage))
            {
                //TODO: Put info from PlayerIdMessage In AppState.
                GMAppState      gmAppState = (GMAppState)this.SubSystem.appState;
                PlayerIdMessage mess       = (PlayerIdMessage)env.Message;

                gmAppState.p1PID = mess.getP1Id();
                gmAppState.p2PID = mess.getP2Id();

                gmAppState.P1EndPoint = SubSystem.ParseEPString(mess.getP1EndPoint());
                Logger.Debug($"Set the Player 1 endpoint to {mess.getP1EndPoint()}");

                gmAppState.P2EndPoint = SubSystem.ParseEPString(mess.getP2EndPoint());
                Logger.Debug($"Set the Player 2 endpoint to {mess.getP2EndPoint()}");


                gmAppState.p1Name = mess.getP1Name();
                gmAppState.p2Name = mess.getP2Name();

                gmAppState.gameId = mess.getGameId();

                //TODO: Not sure what to do with mess.getp1Wins and mess.getp2Wins.

                return(new AckMessage(99, 0, new Identifier(this.SubSystem.ProcessID, SubSystem.GetNextSeqNumber()), this.ConversationId)); //TODO: Not sure what to do with the ID in Ack here.
            }
            else
            {
                Logger.Error($"Did not receive a playerIdMessage, instead received a {env.Message.GetType()}");
            }

            return(null);
        }
Beispiel #2
0
        protected Message ProcessIncomingEnvelope(Envelope env)
        {
            this.IncomingEnvelopes.Enqueue(env);
            Identifier messageNumber = new Identifier(SubSystem.ProcessID, SubSystem.GetNextSeqNumber());

            appState = SubSystem.appState as GMAppState;
            if (env.Message.GetType() == typeof(ShotMessage))
            {
                Logger.Info("Received a shot message in RespondShot convo");
                ShotMessage shotMessage = (ShotMessage)env.Message;
                GameId = shotMessage.getGameId();
                appState.isAHit(shotMessage.getXcord(), shotMessage.getYcord(), shotMessage.getPlayerId());
                appState.isAWin(shotMessage.getPlayerId());
                appState.P1turn = !appState.P1turn;
                appState.LastX  = shotMessage.getXcord();
                appState.LastY  = shotMessage.getYcord();

                ResultMessage resultMessage = new ResultMessage(appState.lastShotResult, GameId, appState.end, appState.end, false, appState.LastX, appState.LastY, messageNumber, this.ConversationId);
                return(resultMessage);
            }
            else
            {
                Logger.Error("Did not receive a shot message in RespondShot convo");
                return(new ErrorMessage(1, messageNumber, ConversationId));
            }
        }
        protected override Message CreateFirstMessage()
        {
            Identifier messageNumber = new Identifier(SubSystem.ProcessID, SubSystem.GetNextSeqNumber());

            ConversationId = messageNumber;

            appState = SubSystem.appState as GMAppState;
            Logger.Info("Result message created in turns convo");
            return(new ResultMessage(appState.lastShotResult, appState.gameId, false, appState.end, this.myTurn, appState.LastX, appState.LastY, messageNumber, ConversationId));
        }
Beispiel #4
0
        protected Message ProcessIncomingEnvelope(Envelope env)
        {
            this.IncomingEnvelopes.Enqueue(env);
            Identifier messageNumber = new Identifier(SubSystem.ProcessID, SubSystem.GetNextSeqNumber());


            if (env.Message.GetType() == typeof(BoardMessage))
            {
                Logger.Info("Received a board message in RespondBoard convo");
                BoardMessage boardMessage = (BoardMessage)env.Message;
                GMAppState   appState     = SubSystem.appState as GMAppState;
                lock (boardAccessLock)
                {
                    if (appState.P1grid == null) //Board message maybe should include player id. That way we dont have to check the endpoints.
                    {
                        appState.P1grid     = boardMessage.getGrid();
                        appState.p1PID      = boardMessage.MessageNumber.Pid;
                        appState.P1EndPoint = env.From;
                        Logger.Info("set player 1's grid in GM");
                    }
                    else
                    {
                        appState.P2grid     = boardMessage.getGrid();
                        appState.p2PID      = boardMessage.MessageNumber.Pid;
                        appState.P2EndPoint = env.From;
                        Logger.Info("set player 2's grid in GM");
                    }
                }
                return(new AckMessage(boardMessage.getGameId(), 0, messageNumber, this.ConversationId));
            }
            else
            {
                Logger.Error("Did not receive a board message in RespondBoard convo");
                return(new ErrorMessage(2, messageNumber, ConversationId));
            }
        }
Beispiel #5
0
        protected override void ExecuteDetails()
        {
            appState = this.SubSystem.appState as GMAppState;

            var      msg      = ProcessIncomingEnvelope(IncomingEnvelope);
            Envelope response = new Envelope(msg, null, RemoteEndPoint, IsTcpConversation);

            DoReliableRespondToRequest(response);

            this.State = PossibleState.Successed;

            //Create Turns to other player.
            Turns respondTurnsToShot = this.SubSystem.conversationFactory.CreateFromConversationType <Turns>();

            if (this.RemoteEndPoint.ToString() == appState.P1EndPoint.ToString())
            {
                respondTurnsToShot.RemoteEndPoint = appState.P2EndPoint;
            }
            else
            {
                respondTurnsToShot.RemoteEndPoint = appState.P1EndPoint;
            }
            respondTurnsToShot.Launch();
        }
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            ILog Logger = LogManager.GetLogger(typeof(Conversation));

            GMAppState appState      = new GMAppState();
            IPEndPoint lobbyEndPoint = new IPEndPoint(IPAddress.Parse("54.159.115.195"), 2201);

            ConversationFactory conFact = new ConversationFactory(); //Create conversation factory
            IPEndPoint          tcpEP   = new IPEndPoint(IPAddress.Any, 2005);
            IPEndPoint          updEP   = new IPEndPoint(IPAddress.Any, 2006);
            SubSystem           subSys  = new SubSystem(conFact, appState, tcpEP, updEP); //Create Communication sub system.

            subSys.start();                                                               //Start the subsystem.

            short count = 2;

            while (count-- > 0)
            {
                GMJoinLobby conversation = conFact.CreateFromConversationType <GMJoinLobby>();
                conversation.RemoteEndPoint = lobbyEndPoint; //Set to remote aws machine.
                conversation.Launch();

                while (appState.P1grid == null || appState.P2grid == null)
                {
                    if (appState.P1grid != null)
                    {
                        Logger.Info("waiting for player 2's grid");
                    }
                    else
                    {
                        Logger.Info("waiting for both grids");
                    }
                    Thread.Sleep(3000);
                }

                Turns conversationTurn1 = conFact.CreateFromConversationType <Turns>();
                conversationTurn1.RemoteEndPoint = appState.P1EndPoint;
                Turns conversationTurn2 = conFact.CreateFromConversationType <Turns>();
                conversationTurn2.myTurn         = false; //This makes sure pl2 know its not his turn.
                conversationTurn2.RemoteEndPoint = appState.P2EndPoint;
                conversationTurn1.Launch();
                conversationTurn2.Launch();

                Logger.Info("Game in progress");
                while (!appState.gameEndingEvent.WaitOne(30) || appState.end == false)
                {
                    Thread.Sleep(1000);
                }
                Logger.Info("Game ending");

                EndGame conversationEndGame = conFact.CreateFromConversationType <EndGame>();
                conversationEndGame.RemoteEndPoint = lobbyEndPoint;
                conversationEndGame.Pid            = appState.P1turn ? appState.p1PID : appState.p2PID;
                conversationEndGame.Launch();

                string p1result = appState.P1turn ? "WIN" : "LOSS";
                string p2result = !appState.P1turn ? "WIN" : "LOSS";

                Logger.Info($"Game over: Player 1 - {p1result}, Player 2 - {p2result}");

                appState.resetState();
            }
        }