Exemple #1
0
 public Game(NetServer server, Players players)
 {
     this.players       = players;
     this.board         = new ServerBoard();
     this.state         = new AwaitConnectionState(this);
     this.server        = server;
     this.packetfactory = new PacketFactoryFlyWheel();
 }
Exemple #2
0
 public Game()
 {
     this.players       = new Players();
     this.board         = new ServerBoard();
     this.state         = new AwaitConnectionState(this);
     this.server        = null;
     this.packetfactory = new PacketFactoryFlyWheel();
 }
Exemple #3
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");
        }