コード例 #1
0
        public void SendUseItem(int item_id)
        {
            CPlayerUseItem msg = new CPlayerUseItem();

            msg.item_id = item_id;
            Client.Instance.Send(msg);
        }
コード例 #2
0
        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();
        }