public Session(Server server, int id, CreateSessionData sessionData) { admin = new SessionAdmin(this); this.server = server; this.id = id; data = sessionData; if(data.MinPlayers > data.MaxPlayers) throw new MinPlayersOutOfRangeException(); if(data.MinPlayers < 2) throw new MinPlayersOutOfRangeException(); if(data.MinPlayers > 8) throw new MinPlayersOutOfRangeException(); if(data.MaxPlayers < 2) throw new MaxPlayersOutOfRangeException(); if(data.MaxPlayers > 8) throw new MaxPlayersOutOfRangeException(); if(data.MaxSpectators < 0) throw new MaxSpectatorsOutOfRangeException(); state = SessionState.WaitingForPlayers; eventMgr = new SessionEventManager(this); players = new Dictionary<int, SessionPlayer>(data.MaxPlayers); playerList = new List<SessionPlayer>(data.MaxPlayers); spectators = new Dictionary<int, SessionSpectator>(data.MaxSpectators); spectatorList = new List<SessionSpectator>(data.MaxSpectators); creatorId = 0; gamesPlayed = 0; remainingCharacters = Utils.GetCharacterTypes(this); eventMgr.StartPolling(); }
public Session(Server server, BinaryReader reader) { admin = new SessionAdmin(this); this.server = server; id = reader.ReadInt32(); try { BinaryFormatter bf = new BinaryFormatter(); data = (CreateSessionData)bf.Deserialize(reader.BaseStream); } catch(InvalidCastException) { throw new FormatException(); } catch(SerializationException) { throw new FormatException(); } try { state = (SessionState)reader.ReadInt32(); } catch(InvalidCastException) { throw new FormatException(); } if(state == SessionState.Playing) state = SessionState.GameFinished; creatorId = reader.ReadInt32(); gamesPlayed = reader.ReadInt32(); int playerCount = reader.ReadInt32(); if(playerCount < 0) throw new FormatException(); playerList = new List<SessionPlayer>(playerCount); players = new Dictionary<int, SessionPlayer>(playerCount); for(int i = 0; i < playerCount; i++) { bool isAI = reader.ReadBoolean(); SessionPlayer player = new SessionPlayer(this, reader); if(isAI && player.ID != creatorId) player.RegisterListener(new AI.AIPlayer()); playerList.Add(player); players.Add(player.ID, player); } spectatorList = new List<SessionSpectator>(); spectators = new Dictionary<int, SessionSpectator>(); eventMgr = new SessionEventManager(this); int currentSheriffId = reader.ReadInt32(); if(currentSheriffId != 0) { sheriffEnumerator = playerList.GetEnumerator(); do { if(!sheriffEnumerator.MoveNext()) throw new FormatException(); } while(sheriffEnumerator.Current.ID != currentSheriffId); } int remCharCount = reader.ReadInt32(); if(playerCount < 0) throw new FormatException(); remainingCharacters = new List<CharacterType>(remCharCount); for(int i = 0; i < remCharCount; i++) remainingCharacters.Add((CharacterType)reader.ReadInt32()); eventMgr.StartPolling(); }