From e8b72fbdf82055d51d804a122b31e74c8f68a55f Mon Sep 17 00:00:00 2001 From: MallocNull Date: Wed, 3 Sep 2014 17:38:57 -0500 Subject: [PATCH] poker what am i doing with my life --- bot/bot/Autonomous.cs | 2 +- bot/bot/Bot.cs | 20 +++- bot/bot/ConditionChecker.cs | 2 +- bot/bot/ResponseCaller.cs | 2 +- bot/bot/_G.cs | 1 + bot/bot/bot.csproj | 1 + bot/bot/responses/poker.cs | 220 ++++++++++++++++++++++++++++++++++++ www/auto.php | 146 +++++++++++++++++++----- 8 files changed, 359 insertions(+), 35 deletions(-) create mode 100644 bot/bot/responses/poker.cs diff --git a/bot/bot/Autonomous.cs b/bot/bot/Autonomous.cs index 8151660..893a1ba 100644 --- a/bot/bot/Autonomous.cs +++ b/bot/bot/Autonomous.cs @@ -6,6 +6,6 @@ using System.Threading.Tasks; namespace bot { class Autonomous { - + } } diff --git a/bot/bot/Bot.cs b/bot/bot/Bot.cs index 16e8fd4..737d86f 100644 --- a/bot/bot/Bot.cs +++ b/bot/bot/Bot.cs @@ -60,13 +60,24 @@ namespace bot { } static void Main(string[] args) { + Console.Write("Loading database info ... "); _G.loadDatabaseInfo(); + Console.WriteLine("OK"); + + Console.Write("Spawning database connections ... "); _G.conn = _G.spawnNewConnection(); _G.errconn = _G.spawnNewConnection(); + Console.WriteLine("OK"); + Console.Write("Loading bot configuration ... "); _G.loadConfig(); - loadResponseList(); + Console.WriteLine("OK"); + Console.Write("Loading response list ... "); + loadResponseList(); + Console.WriteLine("OK"); + + Console.Write("Updating response types on database ... "); tmp = "DELETE FROM resptypes WHERE "; foreach(Type t in ResponseCaller.getResponseTypes()) { string[] typeInfo = (string[])t.GetMethod("getInfo").Invoke(null, null); @@ -78,7 +89,9 @@ namespace bot { } tmp = tmp.Substring(0, tmp.Length - 5); Query.Quiet(tmp, _G.conn); + Console.WriteLine("OK"); + Console.Write("Updating conditions on database ... "); tmp = "DELETE FROM conditions WHERE "; foreach(Type t in ConditionChecker.getConditions()) { string[] typeInfo = (string[])t.GetMethod("getInfo").Invoke(null, null); @@ -90,11 +103,15 @@ namespace bot { } tmp = tmp.Substring(0, tmp.Length - 5); Query.Quiet(tmp, _G.conn); + Console.WriteLine("OK"); + Console.Write("Spawning web driver ... "); _G.driver = new FirefoxDriver(); + Console.WriteLine("OK"); while(true) { try { + Console.Write("Navigating to chat ... "); foreach(NavigationNode node in navigationList) node.performNavigation(_G.driver); try { @@ -102,6 +119,7 @@ namespace bot { } catch(Exception e) { _G.criticalError("Navigation to chat failed! Fix instructions.", true); } + Console.WriteLine("OK"); _G.startThread(Pulse.pulseThread); diff --git a/bot/bot/ConditionChecker.cs b/bot/bot/ConditionChecker.cs index 1e8cf83..aee37da 100644 --- a/bot/bot/ConditionChecker.cs +++ b/bot/bot/ConditionChecker.cs @@ -11,7 +11,7 @@ namespace bot { static void loadConditionTypes() { if(conditionTypes == null) - conditionTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.conditions", StringComparison.Ordinal)).ToArray(); + conditionTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.conditions", StringComparison.Ordinal) && !t.FullName.Contains('+')).ToArray(); } public static bool checkCondition(String conditionName, Message msg, string parameter) { diff --git a/bot/bot/ResponseCaller.cs b/bot/bot/ResponseCaller.cs index de8a464..2723dae 100644 --- a/bot/bot/ResponseCaller.cs +++ b/bot/bot/ResponseCaller.cs @@ -11,7 +11,7 @@ namespace bot { static void loadResponseTypes() { if(responseTypes == null) - responseTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.responses", StringComparison.Ordinal)).ToArray(); + responseTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.responses", StringComparison.Ordinal) && !t.FullName.Contains('+')).ToArray(); } public static void callResponse(String responseName, string parameters, Message msg) { diff --git a/bot/bot/_G.cs b/bot/bot/_G.cs index ba5325c..4dec0d3 100644 --- a/bot/bot/_G.cs +++ b/bot/bot/_G.cs @@ -9,6 +9,7 @@ using OpenQA.Selenium.Internal; using OpenQA.Selenium.Support.UI; using MySql.Data.MySqlClient; using System.Threading; +using System.Security.Cryptography; namespace bot { static class _G { diff --git a/bot/bot/bot.csproj b/bot/bot/bot.csproj index a1d17b5..469a986 100644 --- a/bot/bot/bot.csproj +++ b/bot/bot/bot.csproj @@ -87,6 +87,7 @@ + diff --git a/bot/bot/responses/poker.cs b/bot/bot/responses/poker.cs new file mode 100644 index 0000000..fd69a67 --- /dev/null +++ b/bot/bot/responses/poker.cs @@ -0,0 +1,220 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace bot.responses { + class poker { + class PokerContext { + class Dice { + static private Random rng = new Random(); + static public int[] Roll() { + return new int[] { rng.Next(6) + 1, rng.Next(6) + 1, rng.Next(6) + 1 + , rng.Next(6) + 1, rng.Next(6) + 1}; + } + } + + public int playerMoney; + public int botMoney; + + public int currentBet; + + public PokerContext() { + playerMoney = botMoney = 1000; + currentBet = 1; + } + + public int[] CheckHand(int[] hand) { + int i; + for(i = 1; i < 5; i++) + if(hand[i - 1] != hand[i] - 1) break; + if(i == 5) return new int[] { 4, hand.Sum() }; + + Dictionary groupList = new Dictionary(); + for(i = 1; i < 5; i++) { + if(hand[i] == hand[i - 1]) { + if(groupList.ContainsKey(hand[i])) + groupList[hand[i]]++; + else + groupList.Add(hand[i], 2); + } + } + + if(groupList.Keys.Count == 1) { + switch(groupList.Values.ElementAt(0)) { + case 2: + return new int[] { 1, groupList.Keys.ElementAt(0) }; + case 3: + return new int[] { 3, groupList.Keys.ElementAt(0) }; + case 4: + return new int[] { 6, groupList.Keys.ElementAt(0) }; + case 5: + return new int[] { 7, groupList.Keys.ElementAt(0) }; + } + } else if(groupList.Keys.Count == 2) { + if(groupList.Values.ElementAt(0) == 2 && groupList.Values.ElementAt(1) == 2) + return new int[] { 2, groupList.Keys.ElementAt(0) + groupList.Keys.ElementAt(1) }; + else + return new int[] { 5, groupList.Keys.ElementAt(0) + groupList.Keys.ElementAt(1) }; + } + + return new int[]{0, hand[4]}; + } + + public string GetDieFaceFromInt(int face) { + switch(face) { + case 1: + return "9"; + case 2: + return "10"; + case 3: + return "J"; + case 4: + return "Q"; + case 5: + return "K"; + case 6: + return "A"; + } + + return "WHAT"; + } + + public string GetDiceRollAsString(int[] hand) { + string tmp = ""; + for(int i = 0; i < 5; i++) + tmp += GetDieFaceFromInt(hand[i]) + ((i == 4) ? "" : " "); + return tmp; + } + + public string GetHandNameFromInt(int handresult) { + switch(handresult) { + case 0: + return "High Card"; + case 1: + return "Pair"; + case 2: + return "Two Pair"; + case 3: + return "Three of a Kind"; + case 4: + return "Straight"; + case 5: + return "Full House"; + case 6: + return "Four of a Kind"; + case 7: + return "Five of a Kind"; + } + + return "WHAT"; + } + + public void PlayerWins(string playerName) { + Chat.sendMessage(playerName + " wins! " + _G.propername + " loses " + pokerContexts[playerName].currentBet); + pokerContexts[playerName].botMoney -= pokerContexts[playerName].currentBet; + pokerContexts[playerName].playerMoney += pokerContexts[playerName].currentBet; + } + + public void BotWins(string playerName) { + Chat.sendMessage(_G.propername + " wins! " + playerName + " loses " + pokerContexts[playerName].currentBet); + pokerContexts[playerName].playerMoney -= pokerContexts[playerName].currentBet; + pokerContexts[playerName].botMoney += pokerContexts[playerName].currentBet; + } + + public void Tie(string playerName) { + Chat.sendMessage(_G.propername + " tied with " + playerName); + } + + public void PerformTurn(string playerName) { + int[] yourHand = Dice.Roll(); + Array.Sort(yourHand); + int[] yourResults = CheckHand(yourHand); + Chat.sendMessage(playerName + " rolled " + GetDiceRollAsString(yourHand) + " (" + GetHandNameFromInt(yourResults[0]) + ")"); + + int[] botHand = Dice.Roll(); + Array.Sort(botHand); + int[] botResults = CheckHand(botHand); + Chat.sendMessage(_G.propername + " rolled " + GetDiceRollAsString(botHand) + " (" + GetHandNameFromInt(botResults[0]) + ")"); + + if(botResults[0] > yourResults[0]) + BotWins(playerName); + else if(yourResults[0] > botResults[0]) + PlayerWins(playerName); + else { + if(botResults[1] > yourResults[1]) + BotWins(playerName); + else if(yourResults[1] > botResults[1]) + PlayerWins(playerName); + else + Tie(playerName); + } + } + + public void Raise(int amount) { + if(amount > 0) + currentBet += amount; + Chat.sendMessage("Bet raised to " + currentBet); + } + + public void Bet(int amount) { + if(amount > 0) + currentBet = amount; + Chat.sendMessage("Bet set at " + amount); + } + } + + static private Random rng = new Random(); + static private Dictionary pokerContexts = new Dictionary(); + + static public string[] getInfo() { + return new string[] {typeof(poker).Name, "Dice Poker", + "A sample module that allows a user to play against an AI in dice poker. Takes no arguments. Should accept the commands !register, !bet, !raise, !roll, !check, !help"}; + } + + static public void performOperation(string parameters, Message msg) { + try { + if(msg.msg.ToLower().Trim() == "!register") { + if(!pokerContexts.ContainsKey(msg.name)) { + pokerContexts.Add(msg.name, new PokerContext()); + Chat.sendMessage("You are now registered, and have $1000. The default bet is $1. You can !raise #, !bet #, !roll, or !check the standings."); + } else { + Chat.sendMessage("You are already registered."); + } + } else { + if(pokerContexts.ContainsKey(msg.name)) { + PokerContext ctx = pokerContexts[msg.name]; + switch(msg.msg.ToLower().Trim()) { + case "!help": + Chat.sendMessage("You can !raise #, !bet #, !roll, or !check the standings."); + break; + case "!check": + Chat.sendMessage(msg.name + " has $" + ctx.playerMoney + ". " + _G.propername + " has $" + ctx.botMoney + ". Both sides are currently betting $" + ctx.currentBet); + break; + case "!roll": + ctx.PerformTurn(msg.name); + break; + } + + if(msg.msg.ToLower().Trim().StartsWith("!bet")) { + try { + ctx.Bet(Int32.Parse(msg.msg.Substring(msg.msg.IndexOf(' ') + 1))); + } catch(Exception e) { } + } + + if(msg.msg.ToLower().Trim().StartsWith("!raise")) { + try { + ctx.Raise(Int32.Parse(msg.msg.Substring(msg.msg.IndexOf(' ') + 1))); + } catch(Exception e) { } + } + } else { + Chat.sendMessage("You are not registered. Register with !register."); + } + } + } catch(Exception e) { + Console.WriteLine(e.Message + " " + e.StackTrace); + } + } + } +} diff --git a/www/auto.php b/www/auto.php index 3c01a13..335f568 100644 --- a/www/auto.php +++ b/www/auto.php @@ -13,15 +13,11 @@ if($_POST["editId"]) { } if($_POST["resptype"] && !$_POST["editId"]) { - $c = ""; - for($i=1;;$i++) { - if(!isset($_POST["if". $i ."param"])) break; - $c .= $_POST['if'.$i.'lpar'].",".$_POST['if'.$i.'not'].",".$_POST['if'.$i.'cond'].",".$_POST['if'.$i.'param'].",".$_POST['if'.$i.'rpar'].";"; - if(isset($_POST["op".$i])) $c .= $_POST["op".$i] .";"; - } - - mysql_query("INSERT INTO `responses` (`conditions`,`respid`,`parameters`,`cooldown`) VALUES ('". mysql_real_escape_string($c) ."',". $_POST['resptype'] .",'". mysql_real_escape_string($_POST['parameters']) ."',". (($_POST['ccd']==0)?-1:$_POST['cooldown']) .")") or die(mysql_error()); - mysql_query("UPDATE `updater` SET `responses`=1 WHERE `id`=1"); + if($_POST["autolink"] == -1) + mysql_query("INSERT INTO `autonomous` (`conditions`,`respid`,`parameters`,`cooldown`) VALUES ('". mysql_real_escape_string($c) ."',". $_POST['resptype'] .",'". mysql_real_escape_string($_POST['parameters']) ."',". (($_POST['ccd']==0)?-1:$_POST['cooldown']) .")") or die(mysql_error()); + else + // do query + mysql_query("UPDATE `updater` SET `autonomous`=1 WHERE `id`=1"); header("Location: auto.php"); } @@ -32,13 +28,57 @@ include("header.php"); function confirmDeletion(id) { var q = confirm("Are you sure you want to delete this response?"); - if(q) window.location.href = "resp.php?del="+id; + if(q) window.location.href = "auto.php?del="+id; } function handleRespChange() { document.getElementById("respDesc").innerHTML = document.getElementById(""+document.getElementById("resptype").selectedIndex).innerHTML; } + function doTimeTest(fieldname, fieldtext, canbezero) { + if(!isNaN(parseInt(fieldtext.trim())) && isFinite(parseInt(fieldtext.trim()))) { + if(parseInt(fieldtext) > ((canbezero)?-1:0)) { + return true; + } else { + alert(fieldname +" must be positive and nonzero integer!"); + return false; + } + } else { + alert(fieldname +" must be a finite integer!"); + return false; + } + } + + function evaluateCondition() { + if(document.getElementById("arname").value.trim() != "") { + if(doTimeTest("Periodicity",document.getElementById("period").value)) { + if(doTimeTest("Randomness",document.getElementById("randomness").value)) { + if(document.getElementById("autolink").selectedIndex != 0) { + if(doTimeTest("Link timeout",document.getElementById("timeout").value)) { + if(doTimeTest("Link randomness",document.getElementById("torandom").value)) { + document.getElementById("auto").submit(); + } + } + } else { + document.getElementById("auto").submit(); + } + } + } + } else { + alert("Friendly name cannot be blank!"); + } + } + + function handleAutolinkChange() { + if(document.getElementById("autolink").selectedIndex != 0) { + for(i = 1; i <= 3; i++) + document.getElementById("linktable").getElementsByTagName("tr")[i].style.display = "table-row"; + } else { + for(i = 1; i <= 3; i++) + document.getElementById("linktable").getElementsByTagName("tr")[i].style.display = "none"; + } + } + /*function coolChange() { if(document.getElementById("cdd").selectedIndex == 0) { document.getElementById("cooldown").disabled = true; @@ -75,6 +115,9 @@ include("header.php"); Create New Autonomous Routine
+

+ Friendly name: (for future reference) +

Trigger first routine on "; - for($i = 1; $i <= 12; $i++) { + for($i = 1; $i <= 24; $i++) { echo ""; } ?> @@ -105,11 +148,14 @@ include("header.php"); } ?> - +

+

+ After first trigger, + + + +
    continue triggering every seconds
    with a randomness of± seconds.
+

On trigger, @@ -134,26 +180,64 @@ include("header.php"); ?>

- - - - Parameters: -

- -
+ + + + Parameters: +
+ +

- Cooldown: - - seconds + After triggering, + + + + + + + + + + + + + + + + + + + + + + + + + +
    Force routine to trigger
after + seconds +
with a randomness of± + seconds. +
While waiting to trigger, + . +

- +      - +