Пример #1
0
 public RoomOperationResult StartGame(LoginToken token)
 {
     if (VerifyClient(token))
     {
         if (!loggedInGuidToRoom.ContainsKey(token.token)) { return RoomOperationResult.Invalid; }
         int portNumber;
         var room = loggedInGuidToRoom[token.token];
         var total = room.Seats.Count(pl => pl.Account != null);
         var initiator = room.Seats.FirstOrDefault(pl => pl.Account == loggedInGuidToAccount[token.token]);
         if (room.State == RoomState.Gaming) return RoomOperationResult.Invalid;
         if (total <= 1) return RoomOperationResult.Invalid;
         if (initiator == null || initiator.State != SeatState.Host) return RoomOperationResult.Invalid;
         if (room.Seats.Any(cs => cs.Account != null && cs.State != SeatState.Host && cs.State != SeatState.GuestReady)) return RoomOperationResult.Invalid;
         room.State = RoomState.Gaming;
         foreach (var unready in room.Seats)
         {
             if (unready.State == SeatState.GuestReady) unready.State = SeatState.GuestTaken;
         }
         var gs = new GameSettings() { TimeOutSeconds = room.TimeOutSeconds, TotalPlayers = total, CheatEnabled = CheatEnabled };
         var config = new AccountConfiguration();
         config.AccountIds = new List<LoginToken>();
         config.Accounts = new List<Account>();
         foreach (var addconfig in room.Seats)
         {
             if (addconfig.Account != null)
             {
                 config.AccountIds.Add(new LoginToken() { token = loggedInAccountToGuid[addconfig.Account] });
                 config.Accounts.Add(addconfig.Account);
             }
         }
         GameService.StartGameService(HostingIp, gs, config, room.Id, _OnGameEnds, out portNumber);
         room.IpAddress = HostingIp.ToString();
         room.IpPort = portNumber;
         _NotifyGameStart(loggedInGuidToRoom[token.token].Id, HostingIp, portNumber);
         return RoomOperationResult.Success;
     }
     return RoomOperationResult.Auth;
 }
Пример #2
0
        public static void StartGameService(IPAddress IP, GameSettings setting, AccountConfiguration config, int roomId, GameEndCallback callback, out int portNumber)
        {
            int totalNumberOfPlayers = setting.TotalPlayers;
            int timeOutSeconds = setting.TimeOutSeconds;
            #if DEBUG
            Trace.Listeners.Clear();

            TextWriterTraceListener twtl = new TextWriterTraceListener(Path.Combine(Directory.GetCurrentDirectory(), AppDomain.CurrentDomain.FriendlyName + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".txt"));
            twtl.Name = "TextLogger";
            twtl.TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime;

            ConsoleTraceListener ctl = new ConsoleTraceListener(false);
            ctl.TraceOutputOptions = TraceOptions.DateTime;

            Trace.Listeners.Add(twtl);
            Trace.Listeners.Add(ctl);
            Trace.AutoFlush = true;
            Trace.WriteLine("Log starting");
            Trace.Listeners.Add(new ConsoleTraceListener());
            #endif
            Game game = new RoleGame();
            game.Settings = setting;
            game.Configuration = config;
            Sanguosha.Core.Network.Server server;
            server = new Sanguosha.Core.Network.Server(game, totalNumberOfPlayers, IP);
            portNumber = server.IpPort;
            for (int i = 0; i < totalNumberOfPlayers; i++)
            {
                var player = new Player();
                player.Id = i;
                game.Players.Add(player);
                IUiProxy proxy;
                proxy = new ServerNetworkUiProxy(server, i);
                proxy.TimeOutSeconds = timeOutSeconds;
                proxy.HostPlayer = player;
                game.UiProxies.Add(player, proxy);
            }
            GlobalServerUiProxy pxy = new GlobalServerUiProxy(game, game.UiProxies);
            pxy.TimeOutSeconds = timeOutSeconds;
            game.GlobalProxy = pxy;
            game.NotificationProxy = new DummyNotificationProxy();
            foreach (var g in GameEngine.Expansions.Values)
            {
                game.LoadExpansion(g);
            }
            game.GameServer = server;
            var thread = new Thread(() =>
            {
            #if !DEBUG
                try
                {
            #endif
                    game.Run(); callback(roomId);
            #if !DEBUG
                }
                catch (Exception)
                {
                }
            #endif
            }) { IsBackground = true };
            thread.Start();
        }