public Game(CoreServer server) { State = GameState.Lobby; IsMatch = Config.GetInt("Mode") == 1; IsTag = Config.GetInt("Mode") == 2; CurrentPlayer = 0; LifePoints = new int[2]; Players = new Player[IsTag ? 4 : 2]; CurPlayers = new Player[2]; IsReady = new bool[IsTag ? 4 : 2]; _handResult = new int[2]; _timelimit = new int[2]; Winner = -1; MatchResults = new int[3]; MatchReasons = new int[3]; Observers = new List<Player>(); int lfList = Config.GetInt("Banlist"); if (lfList >= 0 && lfList < BanlistManager.Banlists.Count) Banlist = BanlistManager.Banlists[lfList]; LifePoints[0] = Config.GetInt("StartLp", DEFAULT_LIFEPOINTS); LifePoints[1] = Config.GetInt("StartLp", DEFAULT_LIFEPOINTS); _server = server; _analyser = new GameAnalyser(this); }
public void SendToAllBut(GameServerPacket packet, Player except) { foreach (Player player in Players) if (player != null && !player.Equals(except)) player.Send(packet); foreach (Player player in Observers) if (!player.Equals(except)) player.Send(packet); }
public void ReloadGameConfig() { IsMatch = Config.Mode == 1; IsTag = Config.Mode == 2; Players = new Player[IsTag ? 4 : 2]; IsReady = new bool[IsTag ? 4 : 2]; if (Config.LfList >= 0 && Config.LfList < BanlistManager.Banlists.Count) Banlist = BanlistManager.Banlists[Config.LfList]; LifePoints[0] = Config.StartLp; LifePoints[1] = Config.StartLp; }
public CoreClient(CoreServer server, TcpClient client) { IsConnected = true; Game = server.Game; Player = new Player(this); _server = server; _client = client; _reader = new BinaryReader(_client.GetStream()); _recvQueue = new Queue<GameClientPacket>(); _sendQueue = new Queue<byte[]>(); _receivedLen = -1; }
public Game(CoreServer server, CoreConfig config) { Config = config; State = GameState.Lobby; IsMatch = config.Mode == 1; IsTag = config.Mode == 2; CurrentPlayer = 0; LifePoints = new int[2]; Players = new Player[IsTag ? 4 : 2]; CurPlayers = new Player[2]; IsReady = new bool[IsTag ? 4 : 2]; _handResult = new int[2]; _timelimit = new int[2]; _matchResult = new int[3]; Observers = new List<Player>(); if (config.LfList >= 0 && config.LfList < BanlistManager.Banlists.Count) Banlist = BanlistManager.Banlists[config.LfList]; _server = server; _analyser = new GameAnalyser(this); ReloadGameConfig(); }
public Game(CoreServer server) { State = GameState.Lobby; Mode = Config.GetInt("Mode"); Rule = Config.GetInt("Rule"); IsMatch = Mode == 1; IsTag = Mode == 2; CurrentPlayer = 0; LifePoints = new int[2]; Players = new Player[IsTag ? 4 : 2]; CurPlayers = new Player[2]; IsReady = new bool[IsTag ? 4 : 2]; _handResult = new int[2]; _timelimit = new int[2]; Winner = -1; MatchResults = new int[3]; MatchReasons = new int[3]; Observers = new List<Player>(); int lfList = Config.GetInt("Banlist"); if (lfList >= 0 && lfList < BanlistManager.Banlists.Count) Banlist = BanlistManager.Banlists[lfList]; StartLp = Config.GetInt("StartLp", DEFAULT_LIFEPOINTS); LifePoints[0] = StartLp; LifePoints[1] = StartLp; StartHand = Config.GetInt("StartHand", DEFAULT_START_HAND); DrawCount = Config.GetInt("DrawCount", DEFAULT_DRAW_COUNT); EnablePriority = Config.GetBool("EnablePriority"); NoCheckDeck = Config.GetBool("NoCheckDeck"); NoShuffleDeck = Config.GetBool("NoShuffleDeck"); Timer = Config.GetInt("GameTimer", DEFAULT_TIMER); _server = server; _analyser = new GameAnalyser(this); }
public void RefreshHand(int player, int flag = 0x181fff, Player observer = null) { byte[] result = _duel.QueryFieldCard(player, CardLocation.Hand, flag | 0x100000, false); GamePacketWriter update = GamePacketFactory.Create(GameMessage.UpdateData); update.Write((byte)player); update.Write((byte)CardLocation.Hand); update.Write(result); if (observer == null) CurPlayers[player].Send(update); update = GamePacketFactory.Create(GameMessage.UpdateData); update.Write((byte)player); update.Write((byte)CardLocation.Hand); MemoryStream ms = new MemoryStream(result); BinaryReader reader = new BinaryReader(ms); while (ms.Position < ms.Length) { int len = reader.ReadInt32(); if (len == 4) { update.Write(4); continue; } byte[] raw = reader.ReadBytes(len - 4); if (raw[len - 8] == 0) { update.Write(8); update.Write(0); } else { update.Write(len); update.Write(raw); } } if (observer == null) SendToAllBut(update, player); else observer.Send(update); }
public void RefreshAllObserver(Player observer) { RefreshMonsters(0, observer: observer); RefreshMonsters(1, observer: observer); RefreshSpells(0, observer: observer); RefreshSpells(1, observer: observer); RefreshHand(0, observer: observer); RefreshHand(1, observer: observer); }
public void HandResult(Player player, int result) { if (State != GameState.Hand) return; if (player.Type == (int)PlayerType.Observer) return; if (result < 1 || result > 3) return; if (IsTag && player.Type != 0 && player.Type != 2) return; int type = player.Type; if (IsTag && player.Type == 2) type = 1; if (_handResult[type] != 0) return; _handResult[type] = result; if (_handResult[0] != 0 && _handResult[1] != 0) { GamePacketWriter packet = new GamePacketWriter(StocMessage.HandResult); packet.Write((byte)_handResult[0]); packet.Write((byte)_handResult[1]); SendToTeam(packet, 0); SendToObservers(packet); packet = new GamePacketWriter(StocMessage.HandResult); packet.Write((byte)_handResult[1]); packet.Write((byte)_handResult[0]); SendToTeam(packet, 1); if (_handResult[0] == _handResult[1]) { _handResult[0] = 0; _handResult[1] = 0; SendHand(); return; } if ((_handResult[0] == 1 && _handResult[1] == 2) || (_handResult[0] == 2 && _handResult[1] == 3) || (_handResult[0] == 3 && _handResult[1] == 1)) _startplayer = IsTag ? 2 : 1; else _startplayer = 0; State = GameState.Starting; Players[_startplayer].Send(new GamePacketWriter(StocMessage.SelectTp)); TpTimer = DateTime.UtcNow; } }
public void KickPlayer(Player player, int pos) { if (State != GameState.Lobby) return; if (pos >= Players.Length || !player.Equals(HostPlayer) || player.Equals(Players[pos]) || Players[pos] == null) return; RemovePlayer(Players[pos]); }
public void CustomMessage(Player player, string msg) { string finalmsg = msg; GamePacketWriter packet = new GamePacketWriter(StocMessage.Chat); packet.Write((short)PlayerType.Yellow); packet.Write(finalmsg, finalmsg.Length + 1); SendToAllBut(packet, player); }
public void CustomMessage(Player player, string msg) { string finalmsg = msg; BinaryWriter packet = GamePacketFactory.Create(StocMessage.Chat); packet.Write((short)PlayerType.Yellow); packet.WriteUnicode(finalmsg, finalmsg.Length + 1); SendToAllBut(packet, player); }
private void SendJoinGame(Player player) { BinaryWriter join = GamePacketFactory.Create(StocMessage.JoinGame); join.Write(Banlist == null ? 0U : Banlist.Hash); join.Write((byte)Rule); join.Write((byte)Mode); join.Write(EnablePriority); join.Write(NoCheckDeck); join.Write(NoShuffleDeck); // C++ padding: 5 bytes + 3 bytes = 8 bytes for (int i = 0; i < 3; i++) join.Write((byte)0); join.Write(StartLp); join.Write((byte)StartHand); join.Write((byte)DrawCount); join.Write((short)Timer); player.Send(join); if (State != GameState.Lobby) SendDuelingPlayers(player); }
public void SetRules(BinaryReader packet) { uint lfList = packet.ReadUInt32(); if (lfList >= 0 && lfList < BanlistManager.Banlists.Count) Banlist = BanlistManager.Banlists[BanlistManager.GetIndex(lfList)]; Rule = packet.ReadByte(); Mode = packet.ReadByte(); IsMatch = Mode == 1; IsTag = Mode == 2; IsReady = new bool[IsTag ? 4 : 2]; Players = new Player[IsTag ? 4 : 2]; EnablePriority = packet.ReadByte() > 0; NoCheckDeck = packet.ReadByte() > 0; NoShuffleDeck = packet.ReadByte() > 0; //C++ padding: 5 bytes + 3 bytes = 8 bytes for (int i = 0; i < 3; i++) packet.ReadByte(); int lifePoints = packet.ReadInt32(); LifePoints[0] = lifePoints; LifePoints[1] = lifePoints; StartHand = packet.ReadByte(); DrawCount = packet.ReadByte(); Timer = packet.ReadInt16(); }
public PlayerEventArgs(Player player) { Player = player; }
public void MoveToObserver(Player player) { if (State != GameState.Lobby) return; if (player.Type == (int)PlayerType.Observer) return; if (IsReady[player.Type]) return; int oldType = player.Type; Players[player.Type] = null; IsReady[player.Type] = false; Observers.Add(player); GamePacketWriter change = new GamePacketWriter(StocMessage.HsPlayerChange); change.Write((byte)((player.Type << 4) + (int)PlayerChange.Observe)); SendToAll(change); player.Type = (int)PlayerType.Observer; player.SendTypeChange(); if (OnPlayerMove != null) { OnPlayerMove(this, new PlayerMoveEventArgs(player, oldType)); } }
public void Chat(Player player, string msg) { GamePacketWriter packet = new GamePacketWriter(StocMessage.Chat); packet.Write((short)player.Type); if (player.Type == (int)PlayerType.Observer) { string fullmsg = "[" + player.Name + "]: " + msg; CustomMessage(player, fullmsg); } else { packet.Write(msg, msg.Length + 1); SendToAllBut(packet, player); } if (OnPlayerChat != null) { OnPlayerChat(this, new PlayerChatEventArgs(player, msg)); } }
public bool Equals(Player player) { return ReferenceEquals(this, player); }
public void SetReady(Player player, bool ready) { if (State != GameState.Lobby) return; if (player.Type == (int)PlayerType.Observer) return; if (IsReady[player.Type] == ready) return; if (ready) { int rule = Config.GetInt("Rule"); bool ocg = rule == 0 || rule == 2; bool tcg = rule == 1 || rule == 2; int result = 1; if (player.Deck != null) { result = Config.GetBool("NoCheckDeck") ? 0 : player.Deck.Check(Banlist, ocg, tcg); } if (result != 0) { GamePacketWriter rechange = new GamePacketWriter(StocMessage.HsPlayerChange); rechange.Write((byte)((player.Type << 4) + (int)(PlayerChange.NotReady))); player.Send(rechange); GamePacketWriter error = new GamePacketWriter(StocMessage.ErrorMsg); error.Write((byte)2); // ErrorMsg.DeckError // C++ padding: 1 byte + 3 bytes = 4 bytes for (int i = 0; i < 3; i++) error.Write((byte)0); error.Write(result); player.Send(error); return; } } IsReady[player.Type] = ready; GamePacketWriter change = new GamePacketWriter(StocMessage.HsPlayerChange); change.Write((byte)((player.Type << 4) + (int)(ready ? PlayerChange.Ready : PlayerChange.NotReady))); SendToAll(change); if (OnPlayerReady != null) { OnPlayerReady(this, new PlayerEventArgs(player)); } }
private void SendJoinGame(Player player) { GamePacketWriter join = new GamePacketWriter(StocMessage.JoinGame); join.Write(Banlist == null ? 0U : Banlist.Hash); join.Write((byte)Config.GetInt("Rule")); join.Write((byte)Config.GetInt("Mode")); join.Write(Config.GetBool("EnablePriority")); join.Write(Config.GetBool("NoCheckDeck")); join.Write(Config.GetBool("NoShuffleDeck")); // C++ padding: 5 bytes + 3 bytes = 8 bytes for (int i = 0; i < 3; i++) join.Write((byte)0); join.Write(Config.GetInt("StartLp", DEFAULT_LIFEPOINTS)); join.Write((byte)Config.GetInt("StartHand", DEFAULT_START_HAND)); join.Write((byte)Config.GetInt("DrawCount", DEFAULT_DRAW_COUNT)); join.Write((short)Config.GetInt("GameTimer", DEFAULT_TIMER)); player.Send(join); if (State != GameState.Lobby) SendDuelingPlayers(player); }
public void StartDuel(Player player) { if (State != GameState.Lobby) return; if (!player.Equals(HostPlayer)) return; for (int i = 0; i < Players.Length; i++) { if (!IsReady[i]) return; if (Players[i] == null) return; } State = GameState.Hand; SendToAll(new GamePacketWriter(StocMessage.DuelStart)); SendHand(); if (OnGameStart != null) { OnGameStart(this, EventArgs.Empty); } }
private void SendDuelingPlayers(Player player) { for (int i = 0; i < Players.Length; i++) { GamePacketWriter enter = new GamePacketWriter(StocMessage.HsPlayerEnter); int id = i; if (_swapped) { if (IsTag) { if (i == 0 || id == 1) id = i + 2; else id = i - 2; } else id = 1 - i; } enter.Write(Players[id].Name, 20); enter.Write((byte)i); //padding enter.Write((byte)0); player.Send(enter); } }
public void TpResult(Player player, bool result) { if (State != GameState.Starting) return; if (player.Type != _startplayer) return; _swapped = false; if (result && player.Type == (IsTag ? 2 : 1) || !result && player.Type == 0) { _swapped = true; if (IsTag) { Player temp = Players[0]; Players[0] = Players[2]; Players[2] = temp; temp = Players[1]; Players[1] = Players[3]; Players[3] = temp; Players[0].Type = 0; Players[1].Type = 1; Players[2].Type = 2; Players[3].Type = 3; } else { Player temp = Players[0]; Players[0] = Players[1]; Players[1] = temp; Players[0].Type = 0; Players[1].Type = 1; } } CurPlayers[0] = Players[0]; CurPlayers[1] = Players[IsTag ? 3 : 1]; State = GameState.Duel; int seed = Environment.TickCount; _duel = Duel.Create((uint)seed); Random rand = new Random(seed); int startLp = Config.GetInt("StartLp", DEFAULT_LIFEPOINTS); int startHand = Config.GetInt("StartHand", DEFAULT_START_HAND); int drawCount = Config.GetInt("DrawCount", DEFAULT_DRAW_COUNT); bool enablePriority = Config.GetBool("EnablePriority"); bool noShuffleDeck = Config.GetBool("NoShuffleDeck"); _duel.SetAnalyzer(_analyser.Analyse); _duel.SetErrorHandler(HandleError); _duel.InitPlayers(startLp, startHand, drawCount); int opt = 0; if (enablePriority) opt += 0x08; if (noShuffleDeck) opt += 0x10; if (IsTag) opt += 0x20; Replay = new Replay((uint)seed, IsTag); Replay.Writer.WriteUnicode(Players[0].Name, 20); Replay.Writer.WriteUnicode(Players[1].Name, 20); if (IsTag) { Replay.Writer.WriteUnicode(Players[2].Name, 20); Replay.Writer.WriteUnicode(Players[3].Name, 20); } Replay.Writer.Write(startLp); Replay.Writer.Write(startHand); Replay.Writer.Write(drawCount); Replay.Writer.Write(opt); for (int i = 0; i < Players.Length; i++) { Player dplayer = Players[i == 2 ? 3 : (i == 3 ? 2 : i)]; int pid = i; if (IsTag) pid = i >= 2 ? 1 : 0; if (!noShuffleDeck) { List<int> cards = ShuffleCards(rand, dplayer.Deck.Main); Replay.Writer.Write(cards.Count); foreach (int id in cards) { if (IsTag && (i == 1 || i == 3)) _duel.AddTagCard(id, pid, CardLocation.Deck); else _duel.AddCard(id, pid, CardLocation.Deck); Replay.Writer.Write(id); } } else { Replay.Writer.Write(dplayer.Deck.Main.Count); for (int j = dplayer.Deck.Main.Count - 1; j >= 0; j--) { int id = dplayer.Deck.Main[j]; if (IsTag && (i == 1 || i == 3)) _duel.AddTagCard(id, pid, CardLocation.Deck); else _duel.AddCard(id, pid, CardLocation.Deck); Replay.Writer.Write(id); } } Replay.Writer.Write(dplayer.Deck.Extra.Count); foreach (int id in dplayer.Deck.Extra) { if (IsTag && (i == 1 || i == 3)) _duel.AddTagCard(id, pid, CardLocation.Extra); else _duel.AddCard(id, pid, CardLocation.Extra); Replay.Writer.Write(id); } } GamePacketWriter packet = GamePacketFactory.Create(GameMessage.Start); packet.Write((byte)0); packet.Write(startLp); packet.Write(startLp); packet.Write((short)_duel.QueryFieldCount(0, CardLocation.Deck)); packet.Write((short)_duel.QueryFieldCount(0, CardLocation.Extra)); packet.Write((short)_duel.QueryFieldCount(1, CardLocation.Deck)); packet.Write((short)_duel.QueryFieldCount(1, CardLocation.Extra)); SendToTeam(packet, 0); packet.SetPosition(2); packet.Write((byte)1); SendToTeam(packet, 1); packet.SetPosition(2); if (_swapped) packet.Write((byte)0x11); else packet.Write((byte)0x10); SendToObservers(packet); RefreshExtra(0); RefreshExtra(1); _duel.Start(opt); TurnCount = 0; LifePoints[0] = startLp; LifePoints[1] = startLp; TimeReset(); Process(); }
private void InitNewSpectator(Player player) { GamePacketWriter packet = GamePacketFactory.Create(GameMessage.Start); packet.Write((byte)(_swapped ? 0x11 : 0x10)); packet.Write(LifePoints[0]); packet.Write(LifePoints[1]); packet.Write((short)0); // deck packet.Write((short)0); // extra packet.Write((short)0); // deck packet.Write((short)0); // extra player.Send(packet); GamePacketWriter turn = GamePacketFactory.Create(GameMessage.NewTurn); turn.Write((byte)0); player.Send(turn); if (CurrentPlayer == 1) { turn = GamePacketFactory.Create(GameMessage.NewTurn); turn.Write((byte)0); player.Send(turn); } GamePacketWriter reload = GamePacketFactory.Create(GameMessage.ReloadField); byte[] fieldInfo = _duel.QueryFieldInfo(); reload.Write(fieldInfo, 1, fieldInfo.Length - 1); player.Send(reload); RefreshAllObserver(player); }
public void Surrender(Player player, int reason, bool force = false) { if (State == GameState.End) return; if (!force && State != GameState.Duel) return; if (player.Type == (int)PlayerType.Observer) return; GamePacketWriter win = GamePacketFactory.Create(GameMessage.Win); int team = player.Type; if (IsTag) team = player.Type >= 2 ? 1 : 0; else if (State == GameState.Hand) team = 1 - team; win.Write((byte)(1 - team)); win.Write((byte)reason); SendToAll(win); MatchSaveResult(1 - team, reason); EndDuel(reason == 4); }
public void AddPlayer(Player player) { if (State != GameState.Lobby) { player.Type = (int)PlayerType.Observer; if (State != GameState.End) { SendJoinGame(player); player.SendTypeChange(); player.Send(new GamePacketWriter(StocMessage.DuelStart)); Observers.Add(player); if (State == GameState.Duel) InitNewSpectator(player); } if (OnPlayerJoin != null) { OnPlayerJoin(this, new PlayerEventArgs(player)); } return; } if (HostPlayer == null) HostPlayer = player; int pos = GetAvailablePlayerPos(); if (pos != -1) { GamePacketWriter enter = new GamePacketWriter(StocMessage.HsPlayerEnter); enter.Write(player.Name, 20); enter.Write((byte)pos); //padding enter.Write((byte)0); SendToAll(enter); Players[pos] = player; IsReady[pos] = false; player.Type = pos; } else { GamePacketWriter watch = new GamePacketWriter(StocMessage.HsWatchChange); watch.Write((short)(Observers.Count + 1)); SendToAll(watch); player.Type = (int)PlayerType.Observer; Observers.Add(player); } SendJoinGame(player); player.SendTypeChange(); for (int i = 0; i < Players.Length; i++) { if (Players[i] != null) { GamePacketWriter enter = new GamePacketWriter(StocMessage.HsPlayerEnter); enter.Write(Players[i].Name, 20); enter.Write((byte)i); //padding enter.Write((byte)0); player.Send(enter); if (IsReady[i]) { GamePacketWriter change = new GamePacketWriter(StocMessage.HsPlayerChange); change.Write((byte)((i << 4) + (int)PlayerChange.Ready)); player.Send(change); } } } if (Observers.Count > 0) { GamePacketWriter nwatch = new GamePacketWriter(StocMessage.HsWatchChange); nwatch.Write((short)Observers.Count); player.Send(nwatch); } if (OnPlayerJoin != null) { OnPlayerJoin(this, new PlayerEventArgs(player)); } }
public void RefreshSpells(int player, int flag = 0x681fff, Player observer = null) { byte[] result = _duel.QueryFieldCard(player, CardLocation.SpellZone, flag, false); GamePacketWriter update = GamePacketFactory.Create(GameMessage.UpdateData); update.Write((byte)player); update.Write((byte)CardLocation.SpellZone); update.Write(result); if (observer == null) SendToTeam(update, player); update = GamePacketFactory.Create(GameMessage.UpdateData); update.Write((byte)player); update.Write((byte)CardLocation.SpellZone); MemoryStream ms = new MemoryStream(result); BinaryReader reader = new BinaryReader(ms); for (int i = 0; i < 8; i++) { int len = reader.ReadInt32(); if (len == 4) { update.Write(4); continue; } byte[] raw = reader.ReadBytes(len - 4); if ((raw[11] & (int)CardPosition.FaceDown) != 0) { update.Write(8); update.Write(0); } else { update.Write(len); update.Write(raw); } } if (observer == null) { SendToTeam(update, 1 - player); SendToObservers(update); } else { observer.Send(update); } }
public void RemovePlayer(Player player) { if (player.Equals(HostPlayer) && State == GameState.Lobby) { _server.Stop(); return; } if (player.Type == (int)PlayerType.Observer) { Observers.Remove(player); if (State == GameState.Lobby) { GamePacketWriter nwatch = new GamePacketWriter(StocMessage.HsWatchChange); nwatch.Write((short) Observers.Count); SendToAll(nwatch); } player.Disconnect(); } else if (State == GameState.Lobby) { Players[player.Type] = null; IsReady[player.Type] = false; GamePacketWriter change = new GamePacketWriter(StocMessage.HsPlayerChange); change.Write((byte)((player.Type << 4) + (int) PlayerChange.Leave)); SendToAll(change); player.Disconnect(); } else Surrender(player, 4, true); if (OnPlayerLeave != null) { OnPlayerLeave(this, new PlayerEventArgs(player)); } }
public void RefreshGrave(int player, int flag = 0x81fff, Player observer = null) { byte[] result = _duel.QueryFieldCard(player, CardLocation.Grave, flag, false); GamePacketWriter update = GamePacketFactory.Create(GameMessage.UpdateData); update.Write((byte)player); update.Write((byte)CardLocation.Grave); update.Write(result); if (observer == null) SendToAll(update); else observer.Send(update); }
public void MoveToDuelist(Player player) { if (State != GameState.Lobby) return; int pos = GetAvailablePlayerPos(); if (pos == -1) return; int oldType = player.Type; if (player.Type != (int)PlayerType.Observer) { if (!IsTag || IsReady[player.Type]) return; pos = (player.Type + 1) % 4; while (Players[pos] != null) pos = (pos + 1) % 4; GamePacketWriter change = new GamePacketWriter(StocMessage.HsPlayerChange); change.Write((byte)((player.Type << 4) + pos)); SendToAll(change); Players[player.Type] = null; Players[pos] = player; player.Type = pos; player.SendTypeChange(); } else { Observers.Remove(player); Players[pos] = player; player.Type = pos; GamePacketWriter enter = new GamePacketWriter(StocMessage.HsPlayerEnter); enter.Write(player.Name, 20); enter.Write((byte)pos); //padding enter.Write((byte)0); SendToAll(enter); GamePacketWriter nwatch = new GamePacketWriter(StocMessage.HsWatchChange); nwatch.Write((short)Observers.Count); SendToAll(nwatch); player.SendTypeChange(); } if (OnPlayerMove != null) { OnPlayerMove(this, new PlayerMoveEventArgs(player, oldType)); } }