Advertisement
yani-valeva

Buy Items for User in Game

Jun 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. CREATE PROCEDURE `usp_items_validations`(IN `item_name` VARCHAR(50), IN `user_game_id` INT)
  2. BEGIN
  3. DECLARE `itemId` INT;
  4. DECLARE `item_min_level` INT;
  5. DECLARE `alexMoney` INT;
  6. DECLARE `itemPrice` DECIMAL(19, 4);
  7. DECLARE `gameLevel` INT;
  8. SET `itemId` := (SELECT i.`id`
  9. FROM `items` AS i
  10. WHERE i.`name` = `item_name`);
  11. SET `item_min_level` := (SELECT i.`min_level`
  12. FROM `items` AS i
  13. WHERE i.`name` = `item_name`);
  14. SET `alexMoney` := (SELECT ug.`cash`
  15. FROM `users_games` AS ug
  16. WHERE ug.`id` = `user_game_id`);
  17. SET `itemPrice` := (SELECT i.`price`
  18. FROM `items` AS i
  19. WHERE i.`id` = `itemId`);
  20. SET `gameLevel` := (SELECT ug.`level`
  21. FROM `users_games` AS ug
  22. WHERE ug.`id` = `user_game_id`);
  23. START TRANSACTION;
  24. INSERT INTO `user_game_items`
  25. VALUES (`itemId`, `user_game_id`);
  26.  
  27. UPDATE `users_games`
  28. SET `cash` = `cash` - `itemPrice`
  29. WHERE `id` = `user_game_id`;
  30.  
  31. IF (`alexMoney` < `itemPrice`) THEN
  32. ROLLBACK;
  33. ELSEIF (`item_min_level` > `gameLevel`) THEN
  34. ROLLBACK;
  35. ELSE
  36. COMMIT;
  37. END IF;
  38. END $$
  39.  
  40. DELIMITER ;
  41.  
  42. DROP PROCEDURE IF EXISTS `usp_buy_items_for_alex`;
  43. DELIMITER $$
  44. CREATE PROCEDURE `usp_buy_items_for_alex`()
  45. BEGIN
  46. DECLARE `alexId` INT;
  47. DECLARE `gameId` INT;
  48. DECLARE `userGameId` INT;
  49. SET `alexId` := (SELECT u.`id`
  50. FROM `users` AS u
  51. WHERE u.`user_name` = 'Alex');
  52. SET `gameId` := (SELECT g.`id`
  53. FROM `games` AS g
  54. WHERE g.`name` = 'Edinburgh');
  55. SET `userGameId` := (SELECT ug.`id`
  56. FROM `users_games` AS ug
  57. WHERE ug.`game_id` = `gameId` AND ug.`user_id` = `alexId`);
  58.  
  59. CALL `usp_items_validations`('Blackguard', `userGameId`);
  60. CALL `usp_items_validations`('Bottomless Potion of Amplification', `userGameId`);
  61. CALL `usp_items_validations`('Eye of Etlich (Diablo III)', `userGameId`);
  62. CALL `usp_items_validations`('Gem of Efficacious Toxin', `userGameId`);
  63. CALL `usp_items_validations`('Golden Gorget of Leoric', `userGameId`);
  64. CALL `usp_items_validations`('Ziggurat Tooth', `userGameId`);
  65. CALL `usp_items_validations`('The Three Hundredth Spear', `userGameId`);
  66. CALL `usp_items_validations`('The Short Mans Finger', `userGameId`);
  67. CALL `usp_items_validations`('Tzo Krins Gaze', `userGameId`);
  68. CALL `usp_items_validations`('Valtheks Rebuke', `userGameId`);
  69. CALL `usp_items_validations`('Utars Roar', `userGameId`);
  70. CALL `usp_items_validations`('Urn of Quickening', `userGameId`);
  71. CALL `usp_items_validations`('Boots', `userGameId`);
  72. CALL `usp_items_validations`('Bombardiers Rucksack', `userGameId`);
  73. CALL `usp_items_validations`('Cloak of Deception', `userGameId`);
  74. CALL `usp_items_validations`('Hellfire Amulet', `userGameId`);
  75.  
  76. SELECT u.`user_name`, g.`name`, ug.`cash`, i.`name` AS 'Item Name'
  77. FROM `users` AS u
  78. INNER JOIN `users_games` AS ug
  79. ON u.`id` = ug.`user_id`
  80. INNER JOIN `games` AS g
  81. ON ug.`game_id` = g.`id`
  82. INNER JOIN `user_game_items` AS ugi
  83. ON ug.`id` = ugi.`user_game_id`
  84. INNER JOIN `items` AS i
  85. ON ugi.`item_id` = i.`id`
  86. WHERE g.`name` = 'Edinburgh'
  87. ORDER BY i.`name`;
  88. END $$
  89.  
  90. DELIMITER ;
  91.  
  92. CALL `usp_buy_items_for_alex`;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement