1

我有一个接收高分条目的表。但是,如果用户已经在表中有一个条目(通过 GUID 字段而不是用户参数跟踪),如果新条目有更好的时间,我想更新它,否则不要更改现有记录。但是,如果用户在高分表中没有记录,则添加新记录。我还有两个查询参数要传递给查询。

我希望插入操作为表处理这个问题。到目前为止我有这个但是当我在高分表上调用 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。

4

1 回答 1

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);
}

于 2013-05-06T23:13:29.747 回答