private void InitNewSpectator(GameSession player, int pos = -1) { if (m_duel == null) { return; } int deck1 = m_duel.QueryFieldCount(0, CardLocation.Deck); int deck2 = m_duel.QueryFieldCount(1, CardLocation.Deck); int hand1 = m_duel.QueryFieldCount(0, CardLocation.Hand); int hand2 = m_duel.QueryFieldCount(1, CardLocation.Hand); using (GameServerPacket packet = new GameServerPacket(GameMessage.Start)) { if (pos < 0) { packet.Write((byte)(m_swapped ? 0x11 : 0x10)); } else { packet.Write((byte)pos); } packet.Write(LifePoints[0]); packet.Write(LifePoints[1]); packet.Write((short)(deck1 + hand1)); packet.Write((short)m_duel.QueryFieldCount(0, CardLocation.Extra)); packet.Write((short)(deck2 + hand2)); packet.Write((short)m_duel.QueryFieldCount(1, CardLocation.Extra)); player.Send(packet); } using (GameServerPacket draw = new GameServerPacket(GameMessage.Draw)) { draw.Write((byte)0); draw.Write((byte)hand1); for (int i = 0; i < hand1; i++) draw.Write(0); player.Send(draw, false); } using (GameServerPacket draw = new GameServerPacket(GameMessage.Draw)) { draw.Write((byte)1); draw.Write((byte)hand2); for (int i = 0; i < hand2; i++) draw.Write(0); player.Send(draw); } //回合数 using (GameServerPacket turn0 = new GameServerPacket(GameMessage.NewTurn)) { turn0.Write((byte)0); using (GameServerPacket turn1 = new GameServerPacket(GameMessage.NewTurn)) { turn1.Write((byte)1); for (int i = 0; i < TurnCount; i++) { if (i % 2 == 0) { player.Send(turn0, false); } else { player.Send(turn1, false); } } } } // if (CurrentPlayer == 1) // { // GameServerPacket turn = new GameServerPacket(GameMessage.NewTurn); // turn.Write((byte)0); // player.Send(turn); // } player.PeekSend(); InitSpectatorLocation(player, CardLocation.MonsterZone); InitSpectatorLocation(player, CardLocation.SpellZone); InitSpectatorLocation(player, CardLocation.Grave); InitSpectatorLocation(player, CardLocation.Removed); }
public void AddPlayer(GameSession player) { // if(IsJoin(player)){ //// /玩家已经在游戏 // player.LobbyError(Messages.MSG_PLAYER_INGAME); // return; // } Logger.Debug("add " + player.Name + " to " + Name); if (State != GameState.Lobby) { if (State == GameState.End) return; player.Type = (int)PlayerType.Observer; SendJoinGame(player); player.SendTypeChange(); player.Send(GameServerPacket.EmtryMessage(StocMessage.DuelStart)); lock (Observers) Observers.Add(player); if (State == GameState.Duel) { //中途观战 InitNewSpectator(player); } else if (State == GameState.Side) { player.ServerMessage(Messages.MSG_WATCH_SIDE); } ServerApi.OnPlayerEnter(player, this); return; } if (HostPlayer == null) HostPlayer = player; int pos = GetAvailablePlayerPos(); if (pos != -1) { Players[pos] = player; IsReady[pos] = false; player.Type = pos; using (GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter)) { enter.WriteUnicode(player.Name, 20); enter.Write((byte)pos); SendToAll(enter); } // Server.OnPlayEvent(PlayerStatu.PlayerReady, player); } else { using (GameServerPacket watch = new GameServerPacket(StocMessage.HsWatchChange)) { watch.Write((short)(Observers.Count + 1)); SendToAll(watch); } player.Type = (int)PlayerType.Observer; lock (Observers) Observers.Add(player); // if(player.IsAuthentified){ // ServerMessage("[Server] "+player.Name+" watch game.", PlayerType.White); // } } SendJoinGame(player); player.SendTypeChange(); for (int i = 0; i < Players.Length; i++) { if (Players[i] != null) { using (GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter)) { enter.WriteUnicode(Players[i].Name, 20); enter.Write((byte)i); player.Send(enter, false); } if (IsReady[i]) { using (GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange)) { change.Write((byte)((i << 4) + (int)PlayerChange.Ready)); player.Send(change, false); } } } } bool _watch = false; lock (Observers) { _watch = Observers.Count > 0; } if (_watch) { using (GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange)) { nwatch.Write((short)Observers.Count); player.Send(nwatch, false); } } player.PeekSend(); ServerApi.OnPlayerEnter(player, this); }