2013-05-05 56 views
1

我有一张表,可以获得高分条目。但是,如果用户已在表中有一个条目(通过GUID字段进行跟踪,而不是用户参数),我想更新它,如果新条目有更好的时间,否则不要更改现有记录。但是,如果用户在高分表中没有记录,则添加新记录。我也有两个查询参数传递给查询。在HighScore表中插入或更新记录

我想插入操作来处理这个表。我有这到目前为止,但我得到了异常,当我在高分表调用InsertAsync(...)

function insert(item, user, request) { 
    var sql ="select Id from HighScore where PlayerGUID=? AND PlayerBadge=?"; 
    mssql.query(sql, [user.PlayerGUID], [user.PlayerBadge], { 
     success: function(results) { 
      if(results.length > 0) { 
       // leader board record exists so update the current record 
       // Check the existing record and update it is the new time is better 
       console.log("Found existing entry"); 
      } else { 
       // no record exists for this user to insert one 
       request.execute(); 
       console.log("Found existing entry"); 
      } 
     } 
    }); 
} 

任何人都可以提供我实现我的目标的任何援助?

非常感谢,

J.

回答

1

花了一些时间和一些帮助,但这里就是我结束了。它的工作方式与我打算的一样。


function insert(item, user, request) { 
    // Store the passed in item object for us when inserting or updating 
    resultsItem = item; 
    // Store the request object to allow calld functions to send respond commands 
    thisRequest = request; 
    // Retrieve the HighScore table so we can check it for an existing record 
    hsTable = tables.getTable('HighScore'); 
    // Update the leaderboard 
    updateLeaderboard(item); 
} 

// Global variables 
var resultsItem, hsTable, thisRequest; 

function updateLeaderboard(item){ 
    //Filter the table using the where operator to only include those 
    // records for the current PlayerGUID and PlayerBadge fields 
    hsTable.where({ 
     PlayerGUID: item.PlayerGUID, 
     PlayerBadge: item.PlayerBadge 
     }).read({ 
     success:updateScore, 
     error: errorHandler 
     }) 
} 

function updateScore(results){ 
    if(results.length > 0) { 
    // If a record already exists then check the PlayerTime 
     if(results[0].PlayerTime > resultsItem.PlayerTime) 
     { 
      // Update the PlayerTime if it is less than the currently saved value 
      hsTable.update({ 
       id: results[0].id, 
       PlayerTime: resultsItem.PlayerTime 
      }, { 
       success: logSuccess, 
       error: errorHandler 
      }) 
     } else { 
      // Send them OK. Could change this and use the returned code/text to display a custom 
      // message that tells the user that a previous time is faster. 
      thisRequest.respond(statusCodes.OK); 
     } 

    } else { 
     // The record for this PlayerGUID and PlayerBadge exists so write one 
     hsTable.insert({ 
      PlayerName: resultsItem.PlayerName, 
      PlayerCountry: resultsItem.PlayerCountry, 
      PlayerTime: resultsItem.PlayerTime, 
      PlayerBadge: resultsItem.PlayerBadge, 
      PlayerGender: resultsItem.PlayerGender, 
      PlayerDOB: resultsItem.PlayerDOB, 
      PlayerGUID: resultsItem.PlayerGUID 
     }, { 
      success: logSuccess, 
      error: errorHandler 
     }) 
    } 
} 

// Called if there is an error 
function errorHandler(error){ 
    console.error 
    ("An error occurred trying to update leaderboard infor for player" + 
    resultsItem.PlayerName); 
    thisRequest.respond(statusCodes.BAD_REQUEST); 
} 

//Called if things work out ok. 
function logSuccess() 
{ 
    thisRequest.respond(statusCodes.OK); 
}