Esempio n. 1
0
        public void DoRegens(object state)
        {
            var bodies = _db.BodyIds().Where(bodyId => _db.Health(bodyId) > 0).ToArray();
            foreach (var body in bodies)
            {
                if (SafeRandom.Next(5) == 0)
                {
                    int energy = _db.Energy(body);
                    if (energy < 100)
                    {
                        double energyDiff = 100.0 - energy;
                        int newEnergy = (int)Math.Min(100.0, energy + Math.Ceiling(energyDiff / 19.0));
                        _db.Energy(body, newEnergy);

                        var energyCmd = new CmdToClient("Energy", newEnergy.ToString());
                        _clients.TellBody(body, energyCmd.ToString());
                    }

                    int health = _db.Health(body);
                    if (health < 100)
                    {
                        double healthDiff = 100.0 - health;
                        int newHealth = (int)Math.Min(100, health + Math.Ceiling(healthDiff / 19.0));
                        _db.Health(body, newHealth);

                        var healthCmd = new CmdToClient("Health", newHealth.ToString());
                        _clients.TellBody(body, healthCmd.ToString());
                    }
                }
            }
        }
Esempio n. 2
0
        public static void Notify(string scene, string actor)
        {
            var actorBody = Db.Body(actor);

            var bodyUnhidden = new CmdToClient("Unhide", JsonConvert.SerializeObject(actorBody));

            Clients.TellScene(scene, bodyUnhidden.ToString());
        }
Esempio n. 3
0
        public override void Run()
        {
            if (Args.Count < 1)
                Do.Error(Actor, ErrorCode.InvalidTarget);

            string claimedClientId = Args[0];
            string clientId = Connection.Id.ToString();

            Action loginFail = () =>
            {
                var loginFailMsg = new CmdToClient("LoginFail", null);
                Connection.Send(loginFailMsg.ToString());
            };

            if (Args.Count != 1)
                loginFail();
            else
            {

                if (String.IsNullOrWhiteSpace(claimedClientId))
                    loginFail();
                else
                {
                    var client = Clients.ById(claimedClientId);

                    if (client == null)
                        loginFail();
                    else
                    {
                        Connection.AccessLevel = Guest | Player;

                        // Update client index
                        var newClient = new Client(Connection)
                        {
                            Id = clientId,
                            BodyId = client.BodyId
                        };
                        Clients.Remove(client.Id);
                        Clients.Add(newClient);

                        var bodyId = client.BodyId;
                        var body = _db.Body(bodyId);

                        var signedInMsg = new CmdToClient("SignedIn", JsonConvert.SerializeObject(body), clientId);
                        Connection.Send(signedInMsg.ToString());

                        Do.InventoryChecked(bodyId, _db.ItemsByBody(bodyId));
                        Do.EnergyChanged(bodyId, _db.Energy(bodyId));
                        Do.HealthChanged(bodyId, _db.Health(bodyId));

                        var sceneId = _db.BodySpawnScene(bodyId);

                        Do.AddBodyToWorld(sceneId, bodyId);
                    }
                }
            }
        }
Esempio n. 4
0
        public static void BodyBitten(string scene, string actor, string target, bool infected)
        {
            var command = new CmdToClient("Bite",
                JsonConvert.SerializeObject(_db.Body(actor)),
                JsonConvert.SerializeObject(_db.Body(target)),
                (infected ? 1 : 0).ToString());

            Clients.TellScene(scene, command.ToString());
        }
Esempio n. 5
0
        public static void BodyAttacked(string scene, string actor, string target, int damage, string weapon = null)
        {
            var command = new CmdToClient("Attack",
                JsonConvert.SerializeObject(_db.Body(actor)),
                JsonConvert.SerializeObject(_db.Body(target)),
                damage.ToString(),
                weapon == null ? null : weapon);

            Clients.TellScene(scene, command.ToString());
        }
Esempio n. 6
0
        public override void Run()
        {
            if (Args.Count != 1)
                NotifyFail();
            else
            {
                string claimedClientId = Args[0];
                string clientId = Connection.Id.ToString();

                if (String.IsNullOrWhiteSpace(claimedClientId))
                    NotifyFail();
                else
                {
                    var client = Clients.ById(claimedClientId);

                    if (client == null)
                        NotifyFail();
                    else
                    {
                        Connection.AccessLevel = Guest | Player;

                        // Update client index
                        var newClient = new Client(Connection)
                        {
                            Id = clientId,
                            BodyId = client.BodyId
                        };
                        Clients.Remove(client.Id);
                        Clients.Add(newClient);

                        var bodyId = client.BodyId;
                        var body = Db.Body(bodyId);

                        var signedInMsg = new CmdToClient("SignedIn", JsonConvert.SerializeObject(body), clientId);
                        Connection.Send(signedInMsg.ToString());

                        // Inventory
                        var items = Db.ItemsByBody(bodyId);
                        var inventoryCmd = new CmdToClient("Inventory", JsonConvert.SerializeObject(items));
                        Clients.TellBody(bodyId, inventoryCmd.ToString());

                        var energyCmd = new CmdToClient("Energy", Db.Energy(bodyId).ToString());
                        Clients.TellBody(bodyId, energyCmd.ToString());

                        var healthCmd = new CmdToClient("Health", Db.Health(bodyId).ToString());
                        Clients.TellBody(bodyId, healthCmd.ToString());

                        var sceneId = Db.BodySpawnScene(bodyId);

                        SpawnDaemon.SpawnBody(body, sceneId);
                    }
                }
            }
        }
Esempio n. 7
0
        public static void AddBodyToWorld(string scene, string actor)
        {
            _db.PutBodyInScene(actor, scene);

            // Describe scene to actor.
            DescribeScene(scene, actor);

            // Notify scene of the arrival.
            var arrival = new CmdToClient("Arrive", JsonConvert.SerializeObject(_db.Body(actor)));
            Clients.TellScene(scene, arrival.ToString());
        }
Esempio n. 8
0
        public static void Notify(string oldScene, string actor, string newScene)
        {
            // Notify old scene that body left.
            var leaveCommand = new CmdToClient("Leave", actor);
            Clients.TellScene(oldScene, leaveCommand.ToString());

            // Describe new scene to actor.
            Do.DescribeScene(newScene, actor);

            // Notify new scene that body arrived.
            var arriveCommand = new CmdToClient("Arrive", JsonConvert.SerializeObject(Db.Body(actor)));
            Clients.TellScene(newScene, arriveCommand.ToString());
        }
Esempio n. 9
0
        public static void DescribeScene(string scene, string actor)
        {
            var visibleBodies = _db.BodiesByScene(scene).Where(body => !_db.IsBodyHidden(body.Id)).ToArray();
            var visibleItems = _db.ItemsByScene(scene).Where(item => !_db.IsItemHidden(item.Id)).ToArray();

            var description = new CmdToClient("Scene",
                JsonConvert.SerializeObject(_db.Scene(scene)),
                JsonConvert.SerializeObject(visibleBodies),
                JsonConvert.SerializeObject(visibleItems));
            Clients.TellBody(actor, description.ToString());

            var exits = new CmdToClient("Exits", _db.SceneExits(scene).Select(exit => JsonConvert.SerializeObject(exit)).ToArray());
            Clients.TellBody(actor, exits.ToString());
        }
Esempio n. 10
0
        public override void Run()
        {
            Action<string> registerFail = (string reason) =>
            {
                var failMsg = new CmdToClient("RegisterFail", reason);
                Connection.Send(failMsg.ToString());
            };

            if (Args.Count != 2)
                registerFail("Name and gender needed");

            var name = Args[0];

            if (String.IsNullOrWhiteSpace(name))
                registerFail("Name is missing");
            if (name.Any(c => !Char.IsLetter(c)))
                registerFail("Name should contain only letters");
            else if (name.Length < 3 || name.Length > 15)
                registerFail("Name should be 3 to 15 letters");
            else
            {
                name = name[0].ToString().ToUpper() + name.Substring(1).ToLowerInvariant();

                Gender gender;
                if (!Enum.TryParse<Gender>(Args[1], out gender))
                    registerFail("Gender is invalid");
                else
                {
                    var body = _db.CreateBody("body-player");
                    body.Name = name;
                    body.Gender = gender;
                    _db.SetName(body.Id, name);
                    _db.SetGender(body.Id, gender);

                    string clientId = Connection.Id.ToString();

                    var client = new Client(Connection)
                    {
                        Id = clientId,
                        BodyId = body.Id
                    };

                    Clients.Add(client);

                    var createdMsg = new CmdToClient("BodyCreated", clientId);
                    Connection.Send(createdMsg.ToString());
                }
            }
        }
Esempio n. 11
0
        public void SpawnBody(Body body, string sceneId)
        {
            if (String.IsNullOrWhiteSpace(body.Name))
            {
                if (_db.HasRole(body.Id, "infected"))
                    body.Name = Names.RandomZombieName();
                else if (body.Cardinality == 0)
                    body.Name = Names.RandomHumanName();

                _db.SetName(body.Id, body.Name);
            }

            _db.PutBodyInScene(body.Id, sceneId);

            // Describe scene to actor.
            Do.DescribeScene(sceneId, body.Id);

            // Notify scene of the arrival.
            var arrival = new CmdToClient("Arrive", JsonConvert.SerializeObject(body));
            _clients.TellScene(sceneId, arrival.ToString());
        }
Esempio n. 12
0
        public void DoDecays(object state)
        {
            lock (_decays)
            {
                var now = DateTime.Now;
                var expiredDefs = _decays.Where(d => d.Expiry < now).ToArray();
                foreach (var decayDef in expiredDefs)
                {
                    if (SafeRandom.Next(3) == 0)
                    {
                        var sceneId = _db.ItemSceneId(decayDef.Item);
                        if (sceneId != null)
                        {
                            var cmd = new CmdToClient("DecayItem", decayDef.Item);
                            _clients.TellScene(sceneId, cmd.ToString());
                        }

                        _decays.Remove(decayDef);
                        _db.DeleteItem(decayDef.Item);
                    }
                }
            }
        }
Esempio n. 13
0
 public static void ItemConsumed(string scene, string actor, string item, string action)
 {
     var cmd = new CmdToClient(action, actor, JsonConvert.SerializeObject(_db.Item(item)));
     Clients.TellScene(scene, cmd.ToString());
 }
Esempio n. 14
0
        public static void BodyActed(string scene, string actor, string action)
        {
            var command = new CmdToClient(action, actor);

            Clients.TellScene(scene, command.ToString());
        }
Esempio n. 15
0
        public static void BodyUnhidden(string scene, string actor)
        {
            var actorBody = _db.Body(actor);

            var command = new CmdToClient("Unhide", JsonConvert.SerializeObject(actorBody));

            Clients.TellScene(scene, command.ToString());
        }
Esempio n. 16
0
        public static void Notify(string scene, string actor, string target, Item item)
        {
            var command = new CmdToClient("GiveItem", actor, target, JsonConvert.SerializeObject(item));

            Clients.TellScene(scene, command.ToString());
        }
Esempio n. 17
0
 public static void Notify(string scene, string actor, Item item)
 {
     var cmd = new CmdToClient("Drop", actor, JsonConvert.SerializeObject(item));
     Clients.TellScene(scene, cmd.ToString());
 }
Esempio n. 18
0
 public static void ItemHidden(string scene, string actor, string item)
 {
     var cmd = new CmdToClient("HideItem", actor, JsonConvert.SerializeObject(_db.Item(item)));
     Clients.TellScene(scene, cmd.ToString());
 }
Esempio n. 19
0
 public static void Error(string actor, ErrorCode errorCode)
 {
     var msg = new CmdToClient("Error", ((int)errorCode).ToString());
     Clients.TellBody(actor, msg.ToString());
 }
Esempio n. 20
0
        public static void BodyHealed(string scene, string actor, string target, int amount)
        {
            var command = new CmdToClient("Heal", actor, target, amount.ToString());

            Clients.TellScene(scene, command.ToString());
        }
Esempio n. 21
0
		public static void Notify(string actor, Score score)
		{
			var cmd = new CmdToClient("Score", JsonConvert.SerializeObject(score));
			Clients.TellBody(actor, cmd.ToString());
		}
Esempio n. 22
0
 public static void HealthChanged(string actor, int amount)
 {
     var cmd = new CmdToClient("Health", amount.ToString());
     Clients.TellBody(actor, cmd.ToString());
 }
Esempio n. 23
0
        public static void BodyCured(string scene, string actor, string target, Item item, bool success)
        {
            var command = new CmdToClient("Cure", actor, target, JsonConvert.SerializeObject(item), (success ? 1 : 0).ToString());

            Clients.TellScene(scene, command.ToString());
        }
Esempio n. 24
0
        public static void InventoryChecked(string actor, IEnumerable<Item> items)
        {
            var command = new CmdToClient("Inventory", JsonConvert.SerializeObject(items));

            Clients.TellBody(actor, command.ToString());
        }
Esempio n. 25
0
 public static void ItemDecayed(string scene, string item)
 {
     var cmd = new CmdToClient("DecayItem", item);
     Clients.TellScene(scene, cmd.ToString());
 }
Esempio n. 26
0
 public static void ItemPut(string scene, string actor, Item item, Item container)
 {
     var cmd = new CmdToClient("Put", actor, JsonConvert.SerializeObject(item), JsonConvert.SerializeObject(container));
     Clients.TellScene(scene, cmd.ToString());
 }
Esempio n. 27
0
 public void NotifyFail(string reason)
 {
     var failMsg = new CmdToClient("RegisterFail", reason);
     Connection.Send(failMsg.ToString());
 }
Esempio n. 28
0
 public void NotifyFail()
 {
     var loginFailMsg = new CmdToClient("LoginFail", null);
     Connection.Send(loginFailMsg.ToString());
 }
Esempio n. 29
0
 public static void ItemSpawned(string scene, Item item)
 {
     var cmd = new CmdToClient("SpawnItem", JsonConvert.SerializeObject(item));
     Clients.TellScene(scene, cmd.ToString());
 }
Esempio n. 30
0
        public static void ItemFound(string scene, string actor, string item)
        {
            var command = new CmdToClient("FindItem", actor, item);

            Clients.TellScene(scene, command.ToString());
        }