private async void btnJoin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_chatRoomServiceProxy != null)
                {
                    _chatRoomServiceProxy.Leave();
                }
                JoinRoomResponse response = await _chatServiceProxy.JoinOrCreateRoom(cbChatRooms.Text);

                if (_chatChannelDisconnectionSubscription != null)
                {
                    _chatChannelDisconnectionSubscription.Dispose();
                }
                _chatChannel = await _node.ConnectToServer(response.ServerEndpoint);

                _chatRoomServiceProxy = await _chatChannel.GetProxy <IChatRoomService>(response.RoomActorId);

                _chatChannel.SetHandler <IChatRoomServiceCallback>(this);
                _chatChannelDisconnectionSubscription = _chatChannel.DisconnectedEvent.Subscribe((_) => MessageBox.Show("Disconnected from chat room, try connect again"));
                var content = await _chatRoomServiceProxy.Join(response.Ticket);

                txtChat.Clear();
                foreach (var msg in content)
                {
                    txtChat.AppendText(msg + "\n");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
예제 #2
0
 public void AfterServerRegistration(IServerPeer serverPeer)
 {
     foreach (var afterServerRegistration in _afterServerRegistrationEvents)
     {
         afterServerRegistration.AfterRegister(serverPeer);
     }
 }
예제 #3
0
        public override bool OnHandleMessage(IMessage message, IServerPeer sPeer)
        {
            Log.DebugFormat("OnHandleMessage:TexasSitHandler");

            var errorParam = new Dictionary <byte, object>
            {
                { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
                { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] },
            };

            var serverPeer = sPeer as ServerOutbouncePeer;
            var operation  = new SitOperator(serverPeer.Protocol, message);

            if (!operation.IsValid)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.Sit, null, errorParam, operation.GetErrorMessage(), (int)RoomErrorCode.OperationInvalid));
                return(false);
            }
            var peerId = new Guid((Byte[])operation.PeerId);

            var roomID = operation.RoomID;

            errorParam.Add((byte)RoomParameterCode.RoomID, roomID);

            var facade = TexasApplication.GetRoom(roomID);

            if (facade == null)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.Sit, null, errorParam, "RoomId Not Found", (int)RoomErrorCode.RoomIdNotFound));
                return(false);
            }
            var userItem = TexasApplication.GetRoomUser(roomID, peerId);

            if (userItem == null)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.Sit, null, errorParam, "User Not Yet Join Room", (int)RoomErrorCode.UserNotYetJoinRoom));
                return(false);
            }

            var          json         = operation.TexasSitDown;
            TexasSitDown texasSitDown = JsonConvert.DeserializeObject <TexasSitDown>(json) as TexasSitDown;

            texasSitDown.sisDownInfo.UserKey  = userItem.UserKey;
            texasSitDown.sisDownInfo.UserName = userItem.UserName;

            var texasRoomItem = TexasApplication.GetRoomItem(roomID);

            if (userItem.BankBalance < texasRoomItem.WithdrawMin && texasSitDown.sisDownInfo.IsSitDown)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.Sit, null, errorParam, "Need $" + texasRoomItem.WithdrawMin + " to play", (int)RoomErrorCode.AmountTooLow));
                return(false);
            }

            var stateMachine = facade.RetrieveMediator(TexasStateMachine.NAME) as TexasStateMachine;

            stateMachine.Sit(texasSitDown);

            return(true);
        }
 /// <summary>
 /// Do somthing after server registration
 /// </summary>
 /// <param name="serverPeer">Server Peer.</param>
 public void AfterServerRegistration(IServerPeer serverPeer)
 {
     foreach (var afterServerRegistration in _afterServerRegistrationEvents)
     {
         Log.DebugFormat("After - {0}", afterServerRegistration);
         afterServerRegistration.AfterRegister(serverPeer);
     }
 }
예제 #5
0
 public override bool OnHandleMessage(IMessage message, IServerPeer serverPeer)
 {
     if (Log.IsDebugEnabled)
     {
         Log.DebugFormat("Successful Registration");
     }
     _serverApplication.AfterServerRegistration(serverPeer);
     return(true);
 }
예제 #6
0
        public override bool OnHandleMessage(IMessage message, IServerPeer serverPeer)
        {
            var peer = serverPeer as PhotonServerPeer;

            if (peer != null)
            {
                return(OnHandleMessage(message, peer));
            }
            return(false);
        }
예제 #7
0
        public override bool OnHandleMessage(IMessage message, IServerPeer sPeer)
        {
            Log.DebugFormat("OnHandleMessage:TexasStatusHandler");

            var errorParam = new Dictionary <byte, object>
            {
                { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
                { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] },
            };

            var serverPeer = sPeer as ServerOutbouncePeer;
            var operation  = new StatusOperator(serverPeer.Protocol, message);


            if (!operation.IsValid)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.Status, null, errorParam, operation.GetErrorMessage(), (int)RoomErrorCode.OperationInvalid));
                return(false);
            }
            var peerId = new Guid((Byte[])operation.PeerId);

            var roomID = operation.RoomID;

            errorParam.Add((byte)RoomParameterCode.RoomID, roomID);
            var facade = TexasApplication.GetRoom(roomID);

            if (facade == null)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.Status, null, errorParam, "RoomId Not Found", (int)RoomErrorCode.RoomIdNotFound));
                return(false);
            }
            var userItem = TexasApplication.GetRoomUser(roomID, peerId);

            if (userItem == null)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.Status, null, errorParam, "User Not Yet Join Room", (int)RoomErrorCode.UserNotYetJoinRoom));
                return(false);
            }

            var texasModel  = facade.RetrieveProxy(TexasModel.NAME) as TexasModel;
            var texasStatus = texasModel.buildTexasHoldemStatus();


            var param = new Dictionary <byte, object>
            {
                { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
                { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] },
                { (byte)RoomParameterCode.RoomID, roomID },
                { (byte)ParameterCode.TexasStatus, JsonConvert.SerializeObject(texasStatus) },
            };

            serverPeer.SendMessage(new Response((byte)TexasOperationCode.Status, null, param, "Texas Status Success", (int)RoomErrorCode.OK));

            return(true);
        }
예제 #8
0
        public void Initialize(IServerPeer peer)
        {
            serverPeer = peer;

            var networkConfiguration = NetworkConfiguration.GetInstance();

            operationRequestSender = new OperationRequestSender <TOperationCode>(serverPeer.OperationRequestSender, networkConfiguration.LogOperationsRequest);
            subscriptionProvider   = new OperationResponseSubscriptionProvider <TOperationCode>(serverPeer.OperationResponseNotifier, OnOperationRequestFailed,
                                                                                                networkConfiguration.LogOperationsResponse);
            eventHandlerRegister = new EventHandlerRegister <TEventCode>(serverPeer.EventNotifier, networkConfiguration.LogEvents);
        }
예제 #9
0
        public override bool OnHandleMessage(IMessage message, IServerPeer sPeer)
        {
            Log.DebugFormat("OnHandleMessage:TexasStartGameHandler");

            var errorParam = new Dictionary <byte, object>
            {
                { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
                { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] },
            };

            var serverPeer = sPeer as ServerOutbouncePeer;
            var operation  = new StartGameOperator(serverPeer.Protocol, message);


            if (!operation.IsValid)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.StartGame, null, errorParam, operation.GetErrorMessage(), (int)RoomErrorCode.OperationInvalid));
                return(false);
            }
            var peerId = new Guid((Byte[])operation.PeerId);

            var roomID = operation.RoomID;

            errorParam.Add((byte)RoomParameterCode.RoomID, roomID);
            var facade = TexasApplication.GetRoom(roomID);

            if (facade == null)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.StartGame, null, errorParam, "RoomId Not Found", (int)RoomErrorCode.RoomIdNotFound));
                return(false);
            }
            var userItem = TexasApplication.GetRoomUser(roomID, peerId);

            if (userItem == null)
            {
                serverPeer.SendMessage(new Response((byte)TexasOperationCode.StartGame, null, errorParam, "User Not Yet Join Room", (int)RoomErrorCode.UserNotYetJoinRoom));
                return(false);
            }

            // check 4 man
            string         json           = operation.TexasStartGame;
            TexasStartGame texasStartGame = JsonConvert.DeserializeObject <TexasStartGame>(json) as TexasStartGame;

            // Log.DebugFormat("TEXAS GAME VALIDATE");

            // var stateMachine = facade.RetrieveMediator(TexasStateMachine.NAME) as TexasStateMachine;

            var stateMachine = facade.RetrieveMediator(TexasStateMachine.NAME) as TexasStateMachine;

            stateMachine.StartGame(texasStartGame);
            return(true);
        }
예제 #10
0
        public bool AddServer(ServerType type, IServerPeer serverPeer)
        {
            if (PeerList.ContainsKey(type))
            {
                return(false);
            }
            else
            {
                PeerList.Add(type, serverPeer);

                return(true);;
            }
        }
        private async Task <bool> ConnectToFirstWorking(List <IPEndPoint> endpoints)
        {
            bool connected = false;

            while (!connected && endpoints.Count > 0)
            {
                IPEndPoint endpoint = null;
                try
                {
                    endpoint     = endpoints.Last();
                    _mainChannel = await _node.ConnectToServer(endpoint);

                    connected = true;
                }
                catch (Exception)
                {
                    endpoints.RemoveAt(endpoints.Count - 1);
                    MessageBox.Show("Can't connect to " + endpoint);
                }
            }

            return(connected);
        }
        public override bool OnHandleMessage(IMessage message, IServerPeer serverPeer)
        {
            if (base.OnHandleMessage(message, serverPeer))
            {
                Log.InfoFormat("CreateRoomTexasHandler :: " + roomItem.Name);

                //create room handle in application
                if (TexasApplication.CreateRoom(ServerApplication, roomItem))
                {
                    var param = new Dictionary <byte, object>()
                    {
                        { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
                        { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] },
                        { (byte)RoomParameterCode.Item, JsonConvert.SerializeObject(roomItem) },
                        { (byte)ClientParameterCode.ModuleId, message.Parameters[(byte)ClientParameterCode.ModuleId] },
                    };
                    var responseMessage = new Response((byte)RoomOperationCode.Create, null, param, "Room Create Success", (byte)ErrorCode.OK);
                    serverPeer.SendMessage(responseMessage);
                }
            }

            return(true);
        }
        public override bool OnHandleMessage(IMessage message, IServerPeer sPeer)
        {
            Log.DebugFormat("TexasBuyinSuccessHandler");

            var serverPeer = sPeer as ServerOutbouncePeer;

            var operation = new BuyinSuccessOperator(serverPeer.Protocol, message);

            if (!operation.IsValid)
            {
                return(false);
            }

            var roomId = operation.RoomID;
            var peerId = new Guid((Byte[])operation.PeerId);
            var amount = operation.Amount;

            var facade = GameApplicationBase.GetRoom(roomId);

            if (facade == null)
            {
                return(false);
            }

            var userItem = GameApplicationBase.GetRoomUser(roomId, peerId);

            if (userItem == null)
            {
                return(false);
            }

            Log.DebugFormat("TexasBuyinSuccessHandler Good :: " + peerId + " " + userItem.UserKey);

            var userKey = userItem.UserKey;

            var texasModel = facade.RetrieveProxy(TexasModel.NAME) as TexasModel;

            Slot player = texasModel.getSlot(userKey);

            if (player != null)
            {
                player.CurrentCredit += amount;
                Dictionary <byte, object> param = new Dictionary <byte, object>()
                {
                    { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
                    { (byte)ServerParameterCode.SubOperationCode, message.Parameters[(byte)ServerParameterCode.SubOperationCode] },
                    { (byte)RoomParameterCode.RoomID, roomId },
                    { (byte)RoomParameterCode.WithdrawAmount, amount },
                    { (byte)RoomParameterCode.GameCredit, player.CurrentCredit },

                    { (byte)RoomParameterCode.RoomUser, JsonConvert.SerializeObject(userItem) }
                };
                serverPeer.SendMessage(new Response((byte)RoomOperationCode.Withdraw, null, param, "Buyin Success", (int)RoomErrorCode.OK));

                TexasBuyin texasBuyIn = new TexasBuyin();
                texasBuyIn.BuyinInfo           = new BuyinInfo();
                texasBuyIn.BuyinInfo.AvatarKey = player.AvatarKey;
                texasBuyIn.BuyinInfo.Credit    = player.CurrentCredit;
                texasBuyIn.BuyinInfo.Index     = texasModel.getSlotIndex(player);
                texasBuyIn.BuyinInfo.UserKey   = player.UserKey;
                texasBuyIn.BuyinInfo.UserName  = player.UserName;

                param = new Dictionary <byte, object>()
                {
                    { (byte)ParameterCode.TexasBuyin, JsonConvert.SerializeObject(texasBuyIn) },
                };

                var eevent = new Event((byte)TexasEventCode.BuyIn, null, param);

                facade.SendNotification(RoomNotification.SEND_EVENT_TO_ALL_USER, eevent);
            }
            return(true);
        }
        private async void btnJoin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_chatRoomServiceProxy != null)
                {
                    _chatRoomServiceProxy.Leave();
                }
                JoinRoomResponse response = await _chatServiceProxy.JoinOrCreateRoom(cbChatRooms.Text);
                if (_chatChannelDisconnectionSubscription != null)
                {
                    _chatChannelDisconnectionSubscription.Dispose();
                }
                _chatChannel = await _node.ConnectToServer(response.ServerEndpoint);
                _chatRoomServiceProxy = await _chatChannel.GetProxy<IChatRoomService>(response.RoomActorId);
                _chatChannel.SetHandler<IChatRoomServiceCallback>(this);
                _chatChannelDisconnectionSubscription = _chatChannel.DisconnectedEvent.Subscribe((_) => MessageBox.Show("Disconnected from chat room, try connect again"));
                var content = await _chatRoomServiceProxy.Join(response.Ticket);
                txtChat.Clear();
                foreach (var msg in content)
                    txtChat.AppendText(msg + "\n");

            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
        private async Task<bool> ConnectToFirstWorking(List<IPEndPoint> endpoints)
        {
            bool connected = false;
            while (!connected && endpoints.Count > 0)
            {
                IPEndPoint endpoint = null;
                try
                {
                    endpoint = endpoints.Last();
                    _mainChannel = await _node.ConnectToServer(endpoint);
                    connected = true;
                }
                catch (Exception)
                {
                    endpoints.RemoveAt(endpoints.Count - 1);
                    MessageBox.Show("Can't connect to " + endpoint);
                }
            }

            return connected;
        }
예제 #16
0
        public override bool OnHandleMessage(IMessage message, IServerPeer sPeer)
        {
            Log.DebugFormat("TexasDepositSuccessHandler");


            var serverPeer = sPeer as ServerOutbouncePeer;

            var operation = new DepositSuccessOperator(serverPeer.Protocol, message);

            if (!operation.IsValid)
            {
                Log.DebugFormat("TexasDepositSuccessHandler no Good :: " + operation.GetErrorMessage());


                return(false);
            }

            var roomId = operation.RoomID;
            var peerId = new Guid((Byte[])operation.PeerId);
            var amount = operation.Amount;

            var facade = GameApplicationBase.GetRoom(roomId);

            if (facade == null)
            {
                return(false);
            }

            var userItem = GameApplicationBase.GetRoomUser(roomId, peerId);

            if (userItem == null)
            {
                Log.DebugFormat("TexasDepositSuccessHandler user not found ");
                return(false);
            }

            Log.DebugFormat("TexasDepositSuccessHandler Good :: " + peerId + " " + userItem.UserKey);

            var userKey = userItem.UserKey;

            var texasModel = facade.RetrieveProxy(TexasModel.NAME) as TexasModel;

            Slot player = texasModel.getSlot(userKey);

            if (player != null)
            {
                player.CurrentCredit -= amount;
            }

            var param = new Dictionary <byte, object>()
            {
                { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
                { (byte)ServerParameterCode.SubOperationCode, message.Parameters[(byte)ServerParameterCode.SubOperationCode] },
                { (byte)RoomParameterCode.RoomID, roomId },
                { (byte)RoomParameterCode.DepositAmount, amount },
                { (byte)RoomParameterCode.GameCredit, player.CurrentCredit },

                { (byte)RoomParameterCode.RoomUser, JsonConvert.SerializeObject(userItem) }
            };

            serverPeer.SendMessage(new Response((byte)RoomOperationCode.Deposit, null, param, "Deposit Success", (int)RoomErrorCode.OK));
            return(true);
        }
        public override bool OnHandleMessage(IMessage message, IServerPeer sPeer)
        {
            Log.DebugFormat("TEXASWithdrawRoomHandler  ");
            var serverPeer = sPeer as ServerOutbouncePeer;
            var errorParam = new Dictionary <byte, object>
            {
                { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
                { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] },
            };

            var operation = new BuyinSuccessOperator(serverPeer.Protocol, message);

            if (!operation.IsValid)
            {
                return(false);
            }

            var roomId = operation.RoomID;
            var peerId = new Guid((Byte[])operation.PeerId);
            var amount = operation.Amount;

            errorParam.Add((byte)RoomParameterCode.RoomID, roomId);

            Log.DebugFormat("TexasBuyinSuccessHandler: " + roomId + "|" + peerId + "|" + amount);

            var facade = GameApplicationBase.GetRoom(roomId);

            if (facade == null)
            {
                return(false);
            }

            var userItem = GameApplicationBase.GetRoomUser(roomId, peerId);

            if (userItem == null)
            {
                return(false);
            }

            Log.DebugFormat("TexasBuyinSuccessHandler Good :: " + peerId + " " + userItem.UserKey);

            var userKey = userItem.UserKey;

            var texasModel = facade.RetrieveProxy(TexasModel.NAME) as TexasModel;

            Slot player = texasModel.getSlot(userKey);

            if (player == null)
            {
                serverPeer.SendMessage(new Response((byte)RoomOperationCode.Withdraw, null, errorParam, "Player not yet on table", (int)TexasRoomErrorCode.UserNotYetOnTable));
                return(false);
            }
            if (player.isOnGame)
            {
                serverPeer.SendMessage(new Response((byte)RoomOperationCode.Withdraw, null, errorParam, "Player is playing", (int)TexasRoomErrorCode.UserPlaying));
                return(false);
            }

            var texasRoomItem = TexasApplication.GetRoomItem(roomId);

            if (player.CurrentCredit > 30 * texasRoomItem.WithdrawMax / 100)
            {
                serverPeer.SendMessage(new Response((byte)RoomOperationCode.Withdraw, null, errorParam, "Credit is not yet low to withdraw", (int)TexasRoomErrorCode.CreditNotYetLow));
                return(false);
            }
            return(base.OnHandleMessage(message, sPeer));

            /*
             * Log.DebugFormat("WithdrawRoomHandler  ");
             *
             *
             * var serverPeer = sPeer as ServerOutbouncePeer;
             *
             *
             * var errorParam = new Dictionary<byte, object>
             * {
             *  { (byte)ServerParameterCode.PeerId, message.Parameters[(byte)ServerParameterCode.PeerId] },
             *  { (byte)ClientParameterCode.SubOperationCode,message.Parameters[(byte)ClientParameterCode.SubOperationCode]},
             * };
             *
             *
             * var operation = new WithdrawRoomOperator(serverPeer.Protocol, message);
             * if (!operation.IsValid)
             * {
             *
             *  serverPeer.SendMessage(new Response((byte)RoomOperationCode.Withdraw, null, errorParam, operation.GetErrorMessage(), (int)RoomErrorCode.OperationInvalid));
             *  return false;
             * }
             *
             * var roomID = operation.RoomID;
             * var peerId = new Guid((Byte[])operation.PeerId);
             * var amount = operation.Amount;
             * var serverCode = message.Parameters[(byte)ServerParameterCode.SubOperationCode];
             *
             * errorParam.Add((byte)RoomParameterCode.RoomID, roomID);
             *
             * if (amount < 1)
             * {
             *  serverPeer.SendMessage(new Response((byte)RoomOperationCode.Withdraw, null, errorParam, "Amount Withdraw too Low", (int)RoomErrorCode.AmountTooLow));
             *  return false;
             * }
             *
             *
             * var facade = GameApplicationBase.GetRoom(roomID);
             *
             * if (facade == null)
             * {
             *  serverPeer.SendMessage(new Response((byte)RoomOperationCode.Withdraw, null, errorParam, "RoomId Not Found", (int)RoomErrorCode.RoomIdNotFound));
             *  return false;
             *
             * }
             *
             * var userItem = GameApplicationBase.GetRoomUser(roomID, peerId);
             *
             * if (userItem == null)
             * {
             *  serverPeer.SendMessage(new Response((byte)RoomOperationCode.Withdraw, null, errorParam, "User Not Yet Join Room", (int)RoomErrorCode.UserNotYetJoinRoom));
             *  return false;
             * }
             *
             * Log.DebugFormat("WithdrawRoomHandler Good :: " + peerId);
             *
             * var param = new Dictionary<byte, object>
             * {
             *  { (byte)ServerParameterCode.SubOperationCode, serverCode },
             *  { (byte)ServerParameterCode.PeerId, peerId.ToByteArray() },
             *  { (byte)RoomParameterCode.RoomID, roomID },
             *  { (byte)RoomParameterCode.WithdrawAmount, amount },
             *
             * };
             *
             * //send message back to proxy server
             * serverPeer.SendMessage(new Request((byte)ServerOperationCode.Withdraw, null, param));
             *
             * return true;
             */
        }
예제 #18
0
 private void OnNewServerPeer(Server arg1, IServerPeer arg2)
 {
     NewServerPeer?.Invoke(arg1, arg2);
 }