public string GetName()
 {
     return(_player.GetName());
 }
Beispiel #2
0
        private void StartReceiving()
        {
            try
            {
                while (sender.Connected)
                {
                    if (_configuration == "disconnect on connect B")
                    {
                        ForceDisconnect();
                    }

                    byte[] messageReceived = new byte[8192];
                    byte[] messageSent;
                    int    byteRecv = sender.Receive(messageReceived);
                    string message  = Encoding.ASCII.GetString(messageReceived, 0, byteRecv);

                    JArray requestArray = JsonConvert.DeserializeObject <JArray>(message);

                    switch (requestArray[0].ToObject <string>())
                    {
                    case "register":
                        if (_configuration == "disconnect on register")
                        {
                            ForceDisconnect();
                        }
                        if (_configuration == "send json array on register")
                        {
                            messageSent = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(new string[1] {
                                "json array"
                            }));
                            sender.Send(messageSent);
                            break;
                        }
                        //normal functionality:
                        string register = _player.Register(_name);
                        messageSent = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(register));
                        sender.Send(messageSent);
                        break;

                    case "receive-stones":
                        if (_configuration == "disconnect on receive stones")
                        {
                            ForceDisconnect();
                        }
                        _player.ReceiveStones(requestArray[1].ToObject <string>());
                        break;

                    case "make-a-move":
                        if (_configuration == "disconnect on make a move")
                        {
                            ForceDisconnect();
                        }
                        if (_configuration == "send json array on make a move")
                        {
                            messageSent = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(new string[1] {
                                "json array"
                            }));
                            sender.Send(messageSent);
                            break;
                        }
                        if (_configuration == "send json object on make a move")
                        {
                            JObject json = new JObject();
                            json.Add("invalid", "json");
                            messageSent = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json));
                            sender.Send(messageSent);
                            break;
                        }

                        //Normal functionality:
                        string move;
                        try
                        {
                            move = _player.MakeAMove(requestArray[1].ToObject <string[][][]>());
                        }
                        catch (PlayerException e)     //Board history is illegal
                        {
                            move = e.Message;
                        }
                        messageSent = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(move));
                        sender.Send(messageSent);
                        break;

                    case "GetStone":
                        string stone = _player.GetStone();
                        messageSent = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(stone));
                        sender.Send(messageSent);
                        break;

                    case "GetName":
                        string name = _player.GetName();
                        messageSent = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(name));
                        sender.Send(messageSent);
                        break;

                    default:
                        sender.Shutdown(SocketShutdown.Both);
                        sender.Close();
                        throw new PlayerClientException("Invalid operation sent to PlayerClient");
                    }
                }
            }

            catch (Exception)
            {
                //Console.WriteLine(e.Message, this);
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
                //Console.ReadLine();
                throw;
            }

            // Close Socket using the method Close()
            sender.Shutdown(SocketShutdown.Both);
            sender.Close();
        }