Repository navigation
Expand file tree
/
Copy pathPlayer.cs
More file actions
158 lines (152 loc) · 5.32 KB
/
Copy pathPlayer.cs
File metadata and controls
158 lines (152 loc) · 5.32 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TShockAPI;
using System.Threading;
using ChatAssistant;
namespace Factions
{
public class Player
{
public int ID;
public int UserID;
public Faction Faction;
public TSPlayer TSplayer;
public int Power;
public bool tempOutline = false;
public Menu Menu = null;
public bool changingMoney = false;
public Thread UpdateThread = null;
public byte LastState = 0;
public byte IdleCount = 0;
public Player(int id, int power, Faction faction) : this(id, power, faction, -1) { }
public Player(int id, int power, Faction faction, int userID)
{
this.ID = id;
this.UserID = userID;
if (id >= 0 && id < TShock.Players.Length)
{
this.TSplayer = TShock.Players[id];
if (this.TSplayer != null)
this.UserID = this.TSplayer.UserID;
}
this.Faction = faction;
this.Power = power;
}
public bool ChangePower(int change)
{
if (change == 0)
return true;
int newpower = this.Power + change;
if (newpower < -100 || newpower > 100)
return false;
this.Power = newpower;
Factions.DoQuery("UPDATE factions_Players SET Power = @0 WHERE UserID = @1 AND WorldID = @2", newpower, this.UserID, Terraria.Main.worldID);
if (this.Faction != null)
this.Faction.RefreshPower();
this.TSplayer.SendData(PacketTypes.Status, String.Format("Power: {0}\n", this.Power), -1);
return true;
}
public void CloseMenu()
{
this.ClearOutline();
if (this.Menu != null)
this.Menu.Close();
}
public void ClearOutline()
{
if (this.tempOutline)
{
this.tempOutline = false;
this.TSplayer.SendTileSquare(this.TSplayer.TileX, this.TSplayer.TileY, 100);
}
}
public void StartUpdating()
{
try
{
if (this.UpdateThread == null || !this.UpdateThread.IsAlive)
{
var updater = new Updater(this.ID);
this.UpdateThread = new Thread(updater.UpdatePower);
this.UpdateThread.Start();
this.TSplayer.SendData(PacketTypes.Status, String.Format("Power: {0}\n", this.Power), -1);
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
}
public void StopUpdating()
{
try
{
if (this.UpdateThread != null)
this.UpdateThread.Abort();
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
}
// --------------------------------------- STATIC METHODS ---------------------------------------------------
public static Player GetPlayerByUserID(int userid)
{
try
{
for (int i = 0; i < Factions.PlayerList.Length; i++)
{
if (Factions.PlayerList[i] != null && Factions.PlayerList[i].UserID == userid)
return Factions.PlayerList[i];
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
return null;
}
public static Player GetPlayerByName(string name)
{
try
{
for (int i = 0; i < Factions.PlayerList.Length; i++)
{
if (Factions.PlayerList[i] != null && Factions.PlayerList[i].TSplayer.Name == name)
return Factions.PlayerList[i];
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
return null;
}
// -------------------------------- UPDATER ----------------------------------------------------------
private class Updater
{
int who;
byte PowerCount = 0;
public Updater(int who)
{
this.who = who;
}
public void UpdatePower()
{
while (Thread.CurrentThread.IsAlive)
{
var player = Factions.PlayerList[who];
if (player != null)
{
try
{
if (player.IdleCount < 3)
{
player.IdleCount++;
if (this.PowerCount == 10)
player.ChangePower(3);
this.PowerCount++;
if (this.PowerCount > 10)
this.PowerCount = 1;
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
Thread.Sleep(60000);
}
else
Thread.CurrentThread.Abort();
}
}
}
}
}