/// <summary> /// Join an existing City & Seek game. /// </summary> /// <param name="gameId">Game ID</param> /// <param name="gamePassword">Game password</param> /// <param name="username">Username to use</param> /// <returns>Welcome payload</returns> public async Task <WelcomePayload> JoinGameAsync(int gameId, string gamePassword, string username) { var joinGamePayload = new JoinGamePayload(gameId, gamePassword, username); int requestId = RequestId; WebSocket.SendAsync(JsonConvert.SerializeObject(new Packet(Intent.JoinGame, joinGamePayload, requestId)), b => { }); // todo: handle errors var resp = await AwaitResponse(requestId); return(resp.GetPayload <WelcomePayload>()); }
private async Task InitializeAndBegForInfo(GM gameMaster, int player1ID, int player2ID) { JoinGamePayload joinGamePayload = new JoinGamePayload() { TeamID = Team.Blue, }; Message joinMessage1 = new Message() { MessageID = MessageID.JoinTheGame, AgentID = player1ID, Payload = joinGamePayload, }; Message joinMessage2 = new Message() { MessageID = MessageID.JoinTheGame, AgentID = player2ID, Payload = joinGamePayload, }; await gameMaster.AcceptMessage(joinMessage1, CancellationToken.None); await gameMaster.AcceptMessage(joinMessage2, CancellationToken.None); gameMaster.Invoke("StartGame", CancellationToken.None); BegForInfoPayload begForInfoPayload = new BegForInfoPayload() { AskedAgentID = player2ID, }; Message askMessage = new Message() { MessageID = MessageID.BegForInfo, AgentID = player1ID, Payload = begForInfoPayload, }; await gameMaster.AcceptMessage(askMessage, CancellationToken.None); }
public override void OnPacket(Packet packet) { if (packet.Intent != Intent.JoinGame) { return; } JoinGamePayload joinGame = packet.GetPayload <JoinGamePayload>(); // Check game exists if (!Connection.Server.Games.ContainsKey(joinGame.GameId)) { throw new CityAndSeekException("Game doesn't exist!"); } ServerGame game = Connection.Server.Games[joinGame.GameId]; // Check password (not case sensitive) if (!joinGame.GamePassword.Equals(game.Password, StringComparison.OrdinalIgnoreCase)) { throw new CityAndSeekException("Incorrect password!"); } // Check game state if (game.GameState != GameState.Setup) { string message = "Game cannot accept new players right now."; switch (game.GameState) { case GameState.Starting: case GameState.Running: case GameState.Paused: message = "Game is already running."; break; case GameState.Ended: message = "Game has already ended."; break; } throw new CityAndSeekException(message); } int newId = game.Players.Count; var player = new ServerPlayer { Id = newId, Name = joinGame.Username, Token = SecureToken.Generate(), Game = game, Connection = Connection }; // Associate player with this connection Connection.Player = player; // Add player to the game game.AddPlayer(player); // Send welcome var welcome = new WelcomePayload(game, player, player.Token); Connection.SendPacket(new Packet(Intent.Welcome, welcome, packet.Id)); }