上一篇分享了枚举+与或运算符的技巧 枚举的集合
这里分享一下 权限的设置与检查
需求如下:
玩家的联盟(战队)系统中,玩家存在阶级:盟主、官员、使者、群众,而盟主可以设置阶级成员的管理权限,
权限包含:查看联盟成员、联盟交易、联盟帮助、全体邮件、邀请入盟、同意入盟、踢出联盟、联盟转让、修改联盟名称。
盟主的权限:修改联盟名称、查看联盟成员、联盟交易、联盟帮助、全体邮件、邀请入盟、同意入盟、踢出联盟、联盟转让。
官员的权限:查看联盟成员、联盟交易、联盟帮助、全体邮件、邀请入盟、同意入盟、踢出联盟。
使者的权限:查看联盟成员、联盟交易、联盟帮助、邀请入盟。
群众的权限:查看联盟成员、联盟交易、联盟帮助。
为丰富游戏的自由度和盟主的权利,盟主可以对下面的阶级设置其权限,
首先我们需要有一个权限枚举
public enum EAlliancePermissionType : long
{
None = 0,
Member = 1<<0, //查看联盟成员 0000 0000 0001
Trade = 1<<1, //联盟交易 0000 0000 0010
Help = 1<<2, //联盟帮助 0000 0000 0100
Allmail = 1<<3, //全体邮件 0000 0000 1000
Invite = 1<<4, //邀请入盟 0000 0001 0000
Agree = 1<<5, //同意联盟 0000 0010 0000
Kick = 1<<6, //踢出联盟 0000 0100 0000
Transfer = 1<<7, //联盟转让 0000 1000 0000
Name = 1<<8, //修改联盟名称 0001 0000 0000
}
public enum EAllianceRankType
{
Unknow = -1,
Leader,
Officer,
Envoy,
Member,
}
初始联盟权限一般读取配置,阶级权限的值由二进制转十进制 加起来的总和
//0001 1111 1111
public long LeaderPermission = (long)EAlliancePermissionType.Member+
(long)EAlliancePermissionType.Trade+
(long)EAlliancePermissionType.Help+
(long)EAlliancePermissionType.Allmail+
(long)EAlliancePermissionType.Invite+
(long)EAlliancePermissionType.Agree+
(long)EAlliancePermissionType.Kick+
(long)EAlliancePermissionType.Transfer;
(long)EAlliancePermissionType.Name;
//0000 0111 1111
public long OfficerPermission = (long)EAlliancePermissionType.Member+
(long)EAlliancePermissionType.Trade+
(long)EAlliancePermissionType.Help+
(long)EAlliancePermissionType.Allmail+
(long)EAlliancePermissionType.Invite+
(long)EAlliancePermissionType.Agree+
(long)EAlliancePermissionType.Kick;
//0000 0000 1111
public long EnvoyPermission = (long)EAlliancePermissionType.Member+
(long)EAlliancePermissionType.Trade+
(long)EAlliancePermissionType.Help+
(long)EAlliancePermissionType.Invite;
//0000 0000 0111
public long MemberPermission = (long)EAlliancePermissionType.Member+
(long)EAlliancePermissionType.Trade+
(long)EAlliancePermissionType.Help;
有了阶级权限和权限枚举我们可以通过与运算的方式判断某一个阶级是否有某权限,比如:分别判断普通成员是否有邀请他人的权限,普通成员是否有联盟交易的权限。
public bool CheckIsHavePermission(EAlliancePermissionType type, EAllianceRankType rankType)
{
long Permission = 0;
switch(rankType)
{
case EAllianceRankType.Leader:
Permission = LeaderPermission;
break
case EAllianceRankType.Officer:
Permission = OfficerPermission;
break
case EAllianceRankType.Envoy:
Permission = EnvoyPermission;
break
case EAllianceRankType.Member:
Permission = MemberPermission;
break
}
AlliancePermission alliancePermission;
var value = Permission & (long) type;
if (value == (long)type)
{
return true;
}
return false;
}
// 0000 0111 & 0001 0000 == 0000 0000
// False
bool checkInvite = CheckIsHavePermission(EAlliancePermissionType.Invite,EAllianceRankType.Member)
// 0000 0111 & 0000 0100 == 0000 0100
// true
bool checkAllianceHelp = CheckIsHavePermission(EAlliancePermissionType.Help,EAllianceRankType.Member)
权限设置同理可以知道,通过与或可以进行修改,代码如下
/// 开启权限
public long SetPermissionTrue(long rankToPermission, long permissionValue)
{
//0000 0111 | 0010 0000 = 0010 0111
rankToPermission = (rankToPermission | permissionValue);
return rankToPermission;
}
/// 关闭权限
public long SetPermissionFalse(long rankToPermission, long permissionValue)
{
//取反操作
//~0000 0001 = 1111 1110
permissionValue = (~permissionValue);
//0000 0111 & 1111 1110 = 0000 0110
rankToPermission = (permissionValue & rankToPermission);
return rankToPermission;
}