Skip to content

Commit 7c2240e

Browse files
committed
Began fleshing out data access. Added script of current version of database
1 parent 3a2131a commit 7c2240e

File tree

4 files changed

+135
-18
lines changed

4 files changed

+135
-18
lines changed

Database.sql

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Script Date: 10/5/2019 6:26 PM - ErikEJ.SqlCeScripting version 3.5.2.81
2+
-- Database information:
3+
-- Database: C:\ProgramData\PostmanDeliversData\GameDB.sqlite3
4+
-- ServerVersion: 3.27.2
5+
-- DatabaseSize: 16 KB
6+
-- Created: 10/5/2019 4:02 PM
7+
8+
-- User Table information:
9+
-- Number of tables: 2
10+
-- Games: -1 row(s)
11+
-- PlayerMoves: -1 row(s)
12+
13+
SELECT 1;
14+
PRAGMA foreign_keys=OFF;
15+
BEGIN TRANSACTION;
16+
CREATE TABLE [PlayerMoves] (
17+
[Id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
18+
, [GameID] bigint NOT NULL
19+
, [Square] bigint NOT NULL
20+
, [GameBoardAfter] text NOT NULL
21+
, [GameBoardBefore] text NOT NULL
22+
, [Timestamp] bigint NOT NULL
23+
, [Player] bigint NOT NULL
24+
);
25+
CREATE TABLE [Games] (
26+
[Id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
27+
, [PlayerXName] text NOT NULL
28+
, [PlayerOName] text NOT NULL
29+
, [PlayerXID] text NOT NULL
30+
, [PlayerOID] text NOT NULL
31+
, [GameState] bigint NOT NULL
32+
, [Winner] bigint NOT NULL
33+
);
34+
COMMIT;
35+

PostmanDelivers.sln

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.28922.388
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TikTakToe.Engine", "TikTakToe.Engine\TikTakToe.Engine.csproj", "{B518E9EB-D9B5-460A-BB8F-07F2849F2612}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TikTakToe.Engine", "TikTakToe.Engine\TikTakToe.Engine.csproj", "{B518E9EB-D9B5-460A-BB8F-07F2849F2612}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TikTakToe.Engine.Tests", "TikTakToe.Engine.Tests\TikTakToe.Engine.Tests.csproj", "{32A01C4D-654D-4A4E-A579-FDA9422069AC}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TikTakToe.Engine.Tests", "TikTakToe.Engine.Tests\TikTakToe.Engine.Tests.csproj", "{32A01C4D-654D-4A4E-A579-FDA9422069AC}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C2E67A42-FE57-4B29-831D-376F569A87E8}"
11+
ProjectSection(SolutionItems) = preProject
12+
Database.sql = Database.sql
13+
EndProjectSection
914
EndProject
1015
Global
1116
GlobalSection(SolutionConfigurationPlatforms) = preSolution

TikTakToe.Engine/Game.cs

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace TikTakToe.Engine
1010
{
1111
public class Game
1212
{
13-
public Game GenerateNewGame(string playerXName, string playerOName)
13+
public Game GenerateNewGame(string playerXName, string playerOName, string connection)
1414
{
1515
PlayerXName = playerXName;
1616
PlayerOName = playerOName;
@@ -20,7 +20,7 @@ public Game GenerateNewGame(string playerXName, string playerOName)
2020
Board = new GameBoard();
2121
Moves = new List<PlayerMove>();
2222

23-
this.Id = NewGame();
23+
this.Id = NewGame(connection);
2424
return this;
2525
}
2626

@@ -68,24 +68,105 @@ public GameStates GameState
6868
}
6969
}
7070

71-
public Game StartNew()
71+
public Game GetGameByID(int gameID, string connection)
7272
{
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+
}
7483
}
7584

76-
public Game GetGameByID(int gameID)
85+
public MoveResult Move(string xoro, int whichSquare, string playerID, int gameID, string connection)
7786
{
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+
}
79136
}
80137

81-
public void Move(Players player, int whichSquare)
138+
private Players ConvertXorOToPlayer(string xoro)
82139
{
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+
}
83150

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+
}
84165
}
85166

86-
private int NewGame()
167+
private int NewGame(string connection)
87168
{
88-
using (var conn = new SQLiteConnection("Data Source=C:\\ProgramData\\PostmanDeliversData\\GameDB.sqlite3;Version=3;"))
169+
using (var conn = new SQLiteConnection(connection))
89170
{
90171
conn.Open();
91172
int gameId = conn.Query<int>(
@@ -95,7 +176,7 @@ private int NewGame()
95176
select last_insert_rowid();", this
96177
).First();
97178

98-
return gameId;
179+
return gameId;
99180
}
100181
}
101182

TikTakToe.Engine/PlayerMove.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace TikTakToe.Engine
1+
namespace TikTakToe.Engine
62
{
73
public class PlayerMove
84
{
95
public int GameID { get; set; }
106
public int Square { get; set; }
117
public string GameboardBefore { get; set; }
128
public string GameboardAfter { get; set; }
13-
public DateTime? Timestamp { get; set; }
9+
public long Timestamp { get; set; }
1410
public Players Player { get; set; }
1511
}
1612
}

0 commit comments

Comments
 (0)