Ejemplo n.º 1
0
        public ServersideSessions(ServersideLink link)
        {
            this.link                   = link;
            this.storyteller            = new Storyteller(isAuthor: true);
            this.peerSessions           = new Dictionary <int, Session>();
            this.storyListenerFunctions = new Dictionary <short, StoryfanListenerAdder>();

            this.SetFunctionToAddStoryListeners <Session>(Session.OPCODE, (session, listeners) =>
            {
                // Sessions listen to themselves
                listeners.Add(session);
            });

            link.OnPeerConnect = (peer) =>
            {
                // this session is a ghost.
                var sessionId = GenerateUniqueSessionId();
                var session   = new Session("s/" + sessionId);
                session.EntityId  = 0;
                session.SessionId = sessionId;
                session.Username  = "";
                session.PeerId    = peer.Id;
                this.storyteller.Write(session);

                this.peerSessions.Add(session.PeerId, session);
            };
            link.OnPeerDisconnect = (peer) =>
            {
                // wipe my session
                if (UsingPeer(peer.Id, out var session))
                {
                    // disconnect session
                    peerSessions.Remove(peer.Id);
                }
            };
            link.SetPostHandler <Login, Login_Reply>(Login.op, (posted) =>
            {
                if (UsingPeer(posted.Peer.Id, out var session))
                {
                    if (posted.action.username != "" && session.Username == "")
                    {
                        session.Username  = posted.action.username;
                        posted.reply.okay = true;
                        posted.Reply();
                        session.WriteChanges();     // send to anyone who should be able to see this session story (should only be the peer)
                    }
                    else
                    {
                        posted.reply.okay = false;
                        posted.Reply();
                    }
                }
                else
                {
                    posted.Reject(Barebones.Networking.ResponseStatus.NotConnected);
                }
            }
                                                     );
            // do nothing?
        }
Ejemplo n.º 2
0
        private void Start()
        {
            string s = "hello";

            Dj.Temp("Begin size test");
            byte[] b = Capn.Crunchatize(s);
            for (var i = 0; i < 5; i++)
            {
                Dj.Tempf("Iter #{0}, size {1}", i, b.Length);
                b = Capn.Crunchatize(b);
            }

            backupTimer = backupFreqInSeconds;

            worldData = new WorldData(true);
            link      = GetComponent <ServersideLink>();
            sessions  = new ServersideSessions(link);
            sessionToTowerEntities = new Dictionary <string, TowerEntity>();
            entGenerator           = new EntGenerator();
            decoder = new TowerStoryDecoder();

            sessions.SetFunctionToAddStoryListeners <TowerEntity>(TowerEntity.OPCODE, this.GetWhoCanSeeMe_UpdateTracking);
            sessions.SetFunctionToAddStoryListeners <TowerZone>(TowerZone.OPCODE, this.GetWhosListeningHere);

            sessions.storyteller.AddStoryfan(Session.OPCODE, new LambdaStoryfan <Session>(session =>
            {
                if (sessionToTowerEntities.ContainsKey(session.address))
                {
                    if (session.PeerId < 0)
                    {
                        // you're disconnected but your body is still in the world.
                        // bye now!
                    }
                }
                else
                {
                    if (session.PeerId >= 0)
                    {
                        // you're connected but don't have a body. let's remedy that!
                        // int EntityId = (nextTowerEntityId++);

                        var playerTowerEntity = entGenerator.NewPlayerEntity();
                        //playerTowerEntity.EntityId = EntityId;
                        //playerTowerEntity.WorldPos = twin.zero;
                        //playerTowerEntity.Position = twin.zero;

                        session.EntityId = playerTowerEntity.EntityId;

                        sessionToTowerEntities[session.address] = playerTowerEntity;
                        session.WriteChanges();
                        sessions.storyteller.Write(playerTowerEntity); // suddenly, you're embodied
                    }
                }
            }
                                                                                          ));

            link.SetPostHandler <RequestStories, OK>(RequestStories.OPCODE, poster =>
            {
                if (sessions.UsingPeer(poster.Peer.Id, out var session))
                {
                    Dj.Tempf("Server heard request from client");
                    poster.Reply();
                    PushStoriesToSession(session);
                }
            });

            link.SetPostHandler <WorldMove, OK>(WorldMove.OPCODE, poster =>
            {
                try
                {
                    sessions.UsingPeer(poster.Peer.Id, out var session);
                    var ent       = sessionToTowerEntities[session.address];
                    ent.WorldPos += poster.action.dir;
                    ent.WriteChanges();

                    poster.Reply();

                    PushEntitiesToSession(session);
                } catch {
                    poster.Reject(Barebones.Networking.ResponseStatus.Failed);
                }
            });

            SetupStartingZones();
        }