Gang Form Command

Status
Not open for further replies.
May 1, 2015
470
154
Hello,
I'm just wondering how i can check if a gang exists when creating one, I tried using if (table.Rows.Contains(GangName)) but that did not work either, here's what i have at the moment:
Code:
sing (IQueryAdapter Adapter = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                Adapter.SetQuery("SELECT gang_name FROM rp_gangs WHERE gang_name = @gangname");
                Adapter.AddParameter("gangname", GangName);
                table = Adapter.getTable();
            }

            if (table.Rows.Count > 0)
            {
                using (IQueryAdapter Adapter = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                {
                    Adapter.SetQuery("INSERT INTO rp_gangs (gang_name, gang_owner) VALUES (@gangname, @gangowner)");
                    Adapter.AddParameter("gangname", GangName);
                    Adapter.AddParameter("gangowner", GangOwner);
                    Adapter.RunQuery();
                }

                Session.SendWhisper("The gang " + GangName + " has been created.");
                return;
            }
            else
            {
                Session.SendWhisper("That gang already exists!");
                return;
            }
        }
    }
}
Thanks!
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
Do you have a gang manager class that has all the gangs cached? Checked the cached gangs to see if the name exists?
 
May 1, 2015
470
154
I already did that, i said that above.
It doesn't work, it actually stops the command from creating the gang, weird.
Heres my gang manager:
Code:
public sealed class GangManager
    {
        private List<Gangs> _Gangs;

        public GangManager()
        {
            this._Gangs = new List<Gangs>();
        }

        public static Dictionary<int, Gangs> GangData;

        public void Init()
        {
            if (this._Gangs.Count > 0)
                this._Gangs.Clear();

            DataTable Data = null;
            GangData = new Dictionary<int, Gangs>();

            using (IQueryAdapter Adapter = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                Adapter.SetQuery("SELECT * FROM rp_gangs");
                Data = Adapter.getTable();

                if (Data != null)
                {
                    foreach (DataRow Row in Data.Rows)
                    {
                        this._Gangs.Add(new Gangs(Convert.ToString(Row["gangname"]), Convert.ToString(Row["gangowner"])));
                    }
                }
            }
        }

        public bool IsGangOwner(GameClient Session)
        {
            foreach (Gangs Gang in this._Gangs.ToList())
            {
                if (Session.GetHabbo().Username == Gang.GangOwner)
                {
                    continue;
                }
            }
            return true;
        }

        public bool GangExists(string GangName)
        {
            foreach (KeyValuePair<int, Gangs> Gang in GangManager.GangData)
            {
                if (GangManager.GangData[Gang.Key].GangName == GangName)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
 
@Sledmore, any idea about this?
 

Sledmore

Chaturbate Livestreamer
Staff member
FindRetros Moderator
Jul 24, 2010
5,199
3,934
Don't really have the time to go through your code as I'm at work & busy (but lurking on DevBest, haha). Luckily for you, I have access to my Git, here is how I handle HabboRPs (wrote 2 years ago, probably could improve).

GangCommand.cs
PHP:
            if (Session.GetHabbo().GetRP().GetStats().GangId > 0)//Owns a gang.
            {
                if (Session.GetHabbo().GetRP().GetStats().GangRank == 1)
                    Session.SendWhisper("Oops, it appears you're already in a gang!");
                else if (Session.GetHabbo().GetRP().GetStats().GangRank == 2)
                    Session.SendWhisper("Oops, it appears you already own a gang!");
                return;
            }

            string GangName = CommandManager.MergeParams(Params, 1);
            if (String.IsNullOrEmpty(GangName) || String.IsNullOrWhiteSpace(GangName))
            {
                Session.SendWhisper("Oops, it appears there is an issue with that gang name, please choose another.");
                return;
            }

            if (GangName.Length > 25)
            {
                Session.SendWhisper("Oops, gang names must be no more than 25 characters in length!");
                return;
            }

            if (!PlusEnvironment.GetGame().GetGangs().AvailableName(GangName))
            {
                Session.SendWhisper("Oops, it appears that a gang is already using this name, think of a new one!");
                return;
            }

            Gang Gang = null;
            if (!PlusEnvironment.GetGame().GetGangs().TryCreateGang(Session.GetHabbo().Id, GangName, out Gang))
            {
                Session.SendWhisper("It appears an error has occured whilst creating your gang, please try again soon.");
                return;
            }

            Session.SendWhisper("You have successfully formed the " + Gang.Name + " gang!");

            if (!Session.GetHabbo().GetRP().GetStats().IsWorking && Session.GetHabbo().GetRP().GetStats().TimeDead == 0 && Session.GetHabbo().GetRP().GetStats().TimeArrested == 0 &&  Session.GetHabbo().GetRP().GetGameAvatar().GameItem != null)
                Session.GetHabbo().PoofUser(0);

            //Make the fake friendship.
            Session.GetHabbo().GetMessenger().CreateFakeFriendship(null, Gang);
            return;

GangManager.cs

PHP:
using System;
using System.Linq;
using System.Data;
using System.Collections.Generic;
using System.Collections.Concurrent;
using Plus.HabboHotel.Roleplay.Gangs.Turfs;
using Plus.Database.Interfaces;
using Plus.Utilities;

namespace Plus.HabboHotel.Roleplay.Gangs
{
    public class GangManager
    {
        private readonly Object _gangLoadingSync;
        private readonly ConcurrentDictionary<int, Gang> _gangs;//GangId, Gang.
        private readonly ConcurrentDictionary<int, Turf> _turfs;//RoomId, Turf.
        private readonly ConcurrentDictionary<int, Bounty> _bounties;//Id, Bounty

        public GangManager()
        {
            this._gangLoadingSync = new Object();
            this._gangs = new ConcurrentDictionary<int, Gang>();
            this._turfs = new ConcurrentDictionary<int, Turf>();
            this._bounties = new ConcurrentDictionary<int, Bounty>();
        }

        public void Init()
        {
            if (this._turfs.Count > 0)
                this._turfs.Clear();

            // Code removed.

            if (this._bounties.Count > 0)
                this._bounties.Clear();

            // Code removed.
        }

        public bool TryGetGang(int GangId, out Gang Gang)
        {
            Gang = null;

            if (this._gangs.ContainsKey(GangId))
                return this._gangs.TryGetValue(GangId, out Gang);

            lock (this._gangLoadingSync)
            {
                if (this._gangs.ContainsKey(GangId))
                    return this._gangs.TryGetValue(GangId, out Gang);

                DataRow Row = null;
                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                {
                    dbClient.setQuery("SELECT `id`,`name`,`owner_id`,`kills`,`arrests`,`deaths`,`jailbreaks`,`turf_captures`,`level`,`experience`,`badge` FROM `rp_gangs` WHERE `id` = @id LIMIT 1");
                    dbClient.addParameter("id", GangId);
                    Row = dbClient.getRow();

                    if (Row != null)
                    {
                        this._gangs.TryAdd(GangId, new Gang(Convert.ToInt32(Row["id"]), Convert.ToString(Row["name"]), Convert.ToInt32(Row["owner_id"]), Convert.ToInt32(Row["kills"]), Convert.ToInt32(Row["arrests"]), Convert.ToInt32(Row["deaths"]), Convert.ToInt32(Row["jailbreaks"]), Convert.ToInt32(Row["turf_captures"]), Convert.ToInt32(Row["level"]), Convert.ToInt32(Row["experience"]), Convert.ToString(Row["badge"])));
                        return true;
                    }
                }
            }
            return false;
        }

        public bool TryRemoveGang(int GangId, out Gang Gang)
        {
            return this._gangs.TryRemove(GangId, out Gang);
        }

        public bool TryCreateGang(int OwnerId, string Name, out Gang Gang)
        {
            Gang = new Gang(0, Name, OwnerId, 0, 0, 0, 0, 0, 0, 0, "b05114s06114");

            if (string.IsNullOrWhiteSpace(Name))//Eh, extra security?
                return false;

            using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                dbClient.setQuery("INSERT INTO `rp_gangs` (`name`,`owner_id`,`timestamp_created`) VALUES (@name, @ownerId, @timestampCreated)");
                dbClient.addParameter("name", Name);
                dbClient.addParameter("ownerId", OwnerId);
                dbClient.addParameter("timestampCreated", UnixTimestamp.GetNow());
                dbClient.RunQuery();

                Gang.Id = dbClient.SelectLastId();
            }

            if (!this._gangs.TryAdd(Gang.Id, Gang))
                return false;//Ooh, uh...

            Gang.AddMember(OwnerId, 2);
            return true;
        }

        public bool AvailableName(string GangName)
        {
            DataRow Row = null;
            using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                dbClient.setQuery("SELECT * FROM `rp_gangs` WHERE `name` = @name LIMIT 1");
                dbClient.addParameter("name", GangName);
                Row = dbClient.getRow();
            }

            if (Row != null)
                return false;
            return true;
        }

        public bool TryCreateTurf(string Name, int RoomId, int CoordX, int CoordY, out Turf Turf)
        {
            // Code removed.
        }

        public bool TryGetTurf(int RoomId, out Turf Turf)
        {
            return this._turfs.TryGetValue(RoomId, out Turf);
        }

        public bool TryRemoveTurf(int RoomId, out Turf Turf)
        {
            return this._turfs.TryRemove(RoomId, out Turf);
        }

        public bool TryStartBounty(int UserId, int GangId, int TargetUserId, int TargetGangId, int CashBond, out Bounty Bounty)
        {
            // Code removed.
        }

        public ICollection<Turf> GetTurfs
        {
            get { return this._turfs.Values; }
        }

        public ICollection<Gang> GetGangs
        {
            get { return this._gangs.Values; }
        }

        public ICollection<Bounty> GetBounties
        {
            get { return this._bounties.Values; }
        }

        public void ProgressTurfs()
        {
            // Code removed.
        }

        public void Save()
        {
            using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                foreach (Gang Gang in this._gangs.Values.ToList())
                {
                    if (Gang == null)
                        continue;

                    dbClient.RunFastQuery("UPDATE `rp_gangs` SET `kills` = '" + Gang.Kills + "', `arrests` = '" + Gang.Arrests + "', `deaths` = '" + Gang.Deaths + "', `jailbreaks` = '" + Gang.Jailbreaks + "', `turf_captures` = '" + Gang.TurfCaptures + "', `level` = '" + Gang.GangLevel + "', `experience` = '" + Gang.GangExperience + "' WHERE `id` = '" + Gang.Id + "'");
                }

                // Code removed.
            }
        }

        public void Dispose()
        {
            this.Save();

            this._gangs.Clear();
            this._turfs.Clear();
        }
    }
}
 
Status
Not open for further replies.

Users who are viewing this thread

Top