账号是现实世界对用户的描述,而角色则是游戏世界当中用户的体现。
——茂叔
账号
上一篇,我们利用微信的api完成了用户的登录工作,那现在,我们来处理用户的账户和角色信息。
首先,我们在type.cs里面为用户的账户建立一个类。
public struct Account
{
public uint AID;//账户的编号
public string openid;//微信的标识
public string nickname;
public DateTime CreateTime;
public DateTime LastLogin;
}
很简单吧。然后我们在GameServer里面去定义一个账号池,并在构造函数初始化,用来暂时储存账号信息。
……
public List<Account> AccountPool;
public GameServer(MainForm monitor)
{
Status = 0;
Monitor = monitor;
LOG("GameServer创建成功");
gGame = new Game(LOG);
gGame.Name = "像素寒冷";
AccountPool = new List<Account>();
……
然后在GameServer类的ParseClientCommand方法里面,对GameCommand.LOGIN指令做一些修改,检查有没有对应openid的账号,没有就立即创建一个放进我们账号池里面。然后去取得对应账号的用户角色回传给客户端。
……
case GameCommand.LOGIN:
{
string requeststr = "https://api.weixin.qq.com/sns/jscode2session?appid=" + G.AppID + "&secret=" + G.Secret + "&js_code=" + data + "&grant_type=authorization_code";
HttpWebRequest wxRequest = (HttpWebRequest)WebRequest.Create(requeststr);
wxRequest.Method = "GET";
wxRequest.Timeout = 6000;
HttpWebResponse wxResponse = (HttpWebResponse)wxRequest.GetResponse();
Stream stream = wxResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string res = reader.ReadToEnd();
stream.Close();
reader.Close();
JObject json = JObject.Parse(res);
if (json.TryGetValue("openid", out JToken openid))
{
Account account;
lock (AccountPool)
{
account = AccountPool.Find(x => x.openid == openid.ToString());
if (account.openid == null)
{
account = new Account()
{
AID = (uint)G.RND(10000000, 99999999),
openid = openid.ToString(),
CreateTime = DateTime.Now
};
while (AccountPool.Exists(x => x.AID == account.AID))
{
account.AID = (uint)G.RND(10000000, 99999999);
}
AccountPool.Add(account);
}
account.LastLogin = DateTime.Now;
}
JObject playerjson=gGame.GetPlayerJsonWithAccountID(account.AID);
((GameConnection)connection

最低0.47元/天 解锁文章
764

被折叠的 条评论
为什么被折叠?



