/// <summary>
 /// To create a game of given type.
 /// </summary>
 /// <param name="type">game type.</param>
 /// <returns>a game instance</returns>
 public static IGame CreateGame(EnumGameType type)
 {
     IGame game = null;
     switch (type) {
         case EnumGameType.Single:
         case EnumGameType.Multiple:
             game = new LocalGame(type, null); // assign the container later
             break;
         case EnumGameType.Host:
             game = new ServerGame();
             break;
         case EnumGameType.Client:
             game = new ClientGame();
             break;
         default:
             throw new GameException("This type of game is not implmented.", null);
     }
     return game;
 }
Exemple #2
0
        private void StartHostGame(List<Player> players, string hostGameInfo)
        {
            // host game info format : name={0},ip={1},port={2},max_players={3}
            string[] info = hostGameInfo.Split(new char[] { ',', '=' });

            Debug.Assert(info.Length == 8 && players != null && players.Count > 0
                && !string.IsNullOrEmpty(hostGameInfo));

            int port = Convert.ToInt32(info[5]);
            int max = Convert.ToInt32(info[7]);
            Debug.Assert(max >= 2 && max < GameSetting.LIMITED_MAX_PLAYERS);

            if (null != m_serverGame)
                m_serverGame.Dispose();
            m_serverGame = GameFactory.CreateGame(EnumGameType.Host) as ServerGame;
            InitServerCommands();
            m_serverGame.PlayerJoined += new EventHandler<PlayerEventArgs>(ServerGame_PlayerJoined);
            m_serverGame.PlayerLeaved += new EventHandler<PlayerEventArgs>(ServerGame_PlayerLeaved);
            m_serverGame.PlayerPrepared += new EventHandler<PlayerEventArgs>(ServerGame_PlayerPrepared);
            m_serverGame.ServicePort = port;
            m_serverGame.MaxPlayers = max;
            m_serverGame.StartService();

            // A host is also a client when hosting a server game.
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(info[3]), port);
            if (null != m_game)
                m_game.Dispose();
            ClientGame game = (m_game = GameFactory.CreateGame(EnumGameType.Client) as TetrisGame) as ClientGame;
            game.Joined += new EventHandler<PlayerEventArgs>(ClientGame_Joined);
            game.Left += new EventHandler<PlayerEventArgs>(ClientGame_Left);
            game.Left += new EventHandler<PlayerEventArgs>(ClientGame_Left);
            game.PlayerJoined += new EventHandler<PlayerEventArgs>(ClientGame_PlayerJoined);
            game.GameElapsed += new EventHandler(ClientGame_GameElapsed);
            game.Container = m_gameContainer;

            Player player = players[0]; // first player as local player
            player.Name = info[1];
            player.HostName = Dns.GetHostName();
            player.PlayFiled = new PlayPanel(true);
            this.InitPlayer(player);

            game.AddPlayer(player);
            game.Connect("localhost", port);

            this.Text = this.Text + " - Server";
            this.Refresh();

            //WaitForClientForm dlg = new WaitForClientForm();
            //DialogResult result = dlg.ShowDialog();
            //if (result == DialogResult.OK) {
            //    m_serverGame.Start();
            //} else {
            //    game.Disconnect();
            //    m_serverGame.Stop();
            //    game.Dispose();
            //    m_serverGame.Dispose();
            //    m_game = null;
            //    m_serverGame = null;
            //    return;
            //}
        }
Exemple #3
0
 private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
 {
     if (null != m_game) {
         m_game.Dispose();
         m_game = null;
     }
     if (null != m_serverGame) {
         m_serverGame.Dispose();
         m_serverGame = null;
     }
 }