2016-01-21 17 views
-2

我有一個從遊戲中實時返回CS:GO統計數據的庫。我正在製作一個存儲統計數據並對其進行分析的程序。我該如何改進這一邏輯? C#

我有這樣的功能:

private void UpdateKills(GameState gs) 
    { 
     int currentKills = -1; 

     if (lastKills == -1) // first time getting player info 
     { 
      int temp = gs.Player.MatchStats.Kills; 

      currentKills = temp; 
      lastKills = temp; 
     } 
     else 
     { 
      currentKills = gs.Player.MatchStats.Kills; 

      int dif = currentKills - lastKills; 

      if (currentKills == 0 && lastKills != 0) // maybe changed server/map/whatever 
      { 
       lastKills = -1; 
      } 
      else 
      { 
       if (dif != 0 && dif > 0) // player killed someone AND it was not teamkill 
       { 
        ps.Kills += dif; // add the kills to the main variable 
        lastKills = currentKills; 
        dif = 0; 

        playSimpleSound(); 
       } 
      } 
     } 
    } 

這是我的函數處理殺死。大多數情況下,它工作得很好,但有時它只是嚇壞了,我不知道問題是我的邏輯還是圖書館問題。

注:我使用這個庫:github.com/rakijah/CSGSI

我的邏輯是:

  1. 獲取玩家殺死
  2. 增量在PlayerStats那些殺害對象。

我的代碼在邏輯上是正確的嗎?我的代碼可以更「正確」嗎?

+0

你從哪裏得到最後的殺敵? – Eulogy

+0

ps.Kills也來自哪裏?這些靜態變量是什麼? –

+0

你有變量'lastKills'和'ps',這些變量可能被修改,誰知道還有什麼地方。你有奇怪的表達式,如果(dif!= 0 && dif> 0)//玩家殺死了某人並且它不是teamkill',其中'dif!= 0'部分是毫無意義的,因爲'dif> 0'也必須匹配第一部分,完全不清楚這是如何與「玩家殺了某人,而不是團隊技能」有關的。因此,我建議「怪胎」是由於你的邏輯。 –

回答

0

我仍然不能完全肯定這是什麼功能是應該做的,但我簡化它爲您:

private void UpdateKills(GameState gs) 
    { 
     lastKills = currentKills; 
     int currentKills = gs.Player.MatchStats.Kills; 
     int diff = currentKills - lastKills; 

     if (currentKills == 0 && lastKills != 0) // maybe changed server/map/whatever 
     { 
      lastKills = -1; 
     } 
     else if (diff > 0) // player killed someone AND it was not teamkill 
     { 
      ps.Kills += diff; // add the kills to the main variable 
      playSimpleSound(); 
     } 
    } 
+0

該函數應該更新變量'ps.Kills'每當一次殺死發生在CS:去。 – bruxo00

0

好了,經過一番研究,終於undertand您的問題。你不必做任何事情!在API爲您完成所有的工作,看下一圖像:

Console Application

正如你所看到的,控制檯應用程序顯示了我的名字蒸汽,地圖和殺死。我從零殺人開始,然後殺死一個隊友,然後殺死一個敵人。 @rakijah創建的API會自動調整這些值。

現在代碼:

static void Main(string[] args) 
    { 
     CsGoIntegration(); 
    } 

    private static void CsGoIntegration() 
    { 
     var gsl = new GameStateListener(3000); 
     gsl.NewGameState += new NewGameStateHandler(OnNewGameState); 
     if (!gsl.Start()) 
     { 
      Environment.Exit(0); 
     } 
     System.Console.WriteLine("Listening..."); 
    } 

    private static void OnNewGameState(GameState gs) 
    { 
     System.Console.WriteLine("Map: {0}", gs.Map.Name); 
     System.Console.WriteLine("Player Name: {0}", gs.Player.Name); 
     System.Console.WriteLine("Player Kills: {0}", gs.Player.MatchStats.Kills); 
    } 

進展:OP需要存儲的總殺死地圖上的變化也是如此。我用紙和鉛筆進行了實驗,請嘗試運行程序並告訴我它是否有效

private static void Main() 
    { 
     CsGoIntegration(); 
    } 

    private static void CsGoIntegration() 
    { 
     var gsl = new GameStateListener(3000); 
     gsl.NewGameState += OnNewGameState; 
     if (!gsl.Start()) 
     { 
      Environment.Exit(0); 
     } 
     Console.WriteLine("Listening..."); 
    } 

    private static void OnNewGameState(GameState gameState) 
    { 
     SaveMatchsData(gameState); 
    } 

    private static int? _totalKillScore; 
    private static string _lastMapName; 
    private static int? _lastKillScore; 

    private static void SaveMatchsData(GameState gameState) 
    { 
     const string undefinedString = "Undefined"; 

     // If the SaveMatchsData is running and the CSGO server is offline 
     if (gameState.Map.Name == undefinedString && string.IsNullOrEmpty(_lastMapName)) 
      return; 

     // When the match is not started, the Round is -1 
     if (gameState.Map.Name != undefinedString && gameState.Map.Round > -1) 
     { 
      if (string.IsNullOrEmpty(_lastMapName)) 
      { 
       UpdateData(gameState, true); 
      } 
      else 
      { 
       // Same map 
       if (_lastMapName == gameState.Map.Name) 
       { 
        // Check if the Score Changes 
        if (_lastKillScore == gameState.Player.MatchStats.Kills) return; 
        UpdateData(gameState); 
       } 
       // The Map Changes 
       else 
       { 
        UpdateData(gameState, true); 
       } 
      } 

     } 
    } 

    private static void UpdateData(GameState gameState, bool updateMap = false) 
    { 
     if (updateMap) 
      _lastMapName = gameState.Map.Name; 
     _lastKillScore = gameState.Player.MatchStats.Kills; 
     _totalKillScore += gameState.Player.MatchStats.Kills; 
    } 

乾杯。

+0

我不認爲你得到的程序的概念:(所以我知道圖書館爲我提供了CURRENT分數,但是,我的計劃是用來持續存儲統計數據,而不僅僅是一場特定比賽的得分例如:我打一場比賽,我做20-15。 (0-0)如果我現在殺了一個人,得分會是1-0,但我不想要那個,我希望這個遊戲的得分能夠增加以前的得分(26-15)等等等等,希望你能理解我的文字,謝謝你的幫助!:D – bruxo00

+0

你應該編輯這個問題來說明你需要什麼,我會更新代碼來添加你需要的東西 乾杯 – Eulogy

+0

@ bruxo00,gameState.Player.MatchStats.Kills從0或-1開始? – Eulogy