2016-04-28 1 views
0

J'utilise le code suivant:Navigation dans les objets SQL en JavaScript

//Handles every game and their attributes 
function handleGames(){ 
    sql.query('SELECT id FROM games', function (err, rows){ 
     if(err){ 
      console.log(String(err).error.bgWhite); 
     } else { 
      for(var i = 0; i < rows.length; i++) { 
      /** ["id"] 
       ["starttime"] 
       ["status"] 
       ["ct_name"] 
       ["ct_avatar"] 
       ["ct_steamid"] 
       ["ct_bet"] 
       ["t_name"] 
       ["t_avatar"] 
       ["t_steamid"] 
       ["t_bet"]   **/ 

       var timeNow = new Date().getTime(); 
       //console.log(String(timeNow).info.bgWhite); 

       //Game Expiration 
       //0 = Open | 4 = Expired | 5 = Generic Error 

       //Generic error --> Expired 
       if(rows[i]["status"] == 5){ 
        sql.query('UPDATE games SET status = 4 WHERE id = "' + rows[i]["id"] + '"', function (err, rows){ 
         if(err) console.log(String(err).error.bgWhite); 
         console.log(("Updating status for game " + rows[i]["id"] + " to 4").info.bgWhite); 
        }); 
       } 

       //Gamestatus set to expired if game has lasted 5m (300s) 
       if(((rows[i]["starttime"] + 300) >= timeNow) && (rows[i]["status"] == 0)){ 
        sql.query('UPDATE games SET status = 4 WHERE id = "' + rows[i]["id"] + '"', function (err, rows){ 
         if(err) console.log(String(err).error.bgWhite); 
        }); 
       } 
      }; 
     } 
    }); 
} 

Avec les données suivantes:

enter image description here

Le programme est écrit pour le nœud, et doit changer le statut de n'importe quel jeu qui a duré 5 minutes (300s), cependant j'ai du mal à naviguer avec des clés. L'état du jeu n'est pas mis à jour.

JSON Version


SQL Export:

-- phpMyAdmin SQL Dump 
-- version 4.0.10.7 
-- http://www.phpmyadmin.net 
-- 
-- Host: localhost:3306 
-- Generation Time: Apr 28, 2016 at 01:25 PM 
-- Server version: 5.6.26-cll-lve 
-- PHP Version: 5.4.31 

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 
SET time_zone = "+00:00"; 


/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@COLLATION_CONNECTION */; 
/*!40101 SET NAMES utf8 */; 

-- 
-- Database: `csgovoid_data` 
-- 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `games` 
-- 

CREATE TABLE IF NOT EXISTS `games` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT, 
    `starttime` int(11) NOT NULL, 
    `status` int(1) NOT NULL COMMENT '0 = Open, 1 = In progress, 2 = CT Win, 3 = T Win, 4 = Expired, 5 = Generic error', 
    `ct_name` varchar(255) NOT NULL, 
    `ct_avatar` longtext NOT NULL, 
    `ct_steamid` bigint(20) NOT NULL, 
    `ct_bet` float NOT NULL, 
    `t_name` varchar(255) NOT NULL, 
    `t_avatar` longtext NOT NULL, 
    `t_steamid` bigint(20) NOT NULL, 
    `t_bet` float NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; 

-- 
-- Dumping data for table `games` 
-- 

INSERT INTO `games` (`id`, `starttime`, `status`, `ct_name`, `ct_avatar`, `ct_steamid`, `ct_bet`, `t_name`, `t_avatar`, `t_steamid`, `t_bet`) VALUES 
(3, 1461862619, 0, 'Robinlemon > CSGOVoid.net', 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c5/c5ef952e8bd4ff79afcecb9971ec831b21707be7_full.jpg', 76561198065346589, 3, 'Yuuta', 'http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/73/734a259213585f56c15631a56b127018710d2e34_full.jpg', 76561198052096214, 3); 

/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 
+0

obtenez-vous une erreur sur le terminal ??? Poster ici le cas échéant! – vkstack

+0

Pas d'erreur, il suffit de ne pas mettre à jour le statut. Suis-je en utilisant la clé correctement – Robinlemon

+0

obtenez-vous 'Mise à jour de statut pour le jeu ....' sur le terminal ??/ – vkstack

Répondre

1

donc ((new Date()).getTime()-starttime)/1000 donnera u durée en secondes pouvez-vous vous envoyer le code dans un éditeur javascript en ligne comme plunker! Vous êtes ici?

INSERT INTO `games` (`id`, `starttime`, `status`, `ct_name`, `ct_avatar`, `ct_steamid`, `ct_bet`, `t_name`, `t_avatar`, `t_steamid`, `t_bet`) VALUES 
(3, 1461862619, 0, 'Robinlemon > CSGOVoid.net', 'steamcdn-a.akamaihd.net/…', 76561198065346589, 3, 'Yuuta', 'cdn.akamai.steamstatic.com/…', 76561198052096214, 3); 

Ceci est dans votre sauvegarde sql! qui insère une entité avec id (avec AUTO_INCREMENT) 3 !! donc pour les prochaines insertions AUTO_INCREMENT sera 4 !!!

si vous voulez changer de supprimer la ligne de la table et la remise à zéro AUTO_INCREMENT compteur

ALTER TABLE tablename AUTO_INCREMENT = 1