public Session(Server server, int id, CreateSessionData sessionData) { admin = new SessionAdmin(this); this.server = server; this.id = id; data = sessionData; if(data.MinPlayers > data.MaxPlayers) throw new MinPlayersOutOfRangeException(); if(data.MinPlayers < 2) throw new MinPlayersOutOfRangeException(); if(data.MinPlayers > 8) throw new MinPlayersOutOfRangeException(); if(data.MaxPlayers < 2) throw new MaxPlayersOutOfRangeException(); if(data.MaxPlayers > 8) throw new MaxPlayersOutOfRangeException(); if(data.MaxSpectators < 0) throw new MaxSpectatorsOutOfRangeException(); state = SessionState.WaitingForPlayers; eventMgr = new SessionEventManager(this); players = new Dictionary<int, SessionPlayer>(data.MaxPlayers); playerList = new List<SessionPlayer>(data.MaxPlayers); spectators = new Dictionary<int, SessionSpectator>(data.MaxSpectators); spectatorList = new List<SessionSpectator>(data.MaxSpectators); creatorId = 0; gamesPlayed = 0; remainingCharacters = Utils.GetCharacterTypes(this); eventMgr.StartPolling(); }
protected void OnResponse(object o, Gtk.ResponseArgs args) { if(args.ResponseId != Gtk.ResponseType.Ok) { Destroy(); return; } this.Sensitive = false; System.Threading.ThreadPool.QueueUserWorkItem((state) => { CreateSessionData csd = new CreateSessionData( sessionNameEntry.Text, sessionDescriptionEntry.Buffer.Text, minPlayerCountEntry.ValueAsInt, maxPlayerCountEntry.ValueAsInt, maxSpectatorCountEntry.ValueAsInt, playerPasswordEntry.Text, spectatorPasswordEntry.Text, shufflePlayersCheckbox.Active, dodgeCityCheckbox.Active, highNoonCheckbox.Active, aFistfulOfCardsCheckbox.Active, wildWestShowCheckbox.Active); CreatePlayerData cpd = playerDataWidget.PlayerData; try { ConnectionManager.Server.CreateSession(csd, cpd, ConnectionManager.SessionEventListener); } catch(Exception ex) { Gtk.Application.Invoke(delegate { Gdk.Threads.Enter(); ErrorManager.ShowErrorMessage(this, MessageManager.GetErrorMessage(ex)); Destroy(); Gdk.Threads.Leave(); }); return; } Gtk.Application.Invoke(delegate { Gdk.Threads.Enter(); Destroy(); Gdk.Threads.Leave(); }); }); }
public static void Main(string[] cmdArgs) { Console.Title = "Bang# Command-Line Client"; ConsoleHelper.PrintLine("Bang# Command-Line Client"); ConsoleHelper.PrintLine("-------------------------"); ConsoleHelper.PrintLine("Interface version: {0}.{1}", Utils.InterfaceVersionMajor, Utils.InterfaceVersionMinor); ConsoleHelper.PrintLine("Operating system: {0}", Environment.OSVersion); ConsoleHelper.PrintLine("-------------------------"); string address; string portString; if(cmdArgs.Length != 2) { ConsoleHelper.Print("Server Address: "); address = ConsoleHelper.ReadLine(); ConsoleHelper.Print("Server Port: "); portString = ConsoleHelper.ReadLine(); } else { address = cmdArgs[0]; portString = cmdArgs[1]; } int port; try { port = int.Parse(portString); } catch(FormatException) { ConsoleHelper.ErrorLine("Bad number format!"); return; } try { ConsoleHelper.PrintLine("Connecting to {0} on port {1}...", address, port); Utils.OpenClientChannel(); IServer _server = Utils.Connect(address, port); ConsoleHelper.PrintLine(); if(!Utils.IsServerCompatible(_server)) { ConsoleHelper.ErrorLine("Server version {0}.{1} not compatible with client version {2}.{3}!", _server.InterfaceVersionMajor, _server.InterfaceVersionMinor, Utils.InterfaceVersionMajor, Utils.InterfaceVersionMinor); return; } ConsoleHelper.PrintLine("Server name: {0}", _server.Name); ConsoleHelper.PrintLine("Server description: {0}", _server.Description); _server.RegisterListener(Instance); ConsoleHelper.SuccessLine("Connection estabilished!"); ConsoleHelper.PrintLine(); NestedCommand rootCmd = new NestedCommand(); NestedCommand<IServer> serverCmd = new NestedCommand<IServer>(cmd => _server); serverCmd.MakeServerCommand(); NestedCommand<IServer, ISession> sessionCmd = (NestedCommand<IServer, ISession>)serverCmd["session"]; sessionCmd["join"] = new FinalCommand<ISession>((session, cmd) => { CreatePlayerData cpd; string playerName; Password playerPassword; Password password; if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } ConsoleHelper.Print("Session Password: "******"Player Name: "); playerName = ConsoleHelper.ReadLine(); ConsoleHelper.Print("Player Password: "******"Joined session!"); } catch(GameException e) { ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType()); } }); sessionCmd["joinai"] = new FinalCommand<ISession>((session, cmd) => { CreatePlayerData cpd; Password password; if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } ConsoleHelper.Print("Session Password: "******"TestAI"; try { Instance.SetAI(ai); session.Join(password, cpd, Instance.mainSessionListener); ConsoleHelper.SuccessLine("Joined session!"); } catch(GameException e) { Instance.UnsetAI(); ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType()); } }); sessionCmd["replace"] = new FinalCommand<ISession>((session, cmd) => { CreatePlayerData cpd; string playerName; Password playerPassword; Password password; int id; try { id = int.Parse(cmd.Dequeue()); } catch(FormatException) { ConsoleHelper.ErrorLine("Bad number format!"); return; } if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } ConsoleHelper.Print("Session Password: "******"Player Name: "); playerName = ConsoleHelper.ReadLine(); ConsoleHelper.Print("Player Password: "******"Joined session!"); } catch(GameException e) { ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType()); } }); sessionCmd["replaceai"] = new FinalCommand<ISession>((session, cmd) => { CreatePlayerData cpd; Password playerPassword; Password password; int id; try { id = int.Parse(cmd.Dequeue()); } catch(FormatException) { ConsoleHelper.ErrorLine("Bad number format!"); return; } if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } ConsoleHelper.Print("Session Password: "******"Player Password: "******"TestAI"; cpd.Password = playerPassword; try { Instance.SetAI(ai); session.Replace(id, password, cpd, Instance.mainSessionListener); ConsoleHelper.SuccessLine("Joined session!"); } catch(GameException e) { Instance.UnsetAI(); ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType()); } }); sessionCmd["aitestcontinue"] = new FinalCommand<ISession>((session, cmd) => { CreatePlayerData cpd; Password password; int id; try { id = int.Parse(cmd.Dequeue()); } catch(FormatException) { ConsoleHelper.ErrorLine("Bad number format!"); return; } if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } password = new Password("_aitest"); AI.AIPlayer ai = new AI.AIPlayer(); cpd = ai.CreateData; cpd.Name = "TestAI"; cpd.Password = new Password("_aitest"); try { Instance.SetAI(ai); Instance.aiTest = true; session.Replace(id, password, cpd, Instance.mainSessionListener); if(Instance.sessionControl.Session.State != SessionState.Playing) Instance.sessionControl.StartGame(); ConsoleHelper.SuccessLine("Joined AI Test session!"); Console.ReadKey(true); Instance.sessionControl.Disconnect(); Instance.sessionControl = null; Instance.gameControl = null; ConsoleHelper.SuccessLine("AI test session disconnected!"); } catch(GameException e) { ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType()); } Instance.UnsetAI(); Instance.aiTest = false; }); serverCmd["session"] = sessionCmd; serverCmd["test"] = new FinalCommand<IServer>((server, cmd) => { if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } int playerCount = 4; if(cmd.Count != 0) { try { playerCount = int.Parse(cmd.Dequeue()); } catch(FormatException) { ConsoleHelper.ErrorLine("Bad number format!"); return; } } CreateSessionData csd = new CreateSessionData { Name = "Test", Description = "", MinPlayers = playerCount, MaxPlayers = playerCount, MaxSpectators = 0, DodgeCity = true }; CreatePlayerData cpd = new CreatePlayerData { Name = "Human" }; try { server.CreateSession(csd, cpd, Instance.mainSessionListener); ConsoleHelper.SuccessLine("Test session created!"); } catch(GameException e) { ConsoleHelper.ErrorLine("Cannot create session: {0}", e.GetType()); } }); serverCmd["testai"] = new FinalCommand<IServer>((server, cmd) => { if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } int playerCount = 4; if(cmd.Count != 0) { try { playerCount = int.Parse(cmd.Dequeue()); } catch(FormatException) { ConsoleHelper.ErrorLine("Bad number format!"); return; } } CreateSessionData csd = new CreateSessionData { Name = "Test", Description = "", MaxPlayers = playerCount, MinPlayers = playerCount, MaxSpectators = 0, DodgeCity = true }; AI.AIPlayer ai = new AI.AIPlayer(); CreatePlayerData cpd = ai.CreateData; cpd.Name = "TestAI"; try { server.CreateSession(csd, cpd, Instance.mainSessionListener); Instance.SetAI(ai); ConsoleHelper.SuccessLine("Test AI session created!"); } catch(GameException e) { ConsoleHelper.ErrorLine("Cannot create session: {0}", e.GetType()); } }); serverCmd["aitest"] = new FinalCommand<IServer>((server, cmd) => { if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } int playerCount = 4; if(cmd.Count != 0) { try { playerCount = int.Parse(cmd.Dequeue()); } catch(FormatException) { ConsoleHelper.ErrorLine("Bad number format!"); return; } } CreateSessionData csd = new CreateSessionData { Name = "AI Test", Description = "An AI testing session.", MaxPlayers = playerCount, MinPlayers = playerCount, MaxSpectators = 0, PlayerPassword = new Password("_aitest"), DodgeCity = true }; AI.AIPlayer ai = new AI.AIPlayer(); CreatePlayerData cpd = ai.CreateData; cpd.Name = "TestAI"; cpd.Password = new Password("_aitest"); try { server.CreateSession(csd, cpd, Instance.mainSessionListener); Instance.SetAI(ai); Instance.aiTest = true; Instance.sessionControl.StartGame(); ConsoleHelper.SuccessLine("AI test session created!"); Console.ReadKey(true); Instance.sessionControl.Disconnect(); Instance.sessionControl = null; Instance.gameControl = null; Instance.UnsetAI(); Instance.aiTest = false; ConsoleHelper.SuccessLine("AI test session disconnected!"); } catch(GameException e) { ConsoleHelper.ErrorLine("Cannot create session: {0}", e.GetType()); } }); serverCmd["createsession"] = new FinalCommand<IServer>((server, cmd) => { if(Instance.sessionControl != null) { ConsoleHelper.ErrorLine("Already connected to a session!"); return; } ConsoleHelper.Print("Session Name: "); string sessionName = ConsoleHelper.ReadLine(); ConsoleHelper.Print("Session Description: "); string sessionDescription = ConsoleHelper.ReadLine(); int sessionMinPlayers; int sessionMaxPlayers; int sessionMaxSpectators; try { ConsoleHelper.Print("Session MinPlayers: "); sessionMinPlayers = int.Parse(ConsoleHelper.ReadLine()); ConsoleHelper.Print("Session MaxPlayers: "); sessionMaxPlayers = int.Parse(ConsoleHelper.ReadLine()); ConsoleHelper.Print("Session MaxSpectators: "); sessionMaxSpectators = int.Parse(ConsoleHelper.ReadLine()); } catch(FormatException) { ConsoleHelper.ErrorLine("Bad number format!"); return; } ConsoleHelper.Print("Session PlayerPassword: "******"Session SpectatorPassword: "******"Session ShufflePlayers: "); bool sessionShufflePlayers = ConsoleHelper.ReadLine().ToLower() == "y"; ConsoleHelper.Print("Session DodgeCity: "); bool sessionDodgeCity = ConsoleHelper.ReadLine().ToLower() == "y"; ConsoleHelper.Print("Session HighNoon: "); bool sessionHighNoon = ConsoleHelper.ReadLine().ToLower() == "y"; ConsoleHelper.Print("Session FistfulOfCards: "); bool sessionFistfulOfCards = ConsoleHelper.ReadLine().ToLower() == "y"; ConsoleHelper.Print("Session WildWestShow: "); bool sessionWildWestShow = ConsoleHelper.ReadLine().ToLower() == "y"; CreateSessionData csd = new CreateSessionData { Name = sessionName, Description = sessionDescription, MinPlayers = sessionMinPlayers, MaxPlayers = sessionMaxPlayers, MaxSpectators = sessionMaxSpectators, PlayerPassword = sessionPlayerPassword, SpectatorPassword = sessionSpectatorPassword, ShufflePlayers = sessionShufflePlayers, DodgeCity = sessionDodgeCity, HighNoon = sessionHighNoon, FistfulOfCards = sessionFistfulOfCards, WildWestShow = sessionWildWestShow }; ConsoleHelper.Print("Player Name: "); string playerName = ConsoleHelper.ReadLine(); ConsoleHelper.Print("Player Password: "******"Session created!"); } catch(GameException e) { ConsoleHelper.ErrorLine("Cannot create session: {0}", e.GetType()); } }); rootCmd["server"] = serverCmd; NestedCommand<IPlayerSessionControl> sessionControlCommand = new NestedCommand<IPlayerSessionControl>(cmd => { IPlayerSessionControl sessionControl = Instance.sessionControl; if(sessionControl == null) { ConsoleHelper.ErrorLine("Not connected to any session!"); return null; } return sessionControl; }); sessionControlCommand.MakePlayerSessionControlCommand(() => { Instance.sessionControl = null; Instance.gameControl = null; }); rootCmd["sessioncontrol"] = sessionControlCommand; NestedCommand<IPlayerControl> gameControlCommand = new NestedCommand<IPlayerControl>(cmd => { IPlayerControl gameControl = Instance.gameControl; if(gameControl == null) { ConsoleHelper.ErrorLine("Not playing any game!"); return null; } return gameControl; }); gameControlCommand.MakePlayerGameControlCommand(); rootCmd["gamecontrol"] = gameControlCommand; rootCmd["exit"] = new FinalCommand(cmd => { _server.UnregisterListener(Instance); Environment.Exit(0); }); while(true) // command-line loop { try { rootCmd.ReadAndExecute(); } catch(InvalidOperationException) { ConsoleHelper.ErrorLine("Invalid command!"); } } } catch(RemotingException e) { ConsoleHelper.ErrorLine("Remoting error!"); #if DEBUG ConsoleHelper.DebugLine(e.ToString()); #endif return; } catch(SerializationException e) { ConsoleHelper.ErrorLine("Serialization error!"); #if DEBUG ConsoleHelper.DebugLine(e.ToString()); #endif return; } }
public Session(Server server, BinaryReader reader) { admin = new SessionAdmin(this); this.server = server; id = reader.ReadInt32(); try { BinaryFormatter bf = new BinaryFormatter(); data = (CreateSessionData)bf.Deserialize(reader.BaseStream); } catch(InvalidCastException) { throw new FormatException(); } catch(SerializationException) { throw new FormatException(); } try { state = (SessionState)reader.ReadInt32(); } catch(InvalidCastException) { throw new FormatException(); } if(state == SessionState.Playing) state = SessionState.GameFinished; creatorId = reader.ReadInt32(); gamesPlayed = reader.ReadInt32(); int playerCount = reader.ReadInt32(); if(playerCount < 0) throw new FormatException(); playerList = new List<SessionPlayer>(playerCount); players = new Dictionary<int, SessionPlayer>(playerCount); for(int i = 0; i < playerCount; i++) { bool isAI = reader.ReadBoolean(); SessionPlayer player = new SessionPlayer(this, reader); if(isAI && player.ID != creatorId) player.RegisterListener(new AI.AIPlayer()); playerList.Add(player); players.Add(player.ID, player); } spectatorList = new List<SessionSpectator>(); spectators = new Dictionary<int, SessionSpectator>(); eventMgr = new SessionEventManager(this); int currentSheriffId = reader.ReadInt32(); if(currentSheriffId != 0) { sheriffEnumerator = playerList.GetEnumerator(); do { if(!sheriffEnumerator.MoveNext()) throw new FormatException(); } while(sheriffEnumerator.Current.ID != currentSheriffId); } int remCharCount = reader.ReadInt32(); if(playerCount < 0) throw new FormatException(); remainingCharacters = new List<CharacterType>(remCharCount); for(int i = 0; i < remCharCount; i++) remainingCharacters.Add((CharacterType)reader.ReadInt32()); eventMgr.StartPolling(); }
public void CreateSession(CreateSessionData sessionData, CreatePlayerData playerData, IPlayerSessionEventListener listener) { lock(Lock) { int id = sessions.GenerateID(); Session session = new Session(this, id, sessionData); sessions.Add(id, session); Console.Error.WriteLine("INFO: Created session #{0}.", id); session.Join(sessionData.PlayerPassword, playerData, listener); eventMgr.OnSessionCreated(session); SaveState(); } }