地主的權限傳遞就這麼多了,可能寫的比較亂,但是並不復雜,相信大家仔細多看幾遍應該能夠明白的.。如果那裡有錯誤的話歡迎在下面留言欄反饋。謝謝!
下面是server類裡面的監聽客戶端消息的程序:
(由於篇幅所限,這裡只貼出與地主權限傳遞有關的代碼)
public void AccpetClIEnt1Data() //監聽客戶端1的消息
{
NetworkStream Ns1 = clIEnt1.GetStream();
string str1 = "";
while (true)
{
PokerGroup pg = new PokerGroup();
byte[] bytes1 = new byte[108];
Ns1.Read(bytes1, 0, 108);
str1 = Encoding.Default.GetString(bytes1);
(省略部分)
if (str1.StartsWith("AreYouLandLord"))
{
if (DConsole.LandLordNum == 1) //如果默認地主為服務器,那麼根據文章開頭的權限傳遞順序圖,server-client2-client1,clIEnt1為最後一個玩家,所以需要和牌重新來過
{
DConsole.Restart();
continue;
}
DConsole.player1.areYouLandLord = true; //當默認地主不是服務器時,設置服務器的叫地主權限為true,當timer控件讀取到該值時,服務器的叫地主和不叫按鈕馬上就會顯示出來.
continue;
}
if (str1.StartsWith("IamLandLord")) //客戶端1為地主
{
DConsole.lblClIEnt1Name.Text += "(地主)";
DConsole.lblClIEnt1Name.ForeColor = System.Drawing.Color.Red;
SendDataForClient("ClIEntIsLandLord", 2); //通知客戶端2,客戶端1為地主
Thread.Sleep(sleep);
DConsole.PaintClIEnt(20, 1);
SendDataForClIEnt("LandLordPokers", DConsole.LandLordPokers, 1); //地主已選出,把牌發送給所有玩家
Thread.Sleep(sleep);
SendDataForClIEnt("LandLordPokers", DConsole.LandLordPokers, 2); //地主已選出,把牌發送給所有玩家
DConsole.player1.SelectLandLordEnd(); //該方法的具體代碼請看下文
continue;
}
}
}
/// <summary>
/// 循環接收客戶端2的請求數據
/// </summary>
public void AccpetClIEnt2Data()
{
NetworkStream Ns2 = clIEnt2.GetStream();
string str1 = "";
while (true)
{
PokerGroup pg = new PokerGroup();
byte[] bytes2 = new byte[108];
Ns2.Read(bytes2, 0, 108);
str1 = Encoding.Default.GetString(bytes2);
(省略部分)
if (str1.StartsWith("AreYouLandLord")) //客戶端2不要地主
{
if (DConsole.LandLordNum == 2) //如果默認地主為客戶端1,那麼根據文章開頭的權限傳遞順序圖,client1-server-client2,clIEnt2為最後一個玩家,所以需要和牌重新來過
{
DConsole.Restart();
continue;
}
SendDataForClient("AreYouLandLord", 1); //如果默認地主不是客戶端1的話,按照順序把叫地主權限傳遞給client1,clIEnt1的處理程序上文已經列出
continue;
}
if (str1.StartsWith("IamLandLord") ) //客戶端2為地主
{
DConsole.lblClIEnt2Name.Text += "(地主)";
DConsole.lblClIEnt2Name.ForeColor = System.Drawing.Color.Red;
SendDataForClIEnt("LandLordPokers", DConsole.LandLordPokers, 1);
SendDataForClIEnt("LandLordPokers", DConsole.LandLordPokers, 2);
SendDataForClient("ClIEntIsLandLord", 1);
DConsole.PaintClIEnt(20, 2);
DConsole.player1.SelectLandLordEnd();
continue;
}
}
}
private void btnNotLandLord_Click(object sender, EventArgs e)
{
this.player1.areYouLandLord = false;
this.player1.isLandLord = false;
this.btnNeedLandLord.Visible = false;
this.btnNotLandLord.Visible = false;
if (this.server != null) //玩家是服務端
{
if (DConsole.LandLordNum == 3) 如果client2是默認地主,根據權限傳遞順序client2-clIEnt1-server,server為最後玩家,如果server不叫地主,只能和牌重來
{
DConsole.Restart();
return;
}
this.server.SendDataForClient("AreYouLandLord",2); //如果client2不是默認地主,就把權限傳遞給clIEnt2
}
if (this.clIEnt != null) //如果玩家是客戶端
{
this.clIEnt.SendDataForServer("AreYouLandLord"); //告訴服務器自己不叫
}
}