TerrariaMEF /LootBard/BTerrariaMod/ServerWrapper.cs

Language C# Lines 260
MD5 Hash e5bfdb731ebfff623c90e0ea907e9670 Estimated Cost $4,307 (why?)
Repository https://TerrariaMEF.svn.codeplex.com/svn View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// ------------------------------------------------------------------------------
// <copyright file="ServerWrapper.cs" company="MyCompany.com">
//  Copyright © MyCompany.com. All rights reserved.
// </copyright>
// <author>Me</author>
// ------------------------------------------------------------------------------

namespace Terraria
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using BTerrariaMod;
    using BTerrariaMod.Bards;
    using BTerrariaMod.Sugar;
    using Microsoft.Xna.Framework;

    /// <summary>
    /// Wraps the Terraria Server, providing extensibility points for modding.
    /// </summary>
    public class ServerWrapper : Terraria.Main
    {
        /// <summary>
        /// The last status message.
        /// </summary>
        private string lastStatus;

        /// <summary>
        /// The DropBard
        /// </summary>
        private DropBard dropBard;

        /// <summary>
        /// Is Wrapper initialized?
        /// </summary>
        private bool wrapperInitialized = false;

        /// <summary>
        /// The Chest bard
        /// </summary>
        private ChestBard chestBard;

        /// <summary>
        /// The World bard.
        /// </summary>
        private WorldBard worldBard;

        /// <summary>
        /// Initializes a new instance of the <see cref="ServerWrapper"/> class.
        /// </summary>
        /// <param name="args">The args.</param>
        public ServerWrapper(string[] args)
        {
            for (int i = 0; i < (int)args.Length; i++)
            {
                if (args[i].ToLower() == "-config")
                {
                    i++;
                    this.LoadDedConfig(args[i]);
                }

                if (args[i].ToLower() == "-port")
                {
                    i++;
                    try
                    {
                        int num1 = Convert.ToInt32(args[i]);
                        Netplay.serverPort = num1;
                    }
                    catch
                    {
                    }
                }

                if (args[i].ToLower() == "-players" || args[i].ToLower() == "-maxplayers")
                {
                    i++;
                    try
                    {
                        int num2 = Convert.ToInt32(args[i]);
                        SetNetPlayers(num2);
                    }
                    catch
                    {
                    }
                }

                if (args[i].ToLower() == "-pass" || args[i].ToLower() == "-password")
                {
                    i++;
                    Netplay.password = args[i];
                }

                if (args[i].ToLower() == "-world")
                {
                    i++;
                    SetWorld(args[i]);
                }

                if (args[i].ToLower() == "-worldname")
                {
                    i++;
                    SetWorldName(args[i]);
                }

                if (args[i].ToLower() == "-motd")
                {
                    i++;
                    NewMOTD(args[i]);
                }

                if (args[i].ToLower() == "-banlist")
                {
                    i++;
                    Netplay.banFile = args[i];
                }

                if (args[i].ToLower() == "-autoshutdown")
                {
                    autoShut();
                }

                if (args[i].ToLower() == "-secure")
                {
                    Netplay.spamCheck = true;
                }

                if (args[i].ToLower() == "-autocreate")
                {
                    i++;
                    string str1 = args[i];
                    autoCreate(str1);
                }

                if (args[i].ToLower() == "-loadlib")
                {
                    i++;
                    string str2 = args[i];
                    loadLib(str2);
                }
            }

            verboseNetplay = true;

            Console.WriteLine("Starting dedicated server");
            Console.WriteLine("Max recipes is " + Recipe.maxRecipes);
            this.dropBard = new BTerrariaMod.Bards.DropBard(this.AnnounceStatus);
            this.chestBard = new BTerrariaMod.Bards.ChestBard(this.AnnounceStatus);
            this.worldBard = new BTerrariaMod.Bards.WorldBard(this.AnnounceStatus);
        }

        /// <summary>
        /// Processes updates which have occurred in game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        protected override void Update(GameTime gameTime)
        {
            if (this.dropBard != null)
            {
                this.dropBard.AnnounceDrops(item);
            }

            this.SearchOpenChests();

            if (!this.wrapperInitialized && Netplay.anyClients)
            {
                this.InitialUpdate(gameTime);
                this.wrapperInitialized = true;
            }

            this.CheckCommands();
            base.Update(gameTime);
        }

        /// <summary>
        /// Announces the status.
        /// </summary>
        /// <param name="text">The text.</param>
        private void AnnounceStatus(string text)
        {
            if (text != null && text.Length > 0 && text != this.lastStatus)
            {
                Console.WriteLine("Player Names:" + this.ActivePlayers().Select(p => p.name).Aggregate((s1, s2) => s1 + "," + s2));
                Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd-hh:mm:ss") + "Broadcasting:" + text);
                Messenger.SendToAll(text);
            }
            else
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd-hh:mm:ss") + "Not Broadcasting:" + text);
            }

            this.lastStatus = text;
        }

        /// <summary>
        /// Checks the commands.
        /// </summary>
        private void CheckCommands()
        {
            var commandRequests = player.FirstOrDefault(p => p != null && p.active && p.chatText.StartsWith("/"));
            if (commandRequests == null)
            {
                return;
            }

            this.AnnounceStatus("Found command, attempting to process");
            switch (commandRequests.chatText)
            {
                case "/orbs" :
                    this.worldBard.AnnounceOrbs(Main.tile);
                    break;
                case "/spawn":
                    this.worldBard.AnnounceSpawn(Main.spawnTileX, Main.spawnTileY);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Searches the open chests.
        /// </summary>
        private void SearchOpenChests()
        {
            var openChests = this.ActivePlayers().Where(p => p.chest >= 0).Select(p => p.chest);
            foreach (var activePlayer in this.ActivePlayers().Where(p => p.chest >= 0))
            {
                this.chestBard.ChestOpened(activePlayer, chest[activePlayer.chest]);
            }
        }

        /// <summary>
        /// Processes the initial game update.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        private void InitialUpdate(GameTime gameTime)
        {
            Console.WriteLine("server netmode=" + Main.netMode);
        }

        /// <summary>
        /// Returns the active players with the player index.
        /// </summary>
        /// <returns>The active players with the player index.</returns>
        private IEnumerable<Tuple<int, Player>> ActivePlayersWithIndex()
        {
            return player.JoinIndex().Where(p => p != null && p.Item2 != null && p.Item2.active);
        }

        /// <summary>
        /// Returns the active players.
        /// </summary>
        /// <returns>The active players.</returns>
        private IEnumerable<Player> ActivePlayers()
        {
            return player.Where(p => p != null && p.active == true);
        }
    }
}
Back to Top