Ejemplo n.º 1
0
        //if have both player declaration calculate turn show them and send results to client
        private void CheckAndCalculateTurn()
        {
            if (Player1 == null || Player2 == null)
            {
                return;
            }

            Gm.ShowMessage("Calculating turn");
            var turnResults = TurnLogic.ResolveTurn(Player1, Player2, Gm.Map);

            Player1 = null;
            Player2 = null;
            SendData(1, Serializer.Serialize(Gm.Map));
            SendData(0, Serializer.Serialize(turnResults));
            Gm.ShowResultsFromGc(turnResults);
        }
Ejemplo n.º 2
0
        //Recieving information from host
        private void OnReceiveClient(IAsyncResult ar)
        {
            try
            {
                Socket.EndReceive(ar);
                var data = Data;
                Data = new byte[16384];
                Socket.BeginReceive(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(OnReceiveClient), null);

                var newData = new byte[data.Length - 1];
                Array.Copy(data, 1, newData, 0, data.Length - 1);
                switch (data[0])
                {
                case 0:
                    var results = Serializer.Deserialize(newData) as TurnResults;
                    Gm.ShowResultsFromGc(results);
                    break;

                case 1:
                    var map = Serializer.Deserialize(newData) as Map;
                    Gm.Map = map;
                    Gm.ActualizeCharacters();
                    break;

                case 3:
                    var message = Encoding.UTF8.GetString(newData);
                    Gm.ShowMessage(message);
                    break;

                default:
                    throw new SystemException("Got bad first byte");
                }
            }
            catch (SocketException)
            {
                Gm.EndOfGame();
            }
        }