Page 1 of 1

Allowing user's limit of adding....

Posted: Fri Jan 16, 2009 7:34 pm
by dragonsmistress
I need some help figuring this out. I have a script for people to add their dragons to my website in order to receive clicks. However... when they add ICU dragons (emergency dragons) they need to have 2.5 days or less before they die. How can I make it so that people can not add the dragons over that limit? Here is my add script.

Code: Select all

<?
defined("INDEX") or die("You cannot access this file directly");
 
if(!empty($_POST['is_ajax'])) {
    $scrollname = stripslashes($_POST['scrollname']);
    if($act == "read") {
        $file = @file_get_contents("http://dragcave.net/user/" . urlencode($scrollname));
        if(!$file) {
            die(json_encode(array("error"=>"The dragcave website could not be loaded; the scroll was not read.\n\nPlease try again in a couple minutes!")));
        }
        $pattern = "#/image/(.*?)\.gif(?:.*?)<td>(.*?)</td>(?:.*?)(Egg|Hatchling|Adult)#";
        preg_match_all($pattern, $file, $matches);
        
        $list = $matches[1];
        $name = $matches[2];
        $type = $matches[3];
    
        $dragons = array("eggs"=>array(), "hatchlings"=>array());
        
        foreach($list as $id => $item) {
            if(!array_key_exists(strtolower($type[$id]) . "s", $dragons))
                continue;
            $dragons[strtolower($type[$id]) . "s"][] = array("id"=>$item,"name"=>$name[$id],"added"=>"");
        }
        
        $cur_scroll = $DB->get_row("SELECT * FROM scrolls WHERE scrollname='" . sanitize($scrollname) . "' LIMIT 1");
        if(!empty($cur_scroll)) {
            $cur_scroll['eggs'] = unserialize($cur_scroll['eggs']);
            $cur_scroll['hatchlings'] = unserialize($cur_scroll['hatchlings']);
            $cur_scroll['icu'] = unserialize($cur_scroll['icu']);
            foreach($dragons['eggs'] as $id=>$egg) {
                if(in_array($egg['id'], $cur_scroll['eggs']))
                    $dragons['eggs'][$id]['added'] = ' checked="checked"';
                if(in_array($egg['id'], $cur_scroll['icu']))
                    $dragons['eggs'][$id]['icu'] = ' checked="checked"';
            }
            foreach($dragons['hatchlings'] as $id=>$hatchling) {
                if(in_array($hatchling['id'], $cur_scroll['hatchlings']))
                    $dragons['hatchlings'][$id]['added'] = ' checked="checked"';
                if(in_array($hatchling['id'], $cur_scroll['icu']))
                    $dragons['hatchlings'][$id]['icu'] = ' checked="checked"';
            }
        }
        $dragons['total']['eggs'] = count($dragons['eggs']);
        $dragons['total']['hatchlings'] = count($dragons['hatchlings']);
        $dragons['total']['total'] = $dragons['total']['eggs'] + $dragons['total']['hatchlings'];
        $dragons['scrollname'] = stripslashes($scrollname);
        
        die(json_encode($dragons));
    } elseif($act == "submit") {
        $cur_scroll = $DB->get_row("SELECT * FROM scrolls WHERE scrollname='" . sanitize($scrollname) . "' LIMIT 1");
        $dragons['eggs'] = is_array($_POST['eggs']) ? sanitize($_POST['eggs']) : array();
        $dragons['hatchlings'] = is_array($_POST['hatchlings']) ? sanitize($_POST['hatchlings']) : array();
        $dragons['icu'] = is_array($_POST['icu']) ? sanitize($_POST['icu']) : array();
        if(empty($dragons['eggs']) && empty($dragons['hatchlings']) && empty($dragons['icu'])) {
            if(!empty($cur_scroll)) {
                $DB->query("DELETE FROM scrolls WHERE scrollname='" . $cur_scroll['scrollname'] . "' LIMIT 1");
                ?>alert("Your scroll has been removed!");<?
            } else {
                ?>alert("Your scroll was not added!");<?
            }
            die();
        }
        if(empty($cur_scroll)) {
            $DB->query("INSERT INTO scrolls SET scrollname='" . sanitize($scrollname) . "',
                        eggs='" . serialize($dragons['eggs']) . "', 
                        hatchlings='" . serialize($dragons['hatchlings']) . "',
                        icu='" . serialize($dragons['icu']) . "'");
        } else {
            $DB->query("UPDATE scrolls SET eggs='" . serialize($dragons['eggs']) . "', 
                        hatchlings='" . serialize($dragons['hatchlings']) . "',
                        icu='" . serialize($dragons['icu']) . "',
                        last_checked=0
                        WHERE scrollname='" . sanitize($_POST['scrollname']) . "' LIMIT 1");
        }
        ?>alert("Dragons submitted successfully!");<?
        die();
    }
}
?>
<script type="text/javascript">
    function ReadScroll() {
        $("#ReadLink > span").toggle();
        if(String($("#scrollname").val()).replace(/ /g, "").length == 0) {
            alert("You must enter a scroll name!");
            $("#ReadLink > span").toggle();
            $("#scrollname").focus();
            return;
        }
        $.post("<?=$config->url['main']?>add/read", "scrollname=" + $("#scrollname").val() + "&is_ajax=1", ScrollRead, "json");
    }
    
    function ScrollRead(data) {
        if(typeof data['error'] != "undefined") {
            alert(data['error']);
            $("#ReadLink > span").toggle();
            return;
        }
        $("#scroll_name").text(data['scrollname']);
        $("#total_eggs").text(data['total']['eggs']);
        $("#total_hatchlings").text(data['total']['hatchlings']);
        
        $("#eggs_table tr:gt(0), #hatchlings_table tr:gt(0)").remove();
        
        if(data['total']['eggs'] == 0) {
            $("#eggs_header, #eggs_table").hide();
        } else {
            for(var i=0; i < data['total']['eggs']; i++) {
                $("#eggs_table").append('<tr><td style="text-align:left;"><img src="http://dragcave.net/image/' + data['eggs'][i]['id'] + '.gif" onerror="this.src=\'http://dragcave.net/image/' + data['eggs'][i]['id'] + '\';" border="0" /></td><td style="text-align:left;"><a href="http://dragcave.net/viewdragon/' + data['eggs'][i]['id'] + '" target="_blank">View</a></td><td style="text-align:center;"><input type="checkbox" class="add_eggs" name="eggs[]" value="' + data['eggs'][i]['id'] + '"' + data['eggs'][i]['added'] + ' /></td><td><input type="checkbox" class="icu_eggs" name="icu[]" value="' + data['eggs'][i]['id'] + '"' + data['eggs'][i]['icu'] + ' /></td></tr>');
            }
            $("#eggs_header, #eggs_table").show();
        }
        if(data['total']['hatchlings'] == 0) {
            $("#hatchlings_header, #hatchlings_table").hide();
        } else {
            for(var i=0; i < data['total']['hatchlings']; i++) {
                $("#hatchlings_table").append('<tr><td style="text-align:left;"><img src="http://dragcave.net/image/' + data['hatchlings'][i]['id'] + '.gif" onerror="this.src=\'http://dragcave.net/image/' + data['hatchlings'][i]['id'] + '\';" border="0" /></td><td style="text-align:left;">' + data['hatchlings'][i]['name'] + '</td><td style="text-align:left;"><a href="http://dragcave.net/viewdragon/' + data['hatchlings'][i]['id'] + '" target="_blank">View</a></td><td style="text-align:center;"><input type="checkbox" class="add_hatchlings" name="hatchlings[]" value="' + data['hatchlings'][i]['id'] + '"' + data['hatchlings'][i]['added'] + ' /></td><td><input type="checkbox" class="icu_hatchlings" name="icu[]" value="' + data['hatchlings'][i]['id'] + '"' + data['hatchlings'][i]['icu'] + ' /></td></tr>');
            }
            $("#hatchlings_header, #hatchlings_table").show();
        }
        if(data['total']['total'] == 0) {
            $("#add_dragons").hide();
        } else {
            $(".icu_eggs").click(function(){
                var sel = $(this).parents("tr").find(".add_eggs");
                if($(this).is(":checked")) {
                    $(sel).attr("checked","checked");
                }
            });
            $(".icu_hatchlings").click(function(){
                var sel = $(this).parents("tr").find(".add_hatchlings");
                if($(this).is(":checked")) {
                    $(sel).attr("checked","checked");
                }
            });
            $(".add_eggs").click(function(){
                if(!$(this).is(":checked")) {
                    var sel = $(this).parents("tr").find(".icu_eggs");
                    $(sel).removeAttr("checked");
                }
            });
            $(".add_hatchlings").click(function(){
                if(!$(this).is(":checked")) {
                    var sel = $(this).parents("tr").find(".icu_hatchlings");
                    $(sel).removeAttr("checked");
                }
            });
            $("#add_dragons").show();
        }
        
        $("#content > div").toggle();
    }
    
    function NewScroll() {
        $("#scrollname").val("");
        $("#content > div, #ReadLink > span").toggle();
    }
    
    
    var AddDragonsData = "";
    function AddDragons() {
        $("#AddLink > span").toggle();
        AddDragonsData = "";
        $.each($("#eggs_table input:checked, #hatchlings_table input:checked"), function(){AddDragonsData += $(this).attr('name') + "=" + $(this).val() + "&";});
        if(AddDragonsData.length == 0) {
            if(!confirm("You did not select any dragons to be added!\n\nBy pressing Yes and continuing to send blank data, your scroll will be removed from the site if it was previously added or will not be added at all.\n\nDo you wish to continue?")) {
                $("#AddLink > span").toggle();
                return;
            }
        }
        AddDragonsData += "scrollname=" + $("#scroll_name").text();
        
        $.post("<?=$config->url['main']?>add/submit", AddDragonsData + "&is_ajax=1", DragonsAdded);
    }
    
    function DragonsAdded(data) {
        $("#AddLink > span").toggle();
        eval(data);
    }
    
    function SelectAll(id) {
        if($("." + id + ":checked").length < $("." + id).length) {
            if(id == "icu_eggs") {
                $(".add_eggs").attr("checked","checked");
            } else if(id == "icu_hatchlings") {
                $(".add_hatchlings").attr("checked","checked");
            }
            $("." + id).attr("checked","checked");
        } else {
            if(id == "add_eggs") {
                $(".icu_eggs").removeAttr("checked");
            } else if(id == "add_hatchlings") {
                $(".icu_hatchlings").removeAttr("checked");
            }
            $("." + id + ":checked").removeAttr("checked");
        }
    }
</script>
 
<style type="text/css">
 
body {
  background-color: #000000;
  font-family: verdana;
  font-size: 10px;
  color: #FFFFFF;
  text-align: center;
  }
 
  A:link {text-decoration: underline; color: #fac5c5;}
  A:active {text-decoration: underline; color: #fac5c5;}
  A:visited {text-decoration: underline; color: #fac5c5;}
  A:hover {text-decoration: underline; color: #fac5c5; font-weight: bold;}
 
fieldset { border:2px solid #fac5c5;
  text-align:left;
  font-size:10px;
  background-color: #000000;
  width:400px;
  padding-left: 5px;
  }
 
legend {
  background-color: #000000;
  padding-left: 0px;
  border:2px solid #fac5c5;
  font-size:10px;
  font-weight: bold;  
  color: #fac5c5;
  text-align:left;
  }
 
  #sidenav fieldset {
    margin-left: auto;
    margin-right: auto;
  }
 
ul { 
  list-style-type: circle;  
  color: #fac5c5;
  } 
 
table {
  font-family : Verdana,Helvetica,Arial,sans-serif;
  font-size: 10px;
  color: #fac5c5;
  text-align: center;
  }
 
</style>
<div>
    <h2>Add Eggs and Hatchlings from which scroll?</h2>
    <label for="scrollname">Enter your scroll name here:</label>
    <input type="text" name="scrollname" id="scrollname" maxlength="50" />
    <br /><br />
    <div style="text-align: left;" id="ReadLink">
        <span><a href="javascript&#058;;" onclick="ReadScroll();">Read this scroll!</a></span>
        <span style="display:none;"><b>Reading... Please wait!</b></span>
    </div>
</div>
 
<div style="display: none;">
    <h2>The '<span id="scroll_name"></span>' scroll has been read</h2>
    <table>
        <tr>
            <td><b>Total eggs:</b></td>
            <td id="total_eggs"></td>
        </tr>
        <tr>
            <td><b>Total hatchlings:</b></td>
            <td id="total_hatchlings"></td>
        </tr>
    </table>
    <h3 id="eggs_header">Eggs Found</h3>
    <table id="eggs_table" width="100%">
        <tr>
            <th style="text-align:left;">Egg</th>
            <th style="text-align:left;">View</th>
            <th width="10px"><a href="javascript&#058;;" onclick="this.blur();SelectAll('add_eggs');">Select</a></th>
            <th width="10px" nowrap="nowrap"><a href="javascript&#058;;" onclick="this.blur();SelectAll('icu_eggs');">Add to ICU</a></th>
        </tr>
    </table>
    <h3 id="hatchlings_header">Hatchlings Found</h3>
    <table id="hatchlings_table" width="100%">
        <tr>
            <th style="text-align:left;">Dragon</th>
            <th style="text-align:left;">Name</th>
            <th style="text-align:left;">View</th>
            <th width="10px"><a href="javascript&#058;;" onclick="this.blur();SelectAll('add_hatchlings');">Select</a></th>
            <th width="10px" nowrap="nowrap"><a href="javascript&#058;;" onclick="this.blur();SelectAll('icu_hatchlings');">Add to ICU</a></th>
        </tr>
    </table>
    <br /><br />
    <div id="AddLink" style="text-align: center;">
        <span>
            <span id="add_dragons"><a href="javascript&#058;;"onclick="AddDragons();">Add selected dragons</a> ::</span>
            <a href="#" onclick="NewScroll();">Read another scroll</a>
        </span>
        <span style="display:none;">
            <b>Adding selected dragons... Please wait!</b>
        </span>
    </div>
</div>
<br /><br />
<fieldset>
    <legend>Please note</legend>
    <ul>
        <li>To remove your scroll from the hatchery, simply enter your scrollname and click "Read this scroll!". Make sure all boxes are UNchecked and then click "add selected dragons".</li>
        <li><strong>LEETLE TREES ARE NOT TO BE SUBMITTED. THEY WILL BE IMMEDIATELY REMOVED.</strong></li>
        <li>When an egg hatches, it'll automatically be moved from the Eggs section to the Hatchlings section here.</li>
        <li>Frozen hatchlings will be automatically removed during these automated checks.</li>
        <li>Dragons that need to be placed in the ICU will be moved during automated checks.</li>
        <li>To add an emergency dragon to the ICU please check the box next to that dragon. Dragons that have 2.5 days or less ONLY please. Others will be removed.</li>
        <li>These automated processes will not be instantaneous as the server only checks a certain amount of scrolls within a certain amount of time every day.</li>
    </ul>
</fieldset>

Re: Need Help ):

Posted: Fri Jan 16, 2009 7:40 pm
by Burrito
that's a pretty big wall of code to sort through to find what you're looking to do. Also your question is a bit vague.

What I would do is insert a row into a database with the current timestamp. Then select off of that timestamp field where it's < now() plus 60 hours (2.5 days)

Re: Need Help ):

Posted: Fri Jan 16, 2009 7:42 pm
by dragonsmistress
Burrito wrote:that's a pretty big wall of code to sort through to find what you're looking to do. Also your question is a bit vague.

What I would do is insert a row into a database with the current timestamp. Then select off of that timestamp field where it's < now() plus 60 hours (2.5 days)
:/ I'm relatively new to php so could you say that so a noob would understand (:

Re: Allowing user's limit of adding....

Posted: Fri Jan 16, 2009 7:45 pm
by Burrito
first please explain your question in more detail.

Are you looking to create something that can only be used for 2.5 days as I interpreted from your OP?

Re: Allowing user's limit of adding....

Posted: Fri Jan 16, 2009 7:48 pm
by dragonsmistress
When people add a dragon into the ICU I don't want them to be able to add any dragon that has more than 2.5 days to live. I'm not sure how much better I can explain it. The site mine is based off of is dragcave.net. Mine is hatchingdragons.net.... maybe if you see it you'll understand a bit better. Problem is I had someone write that script for me so that's why I am having trouble figuring out how to change it.

Re: Allowing user's limit of adding....

Posted: Fri Jan 16, 2009 7:57 pm
by Burrito
I don't know what the 'ICU' is.

but basically the concept for your issue would be something like this:

Code: Select all

 
$dragonTimeToLive = convertToHours($dragonTimeToLIve);
if($dragonTimeToLIve > 60)
{
  // do whatever you need to (insert to db I assume)
}
 

Re: Allowing user's limit of adding....

Posted: Sat Jan 17, 2009 1:58 pm
by califdon
dragonsmistress wrote:Problem is I had someone write that script for me so that's why I am having trouble figuring out how to change it.
Problem is that it would take one of us hours or days to understand your specific game operation, analyze somebody else's script, determine how to modify it and write the code to do it. It might take the person who wrote the script 10 minutes.

Re: Allowing user's limit of adding....

Posted: Thu Jan 22, 2009 7:31 pm
by dragonsmistress
califdon wrote:
dragonsmistress wrote:Problem is I had someone write that script for me so that's why I am having trouble figuring out how to change it.
Problem is that it would take one of us hours or days to understand your specific game operation, analyze somebody else's script, determine how to modify it and write the code to do it. It might take the person who wrote the script 10 minutes.
Does this mean I'm screwed? :banghead: