private async void OnBackClicked() { if (!LeavingInProgress) { LeavingInProgress = true; var leaveResponse = await Client.Leave(); if (leaveResponse.IsStatusOk()) { Client.State().ClearRoomState(); SceneManager.LoadScene("login", LoadSceneMode.Single); } else { Debug.LogError($"leaving room failed because {leaveResponse.StatusMessage()}"); } LeavingInProgress = false; } }
public static async Task AttemptStartGame() { await AttemptCreateRoom(); PriselClient client = PriselClient.Instance; var clientState = client.State(); if (!clientState.GameStarted) { var response = await client.StartGame(); if (response.IsStatusOk()) { client.State().GameStarted = true; Debug.Log("Successfully started game"); } else { Debug.Log($"start game failed because {response.StatusMessage()}"); } } }
// Start is called before the first frame update void Start() { Client = PriselClient.Instance; var clientState = Client.State(); if (!Client.IsConnected) { NextState = new LoginState(); } else if (String.IsNullOrEmpty(clientState.UserId)) { NextState = new LoginState(); } else if (String.IsNullOrEmpty(clientState.RoomId)) { NextState = new JoinModeState(); } }
public static async Task AttemptCreateRoom() { await AttemptLogin(); PriselClient client = PriselClient.Instance; var clientState = client.State(); if (String.IsNullOrEmpty(clientState.RoomId)) { var response = await client.CreateRoom("room"); if (response.IsStatusOk()) { clientState.RoomName = response.Payload.CreateRoomResponse.Room.Name; clientState.RoomId = response.Payload.CreateRoomResponse.Room.Id; Debug.Log($"Successfully create room with room name {clientState.RoomName}, roomId {clientState.RoomId}"); } else { Debug.Log($"create room failed because {response.StatusMessage()}"); } } }
public static async Task AttemptLogin() { await AttemptConnect(); PriselClient client = PriselClient.Instance; var clientState = client.State(); if (String.IsNullOrEmpty(clientState.UserId)) { var response = await client.Login(USERNAME); if (response.IsStatusOk()) { clientState.UserId = response.Payload.LoginResponse.UserId; clientState.Username = USERNAME; Debug.Log($"Successfully login with username {clientState.Username}, userId {clientState.UserId}"); } else { Debug.Log($"login failed because {response.StatusMessage()}"); } } }
// Start is called before the first frame update async void Start() { if (Application.isEditor && Application.isPlaying) { await TestLoginFlow.AttemptStartGame(); } // Start listen for messages Client.AddAction("animation", (AnimationPayload anim, Packet packet) => { _ = AnimationDispatcher.HandleAnimation(anim.Animation); }); Client.AddAction("announce_start_turn", (AnnounceStartTurnPayload payload, Packet packet) => { bool isCurrentPlayerTurn = payload.Player.Equals(Client.State().GamePlayerId); if (isCurrentPlayerTurn) { EventBus.StartCurrentPlayerTurn?.Invoke(); } }); Client.AddAction("announce_roll", (AnnounceRollPayload payload, Packet packet) => { }); Client.AddAction("announce_pay_rent", (AnnouncePayRentPayload payload, Packet packet) => { }); Client.AddAction("prompt_purchase", (PromptPurchaseRequest request, Packet packet) => { EventBus.PromptPurchase?.Invoke(request, packet); }); Client.AddAction("announce_purchase", (AnnouncePurchasePayload payload, Packet packet) => { EventBus.PropertyChange?.Invoke(payload.Property, Players.Find(player => player.Id.Equals(payload.Player))); }); Client.AddAction("announce_end_turn", (AnnounceEndTurnPayload payload, Packet packet) => { }); Client.AddAction("announce_bankrupt", (AnnounceBankruptPayload payload, Packet packet) => { }); Client.AddAction("announce_game_over", (AnnounceGameOverPayload payload, Packet packet) => { }); Client.AddAction("announce_player_left", (AnnouncePlayerLeftPayload payload, Packet packet) => { }); Client.AddPacketAction("prompt_chance_confirmation", (Packet packet) => { EventBus.PromptChanceConfirmation?.Invoke(packet); }); EventBus.DiceRolled += OnDiceRolled; EventBus.LeaveRoom += OnLeaveRoom; EventBus.PurchaseDecision += OnPurchaseDecision; EventBus.ConfirmChance += OnConfirmChance; // Get initial game state Packet response = await Client.Request(RequestUtils.NewRequest("get_initial_state", Client.NewId)); if (response.IsStatusOk() && response.Payload.ActionPayload.TryUnpack <GetInitialStateResponse>(out var payload)) { SetupPlayers(payload); } }