All pastes #922871 Raw Edit

Unnamed

public text v1 · immutable
#922871 ·published 2008-02-29 02:08 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NLog;
using WCell.Core;
using WCell.Core.Initialization;
using WCell.Core.DBC;
using WCell.Core.Network;

using WCell.RealmServer.Entities;

namespace WCell.RealmServer.Help
{
  

    // Ticket System.
    // The low EntityId is used for the ID of the tickets, since there can only be
    // one ticket per character.
    //
    // ~gremlin

    public static class TicketHandler
    {
        private static Logger log = LogManager.GetCurrentClassLogger();

        // Used for the System Status Packet
        public static uint TicketSystemStatus = 1;

        public static Dictionary<uint, Ticket> dic = new Dictionary<uint, Ticket>();

        // General Functions, Can be used anywhere :)

        public static void CreateTicket(Ticket ticket, uint id)
        {
            Ticket newticket = new Ticket();

            newticket.Id = id;
            newticket.Type = ticket.Type;
            newticket.PositionX = ticket.PositionX;
            newticket.PositionY = ticket.PositionY;
            newticket.PositionZ = ticket.PositionZ;
            newticket.Message = ticket.Message;
            newticket.Timestamp = DateTime.Now.ToLongTimeString();

            dic[id] = newticket;
        }

        public static Ticket GetTicket(uint id)
        {
            Ticket ticket;
            if (dic.TryGetValue(id, out ticket))
            {
                return ticket;
            }
            else
            {
                return new Ticket();
            }
        }

        public static void UpdateTicket(Ticket updticket, uint id)
        {
            Ticket ticket;
            if (dic.TryGetValue(id, out ticket))
            {
                ticket.Message = updticket.Message;
                ticket.Timestamp = updticket.Timestamp;
                dic.Remove(id);
                dic[id] = ticket;
            }

        }

        public static void DeleteTicket(uint id)
        {
            dic.Remove(id);
        }


        // Packet Handlers


        // System Status Packet, Not sure exactly what it does, 
        // i think it tells the client if tickets are active or not.

        [PacketHandler(RealmServerOpCode.CMSG_GMTICKET_SYSTEMSTATUS)]
        public static void HandleSystemStatusPacket(RealmClient client, RealmPacketIn packet)
        {
            using (RealmPacketOut repPacket = new RealmPacketOut(RealmServerOpCode.SMSG_GMTICKET_SYSTEMSTATUS))
            {

                repPacket.WriteUInt((uint)TicketSystemStatus);
                client.Send(repPacket);

            }
        }

        [PacketHandler(RealmServerOpCode.CMSG_GMTICKET_CREATE)]
        public static void HandleCreateTicketPacket(RealmClient client, RealmPacketIn packet)
        {
            if (client.ActiveCharacter == null)
            {
                return;
            }
            Ticket ticket = new Ticket();

            uint id = client.ActiveCharacter.EntityId.Low;
            ticket.Type = packet.ReadUInt32();
            packet.ReadBytes(1);
            ticket.PositionX = packet.ReadFloat();
            ticket.PositionY = packet.ReadFloat();
            ticket.PositionZ = packet.ReadFloat();
            ticket.Message = packet.ReadString();

            CreateTicket(ticket, id);
            SendCreateTicketPacket(client);
            
        }

        [PacketHandler(RealmServerOpCode.CMSG_GMTICKET_GETTICKET)]
        public static void HandleGetTicketPacket(RealmClient client, RealmPacketIn packet)
        {
            SendGetTicketPacket(client, client.ActiveCharacter.EntityId.Low);
        }

        [PacketHandler(RealmServerOpCode.CMSG_GMTICKET_DELETETICKET)]
        public static void HandleDeleteTicketPacket(RealmClient client, RealmPacketIn packet)
        {
            DeleteTicket(client.ActiveCharacter.EntityId.Low);
            SendDeleteTicketPacket(client);
        }

        [PacketHandler(RealmServerOpCode.CMSG_GMTICKET_UPDATETEXT)]
        public static void HandleUpdateTicketPacket(RealmClient client, RealmPacketIn packet)
        {
            Ticket ticket;
            ticket = GetTicket(client.ActiveCharacter.EntityId.Low);
            packet.ReadBytes(1);
            ticket.Message = packet.ReadString();
            ticket.Timestamp = DateTime.Now.ToLongTimeString();
            UpdateTicket(ticket, client.ActiveCharacter.EntityId.Low);
            SendUpdateTicketPacket(client, client.ActiveCharacter.EntityId.Low);
        }

        // Packet Senders

        public static void SendGetTicketPacket(RealmClient client, uint id)
        {
            using (RealmPacketOut packet = new RealmPacketOut(RealmServerOpCode.SMSG_GMTICKET_GETTICKET))
            {
                Ticket ticket;
                ticket = GetTicket(id);

                if (!ticket.Id.Equals(id))
                {
                    return;
                }
                else
                {
                    packet.WriteUInt(6);
                    packet.WriteCString(ticket.Message);
                    packet.WriteByte((byte)ticket.Type);
                    client.Send(packet);
                }

            }
        }

        public static void SendUpdateTicketPacket(RealmClient client, uint id)
        {
            using (RealmPacketOut packet = new RealmPacketOut(RealmServerOpCode.SMSG_GMTICKET_UPDATETEXT))
            {
                
                Ticket ticket;
                ticket = GetTicket(id);

                if (!ticket.Id.Equals(id))
                {
                    packet.WriteUInt(1);
                    client.Send(packet);

                }
                else
                {
                    packet.WriteUInt(2);
                    client.Send(packet);
                }

            }
        }

        // These two are down here because all they do is tell the client 
        // the server got the information.

        public static void SendCreateTicketPacket(RealmClient client)
        {
            using (RealmPacketOut packet = new RealmPacketOut(RealmServerOpCode.SMSG_GMTICKET_CREATE))
            {
                packet.WriteUInt(2);
                client.Send(packet);
            }
        }

        public static void SendDeleteTicketPacket(RealmClient client)
        {
            using (RealmPacketOut packet = new RealmPacketOut(RealmServerOpCode.SMSG_GMTICKET_DELETETICKET))
            {
                packet.WriteUInt(9);
                client.Send(packet);
            }
        }
    }
}