Skip to content
Permalink
Browse files
update from private repo
  • Loading branch information
amPerl committed Jul 3, 2017
1 parent ede1bb5 commit d8690c7e2b584d01fe89843a2ff0b4770fd50123
Show file tree
Hide file tree
Showing 50 changed files with 3,654 additions and 297 deletions.
@@ -0,0 +1,92 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Rice.Game;
using Rice.Server.Core;

namespace Rice
{
public class Admin
{
private static HttpListener listener;

public static void Initialize(Config config)
{
listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8088/");
listener.Start();

listener.BeginGetContext(onGetContext, null);
}

private static void onGetContext(IAsyncResult result)
{
var ctx = listener.EndGetContext(result);
switch (ctx.Request.RawUrl)
{
case "/status":
case "/status/":
onStatus(ctx);
break;

case "/favicon.ico":
break;

default:
Log.WriteLine("Unhandled JSON API request: {0}", ctx.Request.RawUrl);
writeJsonResp(new { status = "error", error = "Invalid API call." }, ctx);
break;
}
listener.BeginGetContext(onGetContext, null);
}

private static void writeJsonResp(object obj, HttpListenerContext ctx)
{
var resp = ctx.Response;
string str = JsonConvert.SerializeObject(obj);
byte[] strBytes = Encoding.UTF8.GetBytes(str);

resp.ContentLength64 = strBytes.Length;
resp.OutputStream.Write(strBytes, 0, strBytes.Length);
resp.OutputStream.Close();
}

private static void onStatus(HttpListenerContext ctx)
{
dynamic status = new ExpandoObject();
status.status = "ok";
status.playercount = RiceServer.GetPlayers().Length;

var servers = new [] {RiceServer.Auth, RiceServer.Lobby, RiceServer.Game, RiceServer.Area, RiceServer.Ranking};
status.servers = servers
.Select(server => new
{
name = server.Name,
clientcount = server.GetClients().Length
});

status.areas = RiceServer.GetAreas()
.Where(a => a.GetPlayerCount() > 0)
.Select(area => new
{
id = area.ID,
playercount = area.GetPlayerCount(),
players = area.GetPlayers()
.Select(p => new
{
name = p.ActiveCharacter.Name,
level = p.ActiveCharacter.Level
})
});

writeJsonResp(status, ctx);
}
}
}
@@ -1,6 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider></providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data></configuration>
@@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using MySql.Data.Entity;
using MySql.Data.MySqlClient;
using Rice.Server.Database.Models;

namespace Rice
{
@@ -28,19 +28,32 @@ public static void AddParameter(this DbCommand command, string name, object valu
}
}

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class RiceContext : DbContext
{
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Character> Characters { get; set; }
public virtual DbSet<QuestState> QuestStates { get; set; }
public virtual DbSet<Item> Items { get; set; }
public virtual DbSet<Vehicle> Vehicles { get; set; }

public RiceContext(DbConnection connection, bool ownsConnection = false) : base(connection, ownsConnection) { }
}

public static class Database
{
static DbConnection conn;

public static void Initialize(Config config)
{
conn = new MySqlConnection(
String.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};",
conn = new MySqlConnection(String.Format(
"Persist Security Info=True; Server={0}; Port={1}; Database={2}; Uid={3}; Pwd={4};",
config.DatabaseHost,
config.DatabasePort,
config.DatabaseName,
config.DatabaseUser,
config.DatabasePassword));
config.DatabasePassword
));
}

public static void Start()
@@ -49,18 +62,73 @@ public static void Start()

try
{
using (var rc = new RiceContext(conn))
{
if (rc.Database.CreateIfNotExists())
{
Log.WriteLine("Database did not exist, created.");

#if DEBUG
// Create some test data to work with
var testUser = new User
{
Name = "admin",
PasswordHash = Utilities.MD5("admin"),
CreateIP = "127.0.0.1",
Status = 1,
Credits = 1000
};

var testChar = new Character
{
Name = "RiceAdmin",
Mito = 12345678910,
Avatar = 2,
Level = 123,
Experience = 123,
City = 1,
TID = -1
};

testUser.Characters = new List<Character> {testChar};

var testVehicle = new Vehicle
{
CarID = 1,
AuctionCount = 0,
CarType = 88,
Color = 0,
Grade = 9,
Kms = 200,
Mitron = 5550f
};

testChar.Vehicles = new List<Vehicle> {testVehicle};
testChar.CurrentCarID = testVehicle.CarID;

rc.Users.Add(testUser);
rc.SaveChanges();
#endif
}
}
conn.Open();
Log.WriteLine("Connected to database.");
}
catch (Exception ex)
catch (MySqlException ex)
{
Log.WriteError("Connection failed: {0}", ex.Message);
Log.WriteError($"Connection failed: {ex.Message}");
}
}

public static DbConnection GetConnection()
private static DbConnection GetConnection()
{
if (conn.State == ConnectionState.Broken)
conn.Close();
if (conn.State == ConnectionState.Closed)
Start();
return conn;
}

public static RiceContext GetContext() => new RiceContext(GetConnection());
}
}
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rice.Server.Core;

namespace Rice.Game
{
public class Area
{
public int ID;
private List<Player> players;

public Area(int id)
{
ID = id;
players = new List<Player>();
}

public void AddPlayer(Player player)
{
if (players.Contains(player))
{
Log.WriteLine("Weird: Player already in area.");
RemovePlayer(player);
}
players.Add(player);
player.ActiveCharacter.Area = this;
Log.WriteLine("Added player {0} to area {1}.", player.ActiveCharacter.Name, ID);
}

public void RemovePlayer(Player player)
{
players.Remove(player);
player.ActiveCharacter.Area = null;
Log.WriteLine("Removed player {0} from area {1}.", player.ActiveCharacter.Name, ID);
}

public Player[] GetPlayers()
{
return players.ToArray();
}

public int GetPlayerCount()
{
return players.Count;
}

public void BroadcastMovement(Player player, byte[] movement) // byte[] temporarily
{
var move = new RicePacket(0x21D); // 114 total length
move.Writer.Write(player.ActiveCharacter.CarSerial);
move.Writer.Write(movement);

foreach (Player areaPlayer in players)
{
if (areaPlayer != player)
areaPlayer.AreaClient.Send(move);
}
}

public void Broadcast(RicePacket packet, RiceClient exclude = null)
{
foreach (var player in GetPlayers())
{
var client = player.AreaClient;

if (exclude == null || client != exclude)
client.Send(packet);
}
}
}
}

0 comments on commit d8690c7

Please sign in to comment.