using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Eleon;
using Eleon.Modding;
using PrimS.Telnet;
namespace JCMod
{
// Client mod
// Class implementing both the new IMod and the legacy ModInterface interfaces as both are available on dedi side
public class JCLocalChatMod : ModInterface
{
IModApi modApi;
ModGameAPI legacyModApi;
List<PlayerInfo> players;
//telnet
private CancellationTokenSource cancellationTokenSource;
Client telnetClient;
// ----- IMod methods -------------------------------------------------
// Called once early when the host process starts - treat this like a constructor for your mod
public void Init(IModApi modApi)
{
this.modApi = modApi;
modApi.Log("Local Chat mod by JCD initialized. My vision is augmented.");
modApi.Log($"GameTime (ticks): {modApi.Application.GameTicks}");
cancellationTokenSource = new CancellationTokenSource();
telnetClient = new Client("hostname", 0, cancellationTokenSource.Token);
}
// Called once just before the game is shut down
// You may use this like a Dispose method for your mod to release unmanaged resources
public void Shutdown()
{
modApi.Log("Local Chat mod by JCD shutdown. Such a shame.");
}
// ----- ModInterface methods -----------------------------------------
// Called once early when the host process starts - treat this like a constructor for your mod
public void Game_Start(ModGameAPI legacyModApi)
{
this.legacyModApi = legacyModApi;
}
// Called once just before the game is shut down
// You may use this like a Dispose method for your mod to release unmanaged resources
public void Game_Exit()
{
}
public void Game_Event(CmdId eventId, ushort seqNr, object data)
{
try
{
switch (eventId)
{
case CmdId.Event_Player_Info:
if (players.Where(e => e.entityId == ((PlayerInfo)data).entityId).Count() == 0)
{
players.Add((PlayerInfo)data);
}
break;
case CmdId.Event_Player_Connected:
legacyModApi.Game_Request(CmdId.Request_Player_Info, (ushort)CmdId.Request_Player_Info, (Id)data);
break;
case CmdId.Event_Player_Disconnected:
players.Remove(players.FirstOrDefault(e => e.entityId == ((Id)data).id));
break;
case CmdId.Event_ChatMessage:
ChatInfo ci = (ChatInfo)data;
if (ci == null) { break; }
PlayerInfo sender = players.Find(player => player.entityId == ci.playerId);
for(var i = 0; i < players.Count; i ++)
{
var cp = players[i];
if (cp.playfield == sender.playfield)
{
if (PV3Distance(cp.pos,sender.pos) < 20f)
{
telnetClient.Write("say p:" + cp.clientId + " {" + sender.playerName + "}: " + ci.msg);
}
}
}
break;
default:
break;
}
}
catch (Exception ex)
{
legacyModApi.Console_Write(ex.Message);
}
}
// called each frame - don't waste time here!
public void Game_Update()
{
}
private float PV3Distance(PVector3 me, PVector3 other)
{
return (float)Math.Sqrt(Math.Pow(me.x - other.x, 2) + Math.Pow(me.y - other.y, 2) + Math.Pow(me.z - other.z, 2));
}
}
}