how do i calculate user inputs in form? - ruby-on-rails

i have a rails app with user inputs, i have to perform a calculation and store its new value. eg of price is given which was earlier written in js and php
if(count($_POST['price']) >= 0 && !empty($_POST['price']))
{
$price = $_POST['price'];
$sql .= " '". $price ."', ";
switch ($price){
case ($price >= 0 && $price <= 21):
$price_rating = 0;
break;
case ($price >= 22 && $price <= 29):
$price_rating = 5;
break;
case ($price >= 30 && $price <= 39):
$price_rating = 7;
break;
case ($price >= 40 && $price <= 49):
$price_rating = 8;
break;
case ($price >= 50 && $price <= 59):
$price_rating = 6;
break;
default:
$price_rating = 0;
}
}
else
{
$price_rating = 0;
$sql .= " '', ";
}

everything is the same... in the according controller you just need to do
#price = params[:price] (which is the same as $price = $_POST['price'];)
if (0..21).include?(#price)
#price_rating = 0
if ... and so on

Related

How do I correct my code to enable my Expect Advisor to take trades?

My EA was taking only 1 trade at a time on a single currency pair, it ignores the other pairs till the current trade is closed. I decided to modify it. Now it's no more trading at all.
int Hour = TimeHour(TimeCurrent());
int DayOfWeek = DayOfWeek();
int total = OrdersTotal();
int count=0;
for(int i=0; i<total; i++)
{
if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol() == _Symbol && OrderMagicNumber()==MagicNumber)
{
Alert("Inside Order Select if");
// Checking if spread is less than 2.0 pips.
if(current_spread <= AllowableSpread)
{
// Checking for days of the week
if(DayOfWeek >= 1 && DayOfWeek <= 5)
{
// Checking for time of the day
if(Hour >= 3 && Hour <= 20)
{
if(ADXValue > 25)
{
if(RSIValue > 50 || RSIValue < 20)
{
if(PreviousFast<PreviousSlow && CurrentFast > CurrentSlow)
ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, Ask-(StopLoss*pips), Ask+(TakeProfit*pips), NULL, MagicNumber, 0, Green);
if(ticket<0)
Print("OrderSend failed with error #",GetLastError());
else
Print("OrderSend placed successfully");
}
if(RSIValue < 50 || RSIValue > 70)
{
if(PreviousFast>PreviousSlow && CurrentFast < CurrentSlow)
ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, Slippage, Bid+(StopLoss*pips), Bid-(TakeProfit*pips), NULL, MagicNumber, 0, Red);
if(ticket<0)
Print("OrderSend failed with error #",GetLastError());
else
Print("OrderSend placed successfully");
}
}
}

Is there a method to calculate the total duration a gif plays?

I want to calculate the total duration a gif plays. It can be either duration of a gif or frame count of the gif. Have tried using FLAnimatedImage, SDWebImage and YYImage but can't really attain what I am looking for. The gif is loaded from remote url and then I want to calculate the duration it plays.
This is the function that returns the total duration in GIF time units (1 unit = 10 msec).
data is a pointer to GIF data, size is its size.
long Duration(uint8_t *data, long size) {
long desc, time = 0;
uint8_t *buff;
if ((size > 13) && data && (data[0] == 71) && (data[1] == 73)
&& (data[2] == 70) && (data[3] == 56) && (data[5] == 97)
&& ((data[4] == 55) || (data[4] == 57))) {
buff = data + 13 + ((data[10] & 0x80)? 6 << (data[10] & 7) : 0);
if ((size -= buff - data) > 0)
while ((desc = *buff++) != 0x3B) {
size--;
if (desc == 0x2C) {
desc = 9 + ((buff[8] & 0x80)? 6 << (buff[8] & 7) : 0);
buff += desc;
if ((size -= desc) <= 0)
break;
}
else if ((desc == 0x21) && (*buff == 0xF9))
time += *(uint16_t*)(buff + 3);
buff++;
if (--size <= 0)
break;
do {
buff += (desc = 1 + *buff);
if ((size -= desc) <= 0)
return time;
} while (desc > 1);
}
}
return time;
}
This function parses GIF images by hand, extracting frame delay information and summing it.

GameMaker Studio 2 Click-Drag Tile Selection

I would like to implement a simple way to select a section of a tilemap, though I'm stuck on the math of updating the selected area as you drag.
I spent about 12 hours reasearching and trying different things to see if I can get this to work correctly. I didn't think this would be as tough as it is.
My prefered method of implementing the selection, is using four variables:
selection_x - X position of the top left corner of the selection
selection_y - Y position of the top left corner of the selection
selection_w - Width of the selection
selection_h - Height of the selection
The scrips tilex() and tiley() return the tile X/Y where the mouse is, calculating for zoom, and view offset.
Here is my attempt:
if(mouse_check_button_pressed(mb_left)) {
selecting = true;
selection_start = [tilex(), tiley()];
selection_stop = [tilex() + 1, tiley() + 1];
selection_x = tilex();
selection_y = tiley();
selection_w = 0;
selection_h = 0;
} else if(mouse_check_button(mb_left)) {
var old = selection_x;
selection_x = tilex() >= selection_x + selection_w ? tilex() : selection_x;
selection_w = tilex() >= selection_x + selection_w ? tilex() - selection_x : (old - selection_x);
var old = selection_y;
selection_y = tiley() >= selection_y + selection_h ? tiley() : selection_y;
selection_h = tiley() >= selection_y + selection_h ? tiley() - selection_y : (old - selection_y);
/*
if(selection_x >= tilex()) {
var old = selection_x;
selection_x = tilex();
selection_w += old - selection_x ;
selection_stop[0] = tilex() + 1;
show_debug_message("x 0");
} else if(selection_x + selection_w <= tilex()) {
selection_w = tilex() - selection_x + 1;
selection_start[0] = tilex();
show_debug_message("x 1");
}
if(selection_y >= tiley()) {
var old = selection_y;
selection_y = tiley();
selection_h += old - selection_y;;
selection_stop[1] = tiley() + 1;
show_debug_message("y 0");
} else if(selection_y + selection_h <= tiley()) {
selection_h = tiley() - selection_y + 1;
selection_start[1] = tiley();
show_debug_message("y 1");
}
*/
/*
if(selection_x > tilex() && selection_y > tiley()) {
} else if(selection_x < tilex() && selection_y < tiley()) {
selection_x = tilex();
selection_y = tiley();
selection_w = selection_x - tilex();
selection_h = selection_y - tiley();
}
*/
/*
if(selection_start[0] > selection_stop[0] || selection_start[1] < selection_stop[1]) {
//selection_start = [tilex(), tiley()];
} else {
selection_stop = [tilex() + 1, tiley() + 1];
}
*/
}
Thanks for reading! Hope this can get resolved soon.
- Searous <3

Limit lines and characters per line in textarea

After looking at many solutions, I got the following solutions that does exactly what I want.
SOLUTION 1 : works well except it does not work in IE(11)
I will much appreciate if someone can help me out fixing this for IE.
code taken from :https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement
function checkRows(oField, oKeyEvent) {
var nKey = (oKeyEvent || /* old IE */ window.event || /* check is not supported! */ { keyCode: 38 }).keyCode,
// put here the maximum number of characters per line:
nCols = 30,
// put here the maximum number of lines:
nRows = 5,
nSelS = oField.selectionStart, nSelE = oField.selectionEnd,
sVal = oField.value, nLen = sVal.length,
nBackward = nSelS >= nCols ? nSelS - nCols : 0,
nDeltaForw = sVal.substring(nBackward, nSelS).search(new RegExp("\\n(?!.{0," + String(nCols - 2) + "}\\n)")) + 1,
nRowStart = nBackward + nDeltaForw,
aReturns = (sVal.substring(0, nSelS) + sVal.substring(nSelE, sVal.length)).match(/\n/g),
nRowEnd = nSelE + nRowStart + nCols - nSelS,
sRow = sVal.substring(nRowStart, nSelS) + sVal.substring(nSelE, nRowEnd > nLen ? nLen : nRowEnd),
bKeepCols = nKey === 13 || nLen + 1 < nCols || /\n/.test(sRow) || ((nRowStart === 0 || nDeltaForw > 0 || nKey > 0) && (sRow.length < nCols || (nKey > 0 && (nLen === nRowEnd || sVal.charAt(nRowEnd) === "\n"))));
return (nKey !== 13 || (aReturns ? aReturns.length + 1 : 1) < nRows) && ((nKey > 32 && nKey < 41) || bKeepCols);
}
<form>
<p>Textarea with fixed number of characters per line:<br />
<textarea cols="50" rows="10" onkeypress="return checkRows(this, event);" onpaste="return false;" /></textarea></p>
</form>
SOLUTION 2
It works in IE but it fails in when editing the lines. You type a line, go back using left arrow keys and type you can type 1 letter and the cursor goes back to the end.
code taken from : http://cgodmer.com/?p=55 that
//limit # of lines of a text area and length of those lines
//<textarea rows="4" chars="40" onkeyup="limitTextareaLine(this, event)" ></textarea>
//Author: CGodmer (Feb 22, 2012)
function limitTextareaLine(x, e, nRows, nChars) {
var rows = $(x).val().split("\n").length; //number of rows
var lineCharLimit = nChars; //number of characters to limit each row to
var rowLimit = nRows; //number of rows to allow
//limit length of lines
for (var i = 0; i < rows; i++) {
var rowLength = $(x).val().split("\n")[i].length;
//check to see if any of the rows have a length greater than the limit
if (rowLength > lineCharLimit) {
//if it does save the beg index of the row
var rowstartindex = $(x).val().indexOf($(x).val().split("\n")[i]);
//use the index to get a new value w/ first lineCharLimit number of characters
var newval = $(x).val().substring(0, rowstartindex + lineCharLimit)
+ $(x).val().substring(rowstartindex + rowLength, $(x).val().length);
//replace that value in the textarea
$(x).val(newval);
//set character position back to end of the modified row
setCaretPosition($(x)[0], rowstartindex + lineCharLimit);
}
}
//limit # of lines to limit to is rows attribute
while($(x).val().split("\n").length > rowLimit) {
$(x).val($(x).val().substring(0, $(x).val().length - $(x).val().split("\n")[rowLimit].length - 1));
}
}
//Set caret position in the supplied control to position
//From: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
function setCaretPosition(ctrl, pos) {
if (ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(pos, pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea rows="5" cols="10" onkeyup="limitTextareaLine(this, event, 5, 10)" ></textarea>
Try this one, it may solve this problem.
function ValidationAddress() {
var allText;
allText = document.getElementById("<%= txtAdd1.ClientID %>").value;
allText = document.getElementById('<%=txtAdd1.ClientID%>').value;
var A = allText.split('\n');
var L = A.length;
if (L > 3 && event.keyCode != 8 && event.keyCode != 46) {
alert("You have exceeded maximum limit.Cannot insert more than 3 lines.");
valert = false;
return false;
}
var arr = allText.split("\n");
for (var i = 0; i < arr.length; i++) {
if (arr[i].length > 10) {
alert("You have exceeded the Maximum Limit..Characters per line is 70 in Address Field !");
valert = false;
return false;
}
}
}

ActionScript Unexpected Slashes, Parenthesis, and Squiggly-brackets?

This ActionScript code I have been working on for a few days now works 100% just fine in JavaScript, but when I try to compile it in ActionScript it says I have unexpected /, ), and } symbols. Is this syntax wrong and if so how should I fix it? I figured I could test it as Javascript for quicker testing using http://jsfiddle.net/ but now I'm like =(
var txt = "This is a [rainbow]test to show that I can[/rainbow] make whatever I want [rainbow]appear as a rainbow[/rainbow] because I am [rainbow]awesome[/rainbow].";
if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
var colors = ['f0f', 'f0c', 'f09', 'f06', 'f03', 'f00', 'f30', 'f60', 'f90', 'fc0', 'ff0', 'cf0', '9f0', '6f0', '3f0', '0f0', '0f3', '0f6', '0f9', '0fc', '0ff', '0cf', '09f', '06f', '03f', '00f', '30f', '60f', '90f', 'c0f'];
function rainbowify(text) {
return text.replace(/\[rainbow\](.+?)\[\/rainbow\]/g, function(_, inner) {
return inner.replace(/./g, function(ch, i) {
return '<font color="#' + colors[i % colors.length] + '">' + ch + '</font>';
});
})
}
txt = rainbowify(txt);
document.write(txt);
}​
Well, this is it:
txt = txt.replace("&apos;", "#");
if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
var firstChar = txt.indexOf("[rainbow]") + 9;
var lastChar = txt.indexOf("[/rainbow]");
while (lastChar <= txt.lastIndexOf("[/rainbow]")) {
var RAINBOWTEXT = '';
var i = firstChar;
while (i < lastChar) {
RAINBOWTEXT += txt.charAt(i);
i++
}
var text = RAINBOWTEXT;
var texty = '';
colors = new Array('ff00ff','ff00cc','ff0099','ff0066','ff0033','ff0000','ff3300','ff6600','ff9900','ffcc00','ffff00','ccff00','99ff00','66ff00','33ff00','00ff00','00ff33','00ff66','00ff99','00ffcc','00ffff','00ccff','0099ff','0066ff','0033ff','0000ff','3300ff','6600ff','9900ff','cc00ff');
i = 0;
while (i <= text.length) {
var t = text.charAt(i);
if (t != undefined) {
texty += "<font color=\"#" + colors[i % colors.length] + "\">" + t + "</font>";
i++;
}
}
texty = texty.replace("> <", "> <");
var REPLACEME = "[rainbow]" + RAINBOWTEXT + "[/rainbow]";
txt = txt.replace(REPLACEME, texty);
if (lastChar == txt.lastIndexOf("[/rainbow]")) {
break;
}
nextChar = lastChar + 10;
firstChar = txt.indexOf("[rainbow]", lastChar) + 9;
lastChar = txt.indexOf("[/rainbow]", lastChar);
}
}
txt = txt.replace("#", "&apos;");

Resources