-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTicTacToePlugin.cs
103 lines (81 loc) · 3.11 KB
/
TicTacToePlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using DarkRift;
using DarkRift.Server;
using Scripts.Models;
using Scripts.Networking;
namespace TicTacToeServerPlugin {
public class TicTacToePlugin : Plugin {
public override bool ThreadSafe => false;
public override Version Version => new Version(0,0,1);
private PlayerModel pendingPlayer;
private Dictionary<ushort, MatchModel> matches;
public TicTacToePlugin(PluginLoadData pluginLoadData) : base(pluginLoadData) {
ClientManager.ClientConnected += OnClientConnected;
ClientManager.ClientDisconnected += OnClientDisconnected;
matches = new Dictionary<ushort, MatchModel>();
}
private void OnClientConnected(object sender, ClientConnectedEventArgs e) {
WriteEvent("hello friend, " + e.Client.ID, DarkRift.LogType.Info);
e.Client.MessageReceived += OnClientMessageReceived;
}
private void OnClientDisconnected(object sender, ClientDisconnectedEventArgs e) {
WriteEvent("goodbye friend, " + e.Client.ID, DarkRift.LogType.Info);
if (pendingPlayer != null && pendingPlayer.Client == e.Client) {
pendingPlayer = null;
}
}
private void OnClientMessageReceived(object sender, MessageReceivedEventArgs e) {
switch(e.Tag) {
case (ushort)Tags.Tag.SET_NAME:
// new player registering
using (Message message = e.GetMessage()) {
using (DarkRiftReader reader = message.GetReader()) {
string name = reader.ReadString();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("hey mister " + name);
Console.ForegroundColor = ConsoleColor.White;
PlayerModel newPlayer = new PlayerModel(e.Client, name);
if (pendingPlayer == null) {
// new player is pending for a match
pendingPlayer = newPlayer;
} else {
// there is already a pending player. lets start a new match
MatchModel match = new MatchModel(pendingPlayer, newPlayer);
matches.Add(match.id, match);
// report clients of the new match
using (DarkRiftWriter writer = DarkRiftWriter.Create()) {
writer.Write(match.id);
writer.Write(match.CurrentPlayerClientID);
using (Message msg = Message.Create((ushort)Tags.Tag.GOT_MATCH, writer)) {
pendingPlayer.Client.SendMessage(msg, SendMode.Reliable);
newPlayer.Client.SendMessage(msg, SendMode.Reliable);
}
}
pendingPlayer = null;
}
}
}
break;
case (ushort)Tags.Tag.SLATE_TAKEN:
using (Message message = e.GetMessage()) {
using (DarkRiftReader reader = message.GetReader()) {
ushort matchId = reader.ReadUInt16();
ushort slateIndex = reader.ReadUInt16();
if (matches.ContainsKey(matchId)) {
MatchModel match = matches[matchId];
match.PlayerTakesSlate(slateIndex, e.Client);
if (match.MatchOver) {
// match is over
Console.WriteLine($"match over. had: {matches.Count} matches");
matches.Remove(matchId);
Console.WriteLine($"match over. now have: {matches.Count} matches");
}
}
}
}
break;
}
}
}
}