{
public override void GameStarted() {
ServerCache.Get("myKey", loaded => {
//myKey was not found in ServerCache. Go and key value from outside source, in this case BigDB.
PlayerIO.BigDB.LoadOrCreate("myTable", "myKey", dbObj => {
//got value now. call loaded so it is stored in ServerCache and have 'done' delegate called (see below)
loaded(dbObj);
}, bigdbError => {
//could not get value from BigDB. Call loaded(null) to clean resources
loaded(null);
});
}, TimeSpan.FromMinutes(30), cachedVal => {
//got value! run logic that uses value.
//no need to check for cachedVal != null since this callback will not be called in this case.
//...
}, error => {
//this code will never be executed. would only execute if 'loaded' delegate is called with a value not allowed in ServerCache.
//We called 'loaded' delegate with a DatabaseObject, which is allowed in ServerCache
});
}
}
]]>
//Make new object and set some properties
DatabaseObject obj = new DatabaseObject();
obj.Set("username", "Adam");
obj.Set("location", "London");
obj.Set("age", 20);
//Create object in table Users with ConnectUserId as key
PlayerIO.BigDB.CreateObject("Users", ConnectUserId, obj,
delegate (DatabaseObject result){
//Change a property and store the update when created
result.Set("location", "Paris");
result.Save(null);
}
);
PlayerIO.BigDB.Load("Users", ConnectUserId, delegate (DatabaseObject result){
if (result != null) {
//Change a property and save back.
result.Set("location", "Amsterdam");
result.Save(null);
}
});
PlayerIO.BigDB.LoadOrCreate("Users", ConnectUserId,
delegate (DatabaseObject result){
if (!result.Contains("username")) {
//Empty object, initialize it
result.Set("username", "Charlie");
result.Set("age", 20);
}
result.Set("location", "London");
result.Save();
}
);
{
username:"Adam",
created:2010-05-12 15:28
location:"London",
age:20,
}
//Get the object where username="Adam"
PlayerIO.BigDB.LoadSingle("Users", "ByUsername", new object[]{"Adam"},
delegate(DatabaseObject result) {
//...
}
);
//Get all users with usernames between "Adam" and "Charlie".
//This would retrieve users named "Adamsson" and "Barney",
//but not users named "Abel" or "Charlotte".
PlayerIO.BigDB.LoadRange("Users", "ByUsername", null, "Adam", "Charlie", 100,
delegate(DatabaseObject[] result) {
//...
}
);
//Get all users up to and including "Adam". This would retrieve
//users named "Aaron" and "Ackerman", but not "Adamsson" or "Barney".
PlayerIO.BigDB.LoadRange("Users", "ByUsername", null, null, "Adam", 100,
delegate(DatabaseObject[] result) {
//...
}
);
//Get all users from "Xerxes". This would retrieve users named
//"Yngwie" and "Zed", but not "Charlie" or "Xantippa".
PlayerIO.BigDB.LoadRange("Users", "ByUsername", null, "Xerxes", null, 100,
delegate(DatabaseObject[] result) {
//...
}
);
//Retrieve the ten first objects by the ByCreated index.
//Since that index is sorted in descending order, this will actually
//retrieve the 10 latest created users.
PlayerIO.BigDB.LoadRange("Users", "ByCreated", null, null, null, 10,
delegate(DatabaseObject[] result) {
//...
}
);
//Get the 10 latest users that were created more than 7 days ago.
var weekago = DateTime.Now.AddDays(-7);
PlayerIO.BigDB.LoadRange("Users", "ByCreated", null, weekago, null, 10,
delegate(DatabaseObject[] result) {
//...
}
);
//Load all users where location is "London"
PlayerIO.BigDB.LoadRange("Users", "ByLocationAgeCreated", null, "London",
"London", 100, delegate(DatabaseObject[] result) {
//...
}
);
//Load all users from London between 20 and 30 years of age
PlayerIO.BigDB.LoadRange("Users", "ByLocationAgeCreated",
new object[]{"London"}, 20, 30, 100,
delegate(DatabaseObject[] result) {
//...
}
);
//Load all users from London that are above 50
PlayerIO.BigDB.LoadRange("Users", "ByLocationAgeCreated",
new object[]{"London"}, 50, null, 100,
delegate(DatabaseObject[] result) {
//...
}
);
//Load all users from Paris that are 30 years old, and were created in April
PlayerIO.BigDB.LoadRange("Users", "ByLocationAgeCreated",
new object[]{"Paris", 30}, new DateTime(2010, 4, 1),
new DateTime(2010, 4, 30), 100,
delegate(DatabaseObject[] result) {
//...
}
);
//Load the 10 latest 20-year old users from Amsterdam
PlayerIO.BigDB.LoadRange("Users", "ByLocationAgeCreated",
new object[]{"Amsterdam", 20}, null, null, 10,
delegate(DatabaseObject[] result) {
//...
}
);
//Delete current Users object.
PlayerIO.BigDB.DeleteKeys("Users", ConnectUserId, null);
//Delete all objects with usernames between "Adam" and "Charle"
PlayerIO.BigDB.DeleteRange("Users", "ByUsername", null, "Adam",
"Charlie", null);
//Delete all objects with created older than one year
PlayerIO.BigDB.DeleteRange("Users", "ByUsername", null,
DateTime.Now.AddYear(-1), null, null);
'car54321' = {
Name: 'OldSpeedy',
LatestLapTimes: [
{
Time:'20:34',
Position:1
},
{
Time:'22:03',
Position:4
},
{
Time:'22:30',
Position:5
}
]
}
//Create new object
DatabaseObject car = new DatabaseObject();
car.Set("Name", "OldSpeedy");
//Make laptimes
DatabaseObject laptime1 = new DatabaseObject();
laptime1.Set("Time", "20:34");
laptime1.Set("Position", 1);
DatabaseObject laptime2 = new DatabaseObject();
laptime2.Set("Time", "22:03");
laptime2.Set("Position", 4);
DatabaseObject laptime3 = new DatabaseObject();
laptime3.Set("Time", "22:30");
laptime3.Set("Position", 5);
//Make array and add laptimes
DatabaseArray laptimes = new DatabaseArray();
laptimes.Add(laptime1);
laptimes.Add(laptime2);
laptimes.Add(laptime3);
//Add array to car object
car.Set("LatestLapTimes", laptimes);
//Save car to table "Cars" under key "car54321"
PlayerIO.BigDB.CreateObject("Cars", "car54321", car, null);
DatabaseObject car = new DatabaseObject();
car.Set("Name", "OldSpeedy");
car.Set("LatestLapTimes", new DatabaseArray());
car.Set("LatestLapTimes.0.Time", "20:34");
car.Set("LatestLapTimes.0.Position", 1);
car.Set("LatestLapTimes.1.Time", "22:03");
car.Set("LatestLapTimes.1.Position", 4);
car.Set("LatestLapTimes.2.Time", "22:30");
car.Set("LatestLapTimes.2.Position", 5);
PlayerIO.BigDB.Load("Cars", "car54321", delegate(DatabaseObject car) {
//Get position from first laptime
DatabaseArray laptimes = car.GetArray("LatestLapTimes");
DatabaseObject laptime = laptimes.GetObject(0);
int position = laptime.GetInt("Position");
//Get position from second laptime
int position2 = laptimes.GetInt("1.Position");
//Or get position from third laptime directly
int position3 = car.GetInt("LatestLapTimes.2.Position");
});
DatabaseArray arr = new DatabaseArray();
//We make an array like this: ['a', 'c']
arr.Add("a");
arr.Add("c");
//Now it will look like this: ['a', 'c', 'd']
arr.Add("d");
//We insert a value where c is to get this: ['a', 'b', 'c', 'd']
arr.Insert(1, "b");
//If we then remove the third value we get this: ['a', 'b', null, 'd']
arr.RemoveAt(2);
//Finally, we set the sixth value and get: ['a', 'b', null, 'd', null, 'f']
arr.Set(5, "f");
'car20312' = {
Name: 'OldSpeedy',
Stats:{
Acceleration: 3.2,
TopSpeed: 50,
Steering: 5,
WorkingBreaks:false
}
LatestLapTimes: ['20:34','22:03','22:30']
}
DatabaseObject car = new DatabaseObject();
car.Set("Name", "OldSpeedy");
//Create nested stats object
DatabaseObject stats = new DatabaseObject();
stats.Set("Acceleration", 3.2);
stats.Set("TopSpeed", 50);
stats.Set("Steering", 5);
stats.Set("WorkingBreaks", false);
car.Set("Stats", stats);
//Create laptimes array
DatabaseArray laptimes = new DatabaseArray();
laptimes.Add("20:34");
laptimes.Add("22:03");
laptimes.Add("22:30");
car.Set("LatestLapTimes", laptimes);
//Save car to table "Cars" under key "car20312"
PlayerIO.BigDB.CreateObject("Cars", "car20312", car, null);
DatabaseObject car = new DatabaseObject();
car.Set("Stats.Acceleration", 3.2);
car.Set("Stats.TopSpeed", 50);
car.Set("Stats.Steering", 5);
car.Set("Stats.WorkingBreaks", false);
PlayerIO.BigDB.Load("Cars", "car20312", delegate(DatabaseObject car) {
string name = car.GetString("Name");
float acceleration = car.GetFloat("Stats.Acceleration");
int topspeed = car.GetInt("Stats.TopSpeed");
});
PlayerIO.BigDB.Load("Cars", "car20312", delegate(DatabaseObject car) {
//Name is a string, throws exception!
int name = car.GetInt("Name");
//Color does not exist, throws exception!
string color = car.GetString("Color");
});
PlayerIO.BigDB.Load("Cars", "car20312", delegate(DatabaseObject car) {
if (!car.Contains("Name")) {
car.Set("Name", "Unknown");
}
if (car.Contains("Stats.Steering")) {
//...
}
});
PlayerIO.BigDB.Load("Cars", "car20312", delegate(DatabaseObject car) {
string color = car.GetString("Color", "Red");
});
PlayerIO.BigDB.Load("Cars", "car20312", delegate(DatabaseObject car) {
car.Remove("Name");
car.Remove("Stats.TopSpeed");
if (!car.Contains("Name")) {
//Name property should be removed now.
}
});
car.Set("Name", "NewSpeedy");
car.Set("Stats.Acceleration", 5.0);
if (car.ExistsInDatabase) {
car.Save();
} else {
PlayerIO.BigDB.CreateObject("Cars", "car12345", null);
}
car.Set("Name", "NewSpeedy");
car.Set("Stats.Acceleration", 5.0);
car.Save(delegate() {
//On successful save...
});
If use the overloads with success callbacks if you need to know when the save has completed successfully
If use the overloads with success callbacks if you need to know when the save has completed successfully
If use the overloads with success callbacks if you need to know when the save has completed successfully
string assetUrl = PlayerIO.GameFS.GetUrl("/assets/image.png");
PayVault vault = player.PayVault;
PayVault vault = PlayerIO.GetUserPayVault("connectUserId");
PayVault vault = player.PayVault;
vault.Refresh(delegate(){
int coins = vault.Coins;
});
PayVault vault = player.PayVault;
vault.Refresh(delegate(){
if (vault.Has("simplecar")) {
//...
}
});
PayVault vault = player.PayVault;
vault.Credit(100, "New player bonus", delegate(){
int newcoins = vault.Coins;
//Show new amount to user.
});
vault.Debit(10, "Race starting fee", delegate() {
//Let player start race
}
PayVault vault = player.PayVault;
vault.Buy(true, new BuyItemInfo[]{
new BuyItemInfo("speedboost")
}, delegate() {
int boosts = vault.Count("speedboost");
//Show new number of boosts to user.
});
PayVault vault = player.PayVault;
vault.Refresh();
VaultItem boost = vault.First("speedboost");
if (boost != null) {
vault.Consume(new VaultItem[]{boost}, delegate() {
//Boost the player's car
});
}
PayVault vault = player.PayVault;
vault.GetBuyCoinsInfo("socialgold",
new Dictionary<string, string>(){{"offerid", "..."}},
delegate(Dictionary<string, string> result) {
string url = result["socialgoldurl"];
//Show URL to player...
});
PayVault vault = player.PayVault;
vault.GetBuyDirectInfo("paypal",
new Dictionary<string, string>(){
{"currency", "USD"},
{"item_name", "Red Supercar"}
},
new BuyItemInfo[]{
new BuyItemInfo("supercar").Set("Color", "Red")
},
delegate(Dictionary<string, string> result) {
string url = result["paypalurl"];
//Show URL to player...
});
PayVault vault = player.PayVault;
vault.ReadHistory(0, 10, delegate(PayVaultHistoryEntry[] history) {
if (history.Length > 0) {
string lastprice = history[0].ProviderPrice;
}
});
//Buy a red 'simplecar' with stripes.
player.PayVault.Buy(true, new BuyItemInfo[]{
new BuyItemInfo("simplecar").Set("Color", "Red").Set("Stripes", true)
}, null);
//Get a 'simplecar' from the Vault
VaultItem car = player.PayVault.First("simplecar");
if (car != null) {
string color = car.GetString("Color");
bool stripes = car.GetBool("Stripes");
}
public override void GameStarted() {
var url = "http://www.mywebpage.com";
PlayerIO.Web.Get(url, delegate(HttpResponse response) {
Console.WriteLine(response.Text);
});
}
public override void GameStarted() {
var url = "http://www.mywebpage.com";
PlayerIO.Web.Get(url, delegate(HttpResponse response) {
Console.WriteLine(response.Text);
});
}
PlayerIO.ErrorLog.WriteError("Error in my method");
try {
//Some code that can throw an exception...
} catch (Exception e) {
PlayerIO.ErrorLog.WriteError("Exception in my method", e);
}
// message of type start with no payload
Message m = Message.Create("start");
// send the message to all connected players
Broadcast(m);
public override void UserJoined(BasePlayer player) {
// send the player a message of type "mapinfo" with two pieces
// of data payload: the mapnam (string) and number of players (int)
player.Send("mapinfo", "fields-of-glory", PlayerCount );
// broadcast to all connected players a message with type "userjoined"
// and a single payload of just the player id (uint)
Broadcast("userjoined", player.Id);
}
public override void UserJoined(BasePlayer player) {
// create a new message of type join
Message joinMessage = Message.Create("join");
// add all the currently connected userids to the message
foreach(var p in Players) {
joinMessage.Add(p.ConnectUserId);
}
// send the message to the player who joined
player.Send(joinMessage);
}
somemessage.Add("a string", 1234, true, false)
{
public override void GameStarted() {
var user1PayVault = PlayerIO.GetUserPayVault("user1");
user1PayVault.Credit(10, "you earned some coins", null);
var user2PayVault = PlayerIO.GetUserPayVault("user2");
user1PayVault.Debit(10, "you lost some coins", null);
}
}
]]>
[RoomType("MyRoomType")]
public class MyGame : Game<MyPlayer> {
...
}
[DebugAction("Stop", DebugAction.Icon.Stop)]
public void Stop() {
Console.WriteLine("Stopping...");
}
[DebugAction("Add Obstacle", DebugAction.Icon.Add)]
public void PositionRock(int x, int y) {
// set a rock on position (x,y)
}
[DebugAction("Add Obstacle", "add.png")]
public void PositionRock(int x, int y) {
// set a rock on position (x,y)
}
public class MyGame : Game<BasePlayer> {
public override void GameStarted() {
// Update the debug view once a second.
AddTimer(delegate {
RefreshDebugView();
}, 1000);
}
public override Image GenerateDebugImage() {
var img = new Bitmap(400, 400);
using(var g = Graphics.FromImage(debugImage)) {
// fill the background with blue
g.FillRectangle(Brushes.Blue, 0, 0, img.Width, img.Height);
// draw the embedded image "plane.png" at the offset (30,30)
var embeddedImage = EmbeddedResource.GetImage("plane.png");
g.DrawImage(embeddedImage, 30, 30);
}
return img;
}
}
This object is loaded via the GetPlayerObject() method or automatically when a player joins the game if the Game.PreloadPlayerObjects property is set to true.
public override Dictionary<string, string> GetDebugValues() {
var dict = new Dictionary<string, string>();
dict["sum"] = (2+2).ToString();
dict["description"] = "...";
return dict;
}
// reading a variable from room data
var somevariable = RoomData["myvarname"];
// looping over all room data
foreach(var kv in RoomData) {
Console.WriteLine("RoomData[" + kv.Key + "] = " + kv.Value );
}
// changing a variable and saving changes
// so future ListRooms have the updated room data
RoomData["myvariable"] = "new value";
RoomData.Save();
public class MyPlayer : BasePlayer {
// put properties here if you want them on your player
}
[RoomType("MyRoomType")]
public class MyGame : Game<MyPlayer> {
public override void GameStarted() {
// a game has been created
}
public override void UserLeft(MyPlayer player) {
// a user has left a game
}
public override void GotMessage(MyPlayer player, Message message) {
// when getting a message, simply send it to all players connected
// to the room.
Broadcast(message);
}
}
// reading a variable from room data
var somevariable = RoomData["myvarname"];
// looping over all room data
foreach(var kv in RoomData) {
Console.WriteLine("RoomData[" + kv.Key + "] = " + kv.Value );
}
// changing a variable and saving changes
// so future ListRooms have the updated room data
RoomData["myvariable"] = "new value";
RoomData.Save();