View Single Post
  #23  
Old 07-07-2012, 01:00 PM
podvoxx podvoxx is offline
Approved Member
 
Join Date: Feb 2010
Posts: 190
Default

Yes, it work Big thanks, Kodiak!
I modify your and my code and get this:

Code:
using System;
using System.Collections.Generic;
using maddox.game;
using maddox.game.world;
using System.Xml;

namespace smpmessage
{
    // ====================================================
    // (based on Small_Bee and FG_Kodiak code)
    // ==================================================== 
    public sealed class Translate
    {
        #region Parameters
        private static readonly Lazy<Translate> Lazy = new Lazy<Translate>(() => new Translate());
        private static readonly Dictionary<string, Dictionary<string, Dictionary<string, string>>> _stringPool = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
        static readonly IGamePlay GamePlay = Strategy.THIS.GamePlay;

        const string XmlLanguagenode = "SMP/LanguageSection";
        private const string DefaultLanguage = "en";
        public static string setupServerLanguage = "off";
        #endregion

        #region Read File
        public static Translate GetInstance
        {
            get { return Lazy.Value; }
        }


        private Translate()
        {
        }

        public static Translate UseConfigFile(string fileName)
        {
            if (_stringPool.Count == 0)
                LoadSettings(fileName);
            return Lazy.Value;
        }

        private static XmlDocument LoadXMLFile(string file)
        {
            // Load the xml-file in a xml-document
            XmlDocument xDoc = new XmlDocument();

            try
            {
                xDoc.Load(file);
            }
            catch (System.IO.FileNotFoundException)
            {
                throw new Exception("Xml-File " + file + " not found");
            }
            catch (Exception ex)
            {
                throw new Exception("Error loading " + file + ": " + ex.Message);
            }

            return xDoc;
        }

        private static void LoadSettings(string languageFile)
        {
            XmlDocument xDoc = LoadXMLFile(languageFile);

            var xmlNodeList = xDoc.SelectNodes(XmlLanguagenode);

            if (xmlNodeList != null)
                foreach (XmlNode sectionNode in xmlNodeList)
                {

                    var strs = new Dictionary<string, Dictionary<string, string>>();

                    foreach (XmlNode textNode in sectionNode.ChildNodes)
                    {

                        var texts = new Dictionary<string, string>();

                        foreach (XmlNode txt in textNode.ChildNodes)
                        {
                            texts.Add(txt.Name, txt.InnerText);
                        }

                        if (textNode.Attributes != null) strs.Add(textNode.Attributes["key"].Value, texts);
                    }

                    if (sectionNode.Attributes != null) _stringPool.Add(sectionNode.Attributes["name"].Value, strs);
                }

        }

        public string GetString(string section, string key, string language)
        {
            if (_stringPool.Count > 0)
            {
                Dictionary<string, Dictionary<string, string>> strs;

                if (_stringPool.TryGetValue(section, out strs))
                {
                    Dictionary<string, string> texts;

                    if (strs.TryGetValue(key, out texts))
                    {
                        string text;
                        if (setupServerLanguage == "off")
                        {
                            if (texts.TryGetValue(language, out text))
                                return text;
                            if (texts.TryGetValue(DefaultLanguage, out text))
                                return text;
                        }
                        else
                        {
                            if (texts.TryGetValue(setupServerLanguage, out text))
                                return text;
                            if (texts.TryGetValue(DefaultLanguage, out text))
                                return text;
                        }
                    }
                }
            }
            return key; // if nothing is found return a empty string -> modify if a error message is needed. 
        }
        
        public string GetString(string section, string key)
        {
            return GetString(section, key, DefaultLanguage);
        }
        
        #endregion

        #region Send Translate Message to List
        public void ToAll(string section, string key, string target, params object[] args)
        {
            switch (target)
            {
                case "ChatAndScreen":
                    {
                        Send(SendMessage.GetPlayerListChatAll(), section, key, "Chat", args);
                        Send(SendMessage.GetPlayerListScreenAll(), section, key, "Screen", args);
                    } break;
                case "Chat":
                    {
                        Send(SendMessage.GetPlayerListChatAll(), section, key, target, target, args);
                        break;
                    }
                case "Screen":
                    {
                        Send(SendMessage.GetPlayerListScreenAll(), section, key, target, target, args);
                    }
                    break;
            }
        }

        public void ToArmy(string section, string key, int army, string target, params object[] args)
        {
            Send(SendMessage.GetPlayerListArmy(army), section, key, target, args);
        }

        public void ToPlayers(string section, string key, Player[] players, string target, params object[] args)
        {
            Send(players, section, key, target, args);
        }
        #endregion

        #region Send
        private void Send(Player[] playerList, string section, string key, string target, params object[] args)
        {
            Dictionary<string, List<Player>> playerLanguageDict = new Dictionary<string, List<Player>>();

            foreach (Player player in playerList)
            {
                string languageName = player.LanguageName();
                if (!playerLanguageDict.ContainsKey(languageName))
                {
                    playerLanguageDict.Add(languageName, new List<Player>());
                }
                playerLanguageDict[languageName].Add(player);
            }

            if (playerList.Length > 0)
            {
                foreach (KeyValuePair<string, List<Player>> kvp in playerLanguageDict)
                {
                    SendMessage.Send(kvp.Value.ToArray(), GetString(section, key, kvp.Key.ToString()), target, args);
                }
            }
        }
        #endregion
    }

    public static class SendMessage
    {
        #region Parameters
        static readonly IGamePlay GamePlay = Strategy.THIS.GamePlay;
        public static string hudON = "off";   
        #endregion      

        #region Send Message to List
        public static void ToAll(string message, string target, params object[] args)
        {
            switch (target)
            {
                case "ChatAndScreen":
                    {
                        Send(GetPlayerListChatAll(), message, "Chat", args);
                        Send(GetPlayerListScreenAll(), message, "Screen", args);
                    } break;
                case "Chat":
                    {
                        Send(GetPlayerListChatAll(), message, target, args);                        
                        break;
                    }
                case "Screen": 
                    {
                        Send(GetPlayerListScreenAll(), message, target, args);                        
                    }
                    break;
            }
        }

        public static void ToArmy(string message, int army, string target, params object[] args)
        {
            Send(GetPlayerListArmy(army), message, target, args);
        }

        public static void ToPlayers(string message, Player[] players, string target, params object[] args)
        {
            Send(players, message, target, args);
        }
        #endregion

        #region Send
        public static void Send(Player[] playerList, string message, string target, params object[] args)
        {            
            if (playerList.Length > 0)
            {
                switch (target)
                {
                    case "ChatAndScreen":
                    {
                        GamePlay.gpLogServer(playerList, message, args);
                        GamePlay.gpHUDLogCenter(playerList, message, args);                       
                    } break;
                    case "Chat":
                    {
                        GamePlay.gpLogServer(playerList, message, args);
                        break;
                    }
                    case "Screen":
                    {
                        GamePlay.gpHUDLogCenter(playerList, message, args);
                        if (hudON == "on")
                        {
                            GamePlay.gpLogServer(playerList, "[HUD]: " + message, args);
                        }
                        break;
                    }
                }
            }

        }
        #endregion

        #region GetPlayerList
        public static Player[] GetPlayerListScreenAll()
        {
            List<Player> players = new List<Player>();
            
            if (GamePlay.gpPlayer() != null)
                if (GamePlay.gpPlayer().Name().ToString() != "Server")
                {
                    players.Add(GamePlay.gpPlayer());
                }
            if (GamePlay.gpRemotePlayers() != null)
                players.AddRange(GamePlay.gpRemotePlayers());
            return players.ToArray();
        }

        public static Player[] GetPlayerListChatAll()
        {
            List<Player> players = new List<Player>();
            
            if (GamePlay.gpPlayer() != null)
                players.Add(GamePlay.gpPlayer());
            if (GamePlay.gpRemotePlayers() != null)
                players.AddRange(GamePlay.gpRemotePlayers());
            return players.ToArray();
        }

        public static Player[] GetPlayerListArmy(int army)
        {
            List<Player> players = new List<Player>();
            List<Player> acceptedPlayers = new List<Player>();
            if (GamePlay.gpPlayer() != null)
                players.Add(GamePlay.gpPlayer());
            if (GamePlay.gpRemotePlayers() != null)
                players.AddRange(GamePlay.gpRemotePlayers());

            if (players != null)
            {
                foreach (Player player in players)
                {
                    if (player.Army() == army) acceptedPlayers.Add(player);
                }               
            }
            return acceptedPlayers.ToArray();
        }
        #endregion
    }
}

Last edited by podvoxx; 07-07-2012 at 01:04 PM.
Reply With Quote