private void OnRecvPlayerChangeItem(IChannel channel, Message message) { CPlayerChangeItem request = message as CPlayerChangeItem; using (var conn = GameDataBase.GetConnection()) { using (var trans = conn.BeginTransaction()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Item Set status='Storing' Where item_id=@old_id;"; cmd.Parameters.AddWithValue("old_id", request.old_id); int res = cmd.ExecuteNonQuery(); if (res != 1) { ClientTipInfo(channel, "Oh! Change Item Error!"); trans.Rollback(); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Item Set status='Using' Where item_id=@new_id;"; cmd.Parameters.AddWithValue("new_id", request.new_id); int res = cmd.ExecuteNonQuery(); if (res != 1) { ClientTipInfo(channel, "Oh! Change Iten Error!"); trans.Rollback(); return; } } trans.Commit(); } } SPlayerChangeItem response = new SPlayerChangeItem(); response.new_id = request.new_id; response.old_id = request.old_id; channel.Send(response); var player = (Player)channel.GetContent(); FrontEnd.Item.DItem ditem = null; foreach (var item in player.wearing) { if (item.Value.item_id == request.old_id) { ditem = item.Value; } } player.wearing.Remove(ditem.item_type); player.inventory.Add(ditem.item_id, ditem); ditem = player.inventory[request.new_id]; player.inventory.Remove(request.new_id); player.wearing.Add(ditem.item_type, ditem); player.refreshAttr(); return; }
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 OnRecvPlayerDropItem(IChannel channel, Message message) { CPlayerDropItem request = message as CPlayerDropItem; using (var conn = GameDataBase.GetConnection()) { using (var trans = conn.BeginTransaction()) { int price = 0; using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Item Set status='Drop' Where item_id=@item_id Returning silver_value;"; cmd.Parameters.AddWithValue("item_id", request.item_id); var silver_value = cmd.ExecuteScalar(); if (silver_value == null) { ClientTipInfo(channel, "Sell Item Error!"); trans.Rollback(); return; } price = (int)silver_value; } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Player Set items_count=items_count-1,silver=silver+@price Where player_id=@player_id;"; cmd.Parameters.AddWithValue("price", price); cmd.Parameters.AddWithValue("player_id", (channel.GetContent() as Player).player_id); if (cmd.ExecuteNonQuery() != 1) { ClientTipInfo(channel, "Sell Item Error!"); trans.Rollback(); return; } } trans.Commit(); } } SPlayerDropItem response = new SPlayerDropItem { item_id = request.item_id }; channel.Send(response); var player = (Player)channel.GetContent(); player.inventory.Remove(request.item_id); }
private void OnRecvPlayerEquipItem(IChannel channel, Message message) { CPlayerEquipItem request = message as CPlayerEquipItem; using (var conn = GameDataBase.GetConnection()) { using (var trans = conn.BeginTransaction()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Item Set status='Using' Where item_id=@item_id;"; cmd.Parameters.AddWithValue("item_id", request.item_id); int res = cmd.ExecuteNonQuery(); if (res != 1) { ClientTipInfo(channel, "Equip Error!"); trans.Rollback(); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Player Set items_count=items_count-1 Where player_id=@player_id"; cmd.Parameters.AddWithValue("player_id", ((Player)channel.GetContent()).player_id); int res = cmd.ExecuteNonQuery(); if (res != 1) { ClientTipInfo(channel, "Equip Error!"); trans.Rollback(); return; } } trans.Commit(); } } SPlayerEquipItem response = new SPlayerEquipItem(); response.item_id = request.item_id; channel.Send(response); var player = (Player)channel.GetContent(); var ditem = player.inventory[request.item_id]; player.inventory.Remove(request.item_id); player.wearing.Add(ditem.item_type, ditem); player.refreshAttr(); }
private void OnRecvGetMarketItems(IChannel channel, Message message) { CGetMarketItems msg = message as CGetMarketItems; SGetMarketItems response = new SGetMarketItems(); List <MarketItem> items = new List <MarketItem>(); using (var conn = GameDataBase.GetConnection()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Select " + "Item.item_id, seller_id, price_type, price, name, health_value, speed_value, damage_value, intelligence_value, defence_value, icon, type " + "From Market,Item Where valid=true And Market.item_id=Item.item_id;"; var reader = cmd.ExecuteReader(); while (reader.Read()) { MarketItem item = new MarketItem(); DItem ditem = new DItem(); ditem.item_id = reader.GetInt32(0); item.owner_id = reader.GetInt32(1); item.costConf.costType = (CostType)System.Enum.Parse(typeof(CostType), reader.GetString(2)); item.costConf.cost = reader.GetInt32(3); ditem.name = reader.GetString(4); ditem.health_value = reader.GetInt32(5); ditem.speed_value = reader.GetInt32(6); ditem.damage_value = reader.GetInt32(7); ditem.intelligence_value = reader.GetInt32(8); ditem.defence_value = reader.GetInt32(9); ditem.icon_name = reader.GetString(10); ditem.item_type = (ItemType)System.Enum.Parse(typeof(ItemType), reader.GetString(11)); item.ditem = ditem; items.Add(item); } reader.Close(); } } response.items = items.ToArray(); channel.Send(response); }
private void OnRecvBuyMarketItem(IChannel channel, Message message) { CBuyMarketItem request = message as CBuyMarketItem; var player = (Player)channel.GetContent(); using (var conn = GameDataBase.GetConnection()) { using (var trans = conn.BeginTransaction()) { int market_id = 0; using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Select market_id From Market Where item_id=@item_id And valid=true;"; cmd.Parameters.AddWithValue("item_id", request.item.ditem.item_id); var res = cmd.ExecuteScalar(); if (res == null) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Buy]"); return; } market_id = (int)res; } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Select player_id From Player Where player_id=@player_id And items_count<40 And gold>=@gold And silver>=@silver;"; cmd.Parameters.AddWithValue("player_id", player.player_id); cmd.Parameters.AddWithValue("gold", request.item.costConf.costType == CostType.Gold ? request.item.costConf.cost : 0); cmd.Parameters.AddWithValue("silver", request.item.costConf.costType == CostType.Gold ? 0 : request.item.costConf.cost); var res = cmd.ExecuteScalar(); if (res == null) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Buy1]"); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Market Set valid=false, buyer_id=@player_id Where market_id=@market_id;"; cmd.Parameters.AddWithValue("market_id", market_id); cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Buy2]"); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Item Set player_id=@player_id, status='Storing' Where item_id=@item_id"; cmd.Parameters.AddWithValue("player_id", player.player_id); cmd.Parameters.AddWithValue("item_id", request.item.ditem.item_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Buy3]"); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Player Set items_count=items_count+1, gold=gold-@gold, silver=silver-@silver Where player_id=@player_id;"; cmd.Parameters.AddWithValue("gold", request.item.costConf.costType == CostType.Gold ? request.item.costConf.cost : 0); cmd.Parameters.AddWithValue("silver", request.item.costConf.costType == CostType.Gold ? 0 : request.item.costConf.cost); cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Buy4]"); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Player Set gold=gold+@gold, silver=silver+@silver Where player_id=@player_id"; cmd.Parameters.AddWithValue("gold", request.item.costConf.costType == CostType.Gold ? request.item.costConf.cost : 0); cmd.Parameters.AddWithValue("silver", request.item.costConf.costType == CostType.Gold ? 0 : request.item.costConf.cost); cmd.Parameters.AddWithValue("player_id", request.item.owner_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Buy5]"); return; } } trans.Commit(); } } SBuyMarketItem response = new SBuyMarketItem(); response.item = request.item; channel.Send(response); player.inventory.Add(request.item.ditem.item_id, request.item.ditem); }
private void OnRecvSellMarketItem(IChannel channel, Message message) { CSellMarketItem request = message as CSellMarketItem; var player = (Player)channel.GetContent(); using (var conn = GameDataBase.GetConnection()) { using (var trans = conn.BeginTransaction()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Select item_id From Item Where item_id=@item_id And status='Storing' And player_id=@player_id;"; cmd.Parameters.AddWithValue("item_id", request.item.ditem.item_id); cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteScalar(); if (res == null) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Sell]"); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Item Set status='Selling' Where item_id=@item_id;"; cmd.Parameters.AddWithValue("item_id", request.item.ditem.item_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Sell1]"); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Insert Into Market(market_id, item_id, seller_id, price, price_type, valid)" + "Values(DEFAULT, @item_id, @seller_id, @price, @price_type, @valid);"; cmd.Parameters.AddWithValue("item_id", request.item.ditem.item_id); cmd.Parameters.AddWithValue("seller_id", player.player_id); cmd.Parameters.AddWithValue("price", request.item.costConf.cost); cmd.Parameters.AddWithValue("price_type", request.item.costConf.costType.ToString()); cmd.Parameters.AddWithValue("valid", true); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Sell2]"); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Player Set items_count=items_count-1 Where player_id=@player_id;"; cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Try again.[Sell3]"); return; } } trans.Commit(); } // trans } //conn SSellMarketItem response = new SSellMarketItem(); response.item = request.item; channel.Send(response); player.inventory.Remove(request.item.ditem.item_id); return; }
private void OnRecvPlayerUseItem(IChannel channel, Message message) { CPlayerUseItem request = message as CPlayerUseItem; int health_value = 0; int speed_value = 0; int damage_value = 0; int intelligence_value = 0; int defence_value = 0; using (var conn = GameDataBase.GetConnection()) { using (var trans = conn.BeginTransaction()) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Item Set status='Drop' Where item_id=@item_id;"; cmd.Parameters.AddWithValue("item_id", request.item_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { ClientTipInfo(channel, "Use Item Error!"); trans.Rollback(); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Select health_value, speed_value, damage_value, intelligence_value, defence_value From Item Where item_id=@item_id;"; cmd.Parameters.AddWithValue("item_id", request.item_id); var reader = cmd.ExecuteReader(); if (!reader.Read()) { ClientTipInfo(channel, "Use Item Error!"); reader.Close(); trans.Rollback(); return; } health_value = reader.GetInt32(0); speed_value = reader.GetInt32(1); damage_value = reader.GetInt32(2); intelligence_value = reader.GetInt32(3); defence_value = reader.GetInt32(4); reader.Close(); } using (var cmd = conn.CreateCommand()) { if (health_value + speed_value + damage_value + intelligence_value + defence_value == 0) { // HP cmd.CommandText = "Update Player Set " + "items_count=items_count-1, " + "hp=health " + "Where player_id=@player_id;"; } else { cmd.CommandText = "Update Player Set " + "items_count=items_count-1, " + "health=health+@health_value, " + "speed=speed+@speed_value, " + "damage=damage+@damage_value, " + "intelligence=intelligence+@intelligence_value, " + "defence=defence+@defence_value " + "Where player_id=@player_id;"; cmd.Parameters.AddWithValue("health_value", health_value); cmd.Parameters.AddWithValue("speed_value", speed_value); cmd.Parameters.AddWithValue("damage_value", damage_value); cmd.Parameters.AddWithValue("intelligence_value", intelligence_value); cmd.Parameters.AddWithValue("defence_value", defence_value); } cmd.Parameters.AddWithValue("player_id", ((Player)channel.GetContent()).player_id); int res = cmd.ExecuteNonQuery(); if (res != 1) { ClientTipInfo(channel, "Use Item Error!"); trans.Rollback(); return; } } // cmd trans.Commit(); } // trans } // conn SPlayerUseItem response = new SPlayerUseItem(); response.item_id = request.item_id; channel.Send(response); var player = (Player)channel.GetContent(); var ditem = player.inventory[request.item_id]; player.inventory.Remove(request.item_id); if (health_value + speed_value + damage_value + intelligence_value + defence_value == 0) { // HP player.currentHP = player.maxHP; } else { player.base_damage += damage_value; player.base_health += health_value; player.base_defence += defence_value; player.base_intelligence += intelligence_value; player.base_speed += speed_value; } player.refreshAttr(); }
static public void OnRecvPlayerTakeItem(IChannel channel, Message message) { SGetItem response = new SGetItem(); CCreateItem request = message as CCreateItem; Player player = channel.GetContent() as Player; DItem item = FItem.CreateRandomItem(request.luck).ToDItem(); using (var conn = GameDataBase.GetConnection()) { using (var trans = conn.BeginTransaction()) { int item_id; using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Select items_count From Player Where player_id=@player_id And items_count<40;"; cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteScalar(); if (res == null) { trans.Rollback(); ClientTipInfo(channel, "Full Inventory! Cannot Get More Items."); return; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Insert Into Item(item_id, player_id, type, status, name," + "health_value, speed_value, damage_value, intelligence_value, defence_value, icon, silver_value)" + "Values(DEFAULT, @player_id, '" + item.item_type + "', 'Storing', @name," + "@health_value, @speed_value, @damage_value, @intelligence_value, @defence_value, @icon, @silver_value) Returning item_id;"; cmd.Parameters.AddWithValue("player_id", player.player_id); //cmd.Parameters.AddWithValue("type", item.item_type.ToString()); cmd.Parameters.AddWithValue("name", item.name); cmd.Parameters.AddWithValue("health_value", item.health_value); cmd.Parameters.AddWithValue("speed_value", item.speed_value); cmd.Parameters.AddWithValue("damage_value", item.damage_value); cmd.Parameters.AddWithValue("intelligence_value", item.intelligence_value); cmd.Parameters.AddWithValue("defence_value", item.defence_value); cmd.Parameters.AddWithValue("icon", item.icon_name); cmd.Parameters.AddWithValue("silver_value", item.silver_value); var res = cmd.ExecuteScalar(); if (res == null) { trans.Rollback(); ClientTipInfo(channel, "Fail to pick new item!"); return; } item_id = (int)res; } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Player Set items_count=items_count+1 Where player_id=@player_id;"; cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Fail to pick new item [2] !"); return; } } trans.Commit(); item.item_id = item_id; response.success = true; response.dItem = item; channel.Send(response); if (request.fromFrontend) { SBroadcastMessage bmsg = new SBroadcastMessage(); bmsg.message = string.Format("{0} 通过寻宝获得物品 {1}", player.user, response.dItem.name); World.Instance.Broundcast(bmsg); } } // trans } // conn }
private void OnRecvBuyTmallItems(IChannel channel, Message message) { CBuyTmallItems request = message as CBuyTmallItems; Player player = (Player)channel.GetContent(); int gold = 0, silver = 0; TmallItem silverItem = null; List <DItem> items = new List <DItem>(); foreach (var item in request.tmallItems) { if (item.costConf.costType == CostType.Silver) { silver += item.costConf.cost * item.count; } else { gold += item.costConf.cost * item.count; } if (item.itemConf.type == ItemType.Others) { silverItem = item; } else { for (int i = 0; i < item.count; i++) { items.Add(FItem.CreateSpecificItem(item.itemConf.name, item.itemConf.icon, item.itemConf.type, request.luck).ToDItem()); } } } using (var conn = GameDataBase.GetConnection()) { using (var trans = conn.BeginTransaction()) { using (var cmd = conn.CreateCommand()) { // Check Player account cmd.CommandText = "Select player_id From Player Where player_id=@player_id And gold>=@gold And silver>=@silver And items_count<=@items_count;"; cmd.Parameters.AddWithValue("player_id", player.player_id); cmd.Parameters.AddWithValue("gold", gold); cmd.Parameters.AddWithValue("silver", silver); cmd.Parameters.AddWithValue("items_count", 40 - items.Count); var res = cmd.ExecuteScalar(); if (res == null) { trans.Rollback(); ClientTipInfo(channel, "Can't Afford that!"); return; } } if (silverItem != null) { using (var cmd = conn.CreateCommand()) { // Silver cmd.CommandText = "Update Player Set silver=silver+@silver Where player_id=@player_id;"; cmd.Parameters.AddWithValue("silver", silverItem.count * 50); cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Please try again [1]."); return; } } } foreach (var item in items) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Insert Into Item(item_id, player_id, type, status, name," + "health_value, speed_value, damage_value, intelligence_value, defence_value, icon, silver_value)" + "Values(DEFAULT, @player_id, '" + item.item_type + "', 'Storing', @name," + "@health_value, @speed_value, @damage_value, @intelligence_value, @defence_value, @icon, @silver_value) Returning item_id;"; cmd.Parameters.AddWithValue("player_id", player.player_id); //cmd.Parameters.AddWithValue("type", item.item_type.ToString()); cmd.Parameters.AddWithValue("name", item.name); cmd.Parameters.AddWithValue("health_value", item.health_value); cmd.Parameters.AddWithValue("speed_value", item.speed_value); cmd.Parameters.AddWithValue("damage_value", item.damage_value); cmd.Parameters.AddWithValue("intelligence_value", item.intelligence_value); cmd.Parameters.AddWithValue("defence_value", item.defence_value); cmd.Parameters.AddWithValue("icon", item.icon_name); cmd.Parameters.AddWithValue("silver_value", item.silver_value); var res = cmd.ExecuteScalar(); if (res == null) { trans.Rollback(); ClientTipInfo(channel, "Error! Please try again [2]."); return; } item.item_id = (int)res; } } using (var cmd = conn.CreateCommand()) { cmd.CommandText = "Update Player Set items_count=items_count+@count, gold=gold-@gold, silver=silver-@silver Where player_id=@player_id;"; cmd.Parameters.AddWithValue("count", items.Count); cmd.Parameters.AddWithValue("gold", gold); cmd.Parameters.AddWithValue("silver", silver); cmd.Parameters.AddWithValue("player_id", player.player_id); var res = cmd.ExecuteNonQuery(); if (res != 1) { trans.Rollback(); ClientTipInfo(channel, "Error! Please try again [3]."); return; } } trans.Commit(); } // trans } // conn SBuyTmallItems response = new SBuyTmallItems(); response.dItems = items.ToArray(); if (silverItem != null) { response.silver = silverItem.count * 50 - silver; } else { response.silver = -silver; } response.gold = -gold; channel.Send(response); foreach (var ditem in items) { player.inventory.Add(ditem.item_id, ditem); } }