Permalink
Browse files

add two stored procedures

  • Loading branch information...
1 parent 5f92b7c commit 04b8fd87b1368ca33fd249535f8ddd3cff071b54 @Ericgan Ericgan committed Aug 29, 2011
Showing with 153 additions and 0 deletions.
  1. +95 −0 sql/sp_deck_add_lord.sql
  2. +58 −0 sql/sp_player_add.sql
View
@@ -0,0 +1,95 @@
+DROP PROCEDURE IF EXISTS `sp_deck_add_lord`//
+
+CREATE PROCEDURE `sp_deck_add_lord`(
+ IN playerId INT,
+ IN deckId INT,
+ IN cardId INT
+)
+BEGIN
+
+ DECLARE antifactId INT;
+ DECLARE oldCardId INT;
+ DECLARE cardCount INT DEFAULT 0;
+
+ DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
+ DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
+
+ START TRANSACTION;
+
+ -- fetch LORD card of the deck
+ SELECT oldCardId = `cardId`, antifactId = `antifactId`
+ FROM `player_decks`
+ WHERE `playerId` = playerId
+ AND `deckId` = deckId
+ AND `order` = 0;
+
+ -- check whether the deck has a LORD card not not
+ IF( IFNULL(oldCardId,0) = 0) THEN
+ -- if not, add this card into the deck as a LORD
+ INSERT INTO `player_decks`(`playerId`, `deckId`, `order`, `cardId`)
+ VALUES(playerId, deckId, 0, cardId);
+ ELSE
+ -- otherwise,
+ -- 1, replace the LORD card
+ UPDATE `player_decks`
+ SET `cardId` = cardId
+ WHERE `playerId` = playerId
+ AND `deckId` = deckId
+ AND `order` = 0;
+
+ -- 2, move the old one LORD back into player's cards collection
+ -- -- check whether player own this card
+
+ SELECT cardCount = COUNT(1)
+ FROM `player_cards`
+ WHERE `playerId` = playerId
+ AND `cardId` = oldCardId;
+
+ IF( IFNULL(cardCount,0)>0) THEN
+ -- if true, add the count of this card in player's cards collection
+ UPDATE `player_cards`
+ SET `count` = IFNULL(`count`,0) + 1
+ WHERE `playerId` = playerId
+ AND `cardId` = oldCardId;
+ ELSE
+ -- otherwise, insert this card into player's cards collection
+ INSERT INTO `player_cards` (`playerId`,`cardId`,`count`)
+ VALUES(playerId,oldCardId,0);
+ END IF;
+
+ -- 3, check if this card has antifact
+ IF( IFNULL(antifactId,0) > 0) THEN
+ -- if true, move this antifact card back into player's cards collection
+ SET cardCount = 0;
+
+ SELECT cardCount = COUNT(1)
+ FROM `player_cards`
+ WHERE `playerId` = playerId
+ AND `cardId` = antifactId;
+
+ IF( IFNULL(cardCount, 0)>0) THEN
+ -- if true, add the count of this card into player's cards collection
+ UPDATE `player_cards`
+ SET `count` = IFNULL(`count`, 0) + 1
+ WHERE `playerId` = playerId
+ AND `cardId` = antifactId;
+ ELSE
+ -- otherwise, insert this card into player's cards collection
+ INSERT INTO `player_cards` (`playerId`, `cardId`, `count`)
+ VALUES(playerId, antifactId, 0);
+ END IF;
+ END IF;
+ END IF;
+
+ -- remove this card from player's cards collection
+ UPDATE `player_cards`
+ SET `count` = CASE
+ WHEN IFNULL(`count`, 0) > 0 THEN `count` - 1
+ ELSE 0
+ END
+ WHERE `playerId` = playerId
+ AND `cardId` = cardId;
+
+ COMMIT;
+
+END//
View
@@ -0,0 +1,58 @@
+DROP PROCEDURE IF EXISTS `sp_player_add`//
+
+CREATE PROCEDURE `sp_player_add`(
+ IN name VARCHAR(50),
+ IN type INT(2) UNSIGNED,
+ IN auth VARCHAR(100),
+ IN email VARCHAR(255),
+ IN stamina INT(10) UNSIGNED,
+ OUT playerId INT
+)
+BEGIN
+
+ DECLARE new_id INT DEFAULT 0;
+ DECLARE cur_order INT DEFAULT 0;
+ DECLARE now_to_sec INT DEFAULT 0;
+
+ DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
+ DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
+
+ START TRANSACTION;
+
+ -- convert current datetime to seconds
+ SET now_to_sec = TIME_TO_SEC(NOW());
+
+ -- 1, add player
+ INSERT INTO `player`(`name`,`type`,`auth`,`email`,`createTime`,`copper`,`gold`,`exp`,`level`,`stamina`,`staminaLastUpdate`,`defaultDeck`)
+ VALUES(name,type,auth,email,now_to_sec,0,0,0,0,stamina,now_to_sec,1);
+
+ -- get last insert id as player's id
+ SET new_id = LAST_INSERT_ID();
+
+ -- 2, add deck to this player's deck collection
+ -- --a. add lord card to player's default deck
+ -- ------meanwhile it will automatically create a new deck for player
+ INSERT INTO `player_decks`(`playerId`,`deckId`,`order`,`cardId`)
+ VALUES(new_id,1,cur_order,1);
+
+ -- --b. add tow normal cards to player's default deck
+ SET cur_order = cur_order + 1;
+ INSERT INTO `player_decks`(`playerId`,`deckId`,`order`,`cardId`)
+ VALUES(new_id,1,cur_order,2);
+
+ SET cur_order = cur_order + 1;
+ INSERT INTO `player_decks`(`playerId`,`deckId`,`order`,`cardId`)
+ VALUES(new_id,1,cur_order,3);
+
+ -- 3. return new player's id
+ SET playerId = new_id;
+
+ COMMIT;
+
+END//
+
+/* sample call
+CALL `sp_player_add`('karenin',0,'111111','eric.ganym@gmail.com',200,@id);
+SELECT @id AS playerId;
+*/
+

0 comments on commit 04b8fd8

Please sign in to comment.