This commit is contained in:
Mootfrost777 2022-04-23 22:26:39 +03:00
parent e607085e72
commit d995c5f2b7
27 changed files with 110 additions and 27 deletions

Binary file not shown.

View file

@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bowling_Server", "Bowling_S
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetLib", "BowlingNetLib\NetLib.csproj", "{8F4FCE29-064E-45E4-AF43-87F31420EF2F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetLib", "BowlingNetLib\NetLib.csproj", "{8F4FCE29-064E-45E4-AF43-87F31420EF2F}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{A3F442D5-C149-401F-BC5C-9BADED32E621}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -29,10 +27,6 @@ Global
{8F4FCE29-064E-45E4-AF43-87F31420EF2F}.Debug|Any CPU.Build.0 = Debug|Any CPU {8F4FCE29-064E-45E4-AF43-87F31420EF2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F4FCE29-064E-45E4-AF43-87F31420EF2F}.Release|Any CPU.ActiveCfg = Release|Any CPU {8F4FCE29-064E-45E4-AF43-87F31420EF2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F4FCE29-064E-45E4-AF43-87F31420EF2F}.Release|Any CPU.Build.0 = Release|Any CPU {8F4FCE29-064E-45E4-AF43-87F31420EF2F}.Release|Any CPU.Build.0 = Release|Any CPU
{A3F442D5-C149-401F-BC5C-9BADED32E621}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3F442D5-C149-401F-BC5C-9BADED32E621}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3F442D5-C149-401F-BC5C-9BADED32E621}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3F442D5-C149-401F-BC5C-9BADED32E621}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View file

@ -14,7 +14,7 @@ namespace Bowling.Classes
public string Name { get; set; } public string Name { get; set; }
[JsonProperty("score")] [JsonProperty("score")]
public List<int> Score { get; set; } public List<int> Score { get; set; } = new List<int>();
public string Serialize() public string Serialize()

View file

@ -121,6 +121,7 @@
this.Controls.Add(this.NameTB); this.Controls.Add(this.NameTB);
this.Name = "Connect"; this.Name = "Connect";
this.Text = "Launcher"; this.Text = "Launcher";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Connect_FormClosing);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();

View file

@ -39,5 +39,9 @@ namespace Bowling
private void ExitBtn_Click(object sender, EventArgs e) private void ExitBtn_Click(object sender, EventArgs e)
{ {
} }
private void Connect_FormClosing(object sender, FormClosingEventArgs e)
{
}
} }
} }

View file

@ -7,12 +7,13 @@ using System.Collections.Generic;
using System.Windows.Forms; using System.Windows.Forms;
using NetLib; using NetLib;
using System.Threading; using System.Threading;
using System.Text;
namespace Bowling namespace Bowling
{ {
public enum GameState public enum GameState
{ {
Menu, Game, Exit, Connect_to_server, EndGame Menu, Game, Exit, Connect_to_server, PauseGame, EndGame
} }
public class Game1 : Game public class Game1 : Game
{ {
@ -33,8 +34,7 @@ namespace Bowling
private Vector2 ballStartPosition; private Vector2 ballStartPosition;
private List<Classes.UI.Label> tableLabels; private List<Classes.UI.Label> tableLabels;
private int intermediateScore; private int intermediateScore;
private Classes.UI.Label lblCountP1; private int counter;
private Classes.UI.Label lblCountP2;
private List<Pin> pins = new List<Pin>(); private List<Pin> pins = new List<Pin>();
@ -52,6 +52,7 @@ namespace Bowling
_graphics.PreferredBackBufferHeight = 1000; _graphics.PreferredBackBufferHeight = 1000;
Content.RootDirectory = "Content"; Content.RootDirectory = "Content";
IsMouseVisible = true; IsMouseVisible = true;
Window.Title = "Bowling";
} }
protected override void Initialize() protected override void Initialize()
@ -70,6 +71,7 @@ namespace Bowling
tableMarginLeft = 20; tableMarginLeft = 20;
tableLabels = new List<Classes.UI.Label>(); tableLabels = new List<Classes.UI.Label>();
intermediateScore = 0; intermediateScore = 0;
counter = 0;
base.Initialize(); base.Initialize();
} }
@ -125,12 +127,34 @@ namespace Bowling
{ {
while (true) while (true)
{ {
player2.Deserialize(NetLib.NetLib.Receive()); string msg = NetLib.NetLib.Receive();
if (msg == "11")
{
ResetAll();
break;
}
else
{
player2.Deserialize(msg);
}
} }
}); });
thread.Start(); thread.Start();
} }
else { gameState = GameState.Menu; } else { gameState = GameState.Exit; }
break;
case GameState.EndGame:
counter++;
if (counter >= 300)
{
gameState = GameState.Menu;
NetLib.NetLib.Send("11");
counter = 0;
}
break;
case GameState.PauseGame:
if (player2.Score.Count == 21 || (player2.Score.Count == 20 && player2.Score[18] + player2.Score[19] != 10)) gameState = GameState.EndGame;
if (tableLabels.Count >= 77) gameState = GameState.EndGame;
break; break;
} }
base.Update(gameTime); base.Update(gameTime);
@ -161,6 +185,20 @@ namespace Bowling
case GameState.Menu: case GameState.Menu:
menu.Draw(_spriteBatch); menu.Draw(_spriteBatch);
break; break;
case GameState.EndGame:
DrawGrid();
foreach (Classes.UI.Label lbl in tableLabels)
{
lbl.Draw(_spriteBatch);
}
break;
case GameState.PauseGame:
DrawGrid();
foreach (Classes.UI.Label lbl in tableLabels)
{
lbl.Draw(_spriteBatch);
}
break;
} }
base.Draw(gameTime); base.Draw(gameTime);
@ -190,9 +228,10 @@ namespace Bowling
{ {
if (player1.Score[18] + player1.Score[19] == 10) NewMove(); if (player1.Score[18] + player1.Score[19] == 10) NewMove();
} }
if (player1.Score.Count == 20 && player1.Score[18] + player1.Score[19] != 10) if (player1.Score.Count == 21 || (player1.Score.Count == 20 && player1.Score[18] + player1.Score[19] != 10))
{ {
gameState = GameState.EndGame; gameState = GameState.PauseGame;
if (player2.Score.Count == 21 || (player2.Score.Count == 20 && player2.Score[18] + player2.Score[19] != 10)) gameState = GameState.EndGame;
} }
SendPlayerData(player1); SendPlayerData(player1);
} }
@ -250,8 +289,9 @@ namespace Bowling
if (i % 2 == 0) score = 0; if (i % 2 == 0) score = 0;
else if (i == 1) score = Sum(player1.Score); else if (i == 1) score = Sum(player1.Score);
else score = Sum(player2.Score); else score = Sum(player2.Score);
Classes.UI.Label lbl = new Classes.UI.Label(score.ToString(), new Vector2(tableMarginLeft + rowWidth * 12 + rowWidth / 2, tableMarginTop + countRows + rowHeight + 5), Classes.UI.Label lbl = new Classes.UI.Label(score.ToString(), new Vector2(tableMarginLeft + rowWidth * 12 + rowWidth / 2 - 15, tableMarginTop + countRows + rowHeight + ((i % 2 == 1) ? 15 : 5)),
Color.Fuchsia); Color.Fuchsia);
if (i % 2 == 1) lbl.FontName = "gameFont2";
lbl.LoadContent(Content); lbl.LoadContent(Content);
tableLabels.Add(lbl); tableLabels.Add(lbl);
countRows += (i != 0 && i % 2 == 1) ? 2 * rowHeight : rowHeight; countRows += (i != 0 && i % 2 == 1) ? 2 * rowHeight : rowHeight;
@ -274,6 +314,14 @@ namespace Bowling
lbl.LoadContent(Content); lbl.LoadContent(Content);
tableLabels.Add(lbl); tableLabels.Add(lbl);
} }
Classes.UI.Label label_p1_name = new Classes.UI.Label(player1.Name[0].ToString(), new Vector2(tableMarginLeft + 2, tableMarginTop + rowHeight),
Color.Red);
label_p1_name.LoadContent(Content);
tableLabels.Add(label_p1_name);
Classes.UI.Label label_p2_name = new Classes.UI.Label(player2.Name[0].ToString(), new Vector2(tableMarginLeft + 2, tableMarginTop + 4 * rowHeight), Color.Red);
label_p2_name.LoadContent(Content);
tableLabels.Add(label_p2_name);
} }
private void NewMove() private void NewMove()
@ -296,5 +344,23 @@ namespace Bowling
{ {
NetLib.NetLib.Send(player.Serialize()); NetLib.NetLib.Send(player.Serialize());
} }
private void ResetAll()
{
ball = new Ball(ballStartPosition, Vector2.Zero, Color.Blue, Gutter_top_y, gutter_bottom_y, gutter_height, _graphics.PreferredBackBufferWidth);
menu = new Menu();
player2 = new Player();
player1 = new Player();
rowWidth = 80;
rowHeight = 50;
tableMarginTop = 20;
tableMarginLeft = 20;
tableLabels = new List<Classes.UI.Label>();
intermediateScore = 0;
counter = 0;
gameState = GameState.Menu;
pins.Clear();
LoadContent();
}
} }
} }

Binary file not shown.

View file

@ -12,7 +12,7 @@ namespace Bowling_Server.Classes
public string Name { get; set; } public string Name { get; set; }
[JsonProperty("score")] [JsonProperty("score")]
public List<int> Score { get; set; } public List<int> Score { get; set; } = new List<int>();
[NonSerialized] [NonSerialized]
public Socket socket; public Socket socket;

View file

@ -1,11 +1,10 @@
using System; using Bowling_Server.Classes;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Bowling_Server.Classes;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading;
using NetLib;
using System.Text; using System.Text;
using System.Threading;
namespace Bowling_Server namespace Bowling_Server
{ {
@ -99,22 +98,41 @@ namespace Bowling_Server
{ {
int dataLength = player.socket.Receive(data); int dataLength = player.socket.Receive(data);
string json = Encoding.ASCII.GetString(data, 0, dataLength); string json = Encoding.ASCII.GetString(data, 0, dataLength);
if (json.Contains("11"))
{
foreach (var player in group)
{
player.socket.Send(Encoding.ASCII.GetBytes("11"));
player.Score = new List<int>();
players.Add(player);
}
break;
}
player.Deserialize(json); player.Deserialize(json);
CastOpponents(group); CastOpponents(group);
} }
} }
catch catch
{
try
{ {
Console.WriteLine("Player disconnected"); Console.WriteLine("Player disconnected");
if (group.IndexOf(player) == 1) if (group.IndexOf(player) == 1)
{ {
group[0].socket.Send(Encoding.ASCII.GetBytes("11"));
players.Add(group[0]); players.Add(group[0]);
} }
else else
{ {
group[1].socket.Send(Encoding.ASCII.GetBytes("11"));
players.Add(group[1]); players.Add(group[1]);
} }
} }
catch
{
Console.WriteLine("Players disconnected. Can't process disconnection. Game ended.");
}
}
}); });
thread.Start(); thread.Start();
} }