Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using UnityEngine.UI;
- public class TwitchChat : MonoBehaviour {
- System.IO.TextReader input;
- System.IO.TextWriter output;
- string buf;
- string nick, owner, server, chan, password;
- int port;
- System.Net.Sockets.TcpClient sock;
- public GameManager gameManager;
- string lastusercolor, lastusercolorname;
- void awake()
- {
- }
- // Use this for initialization
- void Start () {
- sock = new System.Net.Sockets.TcpClient();
- //Get nick, owner, server, port, and channel from user
- nick = "username";
- server = "199.9.253.120"; // for whispers this must be a Group Chat server from http://twitchstatus.com/
- port = 443;
- password = "oauth:XXXXXXXXXXX";
- chan = "#channel";
- //Connect to irc server and get input and output text streams from TcpClient.
- sock.Connect(server, port);
- if (!sock.Connected) {
- Debug.Log("TwitchChat Failed to connect!");
- return;
- }
- input = new System.IO.StreamReader(sock.GetStream());
- output = new System.IO.StreamWriter(sock.GetStream());
- //Starting USER and NICK login commands
- output.Write(
- "PASS " + password + "\r\n" +
- "USER " + nick + " 0 * :" + nick + "\r\n" +
- "NICK " + nick + "\r\n"
- );
- output.Flush();
- InvokeRepeating("FlushBufferDelayed", 5f, 5f);
- }
- void Update () {
- if (!sock.Connected)
- {
- Debug.Log ("lost connection");
- return;
- }
- if (sock.Available > 0)
- buf = input.ReadLine ();
- else
- buf= "";
- try
- {
- if (buf != "")
- {
- Debug.Log(buf);
- //Send pong reply to any ping messages
- if (buf.StartsWith("PING"))
- {
- output.Write(buf.Replace("PING", "PONG") + "\r\n");
- output.Flush();
- }
- //After server sends 001 command, we can join a channel
- if (buf.Split(' ')[1] == "001")
- {
- output.Write(
- "JOIN " + chan + "\r\n"
- );
- output.Flush();
- // after we join request whisper messages
- output.Write(
- "CAP REQ :twitch.tv/commands\r\n"
- );
- output.Flush();
- // after we join request colors and other messages
- output.Write(
- "CAP REQ :twitch.tv/tags\r\n"
- );
- output.Flush();
- output.Write(
- "CAP REQ :twitch.tv/membership\r\n"
- );
- output.Flush();
- }
- if (buf.StartsWith("@")) // is a message priv/whisper with user tags
- {
- if (buf.Split(' ')[2] == "WHISPER" && buf.StartsWith("@color=#")) // user color
- {
- lastusercolorname = buf.Split(';')[1].ToString().Replace("display-name=","");
- lastusercolor = buf.Split(';')[0].ToString().Replace("@color=", "");
- if (gameManager.players != null && gameManager.players.Count > 0)
- {
- foreach (GameObject tf in gameManager.players)
- {
- Player pl = tf.GetComponent<Player>();
- if (pl.isActiveAndEnabled)
- {
- if (pl.TwitchUserName.ToLower() == lastusercolorname.ToLower())
- {
- pl.playerColor = HexToColor(lastusercolor);
- pl.UpdatePlayerColor();
- }
- }
- }
- }
- output.Write(
- "PING\r\n"
- );
- output.Flush();
- }
- // there is new twitch chat messages to parse
- if (buf.Split(' ')[2] == "WHISPER")
- {
- string inputUserName = buf.Split(';')[1].ToString().Replace("display-name=", "");
- string messageText = buf.Split(':')[2];
- if (messageText.ToLower() == "!test")
- {
- for (int i = 1; i < 50; i++)
- {
- gameManager.CreatePlayer("test player " + i);
- }
- }
- if (messageText.ToLower() == "!setup")
- {
- output.Write(
- "PRIVMSG " + chan + " :/mod afastrunner\r\n"
- );
- output.Flush();
- }
- if (messageText.ToLower() == "!play")
- {
- Debug.Log("new player request");
- bool newPlayer = true;
- if (gameManager.players != null && gameManager.players.Count > 0)
- {
- foreach (GameObject tf in gameManager.players)
- {
- Player pl = tf.GetComponent<Player>();
- if (pl.isActiveAndEnabled)
- {
- if (pl.TwitchUserName.ToLower() == inputUserName.ToLower())
- {
- newPlayer = false;
- output.Write(
- "PRIVMSG " + chan + " :/w " + inputUserName + " your allready playing.\r\n"
- );
- output.Flush();
- }
- }
- }
- }
- else
- {
- newPlayer = true;
- }
- if (newPlayer)
- {
- gameManager.CreatePlayer(inputUserName);
- if (gameManager.players != null && gameManager.players.Count > 0)
- {
- foreach (GameObject tf in gameManager.players)
- {
- Player pl = tf.GetComponent<Player>();
- if (pl.isActiveAndEnabled)
- {
- if (pl.TwitchUserName.ToLower() == lastusercolorname.ToLower())
- {
- pl.playerColor = HexToColor(lastusercolor);
- pl.UpdatePlayerColor();
- }
- }
- }
- }
- }
- }
- // go through each player and check if they have new commands
- if (gameManager.players != null && gameManager.players.Count > 0)
- {
- foreach (GameObject tf in gameManager.players)
- {
- Player pl = tf.GetComponent<Player>();
- if (pl.isActiveAndEnabled)
- {
- if (pl.TwitchUserName.ToLower() == inputUserName.ToLower())
- {
- pl.TwitchMessage(messageText); // send message from user to there player instance
- }
- }
- }
- }
- } // end of incoming PRIVMSG parsing
- } // end of whispers
- } // end of buffer
- // go through each player and check if they have whispers to send back to the player via whisper
- if (gameManager.players != null && gameManager.players.Count > 0)
- {
- foreach (GameObject tf in gameManager.players)
- {
- Player pl = tf.GetComponent<Player>();
- if (pl.isActiveAndEnabled)
- {
- if (pl.getMessageToTwitchWhisper(false) != "") // get any message from the player instance that needs to go back to the user in twitch chat
- {
- // Debug.LogWarning("output whisper directly to player");
- output.Write(
- "PRIVMSG " + chan + " :/w " + pl.TwitchUserName + " " + pl.getMessageToTwitchWhisper(true) + " \r\n"
- );
- }
- }
- }
- }
- }
- catch (System.Exception ex)
- {
- Debug.Log("errored" + ex.ToString());
- }
- }
- void FlushBufferDelayed()
- {
- output.Flush();
- }
- Color HexToColor(string hex)
- {
- hex = hex.Replace("#", "");
- byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
- byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
- byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
- return new Color32(r, g, b, 255);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement