@@ -10,7 +10,7 @@ namespace TikTakToe.Engine
10
10
{
11
11
public class Game
12
12
{
13
- public Game GenerateNewGame ( string playerXName , string playerOName )
13
+ public Game GenerateNewGame ( string playerXName , string playerOName , string connection )
14
14
{
15
15
PlayerXName = playerXName ;
16
16
PlayerOName = playerOName ;
@@ -20,7 +20,7 @@ public Game GenerateNewGame(string playerXName, string playerOName)
20
20
Board = new GameBoard ( ) ;
21
21
Moves = new List < PlayerMove > ( ) ;
22
22
23
- this . Id = NewGame ( ) ;
23
+ this . Id = NewGame ( connection ) ;
24
24
return this ;
25
25
}
26
26
@@ -68,24 +68,105 @@ public GameStates GameState
68
68
}
69
69
}
70
70
71
- public Game StartNew ( )
71
+ public Game GetGameByID ( int gameID , string connection )
72
72
{
73
- throw new NotImplementedException ( ) ;
73
+ using ( var conn = new SQLiteConnection ( connection ) )
74
+ {
75
+ conn . Open ( ) ;
76
+ Game gameId = conn . Query < Game > (
77
+ @"SELECT Id, PlayerXName, PlayerOName, PlayerXID, PlayerOID, GameState, Winner
78
+ FROM Games WHERE Id = @Id" , new { @Id = gameID }
79
+ ) . SingleOrDefault ( ) ;
80
+
81
+ return gameId ;
82
+ }
74
83
}
75
84
76
- public Game GetGameByID ( int gameID )
85
+ public MoveResult Move ( string xoro , int whichSquare , string playerID , int gameID , string connection )
77
86
{
78
- throw new NotImplementedException ( ) ;
87
+ Players whichPlayer = ConvertXorOToPlayer ( xoro ) ;
88
+ Game theGame = GetGameByID ( gameID , connection ) ;
89
+
90
+ if ( theGame == null )
91
+ {
92
+ throw new ArgumentException ( "Bad game id" ) ;
93
+ }
94
+
95
+ switch ( whichPlayer )
96
+ {
97
+ case Players . None :
98
+ throw new ArgumentException ( "Bad player" ) ;
99
+ case Players . X :
100
+ if ( theGame . PlayerXID != playerID )
101
+ {
102
+ throw new ArgumentException ( "Bad player id" ) ;
103
+ }
104
+ break ;
105
+ case Players . O :
106
+ if ( theGame . PlayerOID != playerID )
107
+ {
108
+ throw new ArgumentException ( "Bad player id" ) ;
109
+ }
110
+ break ;
111
+ default :
112
+ break ;
113
+ }
114
+
115
+ string currentBoard = Board . RenderBoard ( ) ;
116
+ MoveResult mr = Board . ExecuteTurn ( xoro , whichSquare ) ;
117
+
118
+ if ( mr . IsValid )
119
+ {
120
+ PlayerMove playerMove = new PlayerMove ( ) ;
121
+ playerMove . GameboardBefore = currentBoard ;
122
+ playerMove . GameID = this . Id ;
123
+ playerMove . GameboardAfter = Board . RenderBoard ( ) ;
124
+ playerMove . Square = whichSquare ;
125
+ playerMove . Timestamp = DateTimeOffset . UtcNow . ToUnixTimeSeconds ( ) ;
126
+ playerMove . Player = whichPlayer ;
127
+
128
+ InsertMove ( playerMove , connection ) ;
129
+
130
+ return mr ;
131
+ }
132
+ else
133
+ {
134
+ throw new InvalidOperationException ( $ "Illegal Move. Reason: { mr . Reason } , Game Board:{ mr . GameBoard } ") ;
135
+ }
79
136
}
80
137
81
- public void Move ( Players player , int whichSquare )
138
+ private Players ConvertXorOToPlayer ( string xoro )
82
139
{
140
+ switch ( xoro )
141
+ {
142
+ case "X" :
143
+ return Players . X ;
144
+ case "O" :
145
+ return Players . O ;
146
+ default :
147
+ return Players . None ;
148
+ }
149
+ }
83
150
151
+ private int InsertMove ( PlayerMove playerMove , string connection )
152
+ {
153
+ using ( var conn = new SQLiteConnection ( connection ) )
154
+ {
155
+ conn . Open ( ) ;
156
+ int moveId = conn . Query < int > (
157
+ @"INSERT INTO PlayerMoves
158
+ (GameID, Square, GameBoardAfter, GameBoardBefore, Timestamp, Player)
159
+ VALUES (@GameID, @Square, @GameBoardAfter, @GameBoardBefore, @Timestamp, @Player);
160
+ select last_insert_rowid();" , playerMove
161
+ ) . First ( ) ;
162
+
163
+ return moveId ;
164
+ }
84
165
}
85
166
86
- private int NewGame ( )
167
+ private int NewGame ( string connection )
87
168
{
88
- using ( var conn = new SQLiteConnection ( "Data Source=C: \\ ProgramData \\ PostmanDeliversData \\ GameDB.sqlite3;Version=3;" ) )
169
+ using ( var conn = new SQLiteConnection ( connection ) )
89
170
{
90
171
conn . Open ( ) ;
91
172
int gameId = conn . Query < int > (
@@ -95,7 +176,7 @@ private int NewGame()
95
176
select last_insert_rowid();" , this
96
177
) . First ( ) ;
97
178
98
- return gameId ;
179
+ return gameId ;
99
180
}
100
181
}
101
182
0 commit comments