如何在多人游戏中实现计时器? ASP .NET核心 [英] How to implement timer in a multiplayerchess game? Asp .NET Core

查看:235
本文介绍了如何在多人游戏中实现计时器? ASP .NET核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前拥有的:单例类GameManager,主要由一个ConcurrentDictionary组成,其中包含所有正在进行的游戏(将游戏板和两个玩家的昵称绑定到游戏的ID).我还有一个SignalR集线器GameHub,其中的方法MakeAMove(ulong gameId, Move move)从字典中的相关字典lock检索相关的gameId游戏,并检查调用该方法的玩家是否是现在的玩家应该采取行动,如果是,则对游戏板进行适当的更改,将此举动广播给该游戏的所有观察者,并在必要时结束该游戏,宣布该游戏是X玩家的胜利或平局,并将其从X中移除字典.

This is what I currently have: A singleton class GameManager which mostly consists of a ConcurrentDictionary that contains all ongoing games (binds the game board and nicks of both players to the game's ID). I also have a SignalR hub, GameHub with a method MakeAMove(ulong gameId, Move move) that retrieves the relevant gameId game from the dictionary, locks on it, checks if the player who has invoked the method is the player who is now supposed to make a move and if yes, makes appropriate changes to the game's board, broadcasts this move to all observers of this game and if necessary, concludes the game, declaring it to be a victory of player X or a draw and removing it from the dictionary.

我没有的是计时器.我认为计时器是必要的,因为没有计时器,许多游戏可能永远无法结束,并且永远徘徊在这个命令中.计时器的工作方式应为:如果玩家未能在分配的时间内采取任何行动,游戏将自动结束,并宣布该玩家为失败者.

What I don't have is the timer. I think the timer is necessary, because without it many games may never conclude and forever linger in this dict. The timer should work in such a way that if a player fails to make a move within the allocated time, the game is automatically concluded and such a player is declared to be the loser.

我不知道应该如何实现这样的计时器.我有一些想法,但是我不确定哪一个是正确的(如果有的话).

I don't know how should such a timer be implemented. I have a few ideas, but I'm not sure which one is correct, if any.

我必须解决的唯一想法对我来说似乎并不可怕:ConcurrentDictionarygameId绑定到Game类的实例.然后,每个这样的实例都具有await的方法,直到应该进行移动的玩家的计时器用尽,或者直到GameHub通知该方法已经做出移动命令为止,以先发生的为准.但是,我不知道该如何实现.

The only idea I have to solve this task that doesn't seem to be horrible to me would look like this: ConcurrentDictionary binds gameIds to instances of Game class. Then each such instance has a method that awaits until the timer for the player who is supposed to make a move runs out or until GameHub notifies this method that a move order has been made, whichever happens first. However, I am ignorant how to implement this.

这是正确的方法吗?如果是,那么我应该如何让这种方法等待到计时器用完或GameHub发出移动指令?

Is this the right way to go? If yes, then how am I supposed to make this method wait until the timer runs out or until a move order comes to GameHub?

我建议我应该写这样的东西:

I SUPPOSE I should write something like this:

await Task.WhenAny(
    Task.Delay(players_available_time_left),  
    ??something_to_mean_a_move_order_has_arrived??
);

  • 这是正确的吗?如果是,我该怎么写而不是问号?
  • 推荐答案

    您可以简单地使用一个计时器,该计时器每转一圈启动,并在移动完成后停止:

    You can simply use a timer that you start every turn and stop after move is done :

    private System.Timers.Timer Timer = null;
    private double TimerDelay = 30d * 1000d; // 30 seconds
    private void StartTimer()
    {
        Timer = new System.Timers.Timer() { Interval = TimerDelay , AutoReset = false};
        Timer.Elapsed += Timer_Elapsed;
        Timer.Start();
    } 
    
    private void StopTimer()
    {
        Timer.Stop();
    }     
    
    private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        // stop the timer
        StopTimer();
    
        // call the method to make the player lose and end the game
        EndGame();                
    }  
    

    启动播放器时,转动StartTimer()放开它.当玩家结束时,依次调用StopTimer(),它将停止计时器终止游戏的结束.如果计时器用完,它将自动调用您的游戏结束.

    When you start the player turn call the StartTimer() and let it go. When the player ends it's turn call the StopTimer() and it will stop the timer from calling the end of the game. If the timer runs out it will automatically call your end of game.

    这篇关于如何在多人游戏中实现计时器? ASP .NET核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆