示例#1
0
 static void Main(string[] args)
 {
     Logger = LogManager.GetLogger("default");
     try
     {
         Logger.DebugFormat("Server powered up");
         Logger.DebugFormat("Loading configuration");
         ConfigManager.LoadConfigs();
         string addr = ConfigManager.GetConfig("GameServer.ListenAddress");
         string port = ConfigManager.GetConfig("GameServer.ListenPort");
         Logger.DebugFormat("Trying to listen at {0}:{1}", addr, port);
         TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse(addr), Convert.ToInt32(port)));
         listener.Start();
         while (true)
         {
             TcpClient     client  = listener.AcceptTcpClient();
             ClientServant servant = new ClientServant(client);
             Program.Logger.DebugFormat("New client connected from: {0}", servant.ClientIPEndPoint);
             servant.Start();
         }
     }
     catch (Exception e)
     {
         Logger.FatalFormat("Unhandled exception: {0}, stacktrace: {1}", e.Message, e.StackTrace);
         Logger.Fatal("Server shutdown");
     }
 }
示例#2
0
 public static void PartArena(string arena_id, ClientServant audience)
 {
     lock (Arenas)
     {
         Arena arena;
         if (Arenas.TryGetValue(arena_id, out arena))
         {
             if (arena.m_player1 == audience.ID || arena.m_player2 == audience.ID)
             {
                 foreach (KeyValuePair <string, ClientServant> kv in arena.m_present)
                 {
                     kv.Value.PostMessage(new Message(kv.Key, "part", arena_id, "arena shutdown"));
                     kv.Value.Location = "";
                 }
                 arena.m_present.Clear();
                 Arenas.Remove(arena_id);
             }
             else
             {
                 if (arena.m_present.Remove(audience.ID))
                 {
                     audience.Location = "";
                     foreach (ClientServant cs in arena.m_present.Values)
                     {
                         cs.PostMessage(new Message(audience.ID, "part", arena_id));
                     }
                 }
             }
         }
     }
 }
示例#3
0
文件: Program.cs 项目: hoxily/Wuziqi
 static void Main(string[] args)
 {
     Logger = LogManager.GetLogger("default");
     try
     {
         Logger.DebugFormat("Server powered up");
         Logger.DebugFormat("Loading configuration");
         ConfigManager.LoadConfigs();
         string addr = ConfigManager.GetConfig("GameServer.ListenAddress");
         string port = ConfigManager.GetConfig("GameServer.ListenPort");
         Logger.DebugFormat("Trying to listen at {0}:{1}", addr, port);
         TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse(addr), Convert.ToInt32(port)));
         listener.Start();
         while (true)
         {
             TcpClient client = listener.AcceptTcpClient();
             ClientServant servant = new ClientServant(client);
             Program.Logger.DebugFormat("New client connected from: {0}", servant.ClientIPEndPoint);
             servant.Start();
         }
     }
     catch (Exception e)
     {
         Logger.FatalFormat("Unhandled exception: {0}, stacktrace: {1}", e.Message, e.StackTrace);
         Logger.Fatal("Server shutdown");
     }
 }
示例#4
0
文件: Arena.cs 项目: hoxily/Wuziqi
 public static void CreateArena(string player1_id, ClientServant player1_cs, string player2_id, ClientServant player2_cs)
 {
     Arena arena = new Arena();
     arena.m_player1 = player1_id;
     arena.m_player2 = player2_id;
     arena.m_present.Add(player1_id, player1_cs);
     arena.m_present.Add(player2_id, player2_cs);
     string arena_id = player1_id + "-vs-" + player2_id;
     player1_cs.Location = arena_id;
     player2_cs.Location = arena_id;
     player1_cs.ClearMatchingState();
     player2_cs.ClearMatchingState();
     lock(Arenas)
     {
         Arenas.Add(arena_id, arena);
     }
     player1_cs.PostMessage(new Message(player1_id, "participate", arena_id, "player1"));
     player2_cs.PostMessage(new Message(player2_id, "participate", arena_id, "player2"));
 }
示例#5
0
        public static void CreateArena(string player1_id, ClientServant player1_cs, string player2_id, ClientServant player2_cs)
        {
            Arena arena = new Arena();

            arena.m_player1 = player1_id;
            arena.m_player2 = player2_id;
            arena.m_present.Add(player1_id, player1_cs);
            arena.m_present.Add(player2_id, player2_cs);
            string arena_id = player1_id + "-vs-" + player2_id;

            player1_cs.Location = arena_id;
            player2_cs.Location = arena_id;
            player1_cs.ClearMatchingState();
            player2_cs.ClearMatchingState();
            lock (Arenas)
            {
                Arenas.Add(arena_id, arena);
            }
            player1_cs.PostMessage(new Message(player1_id, "participate", arena_id, "player1"));
            player2_cs.PostMessage(new Message(player2_id, "participate", arena_id, "player2"));
        }
示例#6
0
        public static bool JoinArena(string arena_id, ClientServant audience)
        {
            bool success = true;

            lock (Arenas)
            {
                if (Arenas.ContainsKey(arena_id))
                {
                    foreach (ClientServant cs in Arenas[arena_id].m_present.Values)
                    {
                        cs.PostMessage(new Message(audience.ID, "join", arena_id));
                    }
                    Arenas[arena_id].m_present.Add(audience.ID, audience);
                    audience.Location = arena_id;
                }
                else
                {
                    success = false;
                }
            }
            return(success);
        }
示例#7
0
文件: Arena.cs 项目: hoxily/Wuziqi
 public static bool JoinArena(string arena_id, ClientServant audience)
 {
     bool success = true;
     lock (Arenas)
     {
         if (Arenas.ContainsKey(arena_id))
         {
             foreach(ClientServant cs in Arenas[arena_id].m_present.Values)
             {
                 cs.PostMessage(new Message(audience.ID, "join", arena_id));
             }
             Arenas[arena_id].m_present.Add(audience.ID, audience);
             audience.Location = arena_id;
         }
         else
         {
             success = false;
         }
     }
     return success;
 }
示例#8
0
文件: Arena.cs 项目: hoxily/Wuziqi
 public static void PartArena(string arena_id, ClientServant audience)
 {
     lock (Arenas)
     {
         Arena arena;
         if (Arenas.TryGetValue(arena_id, out arena))
         {
             if (arena.m_player1 == audience.ID || arena.m_player2 == audience.ID)
             {
                 foreach(KeyValuePair<string, ClientServant> kv in arena.m_present)
                 {
                     kv.Value.PostMessage(new Message(kv.Key, "part", arena_id, "arena shutdown"));
                     kv.Value.Location = "";
                 }
                 arena.m_present.Clear();
                 Arenas.Remove(arena_id);
             }
             else
             {
                 if (arena.m_present.Remove(audience.ID))
                 {
                     audience.Location = "";
                     foreach (ClientServant cs in arena.m_present.Values)
                     {
                         cs.PostMessage(new Message(audience.ID, "part", arena_id));
                     }
                 }
             }
         }
     }
 }
示例#9
0
        private void ProcessMessage(Message msg)
        {
            switch (msg.Command)
            {
            case "verify":     // 版本验证
                if (m_state == ClientState.Initial && msg.Parameters.Length == 1 && ConfigManager.GetConfig("GameServer.ProtocolVersion") == msg.Parameters[0])
                {
                    m_state = ClientState.Verified;
                    Message response = new Message(null, "verify", "yes");
                    PostMessage(response);
                }
                else
                {
                    PostMessage(new Message(null, "verify", "no"));
                }
                break;

            case "login":     // 登录验证
                if (m_state != ClientState.Verified)
                {
                    break;
                }
                if (m_state == ClientState.Logined)
                {
                    break;
                }
                if (msg.Parameters.Length == 1)
                {
                    lock (ClientServant.m_onlineClients)
                    {
                        if (ClientServant.m_onlineClients.ContainsKey(msg.Parameters[0]))
                        {
                            PostMessage(new Message(null, "login", "no", "id in use"));
                        }
                        else
                        {
                            m_id = msg.Parameters[0];
                            ClientServant.m_onlineClients.Add(m_id, this);
                            ClientServant.BroadcastMessage(new Message(m_id, "login", "yes"));
                            m_state = ClientState.Logined;
                        }
                    }
                }
                break;

            case "speak":     // 世界范围喊话
                if (m_state != ClientState.Logined && m_state != ClientState.Matching)
                {
                    PostMessage(new Message(null, "speak", "no", "need logined"));
                }
                else
                {
                    if (msg.Parameters.Length == 1)
                    {
                        BroadcastMessage(new Message(m_id, "speak", msg.Parameters[0]));
                    }
                }
                break;

            case "whisper":     // 私聊密语
                if (m_state != ClientState.Logined && m_state != ClientState.Matching)
                {
                    PostMessage(new Message(null, "whisper", "no", "need logined"));
                }
                else
                {
                    if (msg.Parameters.Length == 2)
                    {
                        string        targetId = msg.Parameters[0];
                        ClientServant cs;
                        lock (ClientServant.m_onlineClients)
                        {
                            if (ClientServant.m_onlineClients.TryGetValue(targetId, out cs))
                            {
                                cs.PostMessage(new Message(m_id, "whisper", msg.Parameters[1]));
                            }
                            else
                            {
                                PostMessage(new Message(null, "whisper", "no", "user id not exist"));
                            }
                        }
                    }
                }
                break;

            case "match":     // 自动匹配
                if (m_state != ClientState.Logined)
                {
                    break;
                }
                if (string.IsNullOrEmpty(Location))
                {
                    // 注意两条语句顺序,因为执行AddMatchRequest方法有可能因为匹配上而清除ClientState.Matching状态
                    m_state = ClientState.Matching;
                    Arena.AddMatchRequest(new KeyValuePair <string, ClientServant>(ID, this));
                }
                break;

            case "join":     // 观众加入竞技场观战
                if (m_state != ClientState.Logined)
                {
                    break;
                }
                if (string.IsNullOrEmpty(Location))
                {
                    if (Arena.JoinArena(msg.Parameters[0], this))
                    {
                        PostMessage(new Message(ID, "join", msg.Parameters[0]));
                    }
                }
                break;

            case "part":     // 观众或者参战者离开竞技场
                if (m_state != ClientState.Logined)
                {
                    break;
                }
                if (string.IsNullOrEmpty(Location))
                {
                    break;
                }
                Arena.PartArena(msg.Parameters[0], this);
                break;

            case "list":     // 当前竞技场列表
                if (m_state == ClientState.Logined && string.IsNullOrEmpty(Location))
                {
                    Message response = new Message(null, "list", Arena.GetArenaList().ToArray());
                    PostMessage(response);
                }
                break;

            case "discuss":     // 在竞技场内的讨论
                if (m_state == ClientState.Logined && !string.IsNullOrEmpty(Location) && msg.Parameters.Length == 1)
                {
                    Arena.Discuss(Location, ID, msg.Parameters[0]);
                }
                break;

            case "layat":     // 放置棋子
                if (m_state == ClientState.Logined && !string.IsNullOrEmpty(Location) && msg.Parameters.Length == 2)
                {
                    int x = Convert.ToInt32(msg.Parameters[0]);
                    int y = Convert.ToInt32(msg.Parameters[1]);
                    Arena.LayAt(Location, ID, x, y);
                }
                break;
            }
        }