rendered paste bodyusing System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using PlayerIO.GameLibrary;
using System.Drawing;
namespace tankgame {
//Player class. each player that join the game will have these attributes.
public class Player : BasePlayer {
public int id;
public int X;
public int Y;
public int angle;
public int team;
public int health;
public double last;
public int kills;
public int deaths;
public string realName;
public Player() {
health = 100;
id = 0;
deaths = 0;
kills = 0;
X = 0; //Player mouseX
Y = 0; //Player mouseY
angle = 0;
team = 0;
realName = "";
}
}
//Letter class. Each letter on the screen is represented by an instance of this class.
public class Letter {
public int X;
public int Y;
public Letter(int x, int y) {
this.X = x;
this.Y = y;
}
}
[RoomType("tankgame")]
public class GameCode : Game<Player> {
public int current = 0;
Dictionary<int, IEnumerable<Player>> worldState = new Dictionary<int, IEnumerable<Player>>();
public Player playerx;
public string[] data = {"0","0"};
int[] blockhp = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
//Create array to store our letters
private Letter[] letters = new Letter[230];
// This method is called when an instance of your the game is created
string loadedmap = "";
Boolean isloadedmap = false;
public void doChange(string s){
loadedmap = s;
data = loadedmap.Split(',');
Console.WriteLine("Loaded from db: "+loadedmap);
}
private void callbackx(DatabaseObject iar)
{
if (isloadedmap == false)
{
loadedmap = iar.GetString("data");
data = loadedmap.Split(',');
Console.WriteLine("Loaded from db: " + loadedmap);
}
isloadedmap = true;
// data = loadedmap.Split(',');
//Add the current position of all letters to the init message
string mapdata = "";
foreach (string i in data)
{
mapdata = mapdata + i + ", ";
// l = letters[a];
// m.Add(l.X, l.Y);
}
Console.WriteLine(mapdata);
playerx.Send("map", mapdata);
}
private void callbackz(DatabaseObject iar)
{
if (isloadedmap == false)
{
loadedmap = iar.GetString("data");
data = loadedmap.Split(',');
Console.WriteLine("Loaded from db: " + loadedmap);
}
isloadedmap = true;
// data = loadedmap.Split(',');
//Add the current position of all letters to the init message
string mapdata = "";
foreach (string i in data)
{
mapdata = mapdata + i + ", ";
// l = letters[a];
// m.Add(l.X, l.Y);
}
Console.WriteLine(mapdata);
Broadcast("map", mapdata);
// playerx.Send("map", mapdata);
}
public override void GameStarted() {
// anything you write to the Console will show up in the
// output window of the development server
Console.WriteLine("Game is started");
// add a timer that sends out an update every 100th millisecond
AddTimer(delegate {
//Create update message
Message update = Message.Create("update");
//Add mouse cordinates for each player to the message
foreach(Player p in Players) {
if (p.last != 0 && p.realName != "")
{
Broadcast("player", p.id, p.team, p.X, p.Y, p.angle, p.realName, p.last, p.kills, p.deaths);
}
}
//Broadcast message to all players
}, 1000);
var ms = 1000;
AddTimer(delegate {
worldState.Add(current, Players);
try
{
foreach (Player p in worldState[current])
{
if (p.last != 0 && p.realName != "")
{
Console.WriteLine(current + "NOW: [ " + p.realName + " is at X: " + p.X + " Y: " + p.Y + " ] ");
}
}
foreach (Player p in worldState[current-2000])
{
if (p.last != 0 && p.realName != "")
{
Console.WriteLine((current - 1000) + "PAST: [ " + p.realName + " is at X: " + p.X + " Y: " + p.Y + " ] ");
}
}
}
catch (Exception ex)
{
//
// An exception will be thrown.
//
Console.WriteLine(ex);
}
current = current + ms;
}, ms);
}
// This method is called when the last player leaves the room, and it's closed down.
public override void GameClosed() {
Console.WriteLine("RoomId: " + RoomId);
}
// This method is called whenever a player joins the game
public override void UserJoined(Player player) {
playerx = player;
var test = Convert.ToInt16(player.JoinData["team"]);
player.team = test;
Console.WriteLine("Player joined as " + test + "test");
PlayerIO.BigDB.Load("maps", "0", callbackx);
// Create init message for the joining player
Message m = Message.Create("init");
// Tell player their own id
m.Add(player.Id);
// Send init message to player
player.Send(m);
string mapdata = "";
// foreach (Player i in Players)
// {
// player.Send("joined", i.id, i.team, i.X, i.Y, i.angle, i.realName, i.last);
// Console.WriteLine("told " + player.realName + " about " + i.realName);
// }
}
// This method is called when a player leaves the game
public override void UserLeft(Player player) {
Console.WriteLine("Player " + player.Id + " left the room");
//Inform all other players that user left.
Broadcast("left", player.Id);
}
// This method is called when a player sends a message into the server code
public override void GotMessage(Player player, Message message) {
//Switch on message type
switch(message.Type) {
case "move": {
//Move letter in internal representation
// Letter l = player[message.GetInteger(0)];
player.angle = message.GetInteger(1);
player.X = message.GetInteger(2);
player.Y = message.GetInteger(3);
//inform all players that the letter have been moved
Broadcast("move", message.GetInteger(0), player.angle, player.X, player.Y);
break;
}
case "ping":
{
player.id = message.GetInteger(0);
player.last = message.GetDouble(1);
// Broadcast("ping", player.id, player.last);
break;
}
case "joined":
{
player.id = message.GetInteger(0);
//Set player mouse information
player.team = message.GetInteger(1);
player.X = message.GetInteger(2);
player.Y = message.GetInteger(3);
player.angle = message.GetInteger(4);
player.realName = message.GetString(5);
player.last = message.GetDouble(6);
Broadcast("joined", player.id, player.team, player.X, player.Y, player.angle, player.realName,player.last,player.kills,player.deaths);
break;
}
//connectionx.send(msg.type, msg.senderID, msg.x, msg.y, msg.angle);
case "shoot":
{
Broadcast("shoot", player.id, message.GetInteger(1), message.GetInteger(2), message.GetInteger(3));
break;
}
case "died":
{
var owner = message.GetInteger(1);
player.health = 0;
player.deaths++;
//Add mouse cordinates for each player to the message
foreach (Player p in Players)
{
if (p.id == owner)
{
p.kills++;
}
}
Broadcast("died", player.id,owner);
break;
}
case "message":
{
String messages = message.GetString(1);
if (messages == "clean")
{
isloadedmap = false;
PlayerIO.BigDB.Load("maps", "0", callbackz);
int y = blockhp.Length;
int i = 0;
while (i < y)
{
blockhp[i] = 100;
i++;
}
}
else
{
Broadcast("message", player.id, message.GetString(1));
}
break;
}
case "hitblock":
{
blockhp[message.GetInteger(1)] = blockhp[message.GetInteger(1)] - 20;
if ( blockhp[message.GetInteger(1)] <= 0){
data[message.GetInteger(1)] = "4";
Broadcast("killblock", player.id, message.GetInteger(1), 4);
}
break;
}
case "block":
{
blockhp[message.GetInteger(1)] = 100;
data[message.GetInteger(1)] = message.GetInteger(2).ToString();
Broadcast("block", player.id, message.GetInteger(1),message.GetInteger(2) );
break;
}
}
}
}
}