void OnUserDisconnected(IPlayer player) { // Check if user got join the raffle bool didJoinRaffle = false; ulong thisPlayerId = Convert.ToUInt64(player.Id); foreach (ulong playerId in joinedPlayersId) { if (playerId == thisPlayerId) { // User is in the list didJoinRaffle = true; } } if (didJoinRaffle) { // Create the data then add the data into the list to compensate them when they connect later on DisconnectData data = new DisconnectData(thisPlayerId, raffleJoinName, raffleJoinAmount); disconnectedPlayers.Add(data); // Remove them from the list joinedPlayersId.Remove(thisPlayerId); } bool didJoinBlackjack = false; BlackjackData thisData = null; foreach (BlackjackData bjData in blackjackDatas) { if (bjData.thisPlayer.userID == thisPlayerId) { // User is in the list didJoinBlackjack = true; thisData = bjData; } } if (didJoinBlackjack) { // Create the data then add the data into the list to compensate them when they connect later on DisconnectData data = new DisconnectData(thisPlayerId, thisData.item, thisData.amount); disconnectedPlayers.Add(data); // Remove them from the list blackjackDatas.Remove(thisData); } }
private void BlackjackEndMessage(BasePlayer player, BlackjackData theData, int option) { // Display the base message of the cards string usermessage = lang.GetMessage("BlackjackUserCard", this, player.UserIDString); player.ChatMessage(string.Format(usermessage, string.Join(", ", theData.mycard.ToArray()), theData.myCardValue)); string botmesage = lang.GetMessage("BlackjackBotCard", this, player.UserIDString); player.ChatMessage(string.Format(botmesage, string.Join(", ", theData.aiCard.ToArray()), theData.aiCardValue)); string message = ""; switch (option) { case 1: // User won Item item = ItemManager.CreateByItemID(ItemManager.FindItemDefinition(theData.item).itemid, (int)(theData.amount * blackjackMultiplier)); player.GiveItem(item); // Display user message message = lang.GetMessage("BlackjackUserWon", this, player.UserIDString); player.ChatMessage(string.Format(message, (theData.amount * blackjackMultiplier).ToString(), ItemManager.FindItemDefinition(theData.item).displayName.english)); break; case 2: // User Lost // Display user message message = lang.GetMessage("BlackjackUserLost", this, player.UserIDString); player.ChatMessage(string.Format(message, theData.amount.ToString(), ItemManager.FindItemDefinition(theData.item).displayName.english)); break; case 3: // User tied Item thisItem = ItemManager.CreateByItemID(ItemManager.FindItemDefinition(theData.item).itemid, theData.amount); player.GiveItem(thisItem); // Display user message message = lang.GetMessage("BlackjackUserTied", this, player.UserIDString); player.ChatMessage(string.Format(message, theData.amount.ToString(), ItemManager.FindItemDefinition(theData.item).displayName.english)); break; } }
private void EndBlackjack(BasePlayer player, BlackjackData theData) { BlackjackData baseData = theData; // Check if the player card is below 21 if (theData.myCardValue < 21 && theData.mycard.Count < 5) { // Less than 21, so there's a chance that the bot value is greater // Run a loop if the bot's value is less than 17 for (int i = 0; i < 5; i++) { if (theData.aiCardValue < 17 && theData.aiCard.Count != 5) { // Bot card int botCardValue = theData.aiCardValue; List <string> botCard = theData.aiCard; // Run a loop twice since they can only get 2 card int random = UnityEngine.Random.Range(1, cardName.Length); botCardValue = DetermineCardValue(botCard, cardName[random]); botCard.Add(cardName[random]); // Update the data into the data theData.aiCardValue = botCardValue; theData.aiCard = botCard; } else { break; } } if (theData.aiCardValue > 21) { // User wins BlackjackEndMessage(player, theData, 1); } else if (theData.aiCardValue == 21) { // Bot wins BlackjackEndMessage(player, theData, 2); } else if (theData.aiCardValue < 21 && theData.aiCard.Count == 5) { // Bot wins BlackjackEndMessage(player, theData, 2); } else { // Check if the user's card is greater than the bot's card if (theData.myCardValue > theData.aiCardValue) { // User wins BlackjackEndMessage(player, theData, 1); } else if (theData.myCardValue < theData.aiCardValue) { // Bot wins BlackjackEndMessage(player, theData, 2); } else { // Tie BlackjackEndMessage(player, theData, 3); } } } else if (theData.myCardValue == 21) { // Player wins BlackjackEndMessage(player, theData, 1); } else if (theData.mycard.Count == 5 && theData.myCardValue < 21) { // Player wins BlackjackEndMessage(player, theData, 1); } else { // Bot wins BlackjackEndMessage(player, theData, 2); } blackjackDatas.Remove(baseData); }
private void Blackjack(BasePlayer player, string command, string[] args) { // This command is split into 3 parts based on the args // 1: /blackjack <amount> // 2: /blackjack hit // 3: /blackjack stand // TODO: Store and check if player hit already then disconnect. If they did then don't refund them. // Check if args was not specified or there was more than one args if (blackjackHasError) { return; } if (args == null || args.Length != 1) { // Display an error to user string message = lang.GetMessage("BlackjackNoArgs", this, player.UserIDString); player.ChatMessage(string.Format(message, blackjackFeeName)); return; } if (args[0].ToLower() == "hit") { // Hit // Check if user has a datastored bool hasData = false; BlackjackData thisData = null; foreach (BlackjackData data in blackjackDatas) { if (data.thisPlayer == player) { hasData = true; thisData = data; } } if (!hasData) { // Display an error string message = lang.GetMessage("BlackjackNoSession", this, player.UserIDString); player.ChatMessage(message); return; } // So by right here player should have a data stored already means that they already used /blackjack <amount> // HIT // User's card int userCardValue = thisData.myCardValue; List <string> userCard = thisData.mycard; // Choose a random card and add to the list int random = UnityEngine.Random.Range(1, cardName.Length); userCardValue = DetermineCardValue(userCard, cardName[random]); userCard.Add(cardName[random]); // Store then this send a message to the user foreach (BlackjackData data in blackjackDatas) { if (data.thisPlayer == player) { data.myCardValue = userCardValue; data.mycard = userCard; data.endTime = Time.time; thisData = data; } } // Check if the value is greater than 21 if (thisData.myCardValue >= 21 || thisData.mycard.Count >= 5) { // End it EndBlackjack(player, thisData); } else { // Send it to user string usermessage = lang.GetMessage("BlackjackUserCard", this, player.UserIDString); player.ChatMessage(string.Format(usermessage, string.Join(", ", thisData.mycard.ToArray()), thisData.myCardValue)); string botmesage = lang.GetMessage("BlackjackBotCard", this, player.UserIDString); player.ChatMessage(string.Format(botmesage, string.Join(", ", thisData.aiCard.ToArray()), thisData.aiCardValue)); string instruction = lang.GetMessage("BlackjackArgChoose", this, player.UserIDString); player.ChatMessage(instruction); // Start a timer timer.Once(blackjackTimeout, () => { bool hasUserData = false; BlackjackData currentData = null; foreach (BlackjackData data in blackjackDatas) { if (data.thisPlayer == player) { hasUserData = true; currentData = data; } } if (hasUserData) { if ((Time.time - currentData.endTime) >= blackjackTimeout) { EndBlackjack(player, currentData); } } }); } } else if (args[0].ToLower() == "stand") { // Stand // Check if user has a datastored bool hasData = false; BlackjackData thisData = null; foreach (BlackjackData data in blackjackDatas) { if (data.thisPlayer == player) { hasData = true; thisData = data; } } if (!hasData) { // Display an error string message = lang.GetMessage("BlackjackNoSession", this, player.UserIDString); player.ChatMessage(message); return; } // End it EndBlackjack(player, thisData); } else { // Check if the arg can be converted to amount int amount; bool tryConvert = int.TryParse(args[0], out amount); if (!tryConvert) { // Can't convert the string to int so the arg wasn't a number // Display error // Check if they have a blackjack currently running bool hasBjRunning = false; foreach (BlackjackData bjData in blackjackDatas) { if (bjData.thisPlayer == player && bjData.isActive) { hasBjRunning = true; } } if (hasBjRunning) { // Bj was running so by right they should send hit or stand string message = lang.GetMessage("BlackjackArgChoose", this, player.UserIDString); player.ChatMessage(message); return; } else { // Bj was not running so by right they should enter a value string message = lang.GetMessage("BlackjackArgNotNumber", this, player.UserIDString); player.ChatMessage(message); return; } } else { // Check if the input amount meet the minimum requirement if (amount < blackjackMinimumFee) { // Amount did not meet the minimum string message = lang.GetMessage("BlackjackMinimumFail", this, player.UserIDString); player.ChatMessage(string.Format(message, blackjackMinimumFee, blackjackFeeName)); return; } // Check if user already joined the blackjack bool joined = false; foreach (BlackjackData bjData in blackjackDatas) { if (bjData.thisPlayer == player && bjData.isActive) { joined = true; } else if (bjData.thisPlayer == player && !bjData.isActive) { // Cooldown... string message = lang.GetMessage("BlackjackCooldown", this, player.UserIDString); player.ChatMessage(string.Format(message)); //TODO } } if (joined) { // User already joined so just give them an error message and end it string message = lang.GetMessage("BlackjackArgChoose", this, player.UserIDString); player.ChatMessage(message); return; } // Check if user has the required stuff in their inventory to join int itemId = ItemManager.FindItemDefinition(blackjackFee).itemid; int itemAmount = player.inventory.GetAmount(itemId); if (itemAmount < amount) { string message = lang.GetMessage("BlackjackInsufficient", this, player.UserIDString); player.ChatMessage(string.Format(message, blackjackFeeName, amount)); return; } // By right here, they should meet all the requirements // Create the data BlackjackData thisData = new BlackjackData(); thisData.thisPlayer = player; thisData.isActive = true; // Retrieve the fee player.inventory.Take(null, itemId, amount); // Start the blackjack interaction between user and an ai string[] cardName = { "A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "K", "Q", "J" }; // Bot card int botCardValue = 0; List <string> botCard = new List <string>(); // Run a loop twice since they can only get 2 card for (int i = 0; i < 2; i++) { int random = UnityEngine.Random.Range(1, cardName.Length); botCardValue = DetermineCardValue(botCard, cardName[random]); botCard.Add(cardName[random]); } // User's card int userCardValue = 0; List <string> userCard = new List <string>(); // Run a loop twice since they can only get 2 card for (int i = 0; i < 2; i++) { int random = UnityEngine.Random.Range(1, cardName.Length); userCardValue = DetermineCardValue(userCard, cardName[random]); userCard.Add(cardName[random]); } // Store the data thisData.aiCard = botCard; thisData.aiCardValue = botCardValue; thisData.mycard = userCard; thisData.myCardValue = userCardValue; thisData.endTime = Time.time; thisData.amount = amount; thisData.item = blackjackFee; blackjackDatas.Add(thisData); // Send a message to user string usermessage = lang.GetMessage("BlackjackUserCard", this, player.UserIDString); player.ChatMessage(string.Format(usermessage, string.Join(", ", userCard.ToArray()), userCardValue)); string botmesage = lang.GetMessage("BlackjackBotCard", this, player.UserIDString); player.ChatMessage(string.Format(botmesage, string.Join(", ", botCard.ToArray()), botCardValue)); string instruction = lang.GetMessage("BlackjackArgChoose", this, player.UserIDString); player.ChatMessage(instruction); // Start a timer timer.Once(blackjackTimeout, () => { bool hasUserData = false; BlackjackData currentData = null; foreach (BlackjackData data in blackjackDatas) { if (data.thisPlayer == player) { hasUserData = true; currentData = data; } } if (hasUserData) { if ((Time.time - currentData.endTime) >= blackjackTimeout) { EndBlackjack(player, currentData); } } }); } } }