public bool AddPlayer(ServerPlayer player) { if (this.HasPlayer(player)) { return true; } if (this.IsFull()) { return false; } for (int i = 0; i < Settings.MaxPartySize; i++) if (_members[i] == null) { _members[i] = player; ServerMessage msg = new ServerMessage(Protocol.netSquadJoin); byte n = (byte)i; msg.WriteByte(n); this.Broadcast(msg, player.accountID); player.SendMessage(msg, player.accountID); msg = null; for (int j = 0; j < Settings.MaxPartySize; j++) if (_members[j]!= null && j!=i) { msg = new ServerMessage(Protocol.netSquadJoin); msg.WriteByte(n); player.SendMessage(msg, _members[i].accountID); } break; } return true; }
public void AddPlayer(ServerPlayer player) { if (player == null) return; foreach (ServerPlayer other in _players) if (other.accountID == player.accountID) { return; } foreach (ServerPlayer other in _players) { ServerMessage msg = new ServerMessage(Protocol.netPlayerAdd); msg.WriteInteger(this._ID); msg.WriteString(this._region.Name); msg.WriteInteger(other.TX); msg.WriteInteger(other.TY); msg.WriteByte(other.level); player.SendMessage(msg, other.accountID); msg = null; msg = other.GetKeysMessage(); if (msg!=null) { player.SendMessage(msg, other.accountID); msg = null; } } _players.Add(player); }
public void AddPlayer(ServerPlayer p, byte teamSize) { if (p == null) return; p.monstersPerBattle = teamSize; p.battle = this; p.turnState = BattleTurn.turnWaiting; _players.Add(p); }
public int Run() { Utils.Log("Initializing Minimon .NET server...", -1); Utils.Log("Running at port " + Settings.GamePort.ToString(), -1); Utils.Log("Starting threads", -1); this.updateThread = new Thread(new ThreadStart(this.Update)); this.updateThread.Start(); this.tcpListener = new TcpListener(IPAddress.Any, Settings.GamePort); this.listenThread = new Thread(new ThreadStart(ListenForClients)); this.listenThread.Start(); Utils.Log("Ready...accepting new connections", -1); while (!shutdown) { ServerQueuedMessage queued = messagequeue.Dequeue(); if (queued == null || queued.client == null) continue; lockedClient = queued.client; Utils.Log("Client " + queued.client.accountID.ToString() + " opcode " + queued.msg.Opcode + " -> begin", 2); protocol.HandleMessage(queued.msg, queued.client); lockedClient = null; } Utils.Log("Server is shutting down...", -1); return 0; }
public void RemovePlayer(ServerPlayer player) { for (int i = 0; i < Settings.MaxPartySize; i++) if (_members[i] == player) { _members[i] = null; player.party = null; } ServerMessage msg = new ServerMessage(Protocol.netSquadLeave); this.Broadcast(msg, player.accountID); if (this.GetMemberCount()<2) { msg = new ServerMessage(Protocol.netSquadDisband); this.Broadcast(msg, 0); msg = null; } }
public BattleCaptureCommand(ServerPlayer owner) : base(owner) { this.opcode = ServerBattle.opBattleCapture; }
public ServerBattleCommand(ServerPlayer owner) { this.owner = owner; }
public void SetPlayerReady(ServerPlayer p) { if (!loading) return; p.turnState = BattleTurn.turnWaiting; byte count = 0; foreach (ServerPlayer player in _players) if (player.turnState != BattleTurn.turnLoading) { count++; } if (count< this._players.Count) return; this.loading = false; // after sending info to everyone we let them start the battle foreach (ServerPlayer player in _players) { ServerMessage msg = new ServerMessage(Protocol.netBattleStart); msg.WriteByte(count); // player count msg.WriteByte(this.secondsPerTurn); // player timer msg.WriteByte(this.battleMode); // team index msg.WriteInteger(this.seed); msg.WriteByte(player.battleTeam); // team index msg.WriteByte(player.battleSide); // local side msg.WriteByte(player.monstersPerBattle); player.rules.Write(msg); player.SendMessage(msg, player.accountID); } }
public bool HasPlayer(ServerPlayer p) { if (p == null) { return false; } foreach (ServerPlayer player in _players) if (p.accountID == player.accountID) { return true; } return false; }
private void OnClientRemove(ServerPlayer player) { if (player == null) return; Utils.Log("Client with id " + player.accountID + " has left the server.", 0); }
private void HandleClientComm(object obj) { TcpClient tcpClient = (TcpClient)obj; NetworkStream clientStream = tcpClient.GetStream(); ServerPlayer client = this.AllocateClient(tcpClient); if (client == null) { client = new ServerPlayer(tcpClient); ServerMessage dest = new ServerMessage(Protocol.netServerError); dest.WriteWord(Protocol.errServerFull); dest.WriteString(null); client.SendMessage(dest, 0); return; } playersOnline++; int bytesRead; byte[] buffer = new byte[1024]; while (!client.isRemoved() && !this.shutdown) { ServerQueuedMessage temp = new ServerQueuedMessage(); temp.client = client; bytesRead = 0; try { if (client.outdatedVersion) { ServerMessage dest = new ServerMessage(Protocol.netServerError); dest.WriteWord(Protocol.errInvalidVersion); dest.WriteString(null); client.SendMessage(dest, 0); } if (client.accountID>0) Utils.Log("Waiting for client " + client.accountID + " packets..", 2); ServerMessage msg = new ServerMessage(clientStream, buffer, ref bytesRead); } catch (Exception e) { //a socket error has occured Utils.Log("Socket error in client with id " + client.accountID, -1); break; } if (bytesRead == 0) { Utils.Log("Client with id " + client.accountID + " disconnected", 0); //the client has disconnected from the server break; } //protocol.HandleMessage(msg.header, msg.client, msg.data); messagequeue.Enqueue(temp); } OnClientRemove(client); client.removed = true; this.playersOnline--; tcpClient.Close(); tcpClient = null; System.GC.Collect(); }
private ServerPlayer AllocateClient(TcpClient client) { lock (_players) { if (_players.Count < Settings.MaxPlayers) { ServerPlayer result = new ServerPlayer(client); _players.Add(result); return result; } } return null; }
public void StartTrade(ServerPlayer A, ServerPlayer B) { }
public void StartDuel(ServerPlayer A, ServerPlayer B) { }
public void StartCardGame(ServerPlayer A, ServerPlayer B) { }
public void Broadcast(ServerMessage msg, ServerPlayer exclude) { foreach (ServerPlayer player in _players) if (player != exclude) { player.SendMessage(msg, 0); } }
public void Cancel(ServerPlayer ignore, byte cancelReason) { if (this.finished) return; foreach (ServerPlayer player in _players) { ServerMessage msg = new ServerMessage(Protocol.netBattleCancel); msg.WriteByte(cancelReason); player.SendMessage(msg, 0); player.ClearBattles(); } this.finished = true; }
public void RemovePlayer(ServerPlayer player) { _players.Remove(player); }
public void ReceiveCommand(ServerPlayer p, ServerMessage msg) { byte opcode = msg.ReadByte(); ServerBattleCommand cmd = null; switch (opcode) { case ServerBattle.opBattleAttack: cmd = new BattleAttackCommand(p); break; case ServerBattle.opBattleItem: cmd = new BattleItemCommand(p); break; case ServerBattle.opBattleSwitch: cmd = new BattleSwitchCommand(p); break; case ServerBattle.opBattleDefend: cmd = new BattleDefendCommand(p); break; case ServerBattle.opBattleCapture: cmd = new BattleCaptureCommand(p); break; default: return; } cmd.Read(msg); this.AddCommand(cmd); actionTime = Utils.GetTickCount(); }
public BattleCommandWithSource(ServerPlayer owner) : base(owner) { }
public BattleAttackCommand(ServerPlayer owner) : base(owner) { this.opcode = ServerBattle.opBattleAttack; }
public BattleDefendCommand(ServerPlayer owner) : base(owner) { this.opcode = ServerBattle.opBattleDefend; }
public virtual void SendTo(ServerPlayer player) { ServerMessage msg; if (player == null) return; msg = new ServerMessage(Protocol.netBattleCommand); this.Write(msg); player.SendMessage(msg, owner.accountID); }
public BattleItemCommand(ServerPlayer owner) : base(owner) { this.opcode = ServerBattle.opBattleItem; }
public bool HasPlayer(ServerPlayer player) { foreach (ServerPlayer member in _members) if (member.accountID == player.accountID) { return true; } return false; }
public BattleSwitchCommand(ServerPlayer owner) : base(owner) { this.opcode = ServerBattle.opBattleSwitch; }
public BattleTargetedCommand(ServerPlayer owner) : base(owner) { }
public void RemoveClient(ServerPlayer player) { if (player != null) player.RemoveFromServer(); }