Exemplo n.º 1
0
        protected WaitMove(Game game, Ownership player) : base(game)
        {
            // TODO: ADD "IF DEBUG"
            Console.WriteLine("Enter WaitMove on {0}", player.ToString());

            this.player = player;

            game.SendBoardToUsers();

            // Let player know we are waiting
            var this_player_message = game.CreateStatusMessage(ClientGameStates.YourMove, info: "");

            game.server.SendMessage(this_player_message, game.players.GetConnection(player),
                                    NetDeliveryMethod.ReliableOrdered);

            // Let other player know what's going on
            var other_player_message = game.CreateStatusMessage(ClientGameStates.WaitOtherPlayerMove, info: "");

            game.server.SendMessage(other_player_message, game.players.GetConnection(Players.OtherPlayer(player)),
                                    NetDeliveryMethod.ReliableOrdered);

            game.server.FlushSendQueue();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            bool debug = true;

            var config = new NetPeerConfiguration("StrategoLAN");
            var client = new NetClient(config);

            client.Start();
            client.Connect(host: "127.0.0.1", port: 11112);
            Console.WriteLine("Connected!");

            var board = new ClientBoard();
            // is able to recycle incomming packets
            var factory = new PacketFactoryFlyWheel();
            // basic factory creating outgoing packets without any recycling (thread-safe)
            var static_packet_factory = new StaticPacketFactory();
            NetIncomingMessage message;
            var       game_started = false;
            Ownership this_player  = Ownership.Board;


            // TODO: while(true) Should be replaced with Application.Idle handler!
            while (true)
            {
                if ((message = client.ReadMessage()) != null)
                {
                    if (debug)
                    {
                        Console.WriteLine(String.Format("Message: {0}", message.ToString()));
                        Console.WriteLine(String.Format("Data: {0}", message.PeekString()));
                    }


                    if (message.MessageType == NetIncomingMessageType.Data)
                    {
                        Packet packet = factory.ReadNetMessage(message);

                        // TODO: ADD IF DEBUG
                        Console.WriteLine("Got data packet: {0}", packet.ToString());

                        switch (packet)
                        {
                        case BoardPacket bp:
                            if (!game_started)
                            {
                                board.SetBoardFromStringArray(bp.BoardState, force_creation: true);
                            }
                            else if (!board.SetBoardFromStringArray(bp.BoardState))
                            {
                                Console.WriteLine("Error loading new board from server");
                            }
                            board.PrintBoard();
                            break;

                        case ServerToClientGameStatusUpdatePacket sp:

                            //TODO: add "if debug
                            Console.WriteLine("Got status message: {0}, {1}", sp.state.ToString(), sp.info);

                            switch (sp.state)
                            {
                            case ClientGameStates.InitialConnection when sp.info == Ownership.FirstPlayer.ToString() || sp.info == Ownership.SecondPlayer.ToString():
                                if (this_player == Ownership.Board)
                                {
                                    this_player = sp.info == Ownership.FirstPlayer.ToString()
                                                ? Ownership.FirstPlayer
                                                : Ownership.SecondPlayer;
                                    Console.WriteLine("You are connected as {0}", this_player.ToString());
                                }
                                else
                                {
                                    // TODO: ADD "IF VERBOSE"
                                    Console.WriteLine("unexpected Initial Conecnction. signed as {0}, received confirm for {1}", this_player.ToString(), sp.info);
                                }

                                break;

                            case ClientGameStates.YourMove when sp.info == "":
                            case ClientGameStates.Error when sp.info == "Invalid move. try again":
                                game_started = true;
                                // Ask player for move and pack it in a packet to send
                                UserGetMoveAndSend((AttemptMovePacket)static_packet_factory.GetPacketInstance(PacketHeader.AttemptMovePacket),
                                                   client);
                                break;

                            case ClientGameStates.Error when sp.info == "Starting game..":
                                game_started = true;
                                break;

                            case ClientGameStates.WaitForBoard
                                when sp.info == "Waiting for players to place their pieces on the board..":
                            case ClientGameStates.Error when sp.info == "Illegal board sent by user":
                            case ClientGameStates.Error
                                when sp.info == "Unexpected packet. Waiting for user to send board":
                                // Ask player to select initial location, pack it and send it
                                UserGetInitialBoardAndSend((BoardPacket)static_packet_factory.GetPacketInstance(PacketHeader.BoardPacket), client, this_player);
                                break;

                            case ClientGameStates.WaitForStart when sp.info != "Waiting for other player to press start..":
                                // Ask player to START, pack it and send it
                                UserGetStartSignalAndSend((PlayerReadyPacket)static_packet_factory.GetPacketInstance(PacketHeader.PlayerReady), client);
                                break;

                            case ClientGameStates.WaitOtherPlayerMove:
                                game_started = true;
                                Console.WriteLine("{0} : {1}", sp.state.ToString(), sp.info);
                                break;

                            default:
                                Console.WriteLine("{0} : {1}", sp.state.ToString(), sp.info);
                                break;
                            }
                            break;

                        default:
                            Console.WriteLine("Unexpected packet! {0}", packet.ToString());
                            break;
                        }
                    }
                }
                client.FlushSendQueue();

                // Console.WriteLine("sleep..");
                Thread.Sleep(100);
            }

            Console.WriteLine("wake up, close all..");
            client.Disconnect("Timed leave");
            client.Shutdown("Leave");
        }
Exemplo n.º 3
0
 public void SignOut(NetConnection user)
 {
     lock (_Players)
     {
         Ownership player = GetPlayerFromConnection(user);
         if (player == Ownership.Board)
         {
             Console.WriteLine("Can't Signout {0}, IP: {1}, No such user!", user.ToString(),
                               user.RemoteEndPoint.ToString());
         }
         else
         {
             _Players[(int)player - 1] = null;
             Console.WriteLine(String.Format("Signed out user {0} to Player {1}", user.ToString(), player.ToString()));
         }
     }
 }
Exemplo n.º 4
0
        // Should return a message
        public Ownership SignUp(NetConnection user)
        {
            Ownership ans = Ownership.Board;

            lock (_Players)
            {
                if (_Players.Contains(user))
                {
                    Console.Write("{0} AlreadyConnected and trying to SignUp again!", user.ToString());
                    ans = GetPlayerFromConnection(user);
                }
                else
                {
                    // Assign user with a Player ("FirstPlayer" etc)
                    int player_index = 0;
                    while (player_index < _Players.Length)
                    {
                        if (_Players[player_index] == null)
                        {
                            _Players[player_index] = user;
                            ans = (Ownership)(player_index + 1);
                            Console.WriteLine(String.Format("Signed user {0} to Player {1}", user.ToString(), ans.ToString()));
                            break;
                        }
                        player_index++;
                    }

                    if (player_index >= _Players.Length)
                    {
                        Console.WriteLine("Too many players connected! attempt by {0}, IP: {1}. Connection will be refused.", user.ToString(), user.RemoteEndPoint.ToString());
                        ans = Ownership.Board;
                    }
                }
            }

            return(ans);
        }