Exemplo n.º 1
0
        private static void ReadCallback(IAsyncResult ar)
        {
            Player player = ar.AsyncState as Player;

            try
            {
                int bytesRead = player.Socket.EndReceive(ar);
                Console.WriteLine("bytesRead = " + bytesRead.ToString());
                if (bytesRead > 0)
                {
                    byte[] tmp = new byte[bytesRead];
                    Array.Copy(player.buff, tmp, bytesRead);
                    GeneralistProto proto = GeneralistProto.Parser.ParseFrom(tmp);
                    LobbyManager.GetInstance().Treat(ref player, proto);
                    BeginRead(ref player);
                }
                else
                {
                    LobbyManager.GetInstance().DeletePlayer(ref player);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.ToString());
                LobbyManager.GetInstance().DeletePlayer(ref player);
            }
        }
Exemplo n.º 2
0
        public Contract getFromCmd(ref Player player, GeneralistProto proto)
        {
            int index = -1;

            for (int i = 0; i < _contracts.Length; ++i)
            {
                if (proto.Gamecmd.Value.Split(' ')[0].Equals(_contracts[i]))
                {
                    index = i;
                }
            }
            if (index == -1)
            {
                throw new Exception("Can't find your contract on the possibility");
            }
            int scale = Int32.Parse(proto.Gamecmd.Value.Split(' ')[1]);

            // DEBUG -- PRINT
            System.Console.Out.WriteLine("Contract: " + _contracts[index] + "value of " + scale);
            //

            Contract contract = new Contract((CoincheServer.Contract.ContractType)index, scale, 1, player.Team);

            return(contract);
        }
Exemplo n.º 3
0
        private void ChangingUsername(ref Player player, GeneralistProto proto)
        {
            string lastname = player.Name;

            player.Name = proto.Lobbycmd.Value;
            PlayerSession.BeginSend(ref player, "You succesfully change your name!!");
            Broadcast(lastname + " change his/her name to " + player.Name, ref player);
        }
Exemplo n.º 4
0
 public void Hand(GeneralistProto proto, ref Player player)
 {
     PlayerSession.BeginSend(ref player, "In your hand:");
     foreach (Card card in player.hand)
     {
         PlayerSession.BeginSend(ref player, Card._names[(int)card.face] + " " + Card._colors[(int)card.color] + " ");
     }
 }
Exemplo n.º 5
0
        private void FindLobbyAndTreat(ref Player player, GeneralistProto proto)
        {
            Lobby lobby = FindLobby(ref player);

            if (lobby == null)
            {
                PlayerSession.BeginSend(ref player, "You can't sent that type of cmd till now");
                return;
            }
            lobby.Treat(proto, ref player);
        }
Exemplo n.º 6
0
 private void Auth(ref Player player, GeneralistProto proto)
 {
     player.Name = proto.Auth.Name;
     player.Team = Team.None;
     players.Add(player);
     PlayerSession.BeginSend(ref player,
                             "#LIST - List all the available lobbies\n" +
                             "#JOIN [ChanName] - Join a channel\n" +
                             "#CREATE [ChanName] - Create a channel\n" +
                             "#USERNAME [NewName] - Change your username(in lobby)");
 }
Exemplo n.º 7
0
        private void LobbyCmd(ref Player player, GeneralistProto proto)
        {
            switch (proto.Lobbycmd.Cmd)
            {
            case CLobby.Types.Cmd.Team:
                JoinningTeam(ref player, proto);
                break;

            case CLobby.Types.Cmd.Username:
                ChangingUsername(ref player, proto);
                break;
            }
        }
Exemplo n.º 8
0
 private void LobbyJoinning(ref Player player, GeneralistProto proto)
 {
     foreach (var lobby in lobbies)
     {
         if (lobby.name.Equals(proto.Servercmd.Value))
         {
             if (lobby.AddPlayer(ref player))
             {
                 players.Remove(player);
             }
             return;
         }
     }
     PlayerSession.BeginSend(ref player, "The lobby you tried to join doesn't exist, type the command #LIST to see all the lobby");
 }
Exemplo n.º 9
0
        private void LobbyCreating(ref Player player, GeneralistProto proto)
        {
            foreach (var lobby in lobbies)
            {
                if (lobby.name.Equals(proto.Servercmd.Value))
                {
                    PlayerSession.BeginSend(ref player, "A lobby already exist with this name");
                    return;
                }
            }
            Lobby newlobby = new Lobby(proto.Servercmd.Value);

            PlayerSession.BeginSend(ref player, "You successfully great your lobby");
            lobbies.Add(newlobby);
            newlobby.AddPlayer(ref player);
        }
Exemplo n.º 10
0
        public void Treat(GeneralistProto proto, ref Player player)
        {
            switch (proto.Gamecmd.Cmd)
            {
            case CGame.Types.Cmd.Contract:
                Contract(proto, ref player);
                break;

            case CGame.Types.Cmd.Card:
                PlayCard(proto, ref player);
                break;

            case CGame.Types.Cmd.Hand:
                Hand(proto, ref player);
                break;
            }
        }
Exemplo n.º 11
0
        private void ServerCmd(ref Player player, GeneralistProto proto)
        {
            switch (proto.Servercmd.Cmd)
            {
            case CServer.Types.Cmd.Listing:
                LobbiesListing(ref player);
                break;

            case CServer.Types.Cmd.Join:
                LobbyJoinning(ref player, proto);
                break;

            case CServer.Types.Cmd.Create:
                LobbyCreating(ref player, proto);
                break;
            }
        }
Exemplo n.º 12
0
        private Boolean TeamFull(GeneralistProto proto)
        {
            int cpt = 0;

            foreach (var player in players)
            {
                if (proto.Lobbycmd.Team == player.Team)
                {
                    ++cpt;
                }
            }
            if (cpt == 2)
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 13
0
        public void Treat(ref Player player, GeneralistProto proto)
        {
            switch (proto.Type)
            {
            case CmdTarget.Authentification:
                Auth(ref player, proto);
                break;

            case CmdTarget.Servercmd:
                ServerCmd(ref player, proto);
                break;

            default:
                FindLobbyAndTreat(ref player, proto);
                break;
            }
        }
Exemplo n.º 14
0
        private void JoinningTeam(ref Player player, GeneralistProto proto)
        {
            if (player.Team == proto.Lobbycmd.Team)
            {
                PlayerSession.BeginSend(ref player, "You can't join a team, you are already in");
                return;
            }
            if (TeamFull(proto))
            {
                PlayerSession.BeginSend(ref player, "The time you are trying to joi is already full.");
                return;
            }
            player.Team = proto.Lobbycmd.Team;
            PlayerSession.BeginSend(ref player, "You join the team you wanted");

            if (IsGameLaunchable())
            {
                game = new Game(ref players);
            }
        }
Exemplo n.º 15
0
        public Card GetCard(GeneralistProto proto, ref Player player)
        {
            Card card  = null;
            int  face  = -1;
            int  color = -1;

            for (int i = 0; i < Card._names.Length; ++i)
            {
                if (proto.Gamecmd.Value.Split(' ')[0].Equals(CoincheServer.Card._names[i]))
                {
                    face = i;
                }
            }
            for (int i = 0; i < Card._colors.Length; ++i)
            {
                if (proto.Gamecmd.Value.Split(' ')[1].Equals(CoincheServer.Card._colors[i]))
                {
                    color = i;
                }
            }
            if (face == -1 || color == -1)
            {
                throw new Exception("You're card doesn't exist, retype it correctly please");
            }

            foreach (Card pcard in player.hand)
            {
                if (pcard.face == (Card.Face)face && pcard.color == (Card.Color)color)
                {
                    card = pcard;
                }
            }
            if (card == null)
            {
                throw new Exception("You doesn't have this card");
            }
            return(card);
        }
Exemplo n.º 16
0
        public void Treat(GeneralistProto proto, ref Player player)
        {
            GameStatus();
            switch (proto.Type)
            {
            case CmdTarget.Chat:
                Broadcast(proto.Chat.Msg, ref player);
                break;

            case CmdTarget.Lobbycmd:
                LobbyCmd(ref player, proto);
                break;

            case CmdTarget.Gamecmd:
                if (this.game == null)
                {
                    PlayerSession.BeginSend(ref player, "You can't send game order, because the game is not running atm");
                    return;
                }
                game.Treat(proto, ref player);
                break;
            }
        }
Exemplo n.º 17
0
        public void PlayCard(GeneralistProto proto, ref Player player)
        {
            Card card;

            if (this.isWaited != CGame.Types.Cmd.Card)
            {
                PlayerSession.BeginSend(ref player, "It's not the time to put a contract up.");
                return;
            }
            if (!HisTurn(ref player))
            {
                PlayerSession.BeginSend(ref player, "Please wait your turn to do a contract.");
                return;
            }

            try {
                card = GetCard(proto, ref player);
            } catch (Exception e) {
                PlayerSession.BeginSend(ref player, e.Message);
                return;
            }

            foreach (PlayedCard toplay in turn)
            {
                if (player.Name.Equals(toplay.player.Name))
                {
                    toplay.card = card;
                }
            }

            Broadcast(player.Name + " played: " + Card._names[(int)card.face] + " " + Card._colors[(int)card.color]);
            player.hand.Remove(card);

            Player tmp;

            if (hasToPlay < 3)
            {
                hasToPlay += 1;
                tmp        = GetToPlay();
                PlayerSession.BeginSend(ref tmp, "It's your turn to play a card.");
            }
            else
            {
                hasToPlay = 0;
                ConcludeTurn();
                if (blue.player1.hand.Count == 0)
                {
                    Broadcast("This hand is over, a new one will begin soon");
                    Reset();
                    if (IsOver())
                    {
                        return;
                    }
                    Broadcast("Your card has been redistribute");
                    isWaited = CGame.Types.Cmd.Contract;
                    return;
                }
                tmp = GetToPlay();
                PlayerSession.BeginSend(ref tmp, "It's your turn to play a card");
            }
        }
Exemplo n.º 18
0
        public void Contract(GeneralistProto proto, ref Player player)
        {
            if (this.isWaited != CGame.Types.Cmd.Contract)
            {
                PlayerSession.BeginSend(ref player, "It's not the time to put a contract up.");
                return;
            }
            if (!HisTurn(ref player))
            {
                PlayerSession.BeginSend(ref player, "Please wait your turn to do a contract.");
                return;
            }
            PlayerSession.BeginSend(ref player, "We are taking into count your contract");
            try {
                Contract contract = getFromCmd(ref player, proto);
                try {
                    NewContract(ref player, ref contract);
                    Broadcast(player.Name + " has put a contract on " + _contracts[(int)contract.type] + " of value " + contract.scale);
                } catch (Exception e) {
                    PlayerSession.BeginSend(ref player, e.Message);
                    return;
                }
            } catch (Exception e) {
                System.Console.Error.WriteLine(e.Message);
                if (proto.Gamecmd.Value.Split(' ')[0].Equals("Coinche"))
                {
                    if (_contract == null)
                    {
                        PlayerSession.BeginSend(ref player, "You can't put a coinche, if there is not existing contract");
                        return;
                    }
                    if (_contract.team == player.Team && _contract.coinche != 2)
                    {
                        PlayerSession.BeginSend(ref player, "You can't put a coinche on your team contract");
                        return;
                    }
                    _contract.coinche *= 2;
                    Broadcast(player.Name + " has put a coinche on the actual contract");
                }
                else if (proto.Gamecmd.Value.Split(' ')[0].Equals("Pass"))
                {
                    Broadcast(player.Name + " has pass this turn");
                }
                else
                {
                    PlayerSession.BeginSend(ref player, "Unrecognized contract");
                    return;
                }
            }
            Player tmp;


            if (hasToPlay < 3)
            {
                hasToPlay += 1;
                tmp        = GetToPlay();
                PlayerSession.BeginSend(ref tmp, "It's your turn to put a contract.");
            }
            else
            {
                if (_contract == null)
                {
                    NextTurn();
                    return;
                }
                hasToPlay = 0;
                Broadcast("The contract turn is now over, first turn of game can start");
                tmp = GetToPlay();
                PlayerSession.BeginSend(ref tmp, "It's your turn to play a card");
                isWaited = CGame.Types.Cmd.Card;
            }
        }