upsert new game instead of add

This commit is contained in:
Ben Harris 2018-01-10 02:09:36 -05:00
parent 4062594638
commit 079fa75df6
1 changed files with 14 additions and 16 deletions

View File

@ -1,7 +1,6 @@
using Discord.Commands;
using dotbot.Services;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Discord;
@ -27,10 +26,7 @@ namespace dotbot.Commands
public async Task StartGame([Summary("mention whom you would like to play with!")] IUser opponent)
{
var gameId = Context.Channel.Id;
_games.Add(gameId, new TicTacToeSession(Context.User, opponent)
{
LastMessage = Context.Message
});
_games[gameId] = new TicTacToeSession(Context.User, opponent) { LastMessage = Context.Message };
await ReplyAsync($"{_games[gameId]}");
}
@ -55,6 +51,7 @@ namespace dotbot.Commands
}
public class TicTacToeSession
{
public bool Active;
@ -67,12 +64,15 @@ namespace dotbot.Commands
public TicTacToeSession(IUser player, IUser opponent)
{
Board = new string[][] {
new string[] { ":one:", ":two:", ":three" },
new string[] { ":one:", ":two:", ":three:" },
new string[] { ":four:", ":five:", ":six:" },
new string[] { ":seven:", ":eight:", ":nine:" }
};
Players[":x:"] = player.Id;
Players[":o:"] = opponent.Id;
Players = new Dictionary<string, ulong>
{
{ ":x:", player.Id },
{ ":o:", opponent.Id }
};
Turn = ":x:";
Active = true;
Tied = false;
@ -80,6 +80,7 @@ namespace dotbot.Commands
public string GetPiece(int i) => Board[(i - 1) / 3][(i - 1) % 3];
public bool PutPiece(int i, string piece)
{
if (GetPiece(i) == ":x:" || GetPiece(i) == ":o:") return false;
@ -89,8 +90,7 @@ namespace dotbot.Commands
internal string DoMove(SocketUserMessage msg)
{
int move;
if (Int32.TryParse(msg.Content, out move))
if (Int32.TryParse(msg.Content, out var move))
if (move > 0 && move < 10)
if (!PutPiece(move, Turn))
return $"unable to place your piece. position **{move}** already occupied by {GetPiece(move)}";
@ -112,10 +112,8 @@ namespace dotbot.Commands
return $"";
}
}
else
return $"**{msg.Content}** is not a valid move. please enter a number between 1 and 9.";
else
return "your move wasn't even a number...";
else return $"**{msg.Content}** is not a valid move. please enter a number between 1 and 9.";
else return "your move wasn't even a number... try again!";
}
@ -133,7 +131,7 @@ namespace dotbot.Commands
else
{
if (Enumerable.Range(1, 9).All(i => GetPiece(i) == ":o:" || GetPiece(i) == ":x:"))
{
{ // check tie condition (all pieces placed)
Tied = true;
Active = false;
}
@ -141,7 +139,7 @@ namespace dotbot.Commands
}
}
public override string ToString() => $"{string.Join("\n", Board.Select(r => $"{string.Join(" ", r)}"))}\n<@{Turn}>'s turn";
public override string ToString() => $"{string.Join("\n", Board.Select(r => $"{string.Join(" ", r)}"))}\n<@{Players[Turn]}>'s turn. send a number 1-9.";
}
}