Ejemplo n.º 1
0
 public SessionSpectator(int id, Session session, CreateSpectatorData data)
 {
     this.id = id;
     this.session = session;
     this.data = data;
     control = new SessionSpectatorControl(this);
 }
Ejemplo n.º 2
0
 public void OnPlayerDisconnected(Session session, SessionPlayer player)
 {
     List<IServerEventListener> listeners = new List<IServerEventListener>(this.listeners);
     foreach(IServerEventListener l in listeners)
         sender.SendEvent(li => {
             li.OnPlayerDisconnected(session, player);
         }, l);
 }
Ejemplo n.º 3
0
 public void OnGameStarted(Session session)
 {
     List<IServerEventListener> listeners = new List<IServerEventListener>(this.listeners);
     foreach(IServerEventListener l in listeners)
         sender.SendEvent(li => {
             li.OnGameStarted(session);
         }, l);
 }
Ejemplo n.º 4
0
        public SessionPlayer(Session session, BinaryReader reader)
        {
            this.session = session;
            id = reader.ReadInt32();
            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                data = (CreatePlayerData)bf.Deserialize(reader.BaseStream);
            }
            catch(InvalidCastException)
            {
                throw new FormatException();
            }
            catch(SerializationException)
            {
                throw new FormatException();
            }

            control = new SessionPlayerControl(this);

            score = reader.ReadInt32();
            if(score < 0)
                throw new FormatException();
            turnsPlayed = reader.ReadInt32();
            if(turnsPlayed < 0)
                throw new FormatException();

            victories = reader.ReadInt32();
            if(victories < 0)
                throw new FormatException();

            int roleVicCount = reader.ReadInt32();
            roleVictories = new Dictionary<Role, int>(roleVicCount);
            for(int i = 0; i < roleVicCount; i++)
            {
                Role role = (Role)reader.ReadInt32();
                int vic = reader.ReadInt32();
                if(vic < 0)
                    throw new FormatException();
                roleVictories.Add(role, vic);
            }

            int characterVicCount = reader.ReadInt32();
            characterVictories = new Dictionary<CharacterType, int>(characterVicCount);
            for(int i = 0; i < characterVicCount; i++)
            {
                CharacterType character = (CharacterType)reader.ReadInt32();
                int vic = reader.ReadInt32();
                if(vic < 0)
                    throw new FormatException();
                characterVictories.Add(character, vic);
            }
        }
Ejemplo n.º 5
0
        public SessionPlayer(int id, Session session, CreatePlayerData data)
        {
            this.id = id;
            this.session = session;
            this.data = data;
            control = new SessionPlayerControl(this);

            score = 0;
            turnsPlayed = 0;
            victories = 0;
            List<Role> roles = Utils.GetRoles();
            roleVictories = new Dictionary<Role, int>(roles.Count);
            foreach(Role role in roles)
                roleVictories[role] = 0;
            List<CharacterType> characters = Utils.GetCharacterTypes(session);
            characterVictories = new Dictionary<CharacterType, int>(characters.Count);
            foreach(CharacterType character in characters)
                characterVictories[character] = 0;
        }
Ejemplo n.º 6
0
 public void OnSpectatorLeftSession(Session session, SessionSpectator spectator)
 {
     List<IServerEventListener> listeners = new List<IServerEventListener>(this.listeners);
     foreach(IServerEventListener l in listeners)
         sender.SendEvent(li => {
             li.OnSpectatorLeftSession(session, spectator);
         }, l);
 }
Ejemplo n.º 7
0
 public SessionAdmin(Session parent)
 {
     this.parent = parent;
 }
Ejemplo n.º 8
0
        private bool LoadState()
        {
            Console.Error.Write("INFO: Loading server state... ");
            try
            {
                Stream stream = File.OpenRead(StatePath);
                using(BinaryReader reader = new BinaryReader(stream))
                {
                    char[] magic = reader.ReadChars(StateMagic.Length);
                    if(!magic.SequenceEqual(StateMagic))
                        return false;
                    uint version = reader.ReadUInt32();
                    if(version != StateVersion)
                        return false;

                    int sessionCount = reader.ReadInt32();
                    if(sessionCount < 0)
                        return false;

                    sessions = new Dictionary<int, Session>(sessionCount);
                    for(int i = 0; i < sessionCount; i++)
                    {
                        Session session = new Session(this, reader);
                        sessions.Add(session.ID, session);
                    }
                }
                Console.Error.WriteLine("Success!");
                return true;
            }
            catch
            {
                Console.Error.WriteLine("Error!");
                return false;
            }
        }
Ejemplo n.º 9
0
 public void RemoveSession(Session session)
 {
     lock(Lock)
     {
         Console.Error.WriteLine("INFO: Removing session #{0}...", session.ID);
         sessions.Remove(session.ID);
         eventMgr.OnSessionEnded(session);
         session.Disconnect();
         SaveState();
     }
 }
Ejemplo n.º 10
0
        public void CreateSession(CreateSessionData sessionData, CreatePlayerData playerData, IPlayerSessionEventListener listener)
        {
            lock(Lock)
            {
                int id = sessions.GenerateID();
                Session session = new Session(this, id, sessionData);
                sessions.Add(id, session);
                Console.Error.WriteLine("INFO: Created session #{0}.", id);

                session.Join(sessionData.PlayerPassword, playerData, listener);
                eventMgr.OnSessionCreated(session);
                SaveState();
            }
        }