我该如何改进这种逻辑? C#

时间:2016-01-21 21:32:20

标签: c# logic

我有一个库,可以从游戏中实时返回CS:GO统计数据。我正在创建一个程序来存储统计数据并对其进行分析。

我有这个功能:

    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对象中的那些杀戮。
  3. 我的代码在逻辑上是否正确?我的代码可以更“正确”吗?

2 个答案:

答案 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();
        }
    }

答案 1 :(得分:0)

好吧,经过一些研究,最终解决了你的问题。你不必做任何事情!,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;
    }

干杯。