/// <summary>
        /// <see cref="Game"/>对象创建成功后,进行初始化或修改界面的操作。
        /// </summary>
        /// <param name="creation">游戏创建操作的结果。</param>
        private async void OnGameCreationSucceeded(CreateGameResult creation)
        {
            // 如果有在进行游戏,需要关闭它。以防止其继续推送游戏事件或造成对象泄漏。
            _game?.Close();
            _game = creation.Game;

            // Game 类的事件不一定在 UI 线程触发,我们需要包装所有涉及 UI 的操作,都在UI线程进行。
            // PostAsync 会将操作转移到 UI 线程执行。
            _game.CountdownTicked += (g, cd) => PostAsync(() => UpdateCountdown(cd.Seconds));
            _game.NewRoundEntered += (g, a) => PostAsync(() => OnNewRound(g, a));
            _game.ErrorOccurred   += (g, s) => PostAsync(() => WriteLog(s + " Please restart game."));

            // 将界面调整到运行状态。
            MainGrid.IsEnabled = true;
            Blocker.Visibility = Visibility.Collapsed;
            WriteLog($"=== Entered room {creation.Game.RoomId} ({creation.Game.NumberMode}-number) ===");
            WriteLog($"Current user ID: {_game.UserId}");
            WriteLog($"Current round: {creation.InitialRound.RoundId}");
            UpdateCountdown(creation.InitialCountdown.Seconds);

            bool twoNumbers = creation.Game.NumberMode == RoomNumberMode.Two;

            Number2InputTextBox.Visibility = twoNumbers ? Visibility.Visible : Visibility.Collapsed;
            NumberInputTextBox.Width       = twoNumbers ? 40 : 100;

            var nickname = creation.Game.Nickname;

            if (nickname != null)
            {
                NicknameTextBox.Text = nickname;
            }

            await ShowHistoryAsync();
        }
Beispiel #2
0
        public readonly uint Unknown;                   // If game creation succeeded, this is a nonzero value whose meaning is unknown.

        public CreateGameResponse(byte[] data)
            : base(data)
        {
            this.RequestID = BitConverter.ToUInt16(data, 1);
            this.Unknown   = BitConverter.ToUInt32(data, 3);
            this.Result    = (CreateGameResult)BitConverter.ToUInt32(data, 7);
        }
Beispiel #3
0
        private void Realm_OnCreateGameResponse(CreateGameResponse Packet)
        {
            CreateGameResult result = Packet.Result;

            if (result == CreateGameResult.Sucess)
            {
                this.Realm.WriteToLog("Game Created", Color.Green);
            }
            else
            {
                this.Realm.WriteToLog("Game Creation Failed, Reason: " + Packet.Result, Color.Red);
                this.FailToCreateGameEvent?.Invoke();
            }
        }
Beispiel #4
0
        public async void CreateRoom(BaseClient <ClientData> client, byte[] datas)
        {
            CreateGameRequest model_create_room;

            NetHelp.RecvData <CreateGameRequest>(datas, out model_create_room);
            Log.Debug("用户名:{0},创建房间", model_create_room.UserName);


            var player  = GrainClient.GrainFactory.GetGrain <IPlayerGrain>(client.t.playerId);
            var game_id = await player.CreateGame();

            CreateGameResult result = new CreateGameResult();

            result.RoomId = game_id.ToString();
            client.Send <CreateGameResult>(Op.Client.CreateRoom, result);
        }
Beispiel #5
0
        public IHttpActionResult Post([FromBody] CreateGameParams Param)
        {
            CreateGameResult Result = new CreateGameResult();

            if (Param == null)
            {
                return(new RawJsonActionResult(Newtonsoft.Json.JsonConvert.SerializeObject(Result)));
            }
            if (Param.ServiceKey != BaseObjects.SERVICE_PASS)
            {
                return(new RawJsonActionResult(Newtonsoft.Json.JsonConvert.SerializeObject(Result)));
            }

            Result = Game.CreateGame(Param.UserID, Param.QuestionGroupID, Param.PlayerCount);

            return(new RawJsonActionResult(Newtonsoft.Json.JsonConvert.SerializeObject(Result)));
        }
Beispiel #6
0
        /// <summary>
        /// <see cref="Game"/>对象创建成功后,进行初始化或修改界面的操作。
        /// </summary>
        /// <param name="creation">游戏创建操作的结果。</param>
        private async void OnGameCreationSucceeded(CreateGameResult creation)
        {
            // 如果有在进行游戏,需要关闭它。以防止其继续推送游戏事件或造成对象泄漏。
            _game?.Close();
            _game = creation.Game;

            //TODO 对这里的一些细节操作,我们可以再加抽象。需要注意的是变量 g,这个 g 和 _game 字段是不可混用的。
            //TODO 因为 g 真正表示了在过去时间点上,发送了事件的 Game 对象。而通过 _game,我们总是取得当前正在进行的 Game 对象。
            //TODO 我们在这里显式地、重复地使用 g,就很容易不小心混用了。
            //TODO 我们可以把 `_game.SomeEvent += aHandler` 操作抽象成 Action,并统一地管理变量 g 的传递,这样我们就能防患于未然。
            //
            // Game 类的事件不一定在 UI 线程触发,我们需要包装所有涉及 UI 的操作,使其在UI线程进行。
            // 否则对UI控件的操作会抛异常(这是 WPF 作为界面框架的一种特性)。
            // PostAsync 会将操作转移到 UI 线程执行,这个执行是异步的。
            _game.CountdownTicked += (g, cd) => PostAsync(g, () => UpdateCountdown(cd.Seconds));
            _game.NewRoundEntered += (g, a) => PostAsync(g, () => OnNewRound(g, a));
            _game.ErrorOccurred   += (g, s) => PostAsync(g, () => WriteLog(s + " Please restart game."));

            // 将界面调整到运行状态。
            MainGrid.IsEnabled = true;
            Blocker.Visibility = Visibility.Collapsed;
            WriteLog($"=== Entered room {creation.Game.RoomId} ({creation.Game.NumberMode}-number) ===");
            WriteLog($"Current user ID: {_game.UserId}");
            WriteLog($"Current round: {creation.InitialRound.RoundId}");
            UpdateCountdown(creation.InitialCountdown.Seconds);

            bool twoNumbers = creation.Game.NumberMode == RoomNumberMode.Two;

            Number2InputTextBox.Visibility = twoNumbers ? Visibility.Visible : Visibility.Collapsed;
            NumberInputTextBox.Width       = twoNumbers ? 40 : 100;

            var nickname = creation.Game.Nickname;

            if (nickname != null)
            {
                NicknameTextBox.Text = nickname;
            }

            await ShowHistoryAsync();
        }
        public readonly uint Unknown; // If game creation succeeded, this is a nonzero value whose meaning is unknown.

        #endregion Fields

        #region Constructors

        public CreateGameResponse(byte[] data)
            : base(data)
        {
            this.RequestID = BitConverter.ToUInt16(data, 1);
            this.Unknown = BitConverter.ToUInt32(data, 3);
            this.Result = (CreateGameResult) BitConverter.ToUInt32(data, 7);
        }