如何跟踪同一用户(对象)的多个实例
我正在用 PHP 编写一个 IRC 机器人,我想让它比大多数其他机器人更复杂一点。
我的应用程序中有以下类:
Bot
- 实际的机器人类,它处理机器人可以获得的所有功能和命令。Registry
- 全局设置和变量,传递给任何功能类,这样我就不必用变量乱扔全局命名空间。Channel
- 定义通道对象,属性:$name、$nicklist
,其中$name
是通道名称,$nicklist< /code> 是
User
对象的数组。User
- 定义用户对象,属性:$nickname、$fullAddress
,其中$fullAddress
是用户的地址,格式为尼克!用户@主机
。
然而,到目前为止一切都很好,一个用户可能位于多个频道上,并且由于我不想拥有来自同一用户的多个对象(例如,就像一个用户更改了他的昵称一样,我将不得不更新多个元素而不是只有一个)。
我该如何解决这个问题,我正在考虑在 Bot 类的基础上保留一个庞大的用户池,并将这些引用传递给通道,但我真的不知道该怎么做要么 :P
谁能指出我正确的方向吗?如果您愿意,我可以粘贴我当前的代码(它相当长,所以如果没有人需要,我宁愿不这样做)。
谢谢。
I'm writing an IRC bot in PHP, and I wanted to make a s little more sophisticated then most other bots.
I have the following classes in my application:
Bot
- The actual bot class, it handles all of the functionality and commands the bots can get.Registry
- Global settings and variables, passed on to any functioning class so that I don't have to litter the global namespace with variables.Channel
- Defines the channel object, properties:$name, $nicklist
, where$name
is the channel name and$nicklist
is an array ofUser
objects.User
- Defines the user object, properties:$nickname, $fullAddress
, where$fullAddress
is the user's address in the form ofnick!user@host
.
So far so good, however, a user may be on multiple channels, and since I don't want to have multiple objects from the same user (as if a user, for instance, changes his nickname, I'll have to update multiple elements instead of just one).
How would I go about this, I was thinking about holding a grand user pool at the base of the Bot
class, and pass these references to the channels, but I don't really know how to do that either :P
Can anyone point me in the right direction? I can paste my current code if you'd like (it's rather long so I rather not do it if no one needs it).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以向您的用户添加频道 ID 列表,每次用户加入新频道时,您都会在列表中附加新的频道 ID,这样,如果用户离开频道,您将从他的列表中删除该频道 ID。
You could add a list of channel id's to you user and every time the user join to a new channel you will append the list with a new channel id so if a user leave an channel you will remove the channel id from his list.
由于
Bot
对象是需要进行跟踪的对象,因此其上有一组User
对象,并将指针传递给这些对象(又名$ bot->users['name']
) 到Channel
对象。这样,如果用户已在用户列表中找到,则可以传递相同的指针两次。
Since the
Bot
object is the one that needs to do the tracking, have an array ofUser
objects on it, and pass the pointers to those objects (aka$bot->users['name']
) to theChannel
object.That way, the same pointer can be passed twice in case the user is already found on the users list.