Skip to content

Commit b861f8d

Browse files
authored
Merge pull request #5 from KevinYeti/v2
update to V2
2 parents 41da9db + 5af46a3 commit b861f8d

12 files changed

Lines changed: 116 additions & 29 deletions

File tree

api/API_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Y坐标相同的所有单元构成一行.
3030
- 只能攻击自己领地相邻的单元.
3131
- 占领不同的单元所需要的时间不同. 简单的说, 该单元被占领时间越长, 攻击它的所需时间越短.
3232
- 所有基地被占, 判负. 同时清空所有其他被占单元.
33-
33+
- 每个玩家具有能量, 每秒自动获得当前占领单元一半数量的能量, 每次攻击单元消耗当前5%的能量. 能量初始值为10, 上限100.
3434

3535
## 测试脚本
3636
[PostMan脚本下载](/script/MagCore.postman_collection.json)

api/CreatePlayer_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Name: 新创建的玩家昵称
4444

4545
Token: 新创建的玩家安全令牌(暂保留)
4646

47-
Energy: 新创建的玩家能量(暂保留)
47+
Energy: 新创建的玩家能量
4848

4949
Color: 新创建的玩家颜色
5050

api/DataMap_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- Id: 玩家Id
1010
- Name: 玩家昵称
1111
- Token: 玩家安全令牌(暂保留)
12-
- Energy: 玩家能量(暂保留)
12+
- Energy: 玩家能量
1313
- Color: 玩家颜色
1414
- State: 玩家状态
1515
- Index: 玩家标识号

api/GetPlayer_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Name: 玩家昵称
3535

3636
Token: 玩家安全令牌(暂保留)
3737

38-
Energy: 玩家能量(暂保留)
38+
Energy: 玩家能量
3939

4040
Color: 玩家颜色
4141

src/server/MagCore.Core/ActionLogic.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal static int _calcInternal(Model.Action action, Cell target, Player sende
2727
{
2828
if (target.State == CellState.Occupied)
2929
{
30-
int time = 1000;
30+
int time = 2000;
3131
if (target.Owner != sender
3232
&& DateTime.Now > target.OccupiedTime)
3333
{
@@ -37,23 +37,23 @@ internal static int _calcInternal(Model.Action action, Cell target, Player sende
3737
{
3838
var duration = ts.Value.TotalSeconds;
3939
if (duration > 60)
40-
time = 3000;
40+
time = 5000;
4141
else if (duration > 30)
42-
time = 6000;
42+
time = 15000;
4343
else if (duration > 15)
44-
time = 9000;
44+
time = 20000;
4545
else if (duration > 5)
46-
time = 12000;
46+
time = 25000;
4747
else
48-
time = 15000;
48+
time = 30000;
4949
}
5050
}
5151

5252
//Console.WriteLine("Sleep time:" + time.ToString());
5353
return time;
5454
}
5555
else
56-
return 1000;
56+
return 2000;
5757
}
5858
}
5959
}

src/server/MagCore.Core/Game.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ public class Game
1616
private IMap _map { get; set; }
1717
public string Map => _map.Name;
1818

19+
public int ThreadNum = Server.MaxThread;
20+
1921
private GameState _state;
2022
public GameState State { get { return _state; } }
2123

2224
private ConcurrentQueue<Command> _commands = new ConcurrentQueue<Command>();
2325

2426
internal Hashtable Players { get; } = new Hashtable();
2527

26-
internal int ThreadId { get; set; }
28+
internal string _MainThreadName { get; set; }
29+
internal string _EnergyThreadName { get; set; }
2730

2831
internal DateTime CreateTime { get; set; } = DateTime.Now;
2932

@@ -51,13 +54,18 @@ public string ToJson()
5154
return string.Format(json, Id, _map.Name, (int)State, players, _map.Cells());
5255
}
5356

54-
public Game(IMap map)
57+
public Game(IMap map, int thread)
5558
{
5659
_state = GameState.Wait;
57-
_map = map.Clone();
60+
_map = map.Clone();
61+
62+
if (thread <= 0 || thread > Server.MaxThread)
63+
ThreadNum = Server.MaxThread;
64+
else
65+
ThreadNum = thread;
5866

5967
Task.Factory.StartNew(() => {
60-
ThreadId = Thread.CurrentThread.ManagedThreadId;
68+
_MainThreadName = "GameMainThread." + Thread.CurrentThread.ManagedThreadId.ToString();
6169
while (_state == GameState.Wait)
6270
{
6371
var ts = DateTime.Now - CreateTime;
@@ -103,6 +111,34 @@ public Game(IMap map)
103111
Thread.Sleep(10000);
104112
Server.RemoveGame(Id);
105113
});
114+
115+
Task.Factory.StartNew( () => {
116+
_EnergyThreadName = "GameEnergyThread." + Thread.CurrentThread.ManagedThreadId.ToString();
117+
while (true)
118+
{
119+
if (_state == GameState.Playing)
120+
{
121+
ProcessEnergy();
122+
}
123+
else if (_state >= GameState.Recycling)
124+
{
125+
break;
126+
}
127+
Thread.Sleep(1000);
128+
}
129+
});
130+
}
131+
132+
private void ProcessEnergy()
133+
{
134+
foreach (Player player in Players.Values)
135+
{
136+
if (player.State == PlayerState.Playing)
137+
{
138+
double inc = player.Cells.Count * 0.2;
139+
player.AddEnergy(inc);
140+
}
141+
}
106142
}
107143

108144
private void ProcessAttack(Command cmd, IMap map)
@@ -113,12 +149,12 @@ private void ProcessAttack(Command cmd, IMap map)
113149
var cell = map.Locate(cmd.Target);
114150
var player = Core.Players.Get(cmd.Sender);
115151

116-
if (cell != null && cell.CanAttack(player))
152+
if (cell != null && cell.CanAttack(player, ThreadNum))
117153
{
118154
time = ActionLogic.Calc(cmd.Action, cell, player);
119155
Task<bool>.Factory.StartNew(() =>
120156
{
121-
return cell.BeginChangeOwner(player, time);
157+
return cell.BeginChangeOwner(player, time, ThreadNum);
122158
}).ContinueWith((task) =>
123159
{
124160
if (task.Result)

src/server/MagCore.Core/Server.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public static class Server
1414
{
1515
private static string _path = string.Empty;
1616

17+
public static int MaxThread = 2;
18+
1719
private static Hashtable _games = new Hashtable();
1820

1921
private static Hashtable _maps = new Hashtable();
@@ -26,12 +28,12 @@ public static void Start(string path)
2628
Console.WriteLine("MagCore Server Started.");
2729
}
2830

29-
public static string NewGame(string map)
31+
public static string NewGame(string map, int thread)
3032
{
3133
map = map.Trim().ToLower();
3234
if (_maps.ContainsKey(map))
3335
{
34-
var game = new Game(_maps[map] as IMap);
36+
var game = new Game(_maps[map] as IMap, thread);
3537
_games.Add(game.Id, game);
3638

3739
return game.Id;

src/server/MagCore.Model/Cell.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,32 @@ public int OwnerIndex {
4242
public object Locker = new object();
4343

4444
public Player LastOwner { get; set; }
45-
public bool BeginChangeOwner(Player sender, int time)
45+
public bool BeginChangeOwner(Player sender, int time, int thread)
4646
{
4747
lock (this.Locker)
4848
{
49-
if (State == CellState.Flicke)
49+
if (State == CellState.Flicke
50+
|| sender.Energy < 1
51+
|| sender.ThreadLocker >= thread)
5052
return false;
5153
else
5254
{
55+
double inc = -0.1 * sender.Energy;
56+
if (sender.Energy + inc <= 0)
57+
{
58+
return false;
59+
}
60+
sender.AddEnergy(inc);
61+
5362
State = CellState.Flicke;
5463
LastOwner = Owner;
5564
Owner = sender;
5665
if (!sender.Cells.ContainsKey(Key))
5766
sender.Cells.Add(Key, this);
67+
68+
Interlocked.Increment(ref sender.ThreadLocker);
5869
Thread.Sleep(time);
70+
Interlocked.Decrement(ref sender.ThreadLocker);
5971

6072
return true;
6173
}
@@ -82,14 +94,17 @@ public bool EndChangeOwner(Player sender)
8294
LastOwner.Cells.Remove(Key);
8395
return true;
8496
}
97+
8598
}
8699
}
87100

88-
public bool CanAttack(Player player)
101+
public bool CanAttack(Player player, int thread)
89102
{
90103
if (this.State == CellState.Flicke
91104
|| this.Type == CellType.Null
92-
|| player.State == PlayerState.Defeat)
105+
|| player.State == PlayerState.Defeat
106+
|| player.Energy < 1
107+
|| player.ThreadLocker >= thread)
93108
return false;
94109
else
95110
{

src/server/MagCore.Model/Player.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,24 @@ public Player(string name, int index, PlayerColor color)
1818

1919
public string Token { get; set; } = Guid.NewGuid().ToString("N");
2020

21-
public int Energy { get; set; } = 0;
21+
public int ThreadLocker = 0;
22+
23+
private double _energy = 10;
24+
public int Energy
25+
{
26+
get {
27+
return (int)_energy;
28+
}
29+
}
30+
public void AddEnergy(double inc)
31+
{
32+
_energy += inc;
33+
if (_energy < 0)
34+
_energy = 0;
35+
else if (_energy > 100)
36+
_energy = 100;
37+
38+
}
2239

2340
public PlayerColor Color { get; set; }
2441

@@ -36,7 +53,8 @@ public Player(string name, int index, PlayerColor color)
3653
public void Reset()
3754
{
3855
State = PlayerState.Leisure;
39-
Energy = 0;
56+
_energy = 10;
57+
ThreadLocker = 0;
4058
Bases = new Dictionary<string, Cell>();
4159
Cells = new Dictionary<string, Cell>();
4260
}

src/server/MagCore.Server/Controllers/GameController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ public ContentResult Post([FromBody]dynamic json)
3838
try
3939
{
4040
var map = json.Map.ToString();
41-
var game = Core.Server.NewGame(map);
41+
int thread = 0;
42+
if (json.Thread != null)
43+
{
44+
string num = json.Thread.ToString();
45+
Int32.TryParse(num, out thread);
46+
}
47+
var game = Core.Server.NewGame(map, thread);
4248
if (!string.IsNullOrEmpty(game))
4349
return new ContentResult() { StatusCode = (int)HttpStatusCode.OK, Content = game };
4450
else

0 commit comments

Comments
 (0)