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(); }
public void Join(Password password, CreatePlayerData data, IPlayerSessionEventListener listener) { if(state == SessionState.Ended) throw new BadSessionStateException(); lock(Lock) { if(state != SessionState.WaitingForPlayers) throw new BadSessionStateException(); if(players.Count >= this.data.MaxPlayers) throw new TooManyPlayersException(); if(!this.data.PlayerPassword.CheckPassword(password)) throw new BadSessionPasswordException(); int id = players.GenerateID(); if(creatorId == 0) creatorId = id; SessionPlayer player = new SessionPlayer(id, this, data); players.Add(id, player); playerList.Add(player); player.RegisterListener(listener); eventMgr.SendController(player); eventMgr.OnPlayerJoinedSession(player); server.SaveState(); } }