Skip to content

Commit 57fbf5e

Browse files
committed
update server to v1.0.1
1 parent d1f4476 commit 57fbf5e

3 files changed

Lines changed: 91 additions & 63 deletions

File tree

src/sdk/JustRush/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ static void Main(string[] args)
1515
{
1616
string input = string.Empty;
1717

18-
//ServerHelper.Initialize("http://106.75.33.221:6000/");
19-
ServerHelper.Initialize("http://localhost:6000/");
18+
ServerHelper.Initialize("http://106.75.33.221:6000/");
19+
//ServerHelper.Initialize("http://localhost:6000/");
2020

2121
map = MapHelper.GetMap("RectSmall");
2222
game = new Game(map.Rows.Count, map.Rows[0].Count);
@@ -155,6 +155,7 @@ static void RushAttack()
155155
else if (game.State > 1)
156156
{
157157
Console.WriteLine("Game over");
158+
158159
break;
159160
}
160161

src/server/MagCore.Core/ActionLogic.cs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,49 @@ public static int Calc(Model.Action action, Cell target, Player sender)
1111
{
1212
if (target == null || sender == null)
1313
{
14-
throw new ArgumentNullException();
14+
return Int32.MinValue;
1515
}
1616
switch (action)
1717
{
1818
case Model.Action.Attack:
19-
if (target.State == CellState.Empty)
20-
return 1000;
21-
else if (target.State == CellState.Occupied)
22-
{
23-
int time = 1000;
24-
if (target.Owner != sender
25-
&& DateTime.Now > target.OccupiedTime)
26-
{
27-
//attack other
28-
var duration = (DateTime.Now - target.OccupiedTime).Value.TotalSeconds;
29-
if (duration > 60)
30-
time = 3000;
31-
else if (duration > 30)
32-
time = 6000;
33-
else if (duration > 15)
34-
time = 9000;
35-
else if (duration > 5)
36-
time = 12000;
37-
else
38-
time = 15000;
39-
}
40-
41-
//Console.WriteLine("Sleep time:" + time.ToString());
42-
return time;
43-
}
44-
else
45-
return 0;
19+
return _calcInternal(action, target, sender);
4620
default:
4721
return 0;
4822
}
4923

5024
}
25+
26+
internal static int _calcInternal(Model.Action action, Cell target, Player sender)
27+
{
28+
if (target.State == CellState.Occupied)
29+
{
30+
int time = 1000;
31+
if (target.Owner != sender
32+
&& DateTime.Now > target.OccupiedTime)
33+
{
34+
//attack other
35+
var ts = DateTime.Now - target.OccupiedTime;
36+
if (ts != null && ts.HasValue)
37+
{
38+
var duration = ts.Value.TotalSeconds;
39+
if (duration > 60)
40+
time = 3000;
41+
else if (duration > 30)
42+
time = 6000;
43+
else if (duration > 15)
44+
time = 9000;
45+
else if (duration > 5)
46+
time = 12000;
47+
else
48+
time = 15000;
49+
}
50+
}
51+
52+
//Console.WriteLine("Sleep time:" + time.ToString());
53+
return time;
54+
}
55+
else
56+
return 1000;
57+
}
5158
}
5259
}

src/server/MagCore.Core/Game.cs

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,34 @@ public Game(IMap map)
6969
else
7070
Thread.Sleep(1000);
7171
}
72-
while (_state != GameState.Done)
72+
try
7373
{
74-
if (_commands.IsEmpty || _state != GameState.Playing)
74+
while (_state != GameState.Done)
7575
{
76-
Thread.Sleep(100);
77-
continue;
78-
}
79-
else if (_commands.TryDequeue(out var cmd))
80-
{
81-
switch (cmd.Action)
76+
if (_commands.IsEmpty || _state != GameState.Playing)
8277
{
83-
case Model.Action.Attack:
84-
ProcessAttack(cmd, _map);
85-
break;
86-
default:
87-
break;
78+
Thread.Sleep(100);
79+
continue;
80+
}
81+
else if (_commands.TryDequeue(out var cmd))
82+
{
83+
switch (cmd.Action)
84+
{
85+
case Model.Action.Attack:
86+
ProcessAttack(cmd, _map);
87+
break;
88+
default:
89+
break;
90+
}
8891
}
89-
}
9092

91-
ProcessVictory();
93+
ProcessVictory();
94+
}
95+
}
96+
catch (Exception ex)
97+
{
98+
Console.WriteLine(ex.Message);
99+
Console.WriteLine(ex.StackTrace);
92100
}
93101
}).ContinueWith((task) => {
94102
//Recycling
@@ -100,33 +108,45 @@ public Game(IMap map)
100108

101109
private void ProcessAttack(Command cmd, IMap map)
102110
{
103-
var cell = map.Locate(cmd.Target);
104-
var player = Core.Players.Get(cmd.Sender);
105-
int time = ActionLogic.Calc(cmd.Action, cell, player);
106-
107-
if (cell.CanAttack(player))
111+
int time = 0;
112+
try
108113
{
109-
Task<bool>.Factory.StartNew(() =>
110-
{
111-
return cell.BeginChangeOwner(player, time);
112-
}).ContinueWith((task) =>
114+
var cell = map.Locate(cmd.Target);
115+
var player = Core.Players.Get(cmd.Sender);
116+
117+
if (cell != null && cell.CanAttack(player))
113118
{
114-
if (task.Result)
115-
return cell.EndChangeOwner(player);
116-
else
117-
return false;
118-
}).ContinueWith((task) => {
119-
if (task.Result)
120-
ProcessAttackResult(player, cell);
119+
time = ActionLogic.Calc(cmd.Action, cell, player);
120+
Task<bool>.Factory.StartNew(() =>
121+
{
122+
return cell.BeginChangeOwner(player, time);
123+
}).ContinueWith((task) =>
124+
{
125+
if (task.Result)
126+
return cell.EndChangeOwner(player);
127+
else
128+
return false;
129+
}).ContinueWith((task) => {
130+
if (task.Result)
131+
ProcessAttackResult(player, cell);
121132

122-
ClearLostCells();
123-
});
133+
ClearLostCells();
134+
});
135+
}
136+
}
137+
catch (Exception ex)
138+
{
139+
Console.WriteLine("cell pos:" + cmd.Target.ToString());
140+
Console.WriteLine("sender:" + cmd.Sender);
141+
Console.WriteLine("calced time:" + time.ToString());
142+
throw ex;
124143
}
125144
}
126145

127146
private void ProcessVictory()
128147
{
129148
int iCount = 0;
149+
130150
foreach (Player player in Players.Values)
131151
{
132152
if (player.State == PlayerState.Playing)

0 commit comments

Comments
 (0)