Пример #1
0
        public bool Contains(string name, int quantity)
        {
            var itemList = Game.World.WorldData.FindItem(name);

            if (itemList.Count == 0)
            {
                return(false);
            }

            foreach (var item in itemList)
            {
                if (!_inventoryIndex.ContainsKey(item.Id))
                {
                    continue;
                }

                var total = _inventoryIndex.Where(x => x.Key == item.Id).First().Value.Sum(y => y.Count);
                GameLog.Info($"Contains check: {name}, quantity {quantity}: {total} found");
                if (total >= quantity)
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #2
0
        public Guild(string name, User leader, List<User> founders)
        {
            Name = name;
            Uuid = Guid.NewGuid().ToString();
            Ranks = new List<GuildRank>();

            //default ranks, with guid as key so naming can be changed by user
            Ranks.Add(new GuildRank() { Uuid = Guid.NewGuid().ToString(), Name = "Guild Leader", Level = 0 });
            Ranks.Add(new GuildRank() { Uuid = Guid.NewGuid().ToString(), Name = "Council", Level = 1 });
            Ranks.Add(new GuildRank() { Uuid = Guid.NewGuid().ToString(), Name = "Founder", Level = 2 });
            Ranks.Add(new GuildRank() { Uuid = Guid.NewGuid().ToString(), Name = "Member", Level = 3 });
            Ranks.Add(new GuildRank() { Uuid = Guid.NewGuid().ToString(), Name = "Initiate", Level = 4 });
            GameLog.Info($"Guild {name}: Added default ranks");
            GameLog.Info($"Guild {name}: Created guild board");
            Members = new Dictionary<string, GuildMember>();

            var leaderGuid = Ranks.FirstOrDefault(x => x.Name == "Guild Leader").Uuid;
            var founderGuid = Ranks.FirstOrDefault(x => x.Name == "Founder").Uuid;

            Members.Add(leader.Uuid, new GuildMember() { Name = leader.Name, RankUuid = leaderGuid });
            GameLog.Info($"Guild {name}: Adding leader {leader.Name}");
            foreach (var founder in founders)
            {
                Members.Add(founder.Uuid, new GuildMember() { Name = founder.Name, RankUuid = founderGuid });
                GameLog.Info($"Guild {name}: Adding founder {founder.Name}");
            }


        }
Пример #3
0
        public void Redirect(Redirect redirect, bool isLogoff = false)
        {
            GameLog.InfoFormat("Processing redirect");
            GlobalConnectionManifest.RegisterRedirect(this, redirect);
            GameLog.InfoFormat("Redirect: cid {0}", this.ConnectionId);
            GameLog.Info($"Redirect EncryptionKey is {Encoding.ASCII.GetString(redirect.EncryptionKey)}");
            if (isLogoff)
            {
                GlobalConnectionManifest.DeregisterClient(this);
            }
            redirect.Destination.ExpectedConnections.TryAdd(redirect.Id, redirect);

            var endPoint = Socket.RemoteEndPoint as IPEndPoint;

            byte[] addressBytes = IPAddress.IsLoopback(endPoint.Address) ? IPAddress.Loopback.GetAddressBytes() : Game.IpAddress.GetAddressBytes();

            Array.Reverse(addressBytes);

            var x03 = new ServerPacket(0x03);

            x03.Write(addressBytes);
            x03.WriteUInt16((ushort)redirect.Destination.Port);
            x03.WriteByte((byte)(redirect.EncryptionKey.Length + Encoding.ASCII.GetBytes(redirect.Name).Length + 7));
            x03.WriteByte(redirect.EncryptionSeed);
            x03.WriteByte((byte)redirect.EncryptionKey.Length);
            x03.Write(redirect.EncryptionKey);
            x03.WriteString8(redirect.Name);
            x03.WriteUInt32(redirect.Id);
            Thread.Sleep(100);
            Enqueue(x03);
        }
Пример #4
0
        public Board GetBoard(string name)
        {
            Board newBoard;

            if (ContainsKey <Board>(name))
            {
                return(Get <Board>(name));
            }
            var newBoardId = Values <Board>().Count() + 1;

            // Check redis first, then fall back to creation
            if (Redis.KeyExists(Board.GetStorageKey(name)))
            {
                newBoard = Redis.Get <Board>(Board.GetStorageKey(name));
                GameLog.Info("Board: loaded {boardname}", name);
                newBoard.Id = newBoardId;
            }
            else
            {
                newBoard = new Board(name)
                {
                    Id = newBoardId
                };
                newBoard.Save();
                GameLog.Info("Board: Creating {boardname}", name);
            }
            SetWithIndex(name, newBoard, newBoard.Id);
            return(Get <Board>(name));
        }
Пример #5
0
        public void ChangeRankTitle(string oldTitle, string newTitle)
        {
            var rank = Ranks.FirstOrDefault(x => x.Name == oldTitle);

            if (rank != null)
            {
                rank.Name = newTitle;
                GameLog.Info($"Guild {Name}: Renaming rank {oldTitle} to rank {newTitle}");
            }
        }
Пример #6
0
        public void AddRank(string title) //adds a new rank at the lowest tier
        {
            if (Ranks.Any(x => x.Name == title)) return;

            var lowestRank = Ranks.Aggregate((r1, r2) => r1.Level > r2.Level ? r1 : r2);

            var rank = new GuildRank() { Uuid = Guid.NewGuid().ToString(), Name = title, Level = lowestRank.Level + 1 };

            Ranks.Add(rank);
            GameLog.Info($"Guild {Name}: New rank {rank.Name} added as level {rank.Level}");
        }
Пример #7
0
        public void PromoteMember(string name)
        {
            var member      = Members.Single(x => x.Value.Name == name);
            var currentRank = Ranks.FirstOrDefault(x => x.Uuid == member.Value.RankUuid);
            var newRank     = Ranks.FirstOrDefault(x => x.Level == currentRank.Level - 1);

            if (newRank != null && newRank.Level > 0) //we can only have one leader
            {
                member.Value.RankUuid = newRank.Uuid;
                GameLog.Info($"Guild {Name}: Promoting {member.Value.Name} to rank {newRank.Name}");
            }
        }
Пример #8
0
 public void RemoveMember(User user)
 {
     var member = Members.Single(x => x.Value.Name == user.Name);
     if(member.Value.RankUuid == Ranks.Single(x => x.Level == 0).Uuid)
     {
         GameLog.Info($"Guild {Name}: Sorry, the guild leader can't be removed.");
         return;
     }
     Members.Remove(member.Key);
     user.GuildUuid = null;
     GameLog.Info($"Guild {Name}: Removing member {user.Name}");
 }
Пример #9
0
 public bool RemoveGold(uint gold)
 {
     if (gold <= CurrentGold)
     {
         CurrentGold -= gold;
         GameLog.Info($"{gold} gold removed from vault {OwnerUuid}");
         return(true);
     }
     else
     {
         GameLog.Info($"Attempt to remove {gold} gold from vault {OwnerUuid}, but only {CurrentGold} available");
         return(false);
     }
 }
Пример #10
0
        public bool AddGold(uint gold)
        {
            if (gold <= RemainingGold)
            {
                CurrentGold += gold;

                GameLog.Info($"{gold} gold added to vault {OwnerUuid}");
                return(true);
            }
            else
            {
                GameLog.Info($"Attempt to add {gold} gold to vault {OwnerUuid}, but only {RemainingGold} available");
                return(false);
            }
        }
Пример #11
0
        public void AddMember(User user)
        {
            if(user.GuildUuid != null)
            {
                GameLog.Info($"Guild {Name}: Attempt to add {user.Name} to guild, but user is already in another guild.");
                return;
            }

            var lowestRank = Ranks.Aggregate((r1, r2) => r1.Level > r2.Level ? r1 : r2);
            GameLog.Info($"Guild {Name}: Lowest guild rank identified as {lowestRank.Name}");
            Members.Add(user.Uuid, new GuildMember() { Name = user.Name, RankUuid = lowestRank.Uuid });
            user.GuildUuid = Uuid;
            GameLog.Info($"Guild {Name}: Adding new member {user.Name} to rank {lowestRank.Name}");
            
        }
Пример #12
0
        public void DemoteMember(string name)
        {
            var member      = Members.Single(x => x.Value.Name == name);
            var currentRank = Ranks.FirstOrDefault(x => x.Uuid == member.Value.RankUuid);
            var newRank     = Ranks.FirstOrDefault(x => x.Level == currentRank.Level + 1);

            if (newRank != null && newRank.Level > currentRank.Level)
            {
                if (currentRank.Level == 0)
                {
                    GameLog.Info($"Guild {Name}: Sorry, the guild leader cannot be demoted.");
                    return;
                }
                member.Value.RankUuid = newRank.Uuid;
                GameLog.Info($"Guild {Name}: Demoting {member.Value.Name} to rank {newRank.Name}");
            }
        }
Пример #13
0
        /// <summary>
        /// Perform the exchange once confirmation from both sides is received.
        /// </summary>
        /// <returns></returns>
        public void PerformExchange()
        {
            GameLog.Info("Performing exchange");
            foreach (var item in _sourceItems)
            {
                _target.AddItem(item);
            }
            foreach (var item in _targetItems)
            {
                _source.AddItem(item);
            }
            _source.AddGold(_targetGold);
            _target.AddGold(_sourceGold);

            _source.ActiveExchange       = null;
            _target.ActiveExchange       = null;
            _source.Condition.InExchange = false;
            _target.Condition.InExchange = false;
        }
Пример #14
0
        public void RemoveRank() //only remove the lowest tier rank and move all members in rank up one level.
        {
            var lowestRank = Ranks.Aggregate((r1, r2) => r1.Level > r2.Level ? r1 : r2);
            var nextRank   = Ranks.FirstOrDefault(x => x.Level == lowestRank.Level - 1);

            if (nextRank != null && nextRank.Level != 0)
            {
                var moveMembers = Members.Where(x => x.Value.RankUuid == lowestRank.Uuid).ToList();

                foreach (var member in moveMembers)
                {
                    member.Value.RankUuid = nextRank.Uuid;
                    GameLog.Info($"Guild {Name}: Member {member.Value.Name} moved to rank {nextRank.Name} due to rank deletion");
                }

                //remove lowest rank here to avoid missing members
                Ranks.Remove(lowestRank);
                GameLog.Info($"Guild {Name}: Deleted rank {lowestRank.Name}");
            }
        }
Пример #15
0
 public bool RemoveItem(string itemName, ushort quantity = 1)
 {
     if (Items.ContainsKey(itemName))
     {
         if (Items[itemName] > quantity)
         {
             Items[itemName] -= quantity;
             GameLog.Info($"{itemName} [{quantity}] removed from existing item in vault {OwnerUuid}");
         }
         else
         {
             Items.Remove(itemName);
             GameLog.Info($"{itemName} removed from vault {OwnerUuid}");
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #16
0
 public bool AddItem(string itemName, ushort quantity = 1)
 {
     if (CurrentItemCount < ItemLimit)
     {
         if (Items.ContainsKey(itemName))
         {
             Items[itemName] += quantity;
             GameLog.Info($"{itemName} [{quantity}] added to existing item in vault {OwnerUuid}");
         }
         else
         {
             Items.Add(itemName, quantity);
             GameLog.Info($"{itemName} [{quantity}] added as new item in vault {OwnerUuid}");
         }
         return(true);
     }
     else
     {
         GameLog.Info($"Attempt to add {itemName} [{quantity}] to vault {OwnerUuid}, but user doesn't have it?");
         return(false);
     }
 }
Пример #17
0
 /// <summary>
 /// Confirm the exchange. Once both sides confirm, perform the exchange.
 /// </summary>
 /// <returns>Boolean indicating success.</returns>
 public void ConfirmExchange(User requestor)
 {
     if (_source == requestor)
     {
         GameLog.InfoFormat("Exchange: source ({0}) confirmed", _source.Name);
         _sourceConfirmed = true;
         _target.SendExchangeConfirmation(false);
     }
     if (_target == requestor)
     {
         GameLog.InfoFormat("Exchange: target ({0}) confirmed", _target.Name);
         _targetConfirmed = true;
         _source.SendExchangeConfirmation(false);
     }
     if (_sourceConfirmed && _targetConfirmed)
     {
         GameLog.Info("Exchange: Both sides confirmed");
         _source.SendExchangeConfirmation();
         _target.SendExchangeConfirmation();
         PerformExchange();
     }
 }
Пример #18
0
        public void FlushReceiveBuffer()
        {
            lock (ClientState.ReceiveLock)
            {
                try
                {
                    ClientPacket packet;
                    while (ClientState.ReceiveBufferTake(out packet))
                    {
                        if (packet.ShouldEncrypt)
                        {
                            packet.Decrypt(this);
                        }

                        if (packet.Opcode == 0x39 || packet.Opcode == 0x3A)
                        {
                            packet.DecryptDialog();
                        }
                        try
                        {
                            if (Server is Lobby)
                            {
                                GameLog.DebugFormat("Lobby: 0x{0:X2}", packet.Opcode);
                                var handler = (Server as Lobby).PacketHandlers[packet.Opcode];
                                handler.Invoke(this, packet);
                                GameLog.DebugFormat("Lobby packet done");
                                UpdateLastReceived();
                            }
                            else if (Server is Login)
                            {
                                GameLog.Info($"Login: 0x{packet.Opcode:X2}");
                                var handler = (Server as Login).PacketHandlers[packet.Opcode];
                                handler.Invoke(this, packet);
                                GameLog.DebugFormat("Login packet done");
                                UpdateLastReceived();
                            }
                            else
                            {
                                UpdateLastReceived(packet.Opcode != 0x45 &&
                                                   packet.Opcode != 0x75);
                                GameLog.Info($"Queuing: 0x{packet.Opcode:X2}");
                                // Check for throttling
                                var throttleResult = Server.PacketThrottleCheck(this, packet);
                                if (throttleResult == ThrottleResult.OK || throttleResult == ThrottleResult.ThrottleEnd || throttleResult == ThrottleResult.SquelchEnd)
                                {
                                    World.MessageQueue.Add(new HybrasylClientMessage(packet, ConnectionId));
                                }
                                else
                                if (packet.Opcode == 0x06)
                                {
                                    World.ControlMessageQueue.Add(new HybrasylControlMessage(ControlOpcodes.TriggerRefresh, ConnectionId));
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            GameLog.ErrorFormat("EXCEPTION IN HANDLING: 0x{0:X2}: {1}", packet.Opcode, e);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }