private void ParseCommand(Command command) { //Decide what to do with command Console.WriteLine("Parsing command " + command.Type + " from Player " + command.PlayerId + " '" + command.Data + "'"); // Player command is a command issued by the player // Only handled by server if (network.IsServer() && GameStarted() && command.Type == Command.CommandType.Player) { ParsePlayerCommand(command); } else if (network.IsServer() && !GameStarted() && command.Type == Command.CommandType.Player) { ParsePlayerLobbyCommand(command); } else if (network.IsServer() && !GameStarted() && command.Type == Command.CommandType.Player) { network.SendCommand(new Command(Command.CommandType.Text, "Wait for the host to start the game.", command.PlayerId)); return; } else if (network.IsServer() && command.Type == Command.CommandType.PlayerJoined) { int id = int.Parse(command.Data); players[id] = new Player("player_" + id); Debug.WriteLine("added Player with name " + players[id].name); } else if (command.Type == Command.CommandType.PlayerJoined) { myPlayerId = int.Parse(command.Data); Console.WriteLine("My player id: " + myPlayerId); players[myPlayerId] = new Player("SELF"); } else if (command.Type == Command.CommandType.Text) { ((ChatScene)game.GetScene(TojamGame.GameScenes.Chat)).AddMessage(command.Data, command.SourcePlayerId); } else if (command.Type == Command.CommandType.PlayerInfo) { Command.ParsePlayerInfoCommand(players[myPlayerId], command.Data); } else if (GameStarted() && command.Type == Command.CommandType.PictureEvent) { CarPicture.PictureEvent evt = Command.ParsePictureEventCommand(command.Data); ChatScene chatScene = (ChatScene)this.game.GetScene(TojamGame.GameScenes.Chat); chatScene.GetCarPicture().TriggerEvent(evt); } }
public void Update(GameTime gameTime) { // automatically connect on initialization off of config if (network == null && (game.config.isHost || game.config.ipAddress != null)) { ChatScene chatScene = (ChatScene)game.GetScene(TojamGame.GameScenes.Chat); if (game.config.isHost) { network = new Network(); network.Start(true, null); string lip = GetLocalIPAddress(); chatScene.AddMessage("Started hosting on: " + lip); } else { chatScene.AddMessage("joining " + game.config.ipAddress); network = new Network(); network.Start(false, game.config.ipAddress); } } if (network != null) { // Update game state for server only if (network.IsServer() && GameStarted()) { if (carIsDriving) { driveTime += gameTime.ElapsedGameTime.Milliseconds; if (driveTime >= world.GetLocation(carLocation).DriveLength - SignAnimation.effectiveAnimationLength && !signAnimationTriggered) { signAnimationTriggered = true; String nextLocationName = world.GetLocation(carLocation).DriveLocation.Name; Command animationCommand = Command.PreparePictureEventCommand("town", new Dictionary <string, object> { { "townName", nextLocationName } }, Network.SEND_ALL); network.SendCommand(animationCommand); } if (driveTime >= world.GetLocation(carLocation).DriveLength) { Console.WriteLine("Finished drive"); int newLocation = world.GetLocation(carLocation).DriveLocation.Id; Location NewLocation = world.GetLocation(newLocation); carLocation = newLocation; foreach (int pId in players.Keys) { Player p = players[pId]; if (p.carLocation != Player.CarLocation.NotInCar) { p.worldLocation = carLocation; network.SendCommand(new Command(Command.CommandType.Text, "You have reached your next stop " + NewLocation.Name, pId)); if (NewLocation.HasDescription()) { network.SendCommand(new Command(Command.CommandType.Text, NewLocation.Description, pId)); } if (NewLocation.IsDestination) { // TODO win game SendGameSummary(); } } } driveTime = 0; signAnimationTriggered = false; carIsDriving = false; SendAllPlayerInfoCommand(); } } statsUpdate += gameTime.ElapsedGameTime.Milliseconds; if (statsUpdate > 1500) { statsUpdate = 0; foreach (Player p in players.Values) { if (p.alive) { if (this.world.GetLocation(p.worldLocation).Name.ToLower() == "algonquin") { p.hunger = 100; p.thirst = 100; p.tired = 100; } else if (!p.invincible) { //bool die = false; //bool alreadyDead = false; //if (p.hunger < 1 || p.hunger < 1 || p.thirst < 1) alreadyDead = true; //if (!alreadyDead) { if (p.hunger > 0) { p.hunger -= rand.Next(0, 2); if (p.hunger == 0) { p.alive = false; } } if (p.thirst > 0) { p.thirst -= rand.Next(0, 2); if (p.thirst == 0) { p.alive = false; } } if (p.carLocation == Player.CarLocation.NotInCar || (carIsDriving && p.carLocation == Player.CarLocation.DriversSeat)) { if (p.tired > 0) { p.tired -= rand.Next(0, 3); if (p.tired == 0) { p.alive = false; } } } else { p.tired += rand.Next(0, 2); } if (p.tired > 100) { p.tired = 100; } if (!p.haveSickWarned && (p.tired == 40 || p.hunger == 40 || p.thirst == 40)) { p.haveSickWarned = true; network.SendCommand(new Command(Command.CommandType.Text, p.name + " is looking sick...", Network.SEND_ALL)); } if (!p.alive) { network.SendCommand(new Command(Command.CommandType.Text, p.name + " has died", Network.SEND_ALL)); foreach (Player player in this.players.Values) { player.worldLocation = world.GameOverId; player.carLocation = Player.CarLocation.NotInCar; } } } } } } SendAllPlayerInfoCommand(); } } if (network.IsServer()) { // Parse server generated messages foreach (Command command in network.GetLocalCommands()) { ParseCommand(command); } } network.Update(); foreach (Command command in network.GetCommands()) { ParseCommand(command); } } }
private void ParseClientCommand(string command) { // join or start server String[] tokens = command.ToLower().Split(' '); if (tokens.Length == 0) { return; } ChatScene chatScene = (ChatScene)game.GetScene(TojamGame.GameScenes.Chat); switch (tokens[0]) { case "join": if (network != null) { break; } string ip = (tokens.Length > 1) ? tokens[1] : "10.206.236.146"; IPAddress address; if (!IPAddress.TryParse(ip, out address)) { chatScene.AddMessage("'" + ip + "' is not not an IP address"); break; } chatScene.AddMessage("joining " + ip); network = new Network(); network.Start(false, ip); break; case "host": if (network != null) { break; } network = new Network(); network.Start(true, null); string lip = GetLocalIPAddress(); chatScene.AddMessage("Started hosting on: " + lip); break; case "setname": case "say": if (network != null) { if (!network.IsServer()) { // forward lobby commands network.SendCommand(new Command(Command.CommandType.Player, command, myPlayerId)); } else { ParsePlayerLobbyCommand(new Command(Command.CommandType.Player, command, myPlayerId)); } } break; case "start": if (network != null && network.IsServer()) { bool allNamed = true; foreach (Player p in players.Values) { allNamed = allNamed && p.isNamed; } if (allNamed) { gameStarted = true; network.SendCommand(new Command(Command.CommandType.Text, "The game has started!", Network.SEND_ALL)); } else { network.SendCommand( new Command(Command.CommandType.Text, "The game cannot start until all players have set their names. use setname <name> to set name", Network.SEND_ALL)); } } break; default: if (network != null) { if (network.IsServer()) { chatScene.AddMessage("Type 'start' when everyone has joined."); } else { chatScene.AddMessage("Wait for the host to start the game. 'setname <name> to set your name"); } break; } else { chatScene.AddMessage("You must 'join <ip>' or 'host' a game "); } break; } }