private void OnRecvRegister(IChannel channel, Message message) { // write to database CRegister request = message as CRegister; SRegister response = new SRegister(); // ClientTipInfo(channel, "TODO: write register info to database"); var hasAccount = GameDataBase.SQLQueryScalar(string.Format("Select username from Account where username='******';", request.user)); if (hasAccount != null) { // same username response.status = SRegister.Status.Fail; channel.Send(response); return; } // using transaction to create account var conn = GameDataBase.GetConnection(); var trans = conn.BeginTransaction(); var accountInsert = conn.CreateCommand(); accountInsert.CommandText = "Insert Into Account(account_id, username, password) Values(DEFAULT, @username, @password) Returning account_id;"; accountInsert.Parameters.AddWithValue("username", request.user); accountInsert.Parameters.AddWithValue("password", request.password); var account_id = accountInsert.ExecuteScalar(); if (account_id == null) { // Register Fail trans.Rollback(); response.status = SRegister.Status.Error; channel.Send(response); return; } var playerInsert = conn.CreateCommand(); playerInsert.CommandText = "Insert Into Player(player_id, account_id) Values(DEFAULT, @account_id)"; playerInsert.Parameters.AddWithValue("account_id", (int)account_id); var res = playerInsert.ExecuteNonQuery(); if (res > 0) { // Success trans.Commit(); response.status = SRegister.Status.Success; } else { // Fail trans.Rollback(); response.status = SRegister.Status.Error; } channel.Send(response); return; }
private void OnRecvLogin(IChannel channel, Message message) { CLogin request = message as CLogin; SPlayerEnter response = new SPlayerEnter(); var hasUser = GameDataBase.SQLQueryScalar(string.Format( "Select player_id from Player Where account_id IN (Select account_id from Account Where username='******' and password='******');", request.user, request.password )); if (hasUser == null) { response.status = SPlayerEnter.Status.Fail; channel.Send(response); Console.WriteLine("Login Fail : {0} {1}", request.user, request.password); return; } else if (World.Instance.OnlinePlayers.ContainsKey((int)hasUser)) { response.status = SPlayerEnter.Status.Error; channel.Send(response); Console.WriteLine("Player {0} {1} relogin.", request.user, hasUser); return; } Player player = new Player(channel); player.player_id = (int)hasUser; string scene = "Level1"; using (var conn = DataBase.GameDataBase.GetConnection()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Select scene From Player Where player_id = @player_id"; cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteScalar(); if (res == null) { return; } scene = (string)res; } } response.user = request.user; response.token = request.user; response.id = player.entityId; response.scene = scene; response.status = SPlayerEnter.Status.Success; channel.Send(response); World.Instance.AddPlayer(player); Console.WriteLine("User {0} login", request.user); player.scene = scene; // TODO read from database DEntity dentity = World.Instance.EntityData["Ellen"]; player.FromDEntity(dentity); player.forClone = false; player.token = response.user; player.user = response.user; }