Example #1
0
 public TractorHost()
 {
     CurrentGameState = new GameState();
     CurrentHandState = new CurrentHandState(this.CurrentGameState);
     CardsShoe        = new CardsShoe();
     PlayersProxy     = new Dictionary <string, IPlayer>();
 }
Example #2
0
        public void ShuffleCardsTestWithRNGCsp()
        {
            GameRoom  gameRoom = new GameRoom(0, "test", null);
            CardsShoe cardShoe = new CardsShoe();

            cardShoe.Cards = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[][] stats = new int[10][];
            for (int i = 0; i < 10; i++)
            {
                stats[i] = new int[10];
            }

            int testCount = 1000;

            for (int x = 0; x < testCount; x++)
            {
                int N = cardShoe.Cards.Length;
                for (int i = 0; i < N; i++)
                {
                    int r = CommonMethods.RandomNext(i + 1);
                    gameRoom.Swap(cardShoe.Cards, i, r);
                }

                for (int i = 0; i < 10; i++)
                {
                    stats[cardShoe.Cards[i]][i]++;
                }
            }

            for (int i = 0; i < 10; i++)
            {
                System.Console.Out.WriteLine("{0}: {1}", i, string.Join(",", stats[i]));
            }
        }
Example #3
0
        public TractorHost()
        {
            gameConfig = new GameConfig();
            var myreader = new AppSettingsReader();

            try
            {
                MaxRoom                = (int)myreader.GetValue(KeyMaxRoom, typeof(int));
                AllowSameIP            = (bool)myreader.GetValue(KeyAllowSameIP, typeof(bool));
                gameConfig.IsFullDebug = (bool)myreader.GetValue(KeyIsFullDebug, typeof(bool));
            }
            catch (Exception ex)
            {
                log.Debug(string.Format("reading config {0} failed with exception: {1}", KeyAllowSameIP, ex.Message));
            }

            CardsShoe    = new CardsShoe();
            PlayerToIP   = new Dictionary <string, string>();
            PlayersProxy = new Dictionary <string, IPlayer>();
            GameRooms    = new List <GameRoom>();
            RoomStates   = new List <RoomState>();
            for (int i = 0; i < this.MaxRoom; i++)
            {
                GameRoom gameRoom = new GameRoom(i, RoomNames[i], this);
                RoomStates.Add(gameRoom.CurrentRoomState);
                GameRooms.Add(gameRoom);
                if (!Directory.Exists(gameRoom.LogsByRoomFullFolder))
                {
                    Directory.CreateDirectory(gameRoom.LogsByRoomFullFolder);
                }
            }
            SessionIDGameRoom = new Dictionary <string, GameRoom>();

            // setup logger
            AppDomain currentDomain = default(AppDomain);

            currentDomain = AppDomain.CurrentDomain;
            // Handler for unhandled exceptions.
            currentDomain.FirstChanceException += GlobalFirstChanceExceptionHandler;
            // Handler for exceptions in threads behind forms.
            System.Windows.Forms.Application.ThreadException += GlobalThreadExceptionHandler;
        }
Example #4
0
        public void CutCardsTest()
        {
            GameRoom  gameRoom = new GameRoom(0, "test", null);
            CardsShoe cardShoe = new CardsShoe();

            cardShoe.Cards = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int cutPoint = 0;

            gameRoom.CutCards(cardShoe, cutPoint);
            int[] expectedCards = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            CollectionAssert.AreEqual(expectedCards, cardShoe.Cards);

            cardShoe.Cards = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            cutPoint       = 1;
            gameRoom.CutCards(cardShoe, cutPoint);
            expectedCards = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            CollectionAssert.AreEqual(expectedCards, cardShoe.Cards);

            cardShoe.Cards = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            cutPoint       = 5;
            gameRoom.CutCards(cardShoe, cutPoint);
            expectedCards = new int[] { 5, 6, 7, 8, 9, 0, 1, 2, 3, 4 };
            CollectionAssert.AreEqual(expectedCards, cardShoe.Cards);

            cardShoe.Cards = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            cutPoint       = 7;
            gameRoom.CutCards(cardShoe, cutPoint);
            expectedCards = new int[] { 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
            CollectionAssert.AreEqual(expectedCards, cardShoe.Cards);

            cardShoe.Cards = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            cutPoint       = 10;
            gameRoom.CutCards(cardShoe, cutPoint);
            expectedCards = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            CollectionAssert.AreEqual(expectedCards, cardShoe.Cards);
        }
Example #5
0
        public TractorHost()
        {
            gameConfig = new GameConfig();
            var myreader = new AppSettingsReader();

            try
            {
                MaxRoom                = (int)myreader.GetValue(KeyMaxRoom, typeof(int));
                AllowSameIP            = (bool)myreader.GetValue(KeyAllowSameIP, typeof(bool));
                Webport                = (string)myreader.GetValue(KeyWebport, typeof(string));
                gameConfig.IsFullDebug = (bool)myreader.GetValue(KeyIsFullDebug, typeof(bool));
            }
            catch (Exception ex)
            {
                log.Debug(string.Format("reading config {0} failed with exception: {1}", KeyAllowSameIP, ex.Message));
            }

            CardsShoe    = new CardsShoe();
            PlayerToIP   = new Dictionary <string, string>();
            PlayersProxy = new Dictionary <string, IPlayer>();
            GameRooms    = new List <GameRoom>();
            RoomStates   = new List <RoomState>();
            for (int i = 0; i < this.MaxRoom; i++)
            {
                GameRoom gameRoom = new GameRoom(i, RoomNames[i], this);
                RoomStates.Add(gameRoom.CurrentRoomState);
                GameRooms.Add(gameRoom);
                if (!Directory.Exists(gameRoom.LogsByRoomFullFolder))
                {
                    Directory.CreateDirectory(gameRoom.LogsByRoomFullFolder);
                }
            }
            SessionIDGameRoom = new Dictionary <string, GameRoom>();

            var threadStartHostWS = new Thread(() =>
            {
                var server = new WebSocketServer("ws://0.0.0.0:" + Webport);
                server.Start(socket =>
                {
                    socket.OnOpen  = () => Console.WriteLine("Open!");
                    socket.OnClose = () =>
                    {
                        Console.WriteLine("Close!");
                        var ip          = socket.ConnectionInfo.ClientIpAddress;
                        string playerID = getPlayIDByWSProxy(socket);
                        if (!string.IsNullOrEmpty(playerID))
                        {
                            this.handlePlayerDisconnect(playerID);
                        }
                    };
                    socket.OnMessage = (message) =>
                    {
                        WebSocketObjects.WebSocketMessage messageObj = CommonMethods.ReadObjectFromString <WebSocketObjects.WebSocketMessage>(message);
                        if (messageObj == null)
                        {
                            log.Debug(string.Format("failed to unmarshal WS message: {0}", message));
                            return;
                        }

                        var playerProxy = new PlayerWSImpl(socket);

                        if (messageObj.messageType == WebSocketObjects.WebSocketMessageType_PlayerEnterHall)
                        {
                            if (this.PlayersProxy.ContainsKey(messageObj.playerID))
                            {
                                string clientIP = socket.ConnectionInfo.ClientIpAddress;
                                if (this.PlayerToIP.ContainsValue(clientIP))
                                {
                                    playerProxy.NotifyMessage(new string[] { "之前非正常退出", "请重启游戏后再尝试进入大厅" });
                                    this.handlePlayerDisconnect(messageObj.playerID);
                                }
                                else
                                {
                                    playerProxy.NotifyMessage(new string[] { "玩家昵称重名", "请更改昵称后重试" });
                                }
                                return;
                            }
                            this.PlayersProxy.Add(messageObj.playerID, playerProxy);
                        }

                        WSMessageHandler(messageObj.messageType, messageObj.playerID, messageObj.content);
                    };
                });
            });

            threadStartHostWS.Start();

            // setup logger
            AppDomain currentDomain = default(AppDomain);

            currentDomain = AppDomain.CurrentDomain;
            // Handler for unhandled exceptions.
            currentDomain.FirstChanceException += GlobalFirstChanceExceptionHandler;
            // Handler for exceptions in threads behind forms.
            System.Windows.Forms.Application.ThreadException += GlobalThreadExceptionHandler;
        }