2017-08-16 49 views
0

我有一個問題遊戲,我用帆,水線和插座建造。我正在將數據庫從本地傳輸到MySQL。我無法弄清楚的是,如何使用數據庫中遞增的整數來追蹤玩家的勝利。我如何追蹤玩家的勝利?

到目前爲止,我的用戶模式產生這樣的:

[ 
    { 
    "name": "ether", 
    "email": "[email protected]", 
    "password": "sha1$b39f3d56$1$ebd354e8af83b21be4de4ffee90b10ed746553de", 
    "status": "online", 
    "score": 0, 
    "totalwins": 0, 
    "createdAt": "2017-08-16T17:22:04.879Z", 
    "updatedAt": "2017-08-16T17:22:05.153Z", 
    "id": 31, 
    "ip": "::1" 
    } 
] 

當用戶播放一段日子的過程中,我要跟蹤的時候,他們已經被宣佈爲獲勝者的數量。我檢查的勝者在我quiz.js文件位置:

if (highScore > 0) { 
     if (isDraw) { 
      $('#winner-message').html('We have a draw. Well done!'); 
      $('#winner-image').addClass('no-one-wins'); 
     } 
     else { 
      $('#winner-message').html('And the winner is... <strong>' + players[scoreIndex] + '</strong>!'); 
      $('#winner-image').css('background-image', 'url("' + $('#status-table tbody tr').eq(scoreIndex).find('.name').attr('data-avatar') + '")'); 
     } 
     } 
     else { 
     $('#winner-message').html('And the winner is... <strong>no one</strong> :-('); 
     $('#winner-image').addClass('no-one-wins'); 
     } 
    } 

我如何才能讓它,所以我可以更新場+1時,有一個贏家?我覺得我必須像這樣在我的模型中引用它,但我不確定。

winner: function (inputs, cb){ 

    }, 

感謝任何幫助和洞察!

回答

0

假設你已經有玩家在一個變量playerId的ID,你可以寫一些代碼是這樣的:

Players.find(id: playerId).exec(function(error,player) { if (error) (handle error) player.totalwins = player.totalwins+1; delete player.id Players.update(playerId,player) })

+0

我會設定我的用戶模型中的變量,然後運行該代碼? –

+0

此代碼正在更新玩家記錄。如果你願意,你可以更新用戶記錄 – Mikkel