コード例 #1
0
        // Input: Two players
        // Output: the game Id which identifies the key of the dictionary of the game between the two players

        public InitGameResult InitGame(string player1, string player2)
        {
            int    gameId = 0;
            Thread t      = new Thread(() => { gameId = initGameThread(player1, player2); });

            t.Start();
            t.Join();
            cs.updateUserStatus(player1, USER_STATUS.PLAYING);
            cs.updateUserStatus(player2, USER_STATUS.PLAYING);
            InitGameResult res = new InitGameResult();

            res.Player1 = player1;
            res.Player2 = player2;
            res.gameId  = gameId;
            return(res);
        }
コード例 #2
0
        private int initGameThread(string player1, string player2)
        {
            IConnectFourServiceCallback player1CallBack = null;
            IConnectFourServiceCallback player2CallBack = null;

            foreach (KeyValuePair <string, IConnectFourServiceCallback> client in clients)
            {
                if (client.Key == player1)
                {
                    player1CallBack = client.Value;
                }

                if (client.Key == player2)
                {
                    player2CallBack = client.Value;
                }
            }

            if (player1CallBack == null)
            {
                throwUserNotFoundFault(player1);
            }
            else if (player2CallBack == null)
            {
                throwUserNotFoundFault(player2);
            }


            currentGames.Add(currentGameId, initPlayingGame(player1, player2, player1CallBack, player2CallBack));
            //Send to player2 the gameId, player1 will get the gameId from function return value
            InitGameResult game = new InitGameResult();

            game.Player1 = player1;
            game.Player2 = player2;
            game.gameId  = currentGameId;
            player2CallBack.sendGameInfo(game);

            return(currentGameId++);
        }