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

C# 如何改进这种逻辑?C,c#,logic,C#,Logic,我有一个从游戏中实时返回CS:GO统计数据的库。我正在制作一个程序来存储统计数据并进行分析 我有这个功能: private void UpdateKills(GameState gs) { int currentKills = -1; if (lastKills == -1) // first time getting player info { int temp = gs.Player.MatchStat

我有一个从游戏中实时返回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

我的逻辑是:

让玩家杀人 增加PlayerStats对象中的击杀次数。
我的代码逻辑正确吗?我的代码是否更正确?

我仍然不能完全确定这个函数应该做什么,但我为您简化了它:

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

好吧,在做了一些研究之后,我终于明白了你的问题。你什么都不用做!,API为您完成所有工作,请观看下一张图片:

正如你所看到的,控制台应用程序显示了我的蒸汽名称、地图和死亡。我从零杀开始,然后我杀了一个队友,然后是一个敌人。@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;
    }

干杯。

你从哪里得到最后的技能?还有,ps.技能是从哪里来的?这些是静态变量吗?你有变量lastKills和ps,谁知道还有什么地方可以修改。如果dif!=0&&dif>0//玩家杀了人,而dif不是在teamkill中发生的!=0部分是毫无意义的,因为dif>0也必须与第一部分匹配,并且完全不清楚这与玩家杀人有什么关系,而不是团队杀人。因此,我认为这种反常是由于你的逻辑。你的代码中有奇怪的东西。您正在设置dif=0;但是dif是else块范围的局部,所以不管怎样离开else时它将不再存在。在dif中!=0&&dif>0如果dif大于0,则为!=同时为0,因此如果dif>0,就可以写入。定义异常!你期望它做什么?当它崩溃时它真正做什么?该函数应该更新变量ps。每次在cs:go中发生kill时都会kill。我不认为你理解程序的概念:。所以,我知道图书馆给了我当前的分数。但是,我的程序可以连续存储数据,而不仅仅是特定比赛的分数。我正在比赛,打20-15分。下一张地图的分数重置为0-0。如果我现在杀了人,比分将是1比0,但我不想这样。我希望这场比赛的分数比之前的分数增加26-15,以此类推。希望你能理解我的文字,谢谢你的帮助D你应该编辑问题以澄清你需要什么。我将更新代码以添加您需要的内容。Cheers@bruxo00,gameState.Player.MatchStats.Kills从0或-1开始?该变量保存当前的Kills。不支持启动0或-1。这取决于你目前的得分。所以,游戏开始时是零。另外,如果我们更改地图,gameState.Player.MatchStats.Kills设置为-1?