My bot gets very slow when reading a message - discord.js

I have a problem, I have 3 commands that are always active, which detect when spam, links, or flood occurs on a server, but when activated the bot gets very slow and takes a long time to execute a command, what can I do?
commands in the main file
client.commands.get('spam').execute(message, keyv);
client.commands.get('links').execute(message, keyv);
client.commands.get('flood').execute(message, keyv);
spam.js
const Discord = require("discord.js")
module.exports = {
name: 'spam',
description: 'detecta spam',
execute(message, keyv) {
(async () => {
if(message.author.bot) return;
const serverid = message.guild.id;
const channelid = message.channel.id;
var affectbotsenadis = await keyv.get('spam' + "affectbots" + "enadis" + serverid);
var delmsgenadis = await keyv.get('spam' + "delmsg" + "enadis" + serverid);
var getsupsafeenadis = await keyv.get('supsafe' + "enadis" + serverid);
var getenadis = await keyv.get('spamprotection' + "enadis" + channelid + serverid);
if(message.content.includes("acaba") || message.content.includes("unio")) return;
else if(message.content.includes("unanse") && message.content.includes("server")) avise();
else if(message.content.includes("Unanse") && message.content.includes("server")) avise();
else if(message.content.includes("UNANSE") && message.content.includes("SERVER")) avise();
else if(message.content.includes("Únanse") && message.content.includes("server")) avise();
else if(message.content.includes("únanse") && message.content.includes("server")) avise();
else if(message.content.includes("ÚNANSE") && message.content.includes("SERVER")) avise();
else if(message.content.includes("une") && message.content.includes("server")) avise();
else if(message.content.includes("Une") && message.content.includes("server")) avise();
else if(message.content.includes("UNE") && message.content.includes("SERVER")) avise();
else if(message.content.includes("una") && message.content.includes("server")) avise();
else if(message.content.includes("Una") && message.content.includes("server")) avise();
else if(message.content.includes("UNA") && message.content.includes("SERVER")) avise();
else if(message.content.includes("unir") && message.content.includes("server")) avise();
else if(message.content.includes("Unir") && message.content.includes("server")) avise();
else if(message.content.includes("UNIR") && message.content.includes("SERVER")) avise();
else if(message.content.includes("uniros") && message.content.includes("server")) avise();
else if(message.content.includes("Uniros") && message.content.includes("server")) avise();
else if(message.content.includes("UNIROS") && message.content.includes("SERVER")) avise();
else if(message.content.includes("unios") && message.content.includes("server")) avise();
else if(message.content.includes("Unios") && message.content.includes("server")) avise();
else if(message.content.includes("UNIOS") && message.content.includes("SERVER")) avise();
else if(message.content.includes("Unanse") && message.content.includes("serv")) avise();
else if(message.content.includes("unanse") && message.content.includes("serv")) avise();
else if(message.content.includes("UNANSE") && message.content.includes("SERV")) avise();
else if(message.content.includes("Unete") && message.content.includes("server")) avise();
else if(message.content.includes("unete") && message.content.includes("server")) avise();
else if(message.content.includes("UNETE") && message.content.includes("SERVER")) avise();
else if(message.content.includes("ÚNETE") && message.content.includes("SERVER")) avise();
else if(message.content.includes("Únete") && message.content.includes("server")) avise();
else if(message.content.includes("únete") && message.content.includes("server")) avise();
else if(message.content.includes("Unanse") && message.content.includes("servidor")) avise();
else if(message.content.includes("UNANSE") && message.content.includes("SERVIDOR")) avise();
else if(message.content.includes("Únanse") && message.content.includes("servidor")) avise();
else if(message.content.includes("únanse") && message.content.includes("servidor")) avise();
else if(message.content.includes("ÚNANSE") && message.content.includes("SERVIDOR")) avise();
else if(message.content.includes("une") && message.content.includes("servidor")) avise();
else if(message.content.includes("Une") && message.content.includes("servidor")) avise();
else if(message.content.includes("UNE") && message.content.includes("SERVIDOR")) avise();
else if(message.content.includes("unir") && message.content.includes("servidor")) avise();
else if(message.content.includes("Unir") && message.content.includes("servidor")) avise();
else if(message.content.includes("UNIR") && message.content.includes("SERVIDOR")) avise();
else if(message.content.includes("uniros") && message.content.includes("servidor")) avise();
else if(message.content.includes("Uniros") && message.content.includes("servidor")) avise();
else if(message.content.includes("UNIROS") && message.content.includes("SERVIDOR")) avise();
else if(message.content.includes("unios") && message.content.includes("servidor")) avise();
else if(message.content.includes("Unios") && message.content.includes("servidor")) avise();
else if(message.content.includes("UNIOS") && message.content.includes("SERVIDOR")) avise();
function avise(){
if(affectbotsenadis == "false"){
if(message.author.bot) return;
}
if(getsupsafeenadis == "true"){
if(message.member.hasPermission("MANAGE_MESSAGES")) return;
}
if(getenadis == "disable") return;
if(delmsgenadis == "true"){
message.delete();
}
else{
message.react("⚠")
}
const embed = new Discord.RichEmbed()
.setDescription(message.author + ", Cuidado con ese spam")
.setColor("RED")
message.channel.send(embed)
}
})();
},
};
links.js
const Discord = require("discord.js")
module.exports = {
name: 'links',
description: 'molesta al bot',
execute(message, keyv) {
if(message.content.includes('discord.gg/'||'discordapp.com/invite/')) avise2();
else if(message.content.includes("http://") && message.content.includes(".com")) avise2();
else if(message.content.includes("https://") && message.content.includes(".com")) avise2();
else if(message.content.includes("http://") && message.content.includes(".mx")) avise2();
else if(message.content.includes("https://") && message.content.includes(".mx")) avise2();
function avise2(){
(async () => {
const serverid = message.guild.id;
const channelid = message.channel.id;
let affectbotsenadis = await keyv.get('links' + "affectbots" + "enadis" + serverid);
let delmsgenadis = await keyv.get('links' + "delmsg" + "enadis" + serverid);
let getsupsafeenadis = await keyv.get('supsafe' + "enadis" + serverid);
let getenadis = await keyv.get('linksprotection' + "enadis" + channelid + serverid);
if(affectbotsenadis == "false"){
if(message.author.bot) return;
}
if(getsupsafeenadis == "true"){
if(message.member.hasPermission("MANAGE_MESSAGES")) return;
}
if(getenadis == "disable") return;
if(delmsgenadis == "true"){
message.delete();
}
else{
message.react("⚠")
}
const embed = new Discord.RichEmbed()
.setDescription(message.author + ", Cuidado con ese link")
.setColor("RED")
message.channel.send(embed)
})();
}
},
};
flood.js
const Discord = require("discord.js")
const client = new Discord.Client();
module.exports = {
name: 'flood',
description: 'Ping!',
execute(message, keyv){
(async () => {
if(message.author.bot) return;
if(message.content.length >= 450){
const serverid = message.guild.id;
const channelid = message.channel.id;
var getenadis = await keyv.get('floodprotection' + "enadis" + channelid + serverid);
if(getenadis == "disable") return;
message.react("⚠")
const embed = new Discord.RichEmbed()
.setDescription(message.author + ", Cuidado con ese flood")
.setColor("RED")
message.channel.send(embed)
}
})();
},
};
when I remove those commands from the code, the bot is normal again, but when you put them back, it gets slow again, and it takes about 3 seconds to execute a command, what is wrong or what can I do?

Related

How to use an array in an if statement

I am writing the following code including two if statements in swift 5, where I am splitting a sentence into an array including words and punctuations (How to split a sentence into words as well as punctuations and spaces? [Swift]).
As I use similar if statements a lot, it is getting annoying to modify one by one. Is there any way I can make it easier to modify if necessary? (for example by using an array)
The array I am thinking about looks as follows (please pretend that this includes the same characters in the first and the second if-statements in the code..):
let characterToStartNewWord: [String] = [" ","(",")","-", "—", "`", "‘", "/", "*","”", "[", "]", "—", "“", ":", ";", "!", "?"]
code
func sentenceSplitter(text_input: String) -> [String] {
// delete "- "
let text: String = text_input.replacingOccurrences(of: "- ", with: "")
var list = [String]()
let characterToStartNewWord: [String] = [" ","(",")","-", "—", "`", "‘", "/", "*","”", "[", "]", "—", "“"]
var currentSubString = "";
text.enumerateSubstrings(in: text.startIndex..<text.endIndex, options: String.EnumerationOptions.byComposedCharacterSequences) { (substring, substringRange, enclosingRange, value) in
if let _subString = substring {
if (!currentSubString.isEmpty &&
(_subString.compare(" ") == .orderedSame
|| _subString.compare(",") == .orderedSame
|| _subString.compare(".") == .orderedSame
|| _subString.compare(";") == .orderedSame
|| _subString.compare("!") == .orderedSame
|| _subString.compare("?") == .orderedSame
|| _subString.compare(":") == .orderedSame
|| _subString.compare("(") == .orderedSame
|| _subString.compare(")") == .orderedSame
|| _subString.compare("-") == .orderedSame
|| _subString.compare("“") == .orderedSame
|| _subString.compare("*") == .orderedSame
|| _subString.compare("/") == .orderedSame
|| _subString.compare("[") == .orderedSame
|| _subString.compare("]") == .orderedSame
|| _subString.compare("—") == .orderedSame
|| _subString.compare("‘") == .orderedSame
|| _subString.compare("`") == .orderedSame
)
) {
//create word if see any of those character and currentSubString is not empty
list.append(currentSubString)
if _subString == " " {
list.append(_subString)
}
currentSubString = _subString.trimmingCharacters(in: CharacterSet.whitespaces)
characterToStartNewWord.forEach{
if _subString == $0 && _subString != " " {
list.append(_subString)
currentSubString = currentSubString.trimmingCharacters(in: CharacterSet(charactersIn: $0))
}
}
} else {
if (
_subString.compare(" ") != .orderedSame &&
_subString.compare("(") != .orderedSame &&
_subString.compare(")") != .orderedSame &&
_subString.compare("-") != .orderedSame &&
_subString.compare("*") != .orderedSame &&
_subString.compare("”") != .orderedSame &&
_subString.compare("—") != .orderedSame &&
_subString.compare("`") != .orderedSame &&
_subString.compare("‘") != .orderedSame &&
_subString.compare("/") != .orderedSame &&
_subString.compare("[") != .orderedSame &&
_subString.compare("]") != .orderedSame &&
_subString.compare("—") != .orderedSame &&
_subString.compare("‘") != .orderedSame &&
_subString.compare("`") != .orderedSame)
{
currentSubString += _subString
} else {
characterToStartNewWord.forEach{
if _subString == $0 {
list.append(_subString)
}
}
}
}
}
}
//last word
if (!currentSubString.isEmpty) {
list.append(currentSubString)
}
return list
}
No need to use an array. You can simply use a string (collection of characters) to initialize a set and check if it contains or not a character:
let characters = Set(#",;:!?()-*”—`‘/[]—"#)
if characters.contains(char) {
// code
}
If you just need to check if a string starts or not with one of those characters:
let string = "[Test]"
if let first = string.first, characters.contains(first) {
print(true)
}
edit/update:
In your case (I have not tested the code below):
func sentenceSplitter(text_input: String) -> [String] {
let text = text_input.replacingOccurrences(of: "- ", with: "")
var list = [String]()
let characterToStartNewWord = Set(#" ()-—`‘/*”[]—"#)
var currentSubString = ""
text.enumerateSubstrings(
in: text.startIndex...,
options: .byComposedCharacterSequences
) { substring, substringRange, enclosingRange, stop in
if let subString = substring {
if currentSubString.count == 1,
characterToStartNewWord.contains(subString[subString.startIndex]) {
list.append(currentSubString)
if subString == " " { list.append(subString) }
currentSubString = subString.trimmingCharacters(in: .whitespaces)
characterToStartNewWord.forEach {
if subString == String($0),
subString != " " {
list.append(subString)
currentSubString = currentSubString.trimmingCharacters(in: CharacterSet(charactersIn: String($0)))
}
}
} else {
let charSet = Set(" ()-*”—‘/[]—`")
if subString.count == 1, !charSet.contains(subString[subString.startIndex]) {
currentSubString += subString
} else {
characterToStartNewWord.forEach {
if subString == String($0) {
list.append(subString)
}
}
}
}
}
}
}

How to use string variable as Table name in SQL query

Making a winforms application and I want the name of the table in my SQL query to be equal to a variable I have called TreeCodeName. This is my code:
private void button1_Click(object sender, EventArgs e)
{
string TreeNodeName = treeView1.SelectedNode.Name.ToString();
if ((TreeNodeName == "BOOKS") || (TreeNodeName == "CARDS") || (TreeNodeName == "COMPUTERS") || (TreeNodeName == "CONSOLES") || (TreeNodeName == "DEVICES") || (TreeNodeName == "DONORS") || (TreeNodeName == "MAGAZINES") || (TreeNodeName == "MOTHERBOARDS") || (TreeNodeName == "OTHERS") || (TreeNodeName == "PARTS") || (TreeNodeName == "PERIPHERALS") || (TreeNodeName == "POWER_SUPPLY") || (TreeNodeName == "PROCESSORS") || (TreeNodeName == "RAMS") || (TreeNodeName == "SOFTWARE") || (TreeNodeName == "STORAGE") || (TreeNodeName == "TERMINALS"))
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * From Table Where Title = #Title";
command.Parameters.AddWithValue("#Title", TreeNodeName);
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
Whenever I try to test this, I run into an error that says there was a syntax error on FROM. Any way to salvage this?

Why does this C code give me a segmentation fault?

Using the CS50 library, this is my code but it keeps giving me a segmentation fault. I have tried to figure out where it keeps doing a segmentation fault, and for some reason it has to do with my function. Why does it keep doing this?
#import <cs50.h>
#import <stdio.h>
#import <ctype.h>
#import <string.h>
string ciphertext(string plaintext[1], string key[1]);
int main(int argc, string argv[])
{
// Variables
string plaintext2[1];
if (argc == 2)
{
if(strlen(argv[1]) == 26)
{
string text = get_string("plaintext: ");
plaintext2[0] = text;
}
else
{
printf("Key must contain 26 characters.\n");
return 1;
}
}
else
{
printf("Usage: ./substitution key\n");
return 1;
}
string ciphertext2 = ciphertext(&plaintext2[0], &argv[1]);
printf("ciphertext: %s\n", ciphertext2);
}
string ciphertext(string plaintext[1], string key[1])
{
// Variables
string ciphertext[1];
for (int i = 0; i < strlen(plaintext[0]); i++)
{
if (plaintext[0][i] == 'a')
{
ciphertext[0][i] = tolower(key[0][0]);
}
else if (plaintext[0][i] == 'A')
{
ciphertext[0][i] = toupper(key[0][0]);
}
else if (plaintext[0][i] == 'b')
{
ciphertext[0][i] = tolower(key[0][1]);
}
else if (plaintext[0][i] == 'B')
{
ciphertext[0][i] = toupper(key[0][1]);
}
else if (plaintext[0][i] == 'c')
{
ciphertext[0][i] = tolower(key[0][2]);
}
else if (plaintext[0][i] == 'C')
{
ciphertext[0][i] = toupper(key[0][2]);
}
else if (plaintext[0][i] == 'd')
{
ciphertext[0][i] = tolower(key[0][3]);
}
else if (plaintext[0][i] == 'D')
{
ciphertext[0][i] = toupper(key[0][3]);
}
else if (plaintext[0][i] == 'e')
{
ciphertext[0][i] = tolower(key[0][4]);
}
else if (plaintext[0][i] == 'E')
{
ciphertext[0][i] = toupper(key[0][4]);
}
else if (plaintext[0][i] == 'f')
{
ciphertext[0][i] = tolower(key[0][5]);
}
else if (plaintext[0][i] == 'F')
{
ciphertext[0][i] = toupper(key[0][5]);
}
else if (plaintext[0][i] == 'g')
{
ciphertext[0][i] = tolower(key[0][6]);
}
else if (plaintext[0][i] == 'G')
{
ciphertext[0][i] = toupper(key[0][6]);
}
else if (plaintext[0][i] == 'h')
{
ciphertext[0][i] = tolower(key[0][7]);
}
else if (plaintext[0][i] == 'H')
{
ciphertext[0][i] = toupper(key[0][7]);
}
else if (plaintext[0][i] == 'i')
{
ciphertext[0][i] = tolower(key[0][8]);
}
else if (plaintext[0][i] == 'I')
{
ciphertext[0][i] = toupper(key[0][8]);
}
else if (plaintext[0][i] == 'j')
{
ciphertext[0][i] = tolower(key[0][9]);
}
else if (plaintext[0][i] == 'J')
{
ciphertext[0][i] = toupper(key[0][9]);
}
else if (plaintext[0][i] == 'k')
{
ciphertext[0][i] = tolower(key[0][10]);
}
else if (plaintext[0][i] == 'K')
{
ciphertext[0][i] = toupper(key[0][10]);
}
else if (plaintext[0][i] == 'l')
{
ciphertext[0][i] = tolower(key[0][11]);
}
else if (plaintext[0][i] == 'L')
{
ciphertext[0][i] = toupper(key[0][11]);
}
else if (plaintext[0][i] == 'm')
{
ciphertext[0][i] = tolower(key[0][12]);
}
else if (plaintext[0][i] == 'M')
{
ciphertext[0][i] = toupper(key[0][12]);
}
else if (plaintext[0][i] == 'n')
{
ciphertext[0][i] = tolower(key[0][13]);
}
else if (plaintext[0][i] == 'N')
{
ciphertext[0][i] = toupper(key[0][13]);
}
else if (plaintext[0][i] == 'o')
{
ciphertext[0][i] = tolower(key[0][14]);
}
else if (plaintext[0][i] == 'O')
{
ciphertext[0][i] = toupper(key[0][14]);
}
else if (plaintext[0][i] == 'p')
{
ciphertext[0][i] = tolower(key[0][15]);
}
else if (plaintext[0][i] == 'P')
{
ciphertext[0][i] = toupper(key[0][15]);
}
else if (plaintext[0][i] == 'q')
{
ciphertext[0][i] = tolower(key[0][16]);
}
else if (plaintext[0][i] == 'Q')
{
ciphertext[0][i] = toupper(key[0][16]);
}
else if (plaintext[0][i] == 'r')
{
ciphertext[0][i] = tolower(key[0][17]);
}
else if (plaintext[0][i] == 'R')
{
ciphertext[0][i] = toupper(key[0][17]);
}
else if (plaintext[0][i] == 's')
{
ciphertext[0][i] = tolower(key[0][18]);
}
else if (plaintext[0][i] == 'S')
{
ciphertext[0][i] = toupper(key[0][18]);
}
else if (plaintext[0][i] == 't')
{
ciphertext[0][i] = tolower(key[0][19]);
}
else if (plaintext[0][i] == 'T')
{
ciphertext[0][i] = toupper(key[0][19]);
}
else if (plaintext[0][i] == 'u')
{
ciphertext[0][i] = tolower(key[0][20]);
}
else if (plaintext[0][i] == 'U')
{
ciphertext[0][i] = toupper(key[0][20]);
}
else if (plaintext[0][i] == 'v')
{
ciphertext[0][i] = tolower(key[0][21]);
}
else if (plaintext[0][i] == 'V')
{
ciphertext[0][i] = toupper(key[0][21]);
}
else if (plaintext[0][i] == 'w')
{
ciphertext[0][i] = tolower(key[0][22]);
}
else if (plaintext[0][i] == 'W')
{
ciphertext[0][i] = toupper(key[0][22]);
}
else if (plaintext[0][i] == 'x')
{
ciphertext[0][i] = tolower(key[0][23]);
}
else if (plaintext[0][i] == 'X')
{
ciphertext[0][i] = toupper(key[0][23]);
}
else if (plaintext[0][i] == 'y')
{
ciphertext[0][i] = tolower(key[0][24]);
}
else if (plaintext[0][i] == 'Y')
{
ciphertext[0][i] = toupper(key[0][24]);
}
else if (plaintext[0][i] == 'z')
{
ciphertext[0][i] = tolower(key[0][25]);
}
else if (plaintext[0][i] == 'Z')
{
ciphertext[0][i] = toupper(key[0][25]);
}
else
{
ciphertext[0][i] = plaintext[0][i];
}
}
return ciphertext[0];
}
I really need some help, and I don't know why it keeps giving a fault.
The array
string ciphertext[1];
is uninitialized and has indeterminate value.
Accessing such an array the way like this
ciphertext[0][i] = tolower(key[0][0]);
results in undefined behavior.
You need to allocate a character array with the size equal to the value of the expression
strlen( *plaintext ) + 1
Pay attention to that instead the numerous if-else statement you could define a string literal like for example
string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
and then you can use the standard string function strchr like
char *p = strchr( letters, toupper( ( unsigned char)plaintext[0][i] ) );
if ( p != NULL )
{
if ( plaintext[0][i] == *p )
{
ciphertext[0][i] = key[0][p - letters]);
}
else
{
ciphertext[0][i] = tolower(key[0][p - letters]);
}
}
You already have the answer (#Vlad from Moscow), but I would like to point out a few things:
Duplicating is a very bad practice, it is unreadable and more important seriously time-consuming. If you see "100" else if() statements one below the other you should suspect that something is terribly wrong with your code.
Try your best to avoid using numbers in your code. What if you want to change a value that you already copy-pasted a hundred times? Instead of using numbers, I would advise you to use variables and symbolic constants
Try to improve your current code, you can write something like this:
for (int i = 0; i < strlen(plaintext[0]); i++)
{
int ind = 0;
int lower_case = 97, upper_case = 65;
if (plaintext[0][i] == lower_case)
{
ciphertext[0][i] = tolower(key[0][ind]);
}
else if (plaintext[0][i] == upper_case)
{
ciphertext[0][i] = toupper(key[0][ind]);
}
ind++; lower_case++; upper_case++;
}

Trying to poll for a MSP430's Keypad Input

I'm working on a really simplified version of space invaders. I generate some aliens then as time goes on I lower them closer to the bottom of a screen which I'm just using arrays for. I've got it generating and lowering the invaders but when I tried to hit the keypad button to delete an alien it was ignored. I believe its because I'm not constantly looking for the Keypad's input so my checkInput() function isn't doing what it should do. I wanted to poll for it but I am not sure how or where I should do it. Any help would be appreciated.
#include <msp430.h>
#include "peripherals.h"
#include <stdlib.h>
// Function Prototypes
void swDelay(char numLoops);
void countDown();
//void speedCheck();
void generateAliens();
void inputCheck();
void displayAliens();
void initLeds(void);
// Declare globals here
int game;
//int win;
//int turn;
//int levelSpeed;
char arrAliens0[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens1[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens2[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens3[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens4[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens5[5] = {' ', ' ', ' ', ' ', ' '};
char arrTempAliens1[5];
char arrTempAliens2[5];
char arrTempAliens3[5];
char arrTempAliens4[5];
char arrTempAliens5[5];
void main(void)
{
//unsigned char ret_val = 0x0F;
unsigned char currKey=0;
// Define some local variables
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// Useful code starts here
initLeds();
configDisplay();
configKeypad();
while (1) // Forever loop
{
GrClearDisplay(&g_sContext); // Clear the display
while (getKey() != '*')
{
// *** Intro Screen ***
GrStringDrawCentered(&g_sContext, "Space Invaders!", AUTO_STRING_LENGTH, 48, 30, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, "Press '*' to", AUTO_STRING_LENGTH, 48, 50, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, "Start the Game", AUTO_STRING_LENGTH, 48, 65, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
}
currKey = getKey();
if (currKey == '*')
{
game = 1;
//turn = 0;
countDown();
while (game == 1)
{
//speedCheck();
generateAliens();
inputCheck();
//displayAliens();
//turn++;
}
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, "You Lose!", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
}
} // end while (1)
} //end main
void countDown()
{
//3 Count
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, "3", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
//2 Count
GrStringDrawCentered(&g_sContext, "2", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
//1 Count
GrStringDrawCentered(&g_sContext, "1", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
}
/*void speedCheck()
{
if (turn <= 3)
{
levelSpeed = 10;
}
else if ((turn > 3) && (turn <= 6))
{
levelSpeed = 8;
}
else if ((turn > 6) && (turn <= 9))
{
levelSpeed = 4;
}
else if ((turn > 9) && (turn <= 12))
{
levelSpeed = 2;
}
else
{
levelSpeed = 1;
}
}*/
void generateAliens()
{
memcpy(arrTempAliens1, arrAliens1, 5);
memcpy(arrTempAliens2, arrAliens2, 5);
memcpy(arrTempAliens3, arrAliens3, 5);
memcpy(arrTempAliens4, arrAliens4, 5);
memcpy(arrTempAliens5, arrAliens5, 5);
memcpy(arrAliens1, arrAliens0, 5);
memcpy(arrAliens2, arrTempAliens1, 5);
memcpy(arrAliens3, arrTempAliens2, 5);
memcpy(arrAliens4, arrTempAliens3, 5);
memcpy(arrAliens5, arrTempAliens4, 5);
/*arrTempAliens1 = arrAliens1;
arrTempAliens2 = arrAliens2;
arrTempAliens3 = arrAliens3;
arrTempAliens4 = arrAliens4;
arrTempAliens5 = arrAliens5;
arrAliens1 = arrAliens0;
arrAliens2 = arrTempAliens1;
arrAliens3 = arrTempAliens2;
arrAliens4 = arrTempAliens3;
arrAliens5 = arrTempAliens4;*/
int a = rand() % 4;
if (a == 0)
{
arrAliens0[0] = '0';
arrAliens0[1] = ' ';
arrAliens0[2] = ' ';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 1)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = ' ';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 2)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 3)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = '3';
arrAliens0[4] = ' ';
}
else if (a == 4)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = '3';
arrAliens0[4] = '4';
}
displayAliens();
}
void inputCheck()
{
unsigned char currKey = getKey();
if ((arrAliens5[0] && arrAliens5[1] && arrAliens5[2] && arrAliens5[3] && arrAliens5[4]) == ' ')
{
if (currKey == '0')
{
if ((currKey == '0') && (arrAliens4[0] == '0'))
{
arrAliens4[0] = ' ';
setLeds(0x30);
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] == '0'))
{
arrAliens3[0]= ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] == '0'))
{
arrAliens2[0] = ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] != '0') && (arrAliens1[0] == '0'))
{
arrAliens1[0] = ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] != '0') && (arrAliens1[0] != '0') && (arrAliens0[0] == '0'))
{
arrAliens0[0] = ' ';
}
}
if (currKey == '1')
{
if ((currKey == '1') && (arrAliens4[1] == '1'))
{
arrAliens4[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] == '1'))
{
arrAliens3[1]= ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] == '1'))
{
arrAliens2[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] != '1') && (arrAliens1[1] == '1'))
{
arrAliens1[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] != '1') && (arrAliens1[1] != '1') && (arrAliens0[1] == '1'))
{
arrAliens0[1] = ' ';
}
}
if (currKey == '2')
{
if ((currKey == '2') && (arrAliens4[2] == '2'))
{
arrAliens4[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] == '2'))
{
arrAliens3[2]= ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] == '2'))
{
arrAliens2[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] != '2') && (arrAliens1[2] == '2'))
{
arrAliens1[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] != '2') && (arrAliens1[2] != '2') && (arrAliens0[2] == '2'))
{
arrAliens0[2] = ' ';
}
}
if (currKey == '3')
{
if ((currKey == '3') && (arrAliens4[3] == '3'))
{
arrAliens4[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] == '3'))
{
arrAliens3[3]= ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] == '3'))
{
arrAliens2[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] != '3') && (arrAliens1[3] == '3'))
{
arrAliens1[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] != '3') && (arrAliens1[3] != '3') && (arrAliens0[3] == '3'))
{
arrAliens0[3] = ' ';
}
}
if (currKey == '4')
{
if ((currKey == '4') && (arrAliens4[4] == '4'))
{
arrAliens4[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] == '4'))
{
arrAliens3[4]= ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] == '4'))
{
arrAliens2[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] != '4') && (arrAliens1[4] == '4'))
{
arrAliens1[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] != '4') && (arrAliens1[4] != '4') && (arrAliens0[4] == '4'))
{
arrAliens0[4] = ' ';
}
}
}
else if ((arrAliens5[0] | arrAliens5[1] | arrAliens5[2] | arrAliens5[3] | arrAliens5[4]) != ' ')
{
game = 0;
}
}
void displayAliens()
{
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, arrAliens0, 5, 48, 5, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens1, 5, 48, 25, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens2, 5, 48, 45, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens3, 5, 48, 65, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens4, 5, 48, 85, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
}
void swDelay(char numLoops)
{
volatile unsigned int i,j; // volatile to prevent optimization
// by compiler
for (j=0; j<numLoops; j++)
{
i = 50000 ; // SW Delay
while (i > 0) // could also have used while (i)
i--;
}
}
if ((arrAliens5[0] && arrAliens5[1] && arrAliens5[2] && arrAliens5[3] && arrAliens5[4]) == ' ')
This condition is never true because && is a boolean operator and results in either 0 or 1, which can never be equal to the value of the space character.

Interpretate directives for dynamic Html added from a $http response

Directive defined to add numeric input
app.directive('numericCheck', function () {
return {
link: function (scope, element, attr) {
element.on('keypress', function (event) {
event = (event) ? event : window.event;
if(/Firefox/.test(window.navigator.userAgent) && event.keyCode!=0) {
return true;
}
var charCode = (event.which) ? event.which : event.keyCode;
if (charCode > 31 && charCode != 46 && charCode != 47 && charCode != 45 && (charCode < 48 || charCode > 57) || (charCode==45 && (event.target.selectionStart!=0 || event.target.value.indexOf('-')>=0)) || (charCode==46 && event.target.value.indexOf('.')>=0) || (charCode==47 && (event.target.selectionStart==0 || event.target.value.indexOf('/')>=0))) {
return false;
}
return true;
});
}
};
});
The below string is the response added to a div.
{"question":"Two identical charts have different parts shaded. Which one is greater - 0.02 or 0.04?<br><br><br><br><input type='text' id='b1' name='blank_1' class='num_blank' size='5' numeric-check> is the greater number."}
After adding the response to the HTML numeric-check, directive does not work and it allows us to enter all characters instead of only numeric values.
Have you tried to put something like:
app.directive('numericCheck', function () {
return {
link: function (scope, element, attr) {
element.on('keypress', function (event) {
scope.$apply(function(){
event = (event) ? event : window.event;
if(/Firefox/.test(window.navigator.userAgent) && event.keyCode!=0) {
return true;
}
var charCode = (event.which) ? event.which : event.keyCode;
if (charCode > 31 && charCode != 46 && charCode != 47 && charCode != 45 && (charCode < 48 || charCode > 57) || (charCode==45 && (event.target.selectionStart!=0 || event.target.value.indexOf('-')>=0)) || (charCode==46 && event.target.value.indexOf('.')>=0) || (charCode==47 && (event.target.selectionStart==0 || event.target.value.indexOf('/')>=0))) {
return false;
}
return true;
});
});
}
};

Resources