public void JoinGameInstance(JoinGameInstanceMessage message) { var currentPlayer = _websocket.GetCurrentSessionPlayer(); Assert.ThrowIfNull(currentPlayer, "requires a user session", _websocket); var game = Server.Instance.Games.FirstOrDefault(g => g.Name == message.GameName); Assert.ThrowIfNull(game, $"game {game.Name} not found", _websocket); var gameInstance = game.Instances.FirstOrDefault(g => g.Name == message.InstanceName); Assert.ThrowIfNull(gameInstance, $"game instance {game.Name}:{gameInstance.Name} not found", _websocket); if (gameInstance != null) { Server.Instance.JoinGameInstance(gameInstance, _websocket.GetCurrentSessionPlayer()); _websocket.LogMessage($"Player joins game instance '{message.GameName}:{message.InstanceName}'"); var broadcastMessage = new GameInstanceJoinedMessage() { Player = currentPlayer }; _websocket.Broadcast(broadcastMessage, null, true); if (gameInstance.State == GameInstanceState.WaitingForPlayers && gameInstance.Players.Count >= gameInstance.Game.MinPlayers) { Server.Instance.StartGameInstance(gameInstance.Id); } } }
public void DemoCommands() { _currentPlayer = JsonConvert.DeserializeObject <PlayerClientInfo>(File.ReadAllText(CurrentPlayerFilename)); Console.WriteLine($"Player has saved session as {_currentPlayer.Name} with sessionId = {_currentPlayer.SessionId}"); _webSocket = new WebSocket(_url); _webSocket.OnMessage += (object source, MessageEventArgs e) => { string msgTxt = e.Data.ToString(); Message responseMessage = JsonConvert.DeserializeObject <Message>(msgTxt); if (responseMessage.MessageType == MessageTypeNames.ChatSaid) { OnChatSaid(JsonConvert.DeserializeObject <ChatSaidMessage>(msgTxt)); } else if (responseMessage.MessageType == MessageTypeNames.PlayerListing) { OnPlayerListing(JsonConvert.DeserializeObject <PlayerListingMessage>(msgTxt)); } else if (responseMessage.MessageType == MessageTypeNames.GameInstanceCreated) { OnGameInstanceCreated(JsonConvert.DeserializeObject <GameInstanceCreatedMessage>(msgTxt)); } else if (responseMessage.MessageType == MessageTypeNames.JoinedGameInstance) { OnJoinedGameInstance(JsonConvert.DeserializeObject <JoinedGameInstanceMessage>(msgTxt)); } else if (responseMessage.MessageType == MessageTypeNames.RequestFullState) { OnRequestFullState(JsonConvert.DeserializeObject <RequestFullStateMessage>(msgTxt)); } else if (responseMessage.MessageType == MessageTypeNames.StateChanged) { OnStateChanged(JsonConvert.DeserializeObject <StateChangedMessage>(msgTxt)); } else if (responseMessage.MessageType == MessageTypeNames.ServerError) { OnServerError(JsonConvert.DeserializeObject <ServerErrorMessage>(msgTxt)); } }; try { _webSocket.SetCookie(new Cookie(MessageConstants.SessionCookieName, _currentPlayer.SessionId)); _webSocket.Connect(); } catch (Exception e) { File.Delete(App.CurrentPlayerFilename); _currentPlayer = null; Console.WriteLine(e.Message); } while (_currentPlayer != null) { try { Console.WriteLine("Enter a command:"); string command = Console.ReadLine(); if (command.StartsWith("say ", true, null)) { ChatSayMessage chatSayMessage = new ChatSayMessage(); chatSayMessage.say = command.Substring(4); _webSocket.Send(JsonConvert.SerializeObject(chatSayMessage)); } else if (command.StartsWith("list", true, null)) { PlayerListMessage playerListMessage = new PlayerListMessage(); _webSocket.Send(JsonConvert.SerializeObject(playerListMessage)); } else if (command.StartsWith("rename", true, null)) { string newName = command.Substring("rename".Length + 1); PlayerRenameMessage playerRenameMessage = new PlayerRenameMessage(); playerRenameMessage.Name = newName; _currentPlayer.Name = newName; _webSocket.Send(JsonConvert.SerializeObject(playerRenameMessage)); File.WriteAllText(CurrentPlayerFilename, JsonConvert.SerializeObject(_currentPlayer)); } else if (command.StartsWith("create", true, null)) { string[] parts = command.Split(' '); CreateGameInstanceMessage createGameInstanceMessage = new CreateGameInstanceMessage(); createGameInstanceMessage.GameName = parts[1]; createGameInstanceMessage.InstanceName = parts[2]; _webSocket.Send(JsonConvert.SerializeObject(createGameInstanceMessage)); } else if (command.StartsWith("join", true, null)) { string[] parts = command.Split(' '); JoinGameInstanceMessage joinGameInstanceMessage = new JoinGameInstanceMessage(); joinGameInstanceMessage.GameName = parts[1]; joinGameInstanceMessage.InstanceName = parts[2]; _webSocket.Send(JsonConvert.SerializeObject(joinGameInstanceMessage)); } else if (command.StartsWith("sync", true, null)) { RequestFullStateMessage message = new RequestFullStateMessage(); _webSocket.Send(JsonConvert.SerializeObject(message)); } else if (command.StartsWith("move", true, null)) { StateChangeMessage message = new StateChangeMessage(); StateChange batch = new StateChange(); batch.changes = new List <Change2d>(); Random rnd = new Random(); int randomCount = rnd.Next() % 100; for (int index = 0; index < randomCount; index++) { Change2d change = new Change2d(); change.Event = ChangeEvent.Created; change.ObjectID = rnd.Next().ToString(); change.Transform = new Transform2d { Pos = new Vector2d { X = rnd.NextDouble() * 100.0, Y = rnd.NextDouble() % 100.0 }, Vel = new Vector2d { X = rnd.NextDouble() % 100.0, Y = rnd.NextDouble() % 100.0 }, Rot = rnd.NextDouble() * 380.0, Scale = 1.0 }; batch.changes.Add(change); } message.Payload = batch; _webSocket.Send(JsonConvert.SerializeObject(message)); } else if (command.StartsWith("help", true, null)) { Console.WriteLine("FIXME: add a list of commands"); } else { Console.WriteLine("unknown command"); } } catch (Exception e) { File.Delete(App.CurrentPlayerFilename); this._currentPlayer = null; // causes re-login Console.WriteLine(e.Message); } } try { _webSocket.Close(); _webSocket = null; } catch (Exception e) { File.Delete(App.CurrentPlayerFilename); this._currentPlayer = null; // causes re-login Console.WriteLine(e.Message); } }